Professional Documents
Culture Documents
Output: breadth first tree Output: forest with tree, back, forward, cross edges
Uses: Uses:
Find shortest path to source vertex Topological sort by outputting vertices in decreasing order
of finish time
Works on unweighted graphs, dags and undirected
graphs Find SCCS:
1.Do a DFS on G and record finish times.
2.Do a DFS on G_reversed, processing vertices in order of
decreasing finish times from 1.
3. Output each DFS forest in 2 as a strongly connected
component of G
Greedy Proofs
This algorithm progresses as quickly as any other For the sake of contradiction , assume that there is some
algorithm. For every k, the first k jobs that the greedy optimal sequence of jobs, S_o, that finished earlier than
solution picks are the k shortest jobs. Suppose the greedy the sequence produced by our greedy algorithm, S_g. Let
solution ends up picking the L shortest jobs whose total g_i and o_i be the first sets that differ between S_o and
time is less than or equal to T. It couldn’t add the (L+1)th S_g. We know g_i won’t overlap with prior jobs in S_o.
job since this would bring total time over T. So any set L+1 Due to the ordering of the greedy solution, g_i finishes
jobs must have total time > T. Therefore the greedy before or at the same time as o_i. So we can replace o_i
algorithm is optimal with g_i and still have an optimal solution. We can continue
this way until S_g=S_o. So S_o cannot finish before S_g
since it is equal to S_g. This contradicts the assumption,
and therefore there is no sequence that finishes before the
greedy sequence, so S_g is the best possible sequence.
Huffman coding
Properties: as depth of leaves decrease, frequency of symbols decreases. Every node has two children. Full binary tree
Cut property If E is the lightest edge between a subset of vertices and the rest of the graph, then every MST
contains E.
Cycle property If edge E is the maximum weight edge of a cycle, no MST contains E
Other MST -If e is any edge of minimum weight in G, it is part of some MST
Properties -If G contains a unique edge of min weight, it is part of every MST.
-If e is part of some MST of G, it is an edge of min weight across some cut of G
- Multiplying every weight by positive constant, adding a constant, or changing to 2^w wont change
MST, but changes shortest path from source bc may greatly increase the weight of paths that use lots
of edges
Runtime: O[ (n+m)log(n)]
Weighted activity selection : A set s of n activities where each activity a_i has start time s_i and finish time f_i and weight
w_i. Find a maximum weight set of non overlapping activities
proof: proof:
Given n keys (sorted) k1<k2<kn with probability of each key being access p1<p2<pn
Build binary search tree that minimizes average access time
Runtime: n^3
Find all pairs shortest path(the minimum distance from any -Find shortest path from source with negative edge
node to all other nodes in the graph) with negative edge weights
weights (assume no neg cycles) -Negative Cycle detection
dijk is weight of shortest path from vertices i to j for which Bellman-Ford (G,w, s): # s is source vertex
all intermediate vertices are in the set {1...k} For each vertex v in G:
v.distance= infinity
v.predecessor= none
Floyd-Warshall( Weighted_adjacency_matrix W): s.distance=0
n=W.rows For i=1 to V-1
D0=W # D is matrix of shortest path weights For each edge (u,v) in E:
For k=1 to n: If v.distance>u.distance+weight(u,v):
Let Dk = dijk be a new n*n matrix v.distance=u.distance+weight(u,v)
For i=1 to n: v.predecessor=u
For j=1to n:
dijk =min(dijk-1, dikk-1+dkjk-1) For each edge (u,v) in E:
If v.distance>u.distance+weight(u,v):
Return Dn
Return “negative weight cycle reachable from s”
Return “NO negative weight cycle reachable from s”
Runtime : O(n^2)
Sequence alignment
Depth First Search -Topological Sort O(n+m) -DFS Forest (# trees based on order
(DFS) Algorithm (DAGs processed)
only) n = vertices, -start/finish times for each vertex
m = edges -edge types
-Strongly Connected
Component Algorithm
O(m)
(connected graph,
m>= n-1)
Kruskal’s Algorithm Minimum Cost Spanning Greedy O(m log n) Undirected O (+/-)
Tree (MST) ONLY
Prim’s Algorithm Minimum Cost Spanning Greedy O(m log n) Undirected OK (+/-)
Tree (MST) 2 ONLY
(w/min heap, otherwise O(n ))
-Select min cost edge
-Select min edge connected
to current tree unless it forms
a cycle & repeat until
E’=|V|-1