CS_Algos Flashcards
1
Q
divide-and-conquer
A
- take a problem break it down into smaller sub-problems
- solve them recursively
- combine the results of the sub-problems to get the solution to the original problem
2
Q
Merge sort
A
- recursive algorithm
- canonical divide-and-conquer:
- split the data in 2 parts,
- solve recursively left half,
- solve recursively right half
- merge the 2 parts
3
Q
Merge sort runtime
A
O(n log n) - at level j in the *recursive tree*: -- 2^j sub-problems -- n /(2^j) items per sub-problem -- upper bound per sub-problem: --- 6 * #items - total lines executed at level j: 2^j * [6 * n / (2^j)] = 6n - total levels from root to leaf in *recursive tree*: log_2(n) + 1 - total lines executed in recursive tree: [log_2(n) + 1] * 6n
4
Q
Asymptotic time complexity
A
- big-O notation: (default) upper bound of the asymptotic computational complexity (worst-case scenario)
- big-Omega notation: lower bound of the asymptotic computational complexity
- big-Theta notation: (tight estimates) upper and lower bounds coincide: the function on the left can be sandwhiched between constant multiples of the function on the right: max[f, g] = Theta(f + g)
5
Q
Counting inversions
A
- inversion in a permutation p is pair i, j w/ i < j for which p(i) > p(j)
- a comparison sorting algos (i.e. merge-sort) can be adapted to count inversions in a sequence
- can be used as a measure of similarity between ratings sequences of 2 users
- merging and counting the split inversions are done in O(n) and the overall sorting and counting in O(n log n)
6
Q
Quick Sort
A
- choose a good pivot
- split the array in 2 parts: (1) < pivot, (2) > pivot
- in linear time and in place (by swapping elements)
- pivot ends up in the right position
- keep the seen elements in the 2 partitions
- use 2 counters: (a) the separator for 2 parts in seen elements, (b) the next element unseen yet
- sort the partition (1)
- sort the partition (2)
7
Q
Random contraction algo
A
- while there are more than 2 vertices in the graph:
- pick a remaining edge (u,v) uniformly at random
- merge u and v into a single node
- remove self-loops and allow for parallel edges
- return the cut represented by the final 2 vertices
8
Q
BFS
A
- explore the entire graph (all nodes) in layers
- used for shortest path discovery (using dist(v) property), connected components (using unexplored property)
- uses a queue, FIFO,
- initialize it with start of the graph
- append new unseen (unxeplored property) item to the end
- extract the next item to process from the front
- stop when the queue is empty
- O(m+n)
9
Q
DFS
A
- explore the entire graph (all nodes) aggresivelly withbacktrack
- used for topological sort, strongly connected components in DAG
- uses a stack, LIFO,
- initialize it with start of the graph
- append new unseen (unxeplored property) item to the front
- extract the next item to process from the front
- stop when the queue is empty
- O(m+n)
10
Q
Topological sort
A
in a DG is a node labelling such that:
- for f(v) in the set {1..m}
- for any edge (u, v), f(u) < f(v)
11
Q
Strongly connected components
A
- for a DG graph
1. reverse the graph
2. on reversed graph do DFS and mark ‘finnishing time’ as new labelling (start with the main sink node, end of graph)
3. on the normal graph with new labelliing do DFS repeatdly and mark all nodes discovered by a DFS with its leader (start node) : a SCC; start with max label/finnishing time from previous step
12
Q
Heap
A
- container of keyed objects with parent’s key smaller than the children keys and the following operations:
- EXTRACT-MIN O(log n)
- INSERT O(log n)
- HEAPIFY with n items batches O(n)
- DELETE O(log n)
- speeds up Djikstra algo to O(m log n)
- the array representation is by levels in a binary tree such that children of node i are 2i and 2i + 1
13
Q
Sorted arrays
A
- static data structure with following operations
- SEARCH O(log n)
- RANK O(log n)
- SELECT O(1)
- MIN / MAX O(1)
- PRED / SUCC O(1)
- OUTPUT values O(n)
14
Q
Balanced binary search trees
A
- similar to sorted arrays but are trees with key of left child smaller than parent’s, and the key of the right child is greater than parent’s
- dynamic data structure with following operations
- SEARCH O(log n)
- RANK O(log n)
- SELECT O(log n)
- MIN / MAX O(log n)
- PRED / SUCC O(log n)
- INSERT O(log n)
- DELETE O(log n
- OUTPUT values O(n)
15
Q
Search tree property
A
- all keys in the left seach subtree are smaller than the current node key
- all keys in the right search subtree are greater than the current node key