Graph Algorithms
Undirected Graphs
- Finds shortest path
- Can be used to detect graph is bipartite
- Shortest paths encoded int he BFS tree
- Non-tree edges in adjacent layers
DFS - Parenthesis lemma:
- Start and finish time intervals are either disjoint, or one contains the other
- Non-tree edges in DFS tree must be back edges
- Checks for cut vertices or cut edges
Directed Graphs
- BFS still gives shortest paths from source
- BFS and DFS trees have less structure
- Parenthesis lemma still holds for DFS tree
- Directed Acyclic Graphs
- Any directed graphs is a DAG of strongly connected components
- Linear time algorithm to find all SCCs!
Minimum Spanning Trees
Greedy for the rescue!
- Boruvka’s Algorithm
- Pick cheapest edge from a vertex, and contract it
- Recurse on contracted graph
- Cut property:
- Given any cut
, there is an MST containing edge with smallest weight across cut
- Given any cut
- Prim’s Algorithm
- Start from a arbitrary vertex and grow connected component one vertex at a time
- Kruskal’s Algorithm
- Consider edges from cheapest to most expensive, add edge to solution so long as it does not create a cycle
- needs UNION-FIND for that last step
All of the above can be assumed to run intime
Shortest Paths
- Single-source, all weights non-negative: Dijkstra’s Algorithm
- Start from source, and build shortest paths by adding one vertex at a time
- Runtime
- Single-source, arbitrary weights: Bellman-Ford Algorithm
- Cannot do greedy because of negative weights
- DP to the rescue
- Subproblems $D[v,i] := $ captures shortest
→ distance using at most edges - Runtime
- All-pars shortest-paths, (arbitrary weights, no negative cycles): Floyd-Warshall Algorithm
- Subproblems:
shortest → path using only as intermediate vertices - Runtime
- Subproblems:
Max-Flow & Min-Cut
Let
- Flows:
satisfying: - Capacity:
for all - Conservation:
for all - Value:
- Capacity:
- Flow decomposition theorem
- Any integral flow
of value can be decomposed into paths cand cycles such that each appears in exactly of the paths and cycles
- Any integral flow
- Cuts
- A cut is a partition of the vertices into two sets
- Capacity of a cut:
is the total capacity coming out of
- Capacity of a cut:
- A cut is a partition of the vertices into two sets
- Max-Flow Min-Cut Theorem
- The value of the maximum flow equals the minimum capacity of a cut
- Ford-Fulkerson
- Keep finding
→ paths in residual graph, when there is none, found a max-flow and a min-cut
- Keep finding