Presentation is loading. Please wait.

Presentation is loading. Please wait.

Combinatorics Shortest path Maximum flow & minimum cut

Similar presentations


Presentation on theme: "Combinatorics Shortest path Maximum flow & minimum cut"— Presentation transcript:

1 Combinatorics Shortest path Maximum flow & minimum cut
Bipartite matching

2 Shortest path Given a graph like the following
What is the shortest path from A to F? B E 1 3 3 5 A 5 F D C 1

3 A C E D F B 5 3 1 The BFS tree of the graph A 5 B 5 3 What if we use BFS? D C A-F has distance 14 in the tree (A-B-C-E-F) But the shortest path is 13 (A-B-D-C-E-F) BFS does not give the best solution 3 E 1 F

4 How to traverse a graph in order to find the shortest path? Ideas
If we visit the next closest node (the unvisited node that has a smallest distance from A) in each iteration, then when a node X is visited, the distance to X is the smallest one.

5 Why is that? Suppose that a node X is visited with distance d by such a method. If d is not the shortest distance from A to X, then X is visited earlier. (Any later visit to X must have a longer distance). So when X is first visited, the shortest distance to X is found.

6 The algorithm is then: Let source node as s
While(there is some node not visited){ pick an unvisited node u closest to source with distance d for all neighbors v of u if(d + distance(u,v) < distance(s,v)) set distance(s,v) = d + distance(u,v) }

7 Let’s run the algorithm A B ∞ C D E F
Node Distance A B C D E F B E 1 3 3 5 A 5 F D C 1

8 Let’s run the algorithm A B 5 C ∞ D E F
Node Distance A B 5 C D E F B E 1 3 3 5 A 5 F D C 1

9 Let’s run the algorithm A B 5 C 10 D 8 E ∞ F
Node Distance A B 5 C 10 D 8 E F B E 1 3 3 5 A 5 F D C 1

10 Let’s run the algorithm A B 5 C 9 D 8 E ∞ F
Node Distance A B 5 C 9 D 8 E F B E 1 3 3 5 A 5 F D C 1

11 Let’s run the algorithm A B 5 C 9 D 8 E 12 F ∞
Node Distance A B 5 C 9 D 8 E 12 F B E 1 3 3 5 A 5 F D C 1

12 Let’s run the algorithm A B 5 C 9 D 8 E 12 F 13
Node Distance A B 5 C 9 D 8 E 12 F 13 B E 1 3 3 5 A 5 F D C 1

13 Done!! Node Distance A B 5 C 9 D 8 E 12 F 13 B E 1 3 3 5 A 5 F D C 1

14 Some more notes… This algorithm is called the Dijkstra’s algorithm
Limitation: Edge distance cannot be negative Use priority queue in the implementation

15 A sample implementation…
int dist[N]; int pred[N]; struct C{ int u,c; C(){} C(int a,int b):u(a),c(b){} bool operator<(const C&a) const { return c > a.c; }}; // the graph is represented by // a adjacency list // a vertex a has adjc[a] neighbors // adj[a][0] … adj[a][adjc[a]-1] are // neighbors of vertex // cost[a][b] has edge cost for (a,b) int adjc[N]; int adj[N][N]; int cost[N][N]; bool visit[N]; void dijkstra(int s){ memset(visit,0,sizeof(visit)); memset(dist,0x7f,sizeof(dist)); priority_queue<C> pq; pq.push(C(s,0)); dist[s] = 0; while(pq.size()>0){ C c = pq.top(); pq.pop(); int u = c.u; if(visit[u]) continue; visit[u] = true; for(int i=0;i<adjc[u];++i){ int v = adj[u][i]; if(visit[v]) continue; if(c.c+cost[u][v]<dist[v]){ dist[v] = dist[u] + c.c; pred[v] = u; pq.push(C(v,dist[v])); }}}}

16 The runtime is (E+V)logV
For another single source shortest path algorithm, see Bellman-Ford algorithm Can handle negative edge cost Can detect negative cycle Run time is O(VE) For all pair shortest path algorithm, see Floyd-Warshall algorithm (run time is O(v3)) 117, 658, 721, 318, 423, 929, 157, 10603, 10068, 10801, 10171, 10342, 10356, 10389, 10436

