Algorithms Flashcards

(33 cards)

1
Q

Lomuto’s

A
  1. start with s at pivot and i at s+1
  2. when item is smaller than pivot, increment s and then swap s with i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

QuickSort

A

if(left < right):
split = partition
sort left half and right half

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

ways to improve QuickSort

A
  1. better pivot selection
  2. do insertion sort on smaller sets
  3. eliminate recursion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

selection (find nth biggest value)

A
  1. sort set
  2. find nth value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

quickselect (find nth biggest value)

A
  1. choose pivot
  2. partition & place pivot
  3. if n < pivot, search in left half
  4. if n > pivot, seach in right half
  5. else, you found it!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Master Theorem
T(n) = aT(n/b) + n^d

A

a < b^d: theta (n^d)
a = b^d: theta (n^d logn)
a > b^d: theta (n^(logb (a) ) )

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

Closest Pair

A
  1. sort points by x coordinate
  2. divide set into left and right halves on graph
  3. find the closest pair in each half & assign the smaller distance to D
  4. put points on within either side of the vertical line by a distance of D in a separate array
  5. sort this array by y coordinates
  6. for each point, consider only pairs that are within D distance away from each other
  7. find the closest pair in this set and set its distance to D’
  8. the closest pair is one of the minimum between D and D’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Generic Graph Search

A
  1. mark source as explored, all others unexplored
  2. while there is an edge (v, w) with v explored & w unexplored:
  3. choose some edge w (in an alg specific way, depending)
  4. mark w as explored
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Proof of GraphGenericSearch

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

What is BFS (Breadth-First Search)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to implement BFS in linear time (O(n)) using a queue?

A
  1. mark the source explored, initialize Q
  2. while Q is not empty:
  3. remove the vertex from the front of the queue and call it v
  4. for each edge (v, w) in v’s adj list:
  5. if w is unexplored, then add it to the end of Q and mark it explored
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Queue?

A

First in - first out DS

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

What is Depth-First Search (DFS)?

A

go as deep as you can and only backtrack when necessary
- special case of generic graph search

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

How to implement DFS in linear time (O(n)) using recursion?

A
  1. mark source explored
  2. for each edge in S’s adj list, if V is unexplored, then DFS (G, V)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to implement DFS in linear time (O(n)) using a stack?

A
  1. mark all vertices unexplored, initialize a stack
  2. while S is not empty:
  3. pop V from front of S
  4. if V is unexplored, then mark it explored. for each edge in v’s adj list, push W to the front of S
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a Stack?

A

Last in - first out DS

17
Q

What is topological sort?

A

an ordering that follows the directions in a directed graph
- can only occur in graphs that do not have directed cycles

18
Q

How to use DFS to compute topological sort of a directed acyclic graph?

A
  1. mark all vertices explored
  2. curlabl
19
Q

What is a strongly connected component (scc)?

A

A maximum subset of vertices such that there is a directed path from any vertex to any other vertex in the subset

20
Q

How to use DFS to compute the strongest connected components of a directed graph?

A

Kosaraju’s algorithm:
1. Grev = graph with directions reversed
2. Call DFS on each V of Grev to find position f(v) for V
3. Call DFS on each V of G from highest to lowest f(v)

21
Q

Kosaraju pseudocode

A
  1. First pass of DFS computes f(v): TopoSort(Grev)
  2. Second pass of DFS finds SCC in reverse topological order
    Mark all V unexplored, #scc = 0
    For each vertex, in increasing order of f(v):
    If V not explored, then SCC++ & DFSSCC (G,v)
22
Q

2 basic operations of heaps

A
  1. Insert into heap
  2. Extract min (value with smallest key)
23
Q

When to use a heap?

A

When application requires fast min or max computations on a dynamically changing set

24
Q

Heapsort

A
  1. Insert all items into heap
  2. Extract min n # of times
25
Dijkstra's
1. initialize a table with source = 0, everything else = ∞ 2. for the node of focus, update the adj nodes with their cost. if sum thus far + weight < previously assigned weight, change it. otherwise dont. 3. pick the smallest value node 4. continue
26
Prove Dijkstra's
see image
27
Scheduling Problem: specifies the order in which to do certain jobs for fastest completion time
total time = work*(sum of times before it) + work* .... and so on
28
What to do when there are 2 attributes in greedy algorithms?
check if differences or ratios work better
29
cut and paste technique
the optimal solution is made up of optimal subsolutions
30
steps to the cut and paste proof
1. lets assume the algorithm is not optimal 2. then lets replace the suboptimal subsolutions with optimal* ones 3. by assumption, the solutions is already comprised of optimal solutions. 4. therefore the algorithm must be optimal
31
memoization
1. fill in the table 2. traceback
32
Prim's
1. choose a source 2. follow 3 - 4 until done 3. identify fringe vertices 4. choose minimum out of those and add it to MST if it DOES NOT form a cycle 5. return & exit
33
kruskals
1. sort edges in increasing weight order 2. keep picking smallest edge while cycle is not created