.121 15-20 Flashcards

(103 cards)

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
What is the time complexity of an insertion sort?
O(n^2) in all cases
22
How does a merge sort work?
recursively splits array into subarrays till there is just 1 item per array then merges subarrays together by comparing the heads of each
22
Where does a merge sort split arrays?
(start index + end index + 1) /2
22
How does a quick sort work?
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
22
What is the time complexity of a merge sort?
O(nlogn) in all cases
23
What is the time complexity of a quick sort?
O(nlogn) in best + avg case but O(n^2) in worst case
24
Define a tree:
non-linear ADT that stores elements hierarchially as nodes connected by edges w/ NO cycles or loops
25
What are some uses of trees?
file directory structures, XML + HTML, compilers and collision detection
26
What are the relationships within a tree?
root = top node (no parents) children = node w/ parents leaf = node w/ no children descendants/ancestors - relative of relative of
27
What is the height of a node?
no. of nodes of longest path from given node to some leaf
28
What is the depth of a node?
number of nodes from given node to root
29
What is the height of a tree?
height of its root/depth of its deepest nodes
30
What is the diameter/width of the tree?
no. of nodes on longest path between any 2 leaves
31
How many edges does a tree w/ n nodes have and why?
n-1 edges as n nodes each have 1 edge going to them other than root
32
What is a subtree?
taking a node + its descendants from a full tree basically part of a tree
33
What is a k-ary tree?
tree that imposes a max no. of children to each node e.g. binary = max 2
34
What is a balanced tree?
for any node, the height of its child subtrees differ by ≤ 1
35
What functions would a tree ADT class have?
void add (Object new, Object parent) Tree find(Object val) Tree remove(object n) void move(Object n, Object parent)
36
What happens to the children of moved/removed nodes?
children become the children of that node's parents instead
37
What is the significance of find() in tree ADT?
used to search for nodes + used in move and remove functions
38
What do tree traversal algorithms do?
used to enumerate all elements in a tree or decide in which way we look through a tree to find elements
39
How does pre-order traversal work?
dot to left CLR
40
How does post-order traversal work?
dot to right LRC
41
How does in-order traversal work?
dot below LCR
42
What do pre-order, post-order + in-order traversal have in common?
all based on a depth-first search so can be implemented non-recursively using a stack or recursively
43
How does breadth-first traversal work?
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
Give some examples of index systems:
DNS lookup compiler packet routing table dictionary
45
What methods does a dictionary ADT inc.?
void insert (key k, value v) void delete (key k) value find (key k)
46
What are common implementations of a dictionary ADT?
sorted arrays, search trees + hashing
47
What is the difference between using sequential allocation or sorted arrays (dictionary ADT)?
sequential - insert = O(1), find + checking for duplicates = O(n) sorted - find = O(logn), insertion + deletion = O(n) given sorting
48
What is a binary search tree?
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
What functions does a BST ADT need?
delete() - any children will become children of their ancestor insert() find() - if smaller (go left), if bigger (go right)
50
Why do BSTs need to be balanced?
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
What are the types of self-balancing search trees?
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
What are some self-balancing trees that are not height-balanced?
splay trees + treaps - helpful for if you wanted tree to balance for common letter patterns rather than any letter sequence
53
What are the cons of 2-3 trees?
inconvenient to implement -diff. node types - multiple compares per node - moving back up tree to split 4-nodes
54
What is a 2-3 tree?
2 node = 1 key, 2 children 3 node = 2 keys, 3 children same find() function but more complex insert()
55
What is different about a 2-3 tree insert()?
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
What is a graph?
set of nodes + edges that capture relationships
57
What is a simple graph?
graph w/ no self-loops
58
How can graph edges be detailed?
may be directed (arrows) or undirected (every edge = bidirectional) + weighted (associated values on edge)
59
What is a hypergraph?
nodes + hyperedges to capture k-wise relationships ( k > 2) rather than usual pairwise
60
What is a subgraph?
subset of nodes + edges of a graph
61
What is a spanning subgraph/tree?
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
What is a complementary graph?
same set of nodes + contains all the edges NOT in the other graph
63
Explain graph density:
measure from 0-1 that indicates how heavily connected its nodes are
64
What is a strongly/weakly connected graph?
undirected = strong - if path between any 2 vertices directed = weak - undirected ver. is connected but directed is not
65
What methods would a graph ADT inc.?
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
What are the 2 diff. graph implementations?
list of lists or adjacency matrix
67
What are the complexities for graph list implementation?
N = nodes, E = edges mem. = O(N+E) TC = O(1) for addNode + O(N) for rest
68
What is an adjacency matrix?
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
What are the complexities for graph adjacency matrix implementation?
N = nodes, E = edges mem. = O(N^2) TC = O(N^2) for add/removeNode + O(1) for rest
70
What are the ways to traverse a graph?
depth-first or breadth-first
71
How does depth-first graph traversal work?
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
How does breadth-first graph traversal work?
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
Define path:
sequence of non-repeating nodes from node x to y
74
What are the 3 shortest path problems?
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
How to find shortest path for unweighted graphs?
path that inc. least no. of edges - find using BFS
76
What are the algorithms for solving shortest path problems?
Dijkstra's - extended to A*, Bellman-Ford + Floyd-Warshall
77
How do you find shortest distance using Dijkstra's?
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
How do you find shortest path using Dijkstra's?
visited[] = not, distToSource[] = 0/infinity, prevNode[] = null do the same as shortest distance +record prevNode accordingly
79
How do you compute pathfinding using Dijkstra's?
set up arrays as per visit closest unvisited nodes + return path + distance once you find target path = prevNode[node], prevNode[prevNode[node], etc.
80
What is the TC of Dijkstra's algorithm?
worst case = O(n^2) can improve to O(m + n log n) if using better data structs. like priority queues/SBBST
81
When can we use A* algorithm?
have extra info (heuristic) on distances for some nodes to source
82
When can we use Bellman-Ford algorithm?
have edges w/ negative weights (but not cycles)
83
How does A* search work?
uses heuristics to guide the search e.g. if weights represent actual travel distances then can approximate to straight line dist.
84
How does Bellman-Ford algorithm work?
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
What is the worst case TC of the Bellman-Ford algorithm?
O(n ⋅ m)
86
What is a greedy algorithm?
solve your problem in stages + in each stage choose the locally optimal choice fast (approximation) but can be incorrect/non-optimal
87
What is an algorithmic paradigm?
generic framework that underlies a class of algorithms e.g. divide + conquer, greedy + recursion
88
What are some problems solved by greedy algorithms?
travelling salesman, shortest path + vertex 3-colouring
89
How does a greedy solution to the shortest path problem work?
same as Dijkstra's start at source + visit closest unvisited till all are visited
90
How does a greedy solution to the travelling salesman problem work?
repeatedly visit nearest node to current node + once all visited go back to origin !shortest route but correct
91
How does a greedy solution to vertex 3-colouring work?
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
What are the greedy algorithms regarding minimum-weight spanning trees?
Kruskal's + Prim's algorithms
93
How does Kruskal's algorithm work?
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
Kruskal TC:
takes O(|E|log|V|) worst case TC
95
How does Prim's algorithm work?
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
Prim's worst case TC:
O(|E| + |V| log|V|) - best implementation O(|V|^2) or O(|E| log|V|) - easier implementation