Review of some graph algorithms • Graph G(V,E) (Chapter 22) – Directed, undirected – Representation • Adjacency-list, adjacency-matrix • Breadth-first search (BFS), O(V+E) • Depth-first search (DFS), O(V+E), recursive – Topological Sort – Strong connected components 1 Review of some graph algorithms • Minimum Spanning Tree (Chapter 23) – Greedy algorithm – Kruskal’s algorithm: O(ElgV) • Using disjoint set algorithm, similar to connected components – Prim’s algorithm: O(E+VlgV) • Similar to Dijkstra, using min-priority QUEUE • Single-source shortest paths (Chapter 24) – Bellman-Ford algorithm: O(VE) • dj=min{dj,dk+w(j,k)} where k is j’s neighbor – Dijkstra’s algorithm: O(E+VlgV) • Find the closest node n1 which is s’s neighbor, modify other nodes distance • Find the second closest node n2 which is the neighbor of s or n1, modify the distance • Find the third closest node n3, which is the neighbor of s, n1, or n2, ,…. 2 Graph algorithms (cont.) • All-pairs shortest paths (Chapter 25) – Floyd-Warshall algorithms: O(V3). – Dynamic programming, similar to Matrix Chain Multiplications – dij(0)=wij – For k1 to n • for i1 to n – for j1 to n » dij(k)=min(dij(k-1),dik(k-1)+dkj(k-1)) 3 Maximum Flow (chap. 26) • Max-flow problem: – A directed graph G=<V,E>, a capacity function on each edge c(u,v) 0 and a source s and a sink t. A flow is a function f : VVR that satisfies: • Capacity constraints: for all u,vV, f(u,v) c(u,v). • Skew symmetry: for all u,vV, f(u,v)= -f(v,u). • Flow conservation: for all uV-{s,t}, vV f(u,v)=0, or to say, total flow out of a vertex other s or t is 0, or to say, how much comes in, also that much comes out. – Find a maximum flow from s to t. – Denote the value of f as |f|=vVf(s,v), i.e., the total flow out of the source s. • |f|=uVf(u,t), i.e., the total flow into the sink t. 4 Example of max-flow problem 5 Ford-Fulkerson method • Contains several algorithms: – Residue networks – Augmenting paths 6 Residual Networks • Given a flow network G=<V,E> and a flow f, – the residual network of G induced by f is Gf=<V,Ef> where Ef={(u,v)VV: cf(u,v)=c(u,v)-f(u,v), and cf(u,v)>0} – a network with left capacity >0, also a flow network. 7 Residual network and augmenting path 8 Residual network and flow theorem • Lemma 26.2 (page 653): – Let G=<V,E> be a flow network with source s and sink t, and let f be a flow, – Let Gf be the residual network of G induced by f, and let f' be a flow of Gf. – Define the flow sum: f+f' as: – (f+f')(u.v)=f(u.v)+f'(u.v), then – f+f' is a flow in G with value |f+f'|=|f|+|f'|. • Proof: – Capacity constraint, skew symmetry, and flow conservation and finally |f+f'|=|f|+|f'|. 9 Augmenting paths • Let G=<V,E> be a flow network with source s and sink t, and let f be a flow, • An augmenting path p in G is a simple path from s to t in Gf, the residual network of G induced by f. • Each edge (u,v) on an augmenting path admits some additional positive flow from u to v without violating the capacity constraint. • Define residual capacity of p is the maximum amount we can increase the flow: – cf(p)=min{cf(u,v): (u,v) is on p.} 10 Augmenting path • Lemma 26.3 (page 654): – Let G=<V,E> be a flow network with source s and sink t, let f be a flow, and let p be an augmenting path in Gf. Define fp: VVR by: • fp(u,v)= cf(p) if (u,v) is on p. • -cf(p) if (v,u) is on p. • 0 otherwise – Then fp is a flow in Gf with value |fp|=cf(p) >0. • Corollary 26.4 (page 654): – Define f'=f+fp, then f' is a flow in G with value |f'|=|f|+|fp|>|f|. 11 Basic Ford-Fulkerson algorithm Running time: if capacities are in irrational numbers, the algorithm may not terminate. Otherwise, O(|E||f*|) where f* is the maximum flow found by the algorithm: while loop runs f* times, increasing f* by one each loop, finding an augmenting path using depth12 first search or breadth-first search costs |E|. Execution of Ford-Fulkerson 13 An example of loop |f*| times Note: if finding an augmenting path uses breadth-first search, i.e., each augmenting path is a shortest path from s to t in the residue network, while loop runs at most O(|V||E|) times (in fact, each edge can become critical at most |V|/2-1 times), so the total cost is O(|V||E|2). Called Edmonds-Karp algorithm. 14 Network flows with multiple sources and sinks • Some problems can be reduced to maximum flow problem. Here give two examples. • Reduce to network flow with single source and single sink • Introduce a supersource s which is directly connected to each of the original sources si with a capacity c(s,si)= • Introduce a supersink t which is directly connected from each of the original sinks ti with a capacity c(si,s)= 15 Maximum bipartite matching • Matching in a undirected graph G=(V,E) – A subset of edges ME, such that for all vertices vV, at most one edge of M is incident on v. • Maximum matching M – For any matching M′, |M|| M′|. • Bipartite: V=LR where L and R are distinct and all the edges go between L and R. • Practical application of bipartite matching: – Matching a set L of machines with a set R of tasks to be executed simultaneously. – The edge means that a machine can execute a task. 16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 17 Finding a maximum bipartite matching • Construct a flow network G′=(V′,E′,C) from G=(V,E) as follows where =LR: – V′=V{s,t}, introducing a source and a sink – E′={(s,u): uL} E {(v,t): vR} – For each edge, its capacity is unit 1. • As a result, the maximum flow in G′ is a maximum matching in G. 18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 19