+ All Categories
Home > Documents > Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Date post: 04-Jan-2016
Category:
Upload: dustin-stephens
View: 219 times
Download: 0 times
Share this document with a friend
50
Combinatorics • Shortest path • Maximum flow & minimum cut • Bipartite matching
Transcript
Page 1: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Combinatorics

• Shortest path

• Maximum flow & minimum cut

• Bipartite matching

Page 2: 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?

A

C

E

D

F

B5

3

5

1

3 1

Page 3: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

A

C

E

D

F

B5

3

5

1

3 1

What if we use BFS?

The BFS tree of the graph

A

C

E

D

F

B

5

3 5

3

1

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

Page 4: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 5: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 6: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 7: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Let’s run the algorithm

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B ∞

C ∞

D ∞

E ∞

F ∞

Page 8: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Let’s run the algorithm

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B 5

C ∞

D ∞

E ∞

F ∞

Page 9: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Let’s run the algorithm

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B 5

C 10

D 8

E ∞

F ∞

Page 10: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Let’s run the algorithm

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B 5

C 9

D 8

E ∞

F ∞

Page 11: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Let’s run the algorithm

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B 5

C 9

D 8

E 12

F ∞

Page 12: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Let’s run the algorithm

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B 5

C 9

D 8

E 12

F 13

Page 13: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Done!!

A

C

E

D

F

B5

3

5

1

3 1

Node Distance

A 0

B 5

C 9

D 8

E 12

F 13

Page 14: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Some more notes…

• This algorithm is called the Dijkstra’s algorithm

• Limitation: Edge distance cannot be negative

• Use priority queue in the implementation

Page 15: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 16: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• 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

Page 17: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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?

Page 18: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Definitions

• Given• 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)

Page 19: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Consider the following network

• Edge labels in the form: flow / capacity

S

A

B

T

0/1

0/1

0/1

0/1 0/1

Page 20: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 21: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

}

Page 22: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

A sample run of the algorithm

S

A

B

T

1/1

0/1

1/1

0/1 0/1

Maxflow = 1

Page 23: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

A sample run of the algorithm

S

A

B

T

1/1

0/1

1/1

1/1 1/1

Maxflow = 2, done!

Page 24: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• The previous algorithm does not work all the time!

• Consider the following iterations

Page 25: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

A sample run of the algorithm

S

A

B

T

0/1

1/1

1/1

1/1 0/1

Maxflow = 1, and we can’t push more flow!

Page 26: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 27: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Maximum flow

• Residue graph

• Blue lines are backward edges

S T

0/1

1/11/1

1/1 0/1

0/1

0/1

0/1

1/1

1/1

Page 28: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Maximum flow

• The path from S to T with residue capacity > 0 is in red

S T

0/1

1/11/1

1/1 0/1

0/1

0/1

0/1

1/1

1/1

Page 29: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

The final algorithm

• Define– augmenting path: a s-t path that has residue

capacity > 0– G': residue graph

Page 30: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

The final algorithm

• max_flow = 0while(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)

Page 31: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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)

Page 32: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Pseudocode of Edmonds-Karpint 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; while(v!=s){ int u = pred[v]; flow[u][v] += f; flow[v][u] -= f; v = u; } mflow += f; } return mflow;}

Page 33: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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?

Page 34: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Minimum cut

• Theorem– Max flow = min cut

• Prove?– Linear programming duality– Refer to notes/books...– I suggest you read Algorithm Design by Eva

Tardos...

Page 35: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Minimum cut

• How to find the cut edges?• Algorithm

1. run edmonds-karp or ford-fulkerson2. determine Vs ⊆ V reachable from s in residue graph G'3. edges exiting Vs are cut edges

Page 36: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 37: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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 canmatch?

Page 38: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• An example

Men Women Possible matchings

Page 39: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

Bipartite matching

• Given a bipartite graph– G = (A∪B,E)

• Problem:– Define a matching

M ⊆ E, edges in M are pairwise non-adjacenti.e. No two edges in M share a common vertex

– Maximum cardinality bipartite matching:max |M|

Page 40: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 41: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• The resultant graph

Super sourceSuper sink

Page 42: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 43: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• 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

Page 44: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 45: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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…

Page 46: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

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

Page 47: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• 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

Page 48: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• The actual algorithm– assign a fesible π

– For each ai ∈A1. 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 δ

Page 49: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Maximum weighted bipartite matchingUse exactly the algorithm

• Maximum weighted perfect matchingDon't consider minA, always try to find minB and bestBIf bestB not found, no perfect matching

• Minimum weighted perfect matchingAs above, reverse signs of edge costs

• Maximum weighted perfect matching of maximum cardinalityPlease think about it..

Page 50: Combinatorics Shortest path Maximum flow & minimum cut Bipartite matching.

• Runtime O(n(m+nlogn))• Further readings

– LEDA: A platform for combinatorial and geometric computing (Kurl Mehlhorn)

• ProblemsMWB: 10072,10888Perfect matching with minimum heaviest edge: 10804, 10122 (Very difficult!)


Recommended