Algorithms Flashcards
Lomuto’s
- start with s at pivot and i at s+1
- when item is smaller than pivot, increment s and then swap s with i
QuickSort
if(left < right):
split = partition
sort left half and right half
ways to improve QuickSort
- better pivot selection
- do insertion sort on smaller sets
- eliminate recursion
selection (find nth biggest value)
- sort set
- find nth value
quickselect (find nth biggest value)
- choose pivot
- partition & place pivot
- if n < pivot, search in left half
- if n > pivot, seach in right half
- else, you found it!
Master Theorem
T(n) = aT(n/b) + n^d
a < b^d: theta (n^d)
a = b^d: theta (n^d logn)
a > b^d: theta (n^(logb (a) ) )
Closest Pair
- sort points by x coordinate
- divide set into left and right halves on graph
- find the closest pair in each half & assign the smaller distance to D
- put points on within either side of the vertical line by a distance of D in a separate array
- sort this array by y coordinates
- for each point, consider only pairs that are within D distance away from each other
- find the closest pair in this set and set its distance to D’
- the closest pair is one of the minimum between D and D’
Generic Graph Search
- mark source as explored, all others unexplored
- while there is an edge (v, w) with v explored & w unexplored:
- choose some edge w (in an alg specific way, depending)
- mark w as explored
Proof of GraphGenericSearch
What is BFS (Breadth-First Search)?
looks at a graph in layers. layer 0 is the root, layer 1 has the neighbors of the root, layer 2 has the neighbors of the neighbors, etc.
- best used for shortest path distances
How to implement BFS in linear time (O(n)) using a queue?
- mark the source explored, initialize Q
- while Q is not empty:
- remove the vertex from the front of the queue and call it v
- for each edge (v, w) in v’s adj list:
- if w is unexplored, then add it to the end of Q and mark it explored
What is a Queue?
First in - first out DS
What is Depth-First Search (DFS)?
go as deep as you can and only backtrack when necessary
- special case of generic graph search
How to implement DFS in linear time (O(n)) using recursion?
- mark source explored
- for each edge in S’s adj list, if V is unexplored, then DFS (G, V)
How to implement DFS in linear time (O(n)) using a stack?
- mark all vertices unexplored, initialize a stack
- while S is not empty:
- pop V from front of S
- if V is unexplored, then mark it explored. for each edge in v’s adj list, push W to the front of S