17 Network flow Maximum flow
Consider now you are building the internet, consisting of routers, links and some peers. Each link has a bandwidth, can you determine the maximum data flow between any two peers?

18 Definitions Given Problem Find a maximum flow from s to t Subject to
A graph G = (V,E)‏ Capacity function c: E -> R Defines the maximum flow through the edge Source vertex s, sink vertex t Problem Find a maximum flow from s to t Subject to 0 <= f(u,v) <= c(u,v) (Flow does not exceed capacity) ∑f(u,v) = ∑f(v,w) (Sum of in flow equals sum of out flow for vertex v)

19 Consider the following network
Edge labels in the form: flow / capacity A 0/1 0/1 S 0/1 T 0/1 0/1 B

20 How would you solve it? Ideas Let's do it in a greedy way
Find paths that you can push flow to them until you can't push any more We define the residue capacity to be equal to “capacity – flow” Obviously, we want to find a path with residue capacity > 0 for each edge in the path

21 First algorithm: max_flow = 0
First algorithm: max_flow = 0 while(there is a path p with residue capacity > 0){ determine flow f' that can push along p max_flow += f' forall e ∈ p, f(e) += f' }

22 A sample run of the algorithm
1/1 1/1 S 0/1 T 0/1 0/1 B Maxflow = 1

23 A sample run of the algorithm
1/1 1/1 S 0/1 T 1/1 1/1 B Maxflow = 2, done!

24 The previous algorithm does not work all the time!
Consider the following iterations

25 A sample run of the algorithm
1/1 0/1 S 1/1 T 1/1 0/1 B Maxflow = 1, and we can’t push more flow!

26 Maximum flow Ford-Fulkerson algorithm New idea Residue graph
Consider also backward flows Forward flow is f(e)‏ Backward flow is c(e) – f(e)‏ Now you can have a path going backward of an edge decreasing the forward flow

27 Maximum flow Residue graph Blue lines are backward edges 0/1 1/1 0/1
T 0/1 1/1 0/1 1/1

28 Maximum flow The path from S to T with residue capacity > 0 is in red 1/1 0/1 0/1 1/1 S 1/1 0/1 T 0/1 1/1 0/1 1/1

29 The final algorithm Define
augmenting path: a s-t path that has residue capacity > 0 G': residue graph

30 The final algorithm max_flow = 0 while(there is an augmenting path p in G'){ determine flow f' along p max_flow += f' recompute G' forall e(u,v) ∈ p f(u,v) += f' f(v,u) -= f' } Runtime: O(E ∙ maxflow)‏

31 Network flow What if the maxflow found is very large?
How to get an flow insensitive algorithm? DON'T use DFS for finding augmenting path USE BFS instead Use BFS for finding augmenting path Edmonds-Karp algorithm Runtime O(VE2)‏

32 Pseudocode of Edmonds-Karp
int flow[N][N]; int cap[N][N]; int adj[N][N]; int adjc[N]; int pred[N]; bool bfs(int s,int t){ memset(pred,0xff,sizeof(pred)); queue<int> q; q.push(s); while(q.size()>0){ int u = q.front(); q.pop(); if(u==t) return true; for(int i=0;i<adjc[u];++i){ int v = adj[u][i]; if(pred[v]>=0) continue; if(flow[u][v]==cap[u][v]) continue; pred[v] = u; q.push(v); }} return false; } int maxflow(int s,int t){ int mflow = 0; while(bfs(s,t)){ int v = t; int f = 0x7fffffff; while(v!=s){ int u = pred[v]; int r = cap[u][v]-flow[u][v]; if(r < f) f = r; v = u; } v = t; flow[u][v] += f; flow[v][u] -= f; v = u; } mflow += f; return mflow;

33 Minimum cut Minimum cut problem The problem
Given a map of cities and connecting roads,each road is assigned some cost for destroying it, what is the minimum cost for disconnecting city A and B?

34 Minimum cut Theorem Prove? Max flow = min cut
Linear programming duality Refer to notes/books... I suggest you read Algorithm Design by Eva Tardos...

35 Minimum cut How to find the cut edges?
Algorithm 1. run edmonds-karp or ford-fulkerson 2. determine Vs ⊆ V reachable from s in residue graph G' 3. edges exiting Vs are cut edges

36 Applications Image segmentation (ICPC NW Pacific 2006)‏ Finding edge/vertex disjoint paths Escape problem A lot more... Further topics Minimum cost flow (ICPC NW Pacific 2005)‏ K-minimum shortest paths Minimum cost circulation Multicommodity flow (NP-complete!)‏ Please read: Algorithm Design (Jon Kleinberg, Eva Tardos) Network Flows: Theory, Algorithms, and Applications (Ravindra K. Ahuja, Thomas L. Magnanti, James B. Orlin)‏

37 Bipartite matching The Marriage problem
There is only men and women in the world, each men likes some women, you are to match the couples (say you are god now), what is the maximum number of couples you can match?

38 An example Men Women Possible matchings

39 Bipartite matching Given a bipartite graph Problem: G = (A∪B,E)‏
Define a matching M ⊆ E, edges in M are pairwise non-adjacent i.e. No two edges in M share a common vertex Maximum cardinality bipartite matching: max |M|

40 Bipartite matching Relating bipartite matching with maximum flow
Creating a super source and super sink Connect super source to every man Connect super sink to every woman Assign capacity 1 to each edge Find the max flow in the graph

41 The resultant graph Super source Super sink

42 Why does it work? An intuitive idea
If the integer flow f is at maximum, then there are f edges between men and women with flow, they are also pairwise non-adjacent, they will form a matching in the original graph with maximum cardinality f.

43 Think about how to optimize it...
Nice! One algorithm, two problems Think about how to optimize it... Problems Min cut: 10480 Max flow: 10511, 10330, 820, 563 (Hard!), 544 Bipartite matching: 10092, 10080

44 What a nightmare! Welcome to the family of bipartite matching
Maximum cardinality bipartite matching done Maximum weighted bipartite matching Maximum weighted perfect matching Minimum weighted perfect matching Maximum weighted bipartite matching of maximum cardinality Perfect matching with minimum heaviest edge

45 Maximum weighted bipartite matching
Refer to the marriage problem, suppose a man when matched with a women with have certain degree of happiness (measured in integer), can you maximize the total happinss of all men? MWB matching is probably the most difficult algorithm you would see in ICPC..(besides general matching…) Read the following slides at home if you are interested…

46 MWB algorithm by Kurt Mehlhorn Define:
Graph: G = (A∪B,E) Potential π: V -> R Cost: c: E -> R Reduced cost: ĉ: E -> R ĉ(e(u,v)) = π(u) + π(v) – c(e)‏ Theorem max ∑c(e) e∈M = min π(v) v∈A∪B Linear programming duality Refer to books for proof....

47 Define some more terms πis called feasible if reduced costs are non-negative πis called non-negative if π(v) >= 0 ∀v∈A∪B πis called tight if πis feasible π(v) = 0 for all free nodes ĉ(e) = 0 ∀e∈M A tight and non-negative potential function is suficient for a maximum weighted bipartite matching A tight potential function is sufficient for a maximum weighted perfect matching

48 The actual algorithm assign a fesible π
For each ai ∈A 1. compute shortest path from ai to all other vertex with respect to the reduced cost ĉ 2. minA = min dist(v) + π(v) v∈A 3. minB = min π(v) v∈B and free 4. δ= min(minA, minB) forall v∈A, π(v) -= max(0,δ-dist(v)) forall v∈B, π(v) += max(0,δ-dist(v)) 6. augment the path from ai to that defines δ

49 Maximum weighted bipartite matching Use exactly the algorithm
Maximum weighted perfect matching Don't consider minA, always try to find minB and bestB If bestB not found, no perfect matching Minimum weighted perfect matching As above, reverse signs of edge costs Maximum weighted perfect matching of maximum cardinality Please think about it..

50 Runtime O(n(m+nlogn))‏ Further readings
LEDA: A platform for combinatorial and geometric computing (Kurl Mehlhorn)‏ Problems MWB: 10072,10888 Perfect matching with minimum heaviest edge: 10804, (Very difficult!)


Download ppt "Combinatorics Shortest path Maximum flow & minimum cut"

Similar presentations


Ads by Google