Introduction to Algorithms Syllabus Recap on Graphs: un/directed, weighted Shortest Paths: single-source, all-pairs Minimum Spanning Tree: Prim, Kruskal Maximum Flow: Ford-Fulkerson, Edmonds-Karp Maximum (weighted) Bipartite Matching Minimum Cut graph recap Basic graph concepts: E V V directed edges simple: no multi-edges nor loops E symmetric: undirected edges in-/out-/degree /0: absent (un-/directed) path w:e edge weights 1: present (strongly) connected component subgraph, induced graph Handshaking lemma: #E = v V indeg(v) = v V outdeg(v) Adjacency/weight matrix A G V V Powers of A G
graph examples diameter? planar? Connectedness ; s,t V DFS(v) // Is t reachable in G from v? If v is marked visited Return (false); If v=t Return (true); Mark v as visited; For each neighbor u of v do if DFS(u) Return (true); Return (false); Adjacency/weight matrix A G Output: Is there a (directed) path from s to t in G? Reachable(G,s,t) For each vertex v V Mark v as unvisited; Return DFS(s) A u,v = no edge A u,u = 0
Shortest path(s) ; s,t V s V Adjacency/weight matrix A G Output: For every t V, weight d(s,t) of lightest path from s to t. Dijkstra s s Algorithm: Mark all vertices Initialize Q:= :=V. set of unvisited vertices vertices unvisited visited. For each vertex v let d v := ; d s :=0. While Q do Extract from Q a vertex u with least d u. Mark u as visited. For each unvisited neighbor u of v do If d':= :=d u +A uv < d v tentative distance from s Correctness??? then decrease d v :=d' d'. O(n extractmin+ m decreasekey) array array O(n n+m 1) All shortest paths Adjacency/weight matrix A G Output: For every t V, weight d(s,t) of lightest path from s to t. Loop invariant d v d(s,v). Suppose M := { v : d v > d(s,v) ) }. Then δ := min{ d(s,v) ) : v M } and v M For (s,, u, v) Thus d(s,v)= d(s,u)+ )+A uv with d(s,v)= )=δ exist. a lightest path to v, it holds δ > d(s,u) = d u. and u gets extracted from Q before v. While Q do Extract from Q a vertex u with least d u. Mark u as visited. For each unvisited neighbor u of v do in increasing If d':= :=d u +A uv < d v then decrease d v :=d' d'. order w.r.t. d For correctness, recall main loop: While
All shortest paths Adjacency/weight matrix A G Output: For every all s,t V, weight d(s,t) of lightest path from s to t. Floyd-Warshall Algorithm: For all pairs (u,v)) of vertices, initialize d u,v For each vertex u V For each vertex v V For each vertex w V Correctness? If d v,w > d v,u + d u,w d v,w := d v,u then v,u + d u,w u,v :=A u,v A u,v = no edge A u,u = 0 runtime O(n³) Min. Spanning Tree Symmetric Output: T E spanning tree of least weight s.t. (V,T) connected A u,v = no edge A u,u = 0
Prim s Algorithm Symmetric Output: T E spanning tree of least weight 1. Initialize a tree with a single vertex, chosen arbitrarily from the graph. 2. Grow the tree by one edge: Of the edges that connect the tree to vertices not yet in the tree, find the minimum-weight edge, and transfer it to the tree. 3. Repeat step 2 (until all vertices are in the tree). 11 6 3 6 1 10 12 11 3 12 5 4 10 1 5 2 7 4 2 Prim s Algorithm Symmetric Output: T E spanning tree of least weight Initialize F:= :=, Q:= :=V.. Also: d v := and e v :=0 for all v V. While Q do Extract from Q a vertex u with least d u. If e u 0, add edge (u,e u ) to F. For each neighbor v Q of u do If A uv < d v then decrease d v := A uv ; e v :=u; 11 6 3 6 O(n extractmin+ m decreasekey) 10 1 12 11 3 12 5 4 10 1 5 2 7 4 2
Kruskal Algorithm Symmetric Initialize the forest (=set of trees) with edges F:={} :={},, i.e., such that each vertex v V is a separate tree. While E {} and F is not yet spanning: Extract from E edge e of least weight. If e connects two different trees of F then add e to F,, thus? combining two trees into a single one. Output: T E spanning tree of least weight 11 6 3 O(1) after initial sorting 1 10 12 5 7 7 4 Max Flow Input: s,t V, A G Output: f:e max. flow from s to t Goal: maximize v:( :(s,v) E f(s,v) = u:( :(u,t) E f(u,t) f flow (from s to t) Lemma: There exists an integral maximal flow. Def: A flow from s to t in G with weights A 0 is a function f:e such that v V\{s,t}: u:(u,v) E f(u,v) = w:(v,w) E f(v,w). It is admissible if it holds f(u,v) A u,v
Ford-Fulkerson Input: s,t V, A G Output: f:e max. flow from s to t Goal: maximize f := := The residual G v:( :(s,v) E f(s,v) f of a graph G with flow f has edges E f := { (u,v) : A u,v > f(u,v) f(v,u) > 0 } Def: Ford-Fulkerson A Fulkerson: flow from s to Initialize t in G with f 0. weights Correctness? A 0 is Runtime a function Termination? O(m f ) ) f:e While there such exists that v V\{s,t}: a path P = (s=u u:(u,v) E 1, u K f(u,v) =t) from = s w:(v,w) E to t in Gf(v,w). f It is Let admissible α := min{ if Ait uk holds f(u,v) A,u k+1 +1 f(u k,u k+1 +1) ) : k=1 K 1 u,v 1 } and f := f+α P. Edmonds-Karp Input: s,t V, A G Output: f:e max. flow from s to t Goal: maximize f := := The residual G v:( :(s,v) E f(s,v) f of a graph G with flow f has edges E f := { (u,v) : A u,v > f(u,v) f(v,u) > 0 } Edmonds-Karp Ford-Fulkerson Fulkerson: Karp: Initialize f 0. shortest Runtime O(n m²) O(m f ) ) While there exists a path P = (s=u 1, u K =t) from s to t in G f Let α := min{ A uk,u f(u k+1 k,u k+1) ) : k=1 K 1 1 } and f := f+α P.
Specification: Bipartite graph G=(U,V,E) Output: F E max. (weighted) matching U max. Bipartite Matching U V V Edmonds-Karp Ford-Fulkerson Fulkerson: Karp: Initialize f 0. shortest Runtime O(n m²) While there exists a path P from s to t in G f Let α := min{ A uk,u k+1 +1 f(u k,u k+1 +1) ) : k=1 K 1 1 } and f := f+α P. Min Cut Input: s,t V, A G Output: C E min.cut between s,t Def: A cut from s to t in G is a subset C V s.t. s C, t C. It has capacity Theorem: capacity λ(c) = (u,v) E min λ(c) = C cut (s,t) Proof " ":": For every C,f: λ(c) f.. " ":": Consider C V all vertices reachable Goal: minimize imize λ(c) : C cut (s,t) from s in G f for max. f from Ford-Fulkerson. Fulkerson. u C,v C max f f flow (s,t) A u,v