Combinatorics Shortest path Maximum flow & minimum cut

Slides:



Advertisements
Similar presentations
15.082J & 6.855J & ESD.78J October 14, 2010 Maximum Flows 2.
Advertisements

1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
Piyush Kumar (Lecture 6: MaxFlow MinCut Applications)
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
MAXIMUM FLOW Max-Flow Min-Cut Theorem (Ford Fukerson’s Algorithm)
Chapter 10: Iterative Improvement The Maximum Flow Problem The Design and Analysis of Algorithms.
1 Maximum Flow w s v u t z 3/33/3 1/91/9 1/11/1 3/33/3 4/74/7 4/64/6 3/53/5 1/11/1 3/53/5 2/22/2 
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Advanced Algorithm Design and Analysis (Lecture 8) SW5 fall 2004 Simonas Šaltenis E1-215b
Lectures on Network Flows
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
Nick McKeown Spring 2012 Maximum Matching Algorithms EE384x Packet Switch Architectures.
CS138A Network Flows Peter Schröder. CS138A Flow Networks Definitions a flow network G=(V,E) is a directed graph in which each edge (u,v)
CSE 589 Part III The computer is useless It can only answer questions. -- Pablo Picasso.
The max flow problem
Maximum Flows Lecture 4: Jan 19. Network transmission Given a directed graph G A source node s A sink node t Goal: To send as much information from s.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
1 Ford-Fulkerson method Ford-Fulkerson(G) f = 0 while( 9 simple path p from s to t in G f ) f := f + f p output f Runs in time O(|f max | |E|) where f.
Advanced Algorithms Piyush Kumar (Lecture 5: Weighted Matching) Welcome to COT5405 Based on Kevin Wayne’s slides.
MAX FLOW CS302, Spring 2013 David Kauchak. Admin.
Maximum Flow Chapter 26.
Advanced Algorithms Piyush Kumar (Lecture 5: Weighted Matching) Welcome to COT5405 Based on Kevin Wayne’s slides.
Advanced Algorithms Piyush Kumar (Lecture 4: MaxFlow MinCut Applications) Welcome to COT5405.
CS774. Markov Random Field : Theory and Application Lecture 13 Kyomin Jung KAIST Oct
Graphs, BFS, DFS and More…
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
CS 473Lecture ?1 CS473-Algorithms I Lecture ? Network Flows Finding Max Flow.
All Pair Shortest Path IOI/ACM ICPC Training June 2004.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
MAX FLOW APPLICATIONS CS302, Spring 2012 David Kauchak.
Max Flow – Min Cut Problem. Directed Graph Applications Shortest Path Problem (Shortest path from one point to another) Max Flow problems (Maximum material.
1 WEEK 11 Graphs III Network Flow Problems A Simple Maximum-Flow Algorithm Izmir University of Economics.
Maximum Flow Chapter 26. Flow Concepts Source vertex s – where material is produced Sink vertex t – where material is consumed For all other vertices.
CS223 Advanced Data Structures and Algorithms 1 Maximum Flow Neil Tang 3/30/2010.
Chapter 7 April 28 Network Flow.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 25.
10/11/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 21 Network.
Algorithm Design and Analysis (ADA)
Chapter 7 May 3 Ford-Fulkerson algorithm Step-by-step walk through of an example Worst-case number of augmentations Edmunds-Karp modification Time complexity.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 23.
CSEP 521 Applied Algorithms Richard Anderson Lecture 8 Network Flow.
1 Network Flow CSC401 – Analysis of Algorithms Chapter 8 Network Flow Objectives: Flow networks –Flow –Cut Maximum flow –Augmenting path –Maximum flow.
Fall 2003Maximum Flow1 w s v u t z 3/33/3 1/91/9 1/11/1 3/33/3 4/74/7 4/64/6 3/53/5 1/11/1 3/53/5 2/22/2 
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
A directed graph G consists of a set V of vertices and a set E of arcs where each arc in E is associated with an ordered pair of vertices from V. V={0,
11/21/02CSE Max Flow CSE Algorithms Max Flow Problems.
Instructor Neelima Gupta Edited by Divya Gaur(39, MCS '09) Thanks to: Bhavya(9), Deepika(10), Deepika Bisht(11) (MCS '09)
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
Maximum Flow c v 3/3 4/6 1/1 4/7 t s 3/3 w 1/9 3/5 1/1 3/5 u z 2/2
Maximum Flow Chapter 26.
CS4234 Optimiz(s)ation Algorithms
Richard Anderson Lecture 23 Network Flow
Various Graph Algorithms
Instructor: Shengyu Zhang
Maximum Flow c v 3/3 4/6 1/1 4/7 t s 3/3 w 1/9 3/5 1/1 3/5 u z 2/2
Max Flow Min Cut, Bipartite Matching Yin Tat Lee
Flow Networks and Bipartite Matching
Piyush Kumar (Lecture 6: MaxFlow MinCut Applications)
Algorithms (2IL15) – Lecture 7
EE5900 Advanced Embedded System For Smart Infrastructure
Maximum Flow c v 3/3 4/6 1/1 4/7 t s 3/3 w 1/9 3/5 1/1 3/5 u z 2/2
Network Flow.
Lecture 21 Network Flow, Part 1
Network Flow.
Presentation transcript:

Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching

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

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

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.

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.

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) }

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

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

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

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

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

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

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

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

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])); }}}}

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

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?

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)

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

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

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' }

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

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

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

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!

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

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

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

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

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)‏

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)‏

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;

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?

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

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

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)‏

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?

An example Men Women Possible matchings

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|

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

The resultant graph Super source Super sink

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.

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

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

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…

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....

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

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) 5. 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 δ

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..

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, 10122 (Very difficult!)