450 likes | 953 Views
Maximum flow. Network Algorithms 2005. Today. Maximum flow problem Applications Briefly: Ford-Fulkerson; min cut max flow theorem Preflow push algorithm Lift to front algorithm Variants. Problem. Variants in notation, e.g.: Write f(u,v) = -f(v,u). Directed graph G=( V,E )
E N D
Maximum flow Network Algorithms 2005 Network Algorithms: Maximum flow
Today • Maximum flow problem • Applications • Briefly: Ford-Fulkerson; min cut max flow theorem • Preflow push algorithm • Lift to front algorithm • Variants Network Algorithms: Maximum flow
Problem Variants innotation, e.g.: Write f(u,v) = -f(v,u) • Directed graph G=(V,E) • Source s Î V, sink t Î V. • Capacity c(e) ÎZ+ for each e. • Flow: function f: E ®Nsuch that • For all e: f(e) £ c(e) • For all v, except s and t: flow into v equals flow out of v • Flow value: flow out of s • Question: find flow from s to t with maximum value Network Algorithms: Maximum flow
Maximum flow Algoritmiek • Ford-Fulkerson method • Possibly (not likely) exponential time • Edmonds-Karp version: O(nm2): augment over shortest path from s to t • Max Flow Min Cut Theorem • Improved algorithms: Preflow push; scaling • Applications • Variants of the maximum flow problem Network Algorithms: Maximum flow
Variant • Multiple sources, multiple sinks • Possible maximum flow out of certain sources or into some sinks • Models logistic questions G s1 t1 t S sk tr Network Algorithms: Maximum flow
Lower bounds on flow • Edges with minimum and maximum capacity • For all e: l(e) £f(e) £c(e) l(e) Algorithms come later c(e) Network Algorithms: Maximum flow
Applications • Logistics (transportation of goods) • Matching • Matrix rounding problem • … Network Algorithms: Maximum flow
Matrix rounding problem • p * q matrix of real numbers D = {dij}, with row sums ai and column sums bj. • Consistent rounding: round every dijup or down to integer, such that every row sum and column sum equals rounded sum of original matrix • Can be modeled as flow problem with lower and upper bounds on flow through edges Network Algorithms: Maximum flow
Row sumrounded down, Sum rounded up Row 1 Col 1 Column sumrounded down, Sum rounded up [ 16,17] [ ëbijû, ëbijû+1] s t Row p Col q Network Algorithms: Maximum flow
Ford-Fulkerson • Residual network Gf • Start with 0 flow • Repeat • Compute residual network • Find path P from s to t in residual network • Augment flow across P Until no such path P exists Network Algorithms: Maximum flow
Max flow min cut theorem • s-t-cut: partition vertices in sets S, T such that s in S, t in T. Look to edges (v,w) with v in S, w in T. • Capacity of cut: sum of capacities of edges from S to T • Flow across cut • Theorem: minimum capacity of s-t-cut equals maximum flow from s to t. Network Algorithms: Maximum flow
Preflow push • Simple implementation: O(n2m) • Better implementation: O(n3) • Algorithm maintains preflow: some flow out of s which doesn’t reach t • Vertices have height • Flow is pushed to lower vertex • Vertices sometimes are lifted Network Algorithms: Maximum flow
Preflow • Function f: V * V ®R • Skew symmetry: f(u,v) = - f(v,u) • Capacity constraints: f(u,v) £ c(u,v) • Notation: f(V,u) • For all u, except s: f(V,u) ³ 0 (excess flow) • u is overflowing when f(V,u) > 0. • Maintain: e(u) = f(V,u). Network Algorithms: Maximum flow
Height function • h: V ®N: • h(s) = n • h(t) = 0 • For all (u,v) in Ef (residual network):h(u) £ h(v)+1 Network Algorithms: Maximum flow
Initialize • Set height function h • h(s) = n • h(t) = 0 • h(v) = 0 for all v except s • For each edge (s,u) • f(s,u) = c(s,u); f(u,s) = - c(s,u) • e[u] = c(s,u); Do not change Initial preflow Network Algorithms: Maximum flow
Basic operation 1: Push • Suppose e(u)>0, cf(u,v)>0, and h[u]= h[v]+1 • Push as much flow across (u,v) as possible r = min (e[u], cf(u,v)) f(u,v) = f(u,v) + r; f(v,u) = - f(u,v); e[u] = e[u] - r; e[v] = e[v] + r. Network Algorithms: Maximum flow
Basic operation 2: Lift • When no push can be done from overflowing vertex (except s,t) • Suppose e[u]>0, and for all (u,v) in Ef : h[u] £ h[v], u¹s, u¹t • Set h[u] = 1 + min {h[v] | (u,v) in Ef} Network Algorithms: Maximum flow
Preflow push algorithm • Initialize • While push or lift operation possible do • Select an applicable push or lift operation and perform it To do: correctness proof and time analysis Network Algorithms: Maximum flow
Lemmas / Invariants • If there is an overflowing vertex (except t), then a lift or push operation is possible • The height of a vertex never decreases • When a lift operation is done, the height increases by at least one. • h remains a height function during the algorithm Network Algorithms: Maximum flow
Another invariant and the correctness • There is no path in Gf from s to t • Proof: the height drops by at most one across each of the at most n-1 edges of such a path • When the algorithm terminates, the preflow is a maximum flow from s to t • f is a flow, as no vertex except t has excess • As Gf has no path from s to t, f is a maximum flow Network Algorithms: Maximum flow
Time analysis 1: Lemma • If u overflows then there is a simple path from u to s in Gf • Intuition: flow must arrive from s to u: reverse of such flow gives the path • Formal proof skipped Network Algorithms: Maximum flow
Number of lifts • For all u: h[u] < 2n • h[s] remains n. When vertex is lifted, it has excess, hence path to s, with at most n – 1 edges, each allowing a step in height of at most one up. • Each vertex is lifted less than 2n times • Number of lift operations is less than 2n2 Network Algorithms: Maximum flow
Counting pushes • Saturating pushes and not saturating pushes • Saturating: sends cf(u,v) across (u,v) • Non-saturating: sends e[u] < cf(u,v) • Number of saturating pushes • After saturating push across (u,v), edge (u,v) disappears from Gf. • Before next push across (u,v), it must be created by push across (v,u) • Push across (v,u) means that a lift of v must happen • At most 2n lifts per vertex: O(n) sat. pushes across edge • O(nm) saturating pushes Network Algorithms: Maximum flow
Non-saturating pushes • Look at • Initially F = 0. • F increases by lifts in total at most 2n2 • F increases by saturating pushes at most by 2n per push, in total O(n2m) • F decreases at least one by a non-saturating push across (u,v) • After push, u does not overflow • v may overflow after push • h(u) > h(v) • At most O(n2m) pushes Network Algorithms: Maximum flow
Algorithm • Implement • O(n) per lift operation • O(1) per push • O(n2m) time Network Algorithms: Maximum flow
Lift-to-front algorithm • Variant of preflow push using O(n3) time • Vertices are discharged: • Push from edges while possible • If still excess flow, lift, and repeat until no excess flow • Order in which vertices are discharge: • list, • discharged vertex placed at top of list • Go from left to right through list, until end, then start anew Network Algorithms: Maximum flow
Definition and Lemma • Edge (u,v) is admissible • cf(u,v) > 0, i.e., (u,v) in Ef • h(u) = h(v)+1 • The network formed by the admissible edges is acyclic. • If there is a cycle, we get a contradiction by looking at the heights • If (u,v) is admissible and e[u]>0, we can do a push across it. Such a push does not create an admissible edge, but (u,v) can become not admissible. Network Algorithms: Maximum flow
Discharge procedure • Vertices have adjacency list N[u]. Pointer current[u] gives spot in adjacency list. • Discharge(u) • While e[u]>0 do v = current[u]; If v = NIL then {Lift(u); current[u] = head(N[u]);} Elseif cf(u,v) > 0 and h[u] = h[v]+1 then Push(u,v); Else current[u] = next-neighbor[v]; Network Algorithms: Maximum flow
Discharge indeed discharges • If u is overflowing, then we can do either a lift to u, or a push out of u • Pushes and Lifts are done when Preflow push algorithm conditions are met. Network Algorithms: Maximum flow
Lift-to-front algorithm • Maintain linked list L of all vertices except s, t. • Lift-to-front(G,s,t) • Initialise preflow and L • For all v: current[v] = head[N(v)]; • u is head of L • While u not NIL Do oldheight = h[u]; Discharge(u); If h[u] > oldheight then move u to front of list L u = next[u]; Network Algorithms: Maximum flow
Remarks • Note how we go through L. • Often we start again at almost the start of L… • We end when the entire list is done. • For correctness: why do we know that no vertex has excess when we are at the end of L? Network Algorithms: Maximum flow
A definition: Topological sort • A directed acyclic graph is a directed graph without cycles. It has a topological sort: • An ordering of the vertices t: V ® {1, 2, … , n} (bijective function), such that for all edges (v,w) Î E: t(v) < t(w) Network Algorithms: Maximum flow
L is a topological sort of the network of admissible edges • If (u,v) is an admissible edge, then u is before v in the list L. • Initially true: no admissible edges • A push does not create admissible edges • After a lift of u, we place u at the start of L • Edges (u,v) will be properly ordered • Edges (v,u) will be destroyed Network Algorithms: Maximum flow
Lift-to-front algorithm correctly computes a flow • The algorithm maintains a preflow. • Invariant of the algorithm: all vertices before the vertex u in consideration have no excess flow. • Initially true. • Remains true when u is put at start of L. • Any push pushes flow towards the end of L. • L is topological sort of network of admissible edges. • When algorithm terminates, no vertex in L has excess flow. Network Algorithms: Maximum flow
Time analysis - I • O(n2) lift operations. (As in preflow push.) • O(nm) saturating pushes. • Phase of algorithm: steps between two times that a vertex is placed at start of L, (and before first such and last such event.) • O(n2) phases; each handling O(n) vertices. • All work except discharges: O(n3). Network Algorithms: Maximum flow
Time of discharging • Lifts in discharging: O(n) each, O(n3) total • Going to next vertex in adjacency list • O(degree(u)) work between two lifts of u • O(nm) in total • Saturating pushes: O(nm) • Non-saturating pushes: only once per discharge, so O(n3) in total. Conclusion: O(n3) time for the Lift to front algorithm Network Algorithms: Maximum flow
Many other flow algorithms • Push-relabel (variant of preflow push)O(nm log (n2/m)) • Scaling (exercise) Network Algorithms: Maximum flow
Flow with Lower Bounds • Look for maximum flow with for each e: l(e) £f(e) £ c(e) • Problem solved in two phases • First, find admissible flow • Then, augment it to a maximum flow • Admissible flow: any flow f, with • Flow conservation • if vÏ{s,t}, flow into v equals flow out of v • Lower and upper capacity constraints fulfilled: • for each e: l(e) £f(e) £ c(e) Transshipment Network Algorithms: Maximum flow
Finding admissible flow l(e) a b c(e) New sink T’ Do this for each edge l(e) 0 a b c(e)-l(e) New source l(e) S’ Network Algorithms: Maximum flow
Finding admissible flow • Find maximum flow from S’ to T’ • If all edges from S’ (and hence all edges to T’) use full capacity, we have admissible flow: • f’(u,v) = f(u,v) + l(u,v) for all (u,v) in G Network Algorithms: Maximum flow
From admissible flow to maximum flow • Take admissible flow f (in original G) • Compute a maximum flow f’ from s to t in Gf • Here cf(u,v) = c(u,v) – f(u,v) – l(u,v) • f + f’ is a maximum flow from s to t that fulfills upper and lower capacity constraints • Any flow algorithm can be used Network Algorithms: Maximum flow
Circulations • Given: digraph G, lower bounds l, upper capacity bounds d • A circulation fulfills: • For allv: flow into v equals flow out of v • For all (u,v): l(u,v) £ f(u,v) £ c(u,v) • Existence of circulation: as flows with upper and lower bounds Network Algorithms: Maximum flow
From flow to circulation G s t f(t,s) = value(f) Network Algorithms: Maximum flow
A useful theorem • Let f be a circulation. Then f is a nonnegative linear combination of cycles in G. • Proof. Ignore lower bounds. Find a cycle c, with minimum flow on c r, and use induction with f – r * c. • If f is integer, `integer scalared’ linear combination. • Corollary: a flow is the linear combination of cycles and paths from s to t. • Look at the circulation by adding an edge from t to s and giving it flow value(f). Network Algorithms: Maximum flow
Next • Minimum cost flow Network Algorithms: Maximum flow