Combinatorics - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Combinatorics

Description:

Maximum flow & minimum cut. Bipartite matching. Shortest path. Given a graph like the following ... exiting Vs are cut edges. Applications. Image segmentation ... – PowerPoint PPT presentation

Number of Views:138
Avg rating:3.0/5.0
Slides: 51
Provided by: alph6
Category:

less

Transcript and Presenter's Notes

Title: Combinatorics


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?

E
B
1
3
3
5
A
5
F
D
C
1
3

The BFS tree of the graph
A
5
B
5
3
What if we use BFS?
C
D
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) lt distance(s,v)) set
    distance(s,v) d distance(u,v)

7
  • Lets run the algorithm

E
B
1
3
3
5
A
5
F
D
C
1
8
  • Lets run the algorithm

E
B
1
3
3
5
A
5
F
D
C
1
9
  • Lets run the algorithm

E
B
1
3
3
5
A
5
F
D
C
1
10
  • Lets run the algorithm

E
B
1
3
3
5
A
5
F
D
C
1
11
  • Lets run the algorithm

E
B
1
3
3
5
A
5
F
D
C
1
12
  • Lets run the algorithm

E
B
1
3
3
5
A
5
F
D
C
1
13
  • Done!!

E
B
1
3
3
5
A
5
F
D
C
1
14
Some more notes
  • This algorithm is called the Dijkstras algorithm
  • Limitation Edge distance cannot be negative
  • Use priority queue in the implementation

15
A sample implementation
  • int distN
  • int predN
  • struct C
  • int u,c C() C(int a,int b)u(a),c(b)
  • bool operatorlt(const Ca) const
  • return c gt a.c
  • // the graph is represented by
  • // a adjacency list
  • // a vertex a has adjca neighbors
  • // adja0 adjaadjca-1 are
  • // neighbors of vertex
  • // costab has edge cost for (a,b)
  • int adjcN
  • int adjNN
  • int costNN

bool visitN void dijkstra(int s)
memset(visit,0,sizeof(visit))
memset(dist,0x7f,sizeof(dist))
priority_queueltCgt pq pq.push(C(s,0)) dists
0 while(pq.size()gt0) C c pq.top()
pq.pop() int u c.u if(visitu)
continue visitu true for(int
i0iltadjcui) int v adjui
if(visitv) continue
if(c.ccostuvltdistv) distv
distu c.c predv u
pq.push(C(v,distv))
16
  • The runtime is (EV)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
  • A graph G (V,E)?
  • Capacity function c E -gt 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 lt f(u,v) lt 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
T
0/1
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 gt 0 for each edge in the path

21
  • First algorithm max_flow 0 while(there is a
    path p with residue capacity gt 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
A
1/1
1/1
S
T
0/1
0/1
0/1
B
Maxflow 1
23
A sample run of the algorithm
A
1/1
1/1
S
T
0/1
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
A
1/1
0/1
S
T
1/1
0/1
1/1
B
Maxflow 1, and we cant 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
1/1
0/1
S
T
1/1
0/1
1/1
1/1
0/1
28
Maximum flow
  • The path from S to T with residue capacity gt 0 is
    in red

0/1
1/1
0/1
1/1
S
T
1/1
0/1
0/1
1/1
1/1
0/1
29
The final algorithm
  • Define
  • augmenting path a s-t path that has residue
    capacity gt 0
  • G' residue graph

30
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)?

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 flowNN int capNN
  • int adjNN int adjcN
  • int predN
  • bool bfs(int s,int t)
  • memset(pred,0xff,sizeof(pred))
  • queueltintgt q q.push(s)
  • while(q.size()gt0)
  • int u q.front() q.pop()
  • if(ut) return true
  • for(int i0iltadjcui)
  • int v adjui
  • if(predvgt0) continue
  • if(flowuvcapuv) continue
  • predv 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
predv int r capuv-flowuv
if(r lt f) f r v u v
t while(v!s) int u
predv flowuv f flowvu
- 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
  • Max flow min cut
  • Prove?
  • 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?
  • Algorithm1. run edmonds-karp or
    ford-fulkerson2. 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 readAlgorithm 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 canmatch?

38
  • An example

Women
Men
Possible matchings
39
Bipartite matching
  • Given a bipartite graph
  • G (A?B,E)?
  • Problem
  • Define a matchingM ? E, edges in M are pairwise
    non-adjacenti.e. No two edges in M share a
    common vertex
  • Maximum cardinality bipartite matchingmax 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
  • Nice!
  • One algorithm, two problems
  • Think about how to optimize it...
  • ProblemsMin 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 p V -gt R
  • Cost c E -gt R
  • Reduced cost c E -gt R
    c(e(u,v)) p(u) p(v) c(e)?
  • Theorem
  • max ?c(e) e?M min p(v) v?A?B
  • Linear programming duality
  • Refer to books for proof....

47
  • Define some more terms
  • pis called feasible if reduced costs are
    non-negative
  • pis called non-negative if p(v) gt 0 ?v?A?B
  • pis called tight if
  • pis feasible
  • p(v) 0 for all free nodes
  • c(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 p
  • For each ai ?A 1. compute shortest path from ai
    to all other vertex with respect to the
    reduced cost c 2. minA min dist(v) p(v) v?A
    3. minB min p(v) v?B and free 4. d
    min(minA, minB) 5. forall v?A, p(v) -
    max(0,d-dist(v)) forall v?B, p(v)
    max(0,d-dist(v)) 6. augment the path from ai to
    that defines d

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

50
  • Runtime O(n(mnlogn))?
  • Further readings
  • LEDA A platform for combinatorial and geometric
    computing (Kurl Mehlhorn)?
  • ProblemsMWB 10072,10888Perfect matching with
    minimum heaviest edge 10804, 10122 (Very
    difficult!)
Write a Comment
User Comments (0)
About PowerShow.com