360 likes | 372 Vues
CS 575 Design and Analysis of Computer Algorithms Professor Michal Cutler Lecture 19 November 10, 2005. This class. Dijkstra The max-flow min-cut problem The flow network What is a flow? The residual network Augmenting paths Cuts. Shortest Path Algorithms.
E N D
CS 575Design and Analysis ofComputer AlgorithmsProfessor Michal CutlerLecture 19November 10, 2005
This class • Dijkstra • The max-flow min-cut problem • The flow network • What is a flow? • The residual network • Augmenting paths • Cuts
Shortest Path Algorithms • Dijkstra’s algorithm does NOT allow negative edges. • Uses a greedy heuristic. • undirected/directed graph • Bellman-Ford’s and Floyd’s algorithms work correctly for any graph and can detect negative cycles.
Main idea • Assume that the shortest distances from the starting node s to the rest of the nodes are d(s, s) d(s, s1) d(s, s2) … d(s, sn-1) • In this case a shortest path from s to si may include any of the vertices {s1, s2… si-1} but cannot include any sj where j > i. • Dijkstra’s main idea is to select the nodes and compute the shortest distances in the order s, s1, s2 ,…, sn-1
Dijkstra’s greedy selection rule • Assume s1, s2… si-1 have been selected, and their shortest distances have been stored in Solution • Select node si and save d(s, si) if si has the shortest distance from s on a path that may include only s1, s2… si-1 as intermediate nodes. We call such paths special • To apply this selection rule efficiently, we need to maintain for each unselected node v the distance of the shortest special path from s to v, D[v].
Updating D[ ] • After adding near to Solution, D[v] of all nodes v Ï Solution are updated if there is a shorter special path from s to v that contains node near, i.e., if (D[near] + w(near, v ) < D[v]) then D[v]=D[near] + w(near, v ) D[near ] = 5 2 Solution after adding near 2 3 s D[ v ] = 9 is updated to D[v]=5+2=7 3 6
Dijkstra’s Algorithm for Finding the Shortest Distance from a Single Source Dijkstra(G,s)1. for eachv V2. doD [ v ] 3. D [ s ] 04. PQ make-PQ(D,V)5. whilePQ 6. donear PQ.extractMin () 7. for eachv Adj(near )8 ifv PQ &&D [ v ] > D [ near ] +w( near ,v )9. then D [ v ] D [ near ] +w( near, v )10. PQ.decreasePriorityValue (D[v], v )11. return the label D[u] of each vertex u
1. for eachv V2. doD [ v ] 3. D [ s ] 04. PQ make-PQ(D,V)5. whilePQ 6. donear PQ.extractMin () 7. for eachv Adj(near )8 ifD [ v ] > D [ near ] + w( near ,v )9. then D [ v ] D[near] + w(near,v)10. PQ.decreasePriorityValue (D[v ], v )11. return the label D[u] of each vertex u Assume a node in PQ can be accessed in O(1) ** Decrease key for v requires O(lgV ) provided the node in heap with v’s data can be accessed in O(1) Using Heap implementation Lines 1 -4 run in O (V ) Max Size of PQ is | V | (5) Loop = O (V ) - Only decreases(6+(5)) O (V ) O( lg V )(7+(5)) Loop = O(deg(near) ) =O( E ) (8+(7+(5))) O(1)O( E )(9) O(1)(10+(7+(5))) Decrease- Key operation on the heap can be implemented in O( lg V) O( E ). So total time for Dijkstra's Algorithm is O ( V lg V + E lg V ) What is O(E ) ?For Sparse Graph = O(V lg V ) For Dense Graph = O(V2 lg V ) Time Analysis
Example a 4 4 2 b c 2 1 10 5 d e
Maximum flow and minimum cut Cornerstone problem in combinatorial optimization Many applications: Network connectivity, bipartite matching, project selection, network reliability, data mining, image processing, job scheduling for real time applications, baseball elimination, etc.
A flow network v1 5 10 v3 t 7 15 12 s v2 6 • A directed graphG=(V, E) in which each edge (u, v) E has a nonnegative capacityc(u, v) 0. • If (u, v) E , c(u, v) = 0 • Contains a source node s, and a sink node t • Every node u V is on a directed path from s to t.
11,15 Flow • An s-t flow is a function f: VV-> R that satisfies: • Capacity Constraint: (the flow cannot exceed the capacity) for all u, v, f(u, v) c(u, v) • Skew symmetry: (if f is flowing from u to v, –f is flowing from v to u) for all u, v, f(u, v) = -f(v, u)
Flow • Flow conservation: • sum of flows from a node is 0, or • the sum of the flows that leave a node is equal to the sum of the flows that enter it • for all uV –{s, t}
Flow conservation example v1 5, 5 v3 v4 11,15 6,12 v2 • f(v3, v1) +f(v3, v2)+f(v3, v4)=0 • f(v3, v1)= -f(v1, v3)= -5 • f(v3, v2)= -f(v2, v3)= -6 • -5 - 6 + 11= 0 • Flow entering v3 =f(v1, v3) + f(v2, v3)= 5+6 =11, • Flow leaving v3 = f(v3, v4) = 11
An example of a flow v1 5, 5 5,10 v3 t 0, 7 11,15 6,12 s v2 6, 6 • The capacity constraints and skewed symmetry are satisfied for all edges • Conservation is satisfied.
Max flow problem v1 5, 5 9,11 v3 t 4, 7 15,15 10,12 s v2 6, 6 • Find s-t flow f* that maximizes net flow out of the source • The current value of the flow: • For our example |f| = f(s, v1) + f(s, v2) = 9 +6 = 15 is a max flow
t1 s1 s2 S t2 s3 What if there is more than one source? Or sink? s t • Add a super sources and a super sinkt, and an edge from s to each source and from each sink to t. The capacity of these edge is
Working with flows • If X and Y are sets of vertices L1 • For all XV, f(X, X) = 0 • For all X, YV, f(X, Y) = -f(Y, X) • For all X, Y, ZV, with XY = , f(XY, Z)=f(X, Z)+f(Y, Z) and f(Z, XY)= f(Z, X) + f(Z, Y)
Working with flows v1 5, 5 5,10 v3 t 0, 7 11,15 6,12 s v2 6, 6 • Let X={s, v1} Y={v2, v3}, Z={t} • f(X, X)=f(s, v1)+f(v1, s)=5–5= 0 • f(X, Y)= f(s, v2) +f(v1, v2)+f(v1, v3) = 11, f(Y, X) = -11 • f(XY, Z)=f(v3, t) = 11, f(X, Z)=0 and f(Y,Z)=f(v3, t) =11
|f| = total flow out of source = total flow into the sink |f| = f(s,V) (By def.) = f(V,V) - f(V - s, V) (L.1-3) = - f(V - s, V) (L.1-1) = f(V, V - s) (L.1-2) = f(V, t) + f(V, V - s - t) (L.1-3) = f(V, t) (flow conservation) • For next slideU is the maximum capacity, V the number of nodes and E the number of edges in the network.
The Ford and Fulkerson method • Initialize flow f to 0 • while there exists an augmenting path p in residual network • augment flow f along p • returnf • Need to explain what are augmenting paths (and residual networks)
Residual Networks • The amount of additional flow we can push from node u to v before exceeding the capacity c(u, v) is the residual capacity of (u, v) cf(u, v) = c(u, v) –f(u, v) Examples c(u, v)=16 and f(u, v)=11-> cf(u, v) =5 c(u, v)=16 and f(u, v)=-4 -> cf(u, v) =20
Residual networks • Given a flow network G=(V, E) and a flow f, the residual network of Ginduced by f is Gf = (V, Ef) where Ef = {(u, v)VV| cf(u, v) > 0} • The edges in Efare either edges in E or their reversals
Residual networks – how many edges? • If f(u, v) < c(u, v) for (u, v) E -> cf(u, v) > 0 and (u, v) Ef • If f(u, v)<0, for (v, u) E, and f(v, u)>0 -> cf(u, v) > 0 and (u, v) Ef • If neither (u, v) E or (v, u) E then c(u, v)=c(v, u)=0, f(u, v)=f(v, u)=0, and cf(u, v)=cf(v, u)=0 ((u, v) Ef) • So |Ef| 2|E|
v1 5, 5 G 5,11 v3 t 0, 7 11,15 6,12 s v2 6, 6 v1 5 11 Gf 5 6 v3 t 7 4 6 s v2 6 6 The residual network
Lemma L.2 • Given a flow network G with flow f, and a residual network induced by f with flow f’. There is a flow in G with value |f + f’|=|f| + |f’| • Proof outline: • Verify that skew symmetry, capacity constraint and flow conservation are obeyed, and the total flow from s is |f| + |f’|
Augmenting paths • An augmenting pathp is a path from s to t in the residual network Gf. • The maximum amount by which we can increase the flow on an augmenting path p is the residual capacity of p, cf(p) = min{cf(u, v)| (u, v) is on p}
v1 5 11 5 6 v3 t 7 4 6 s v2 6 6 An augmenting path • cf(p) = min{cf(u, v)| (u, v) is on p} = min{6, 7, 6, 4}=4 • Adding 4 to the edges in G results in the maximum flow (for this example) Gf
The updated flow v1 5, 5 9,11 v3 t 4, 7 15,15 10,12 s v2 6, 6
Cuts • A cut (S,T), is a partition of V into S and T=V-S, such that sS, and t T • The net flow across a cut (S, T) is f(S, T) • The capacity of the cut (S, T) is c(S,T) • Only the capacity of edgesfromStoT are included in c(S, T) .
Examples of Cuts T S v3 v1 6, 6 5, 5 1, 6 11, 13 v4 t 5, 5 17, 20 12, 12 s v2 6, 6 • c(S, T) = 6 + 12=18 and f(S, T) = 6 + 12 – 1= 17 = f c(S,T). • Only the capacity of edges from S to T are included in c(S, T) .
Examples of Cuts v3 v1 6, 6 5, 5 1, 6 11, 13 v4 t 5, 5 17, 20 12, 12 s v2 6, 6 • f(S, T) = c(S, T) = f = 5+ 12=17
Demos • Relevant Algorithm Animations/Visualizations (in Java) • http://www.hig.no/~algmet/animate.html • http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/Maxflow.shtml • (Alt-tab)