.121 15-20 Flashcards

1
Q

What is big omega notation?

A

lower bound - T(n) ≥ M×f(n) for all n ≥ n₀

grows no slower than f(n) - ≥

find simplest so also big Ω of anything that grows slower

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is big theta notation?

A

tight bound - M1×f(n) ≤ T(n) ≤ M2×f(n) for all n ≥ n₀

combo of other 2 - grows no slower + no faster than f(n) - ==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you prove big theta?

A

prove big O and big omega
e.g. T(n) ∈ O(n) AND T(n) ∈ Ω(n) → we can say T(n) ∈ Θ(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you prove big omega?

A

replace anything that isn’t the dominant term with a 0 then add up to find M
find n0 by getting equal to the n term and dividing to get n alone

e.g. given T(n) = 3n + 4, f(n) = n
3n ≥ 3n (equal), n ≥ 0
3n + 4 ≥ 3n + 0 when n ≥ 0
M = 3 (given the 3n) and n0 = 0 therefore T(n) ∊ Ω(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you prove big O?

A

get n0 by collecting non-dominant terms + dividing them to get n alone
get M by multiplying non-dominant terms by the dominant term and adding them up

e.g. given T(n) = 3n + 4, f(n) = n
T(n) = 3n + 4 ≤ 3n + 4n so T(n) = 3n + 4 ≤ 7n
4 ≤ 4n → divide by 4 → 1 ≤ n
M=7 and n0=1 → T(n) ≤ 7n for all n ≥ 1 therefore T(n) ∊ O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a recursive algorithm?

A

an algorithm which calls itself w/ smaller input values

obtains the result for the current input by applying simple operations to the returned smaller input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you find the recurrence relation for recursive algorithms?

A

find constant time complexity (no recursion) and write T(1) = T(n) of the one operation
(probs constant so can just write 1)

then do T(n) for prior constant + recursive call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we evaluate recursive time complexity?

A
  • back substitution
  • recursion tree
  • master theorem
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does back substitution work?

A

replace recurrent relation w/ other equivalent options - e.g. T(n-1) + 1 = T(n-2) + 2 or T(n/2) + 1 = T(n/4) + 2

then find pattern of substitution - e.g. T(n-k) + k or T(n/2^k) + k

apply this pattern to T(1) to reach final theta notation
- e.g. if k = n -1 and T(n) = T(n-k) + k = T(1) + n -1 = 1 + n -1 = n therefore T(n) = n thus Θ(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is sorting data useful for?

A

ranking data, more efficient searching (in general) + binary searching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does a selection sort work?

A

find smallest value in input + extract it into new array
repeat till all items are in new array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does an in-place selection sort work?

A

find smallest value in input + swap w/ 1st value of array
repeat till index is at end of array
(change 1st to next index as each item is added)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does an in-place sort mean?

A

sorts the items within the array that was inputted instead of creating a new array to help save memory
O(1) space complexity!!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What do we need to analyse for sorts?

A

correctness + time complexity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the time complexity of a selection sort?

A

O(n^2) in all cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does an insertion sort work?

A

move elements from input to new array in list order
once added check if the element is > than items alr in list + move accordingly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does an in-place insertion sort work?

A

compare values in the list order + swap accordingly
e.g. 1st > 2nd ? 2nd > 3rd? etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the time complexity of an insertion sort?

A

O(n^2) in all cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How does a merge sort work?

A

recursively splits array into subarrays till there is just 1 item per array
then merges subarrays together by comparing the heads of each

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Where does a merge sort split arrays?

A

(start index + end index + 1) /2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How does a quick sort work?

A

pick a pivot (point/index) to split the subarray at
sort so anything greater than the value of the pivot is behind it (swap any elements > pivot w/ one next to them and once you reach no swaps swap current index w/ pivot)
recursively solve subarrays then merge

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the time complexity of a merge sort?

A

O(nlogn) in all cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the time complexity of a quick sort?

A

O(nlogn) in best + avg case
but O(n^2) in worst case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Define a tree:

A

non-linear ADT that stores elements hierarchially as nodes connected by edges w/ NO cycles or loops

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are some uses of trees?

A

file directory structures, XML + HTML, compilers and collision detection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are the relationships within a tree?

A

root = top node (no parents)
children = node w/ parents
leaf = node w/ no children
descendants/ancestors - relative of relative of

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the height of a node?

A

no. of nodes of longest path from given node to some leaf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the depth of a node?

A

number of nodes from given node to root

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the height of a tree?

A

height of its root/depth of its deepest nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the diameter/width of the tree?

A

no. of nodes on longest path between any 2 leaves

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How many edges does a tree w/ n nodes have and why?

A

n-1 edges as n nodes each have 1 edge going to them other than root

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is a subtree?

A

taking a node + its descendants from a full tree
basically part of a tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is a k-ary tree?

A

tree that imposes a max no. of children to each node
e.g. binary = max 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is a balanced tree?

A

for any node, the height of its child subtrees differ by ≤ 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What functions would a tree ADT class have?

A

void add (Object new, Object parent)
Tree find(Object val)
Tree remove(object n)
void move(Object n, Object parent)

36
Q

What happens to the children of moved/removed nodes?

A

children become the children of that node’s parents instead

37
Q

What is the significance of find() in tree ADT?

A

used to search for nodes + used in move and remove functions

38
Q

What do tree traversal algorithms do?

A

used to enumerate all elements in a tree or decide in which way we look through a tree to find elements

39
Q

How does pre-order traversal work?

A

dot to left
CLR

40
Q

How does post-order traversal work?

A

dot to right
LRC

41
Q

How does in-order traversal work?

A

dot below
LCR

42
Q

What do pre-order, post-order + in-order traversal have in common?

A

all based on a depth-first search
so can be implemented non-recursively using a stack or recursively

43
Q

How does breadth-first traversal work?

A

traverse tree from left to right at each level from the root using a queue
(where a level is like root then all of that roots children then all of there children etc.)

44
Q

Give some examples of index systems:

A

DNS lookup
compiler
packet routing table
dictionary

45
Q

What methods does a dictionary ADT inc.?

A

void insert (key k, value v)
void delete (key k)
value find (key k)

46
Q

What are common implementations of a dictionary ADT?

A

sorted arrays, search trees + hashing

47
Q

What is the difference between using sequential allocation or sorted arrays (dictionary ADT)?

A

sequential - insert = O(1), find + checking for duplicates = O(n)
sorted - find = O(logn), insertion + deletion = O(n) given sorting

48
Q

What is a binary search tree?

A

binary tree where each node has a k,v pair + nodes are sorted by their keys
to be sorted, every nodes’ key must be ≥ all keys in its left subtree + strictly < than all keys in its right subtree

49
Q

What functions does a BST ADT need?

A

delete() - any children will become children of their ancestor
insert()
find() - if smaller (go left), if bigger (go right)

50
Q

Why do BSTs need to be balanced?

A

finding is very efficient on balanced trees but not degenerate/unbalanced

w/ random insertion order expected height h = O(log n) + worst case TC = O(h) aka O(log n) - but can use SBBSTs to ensure this height

51
Q

What are the types of self-balancing search trees?

A

2-3 trees
AVL trees (SBBST)
red-black trees (builds upon 2-3 trees to represent 3-node as BT w/ red link)
B-trees (generalisation of 2-3 trees for more keys per node)

52
Q

What are some self-balancing trees that are not height-balanced?

A

splay trees + treaps
- helpful for if you wanted tree to balance for common letter patterns rather than any letter sequence

53
Q

What are the cons of 2-3 trees?

A

inconvenient to implement
-diff. node types
- multiple compares per node
- moving back up tree to split 4-nodes

54
Q

What is a 2-3 tree?

A

2 node = 1 key, 2 children
3 node = 2 keys, 3 children

same find() function but more complex insert()

55
Q

What is different about a 2-3 tree insert()?

A

to maintain perfect balance…
- a 2-node is transformed into a 3-node OR
- a 3-node is split into 3 2-nodes OR
- non-root 3-node w/ a 3/2-node parent transforms into a TEMP 4-node (split later)

56
Q

What is a graph?

A

set of nodes + edges that capture relationships

57
Q

What is a simple graph?

A

graph w/ no self-loops

58
Q

How can graph edges be detailed?

A

may be directed (arrows) or undirected (every edge = bidirectional)
+ weighted (associated values on edge)

59
Q

What is a hypergraph?

A

nodes + hyperedges to capture k-wise relationships ( k > 2)
rather than usual pairwise

60
Q

What is a subgraph?

A

subset of nodes + edges of a graph

61
Q

What is a spanning subgraph/tree?

A

spanning subgraph = formed from all nodes of the graph but only some edges
spanning tree = spanning subgraph + tree
minimal spanning tree = spanning tree w/ min. total sum of edge weights

62
Q

What is a complementary graph?

A

same set of nodes + contains all the edges NOT in the other graph

63
Q

Explain graph density:

A

measure from 0-1 that indicates how heavily connected its nodes are

64
Q

What is a strongly/weakly connected graph?

A

undirected = strong - if path between any 2 vertices
directed = weak - undirected ver. is connected but directed is not

65
Q

What methods would a graph ADT inc.?

A

addNode (Node n)
removeNode (Node n)
addEdge (Node n, Node m)
removeEdge (Node n, Node m)
isAdjacent (Node n, Node m)
getNeighbours (Node n)

66
Q

What are the 2 diff. graph implementations?

A

list of lists or adjacency matrix

67
Q

What are the complexities for graph list implementation?

A

N = nodes, E = edges
mem. = O(N+E)
TC = O(1) for addNode + O(N) for rest

68
Q

What is an adjacency matrix?

A

2D array that can represent graphs
1 indicates edge between nodes, 0 doesn’t
symmetric for undir. so only need to modify 2 cells per edge
only modify 1 cell for dir. graphs

69
Q

What are the complexities for graph adjacency matrix implementation?

A

N = nodes, E = edges
mem. = O(N^2)
TC = O(N^2) for add/removeNode + O(1) for rest

70
Q

What are the ways to traverse a graph?

A

depth-first or breadth-first

71
Q

How does depth-first graph traversal work?

A

using stack to push once visited + pop to backtrack
start from source then visit current node’s neighbours till hit an alr. visited
backtrack + continue

72
Q

How does breadth-first graph traversal work?

A

using queue to enqueue then dequeue once fully visited
start at source + visit all nodes at distance 1, then 2, etc.

can form a BFS tree out of list of traversed edges to find shortest paths

73
Q

Define path:

A

sequence of non-repeating nodes from node x to y

74
Q

What are the 3 shortest path problems?

A

given a graph G = (V, E) + source node s…
shortest distances (to s) = distance from all nodes v ∈ V to s
shortest paths (to s) = for each node v ∈ V the shortest path from v to s
pathfinding (s to target) = shortest path from s to t

75
Q

How to find shortest path for unweighted graphs?

A

path that inc. least no. of edges - find using BFS

76
Q

What are the algorithms for solving shortest path problems?

A

Dijkstra’s
- extended to A*, Bellman-Ford + Floyd-Warshall

77
Q

How do you find shortest distance using Dijkstra’s?

A

initialise visited[] to not + distToSource[] to 0 for source + infinity for rest
visit closest nodes to you then for all its neighbours if path is shorter than update it
do till all nodes = visited

78
Q

How do you find shortest path using Dijkstra’s?

A

visited[] = not, distToSource[] = 0/infinity, prevNode[] = null
do the same as shortest distance +record prevNode accordingly

79
Q

How do you compute pathfinding using Dijkstra’s?

A

set up arrays as per
visit closest unvisited nodes + return path + distance once you find target
path = prevNode[node], prevNode[prevNode[node], etc.

80
Q

What is the TC of Dijkstra’s algorithm?

A

worst case = O(n^2)
can improve to O(m + n log n) if using better data structs. like priority queues/SBBST

81
Q

When can we use A* algorithm?

A

have extra info (heuristic) on distances for some nodes to source

82
Q

When can we use Bellman-Ford algorithm?

A

have edges w/ negative weights (but not cycles)

83
Q

How does A* search work?

A

uses heuristics to guide the search
e.g. if weights represent actual travel distances then can approximate to straight line dist.

84
Q

How does Bellman-Ford algorithm work?

A

compute (over)estimations of the distances from source → all other nodes which you iteratively improve on
improves all edges every iteration rather than picking 1 each time like Dijkstra’s

85
Q

What is the worst case TC of the Bellman-Ford algorithm?

A

O(n ⋅ m)

86
Q

What is a greedy algorithm?

A

solve your problem in stages + in each stage choose the locally optimal choice

fast (approximation) but can be incorrect/non-optimal

87
Q

What is an algorithmic paradigm?

A

generic framework that underlies a class of algorithms
e.g. divide + conquer, greedy + recursion

88
Q

What are some problems solved by greedy algorithms?

A

travelling salesman, shortest path + vertex 3-colouring

89
Q

How does a greedy solution to the shortest path problem work?

A

same as Dijkstra’s
start at source + visit closest unvisited till all are visited

90
Q

How does a greedy solution to the travelling salesman problem work?

A

repeatedly visit nearest node to current node + once all visited go back to origin
!shortest route but correct

91
Q

How does a greedy solution to vertex 3-colouring work?

A

for i = 1 to V, choose for node v[i] the smallest colour that no neighbour has chosen yet

(CANNOT solve vertex 2-colouring though)

92
Q

What are the greedy algorithms regarding minimum-weight spanning trees?

A

Kruskal’s + Prim’s algorithms

93
Q

How does Kruskal’s algorithm work?

A

start w/ tree T = ∅, sort edges from lowest - highest weight
for i = 1 to [E], if edge e[i] doesn’t form a cycle when added to T then add it to T, or also T = T ∪ {e[i]}

94
Q

Kruskal TC:

A

takes O(|E|log|V|) worst case TC

95
Q

How does Prim’s algorithm work?

A

start w/ V[t] = v[1] and E[r] = ∅
while V[t] ≠ V, find min. weight edge {u, w} going from node in T to node outside T, add {u, w} to E[t], w/o loss of generality, u ∈ T, then add w to V[t]

96
Q

Prim’s worst case TC:

A

O(|E| + |V| log|V|) - best implementation
O(|V|^2) or O(|E| log|V|) - easier implementation