COMP36111: Advanced Algorithms I - Lecture 3:...
Transcript of COMP36111: Advanced Algorithms I - Lecture 3:...
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
COMP36111: Advanced Algorithms I
Lecture 3: Flow
Ian Pratt-Hartmann
Room KB2.38: email: [email protected]
2019–20
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• In this lecture, we consider the problem of computing maximalflows through networks of connections having limited capacity,and illustrate some of its unexpected applications.• The lecture is divided into four parts:
• introduction to flow networks;• the Ford-Fulkerson algorithm for computing optimal flows;• the matching problem and its polynomial-time solution;• the Busacker-Gowan algorithm for computing minimal-cost
optimal flows;• an application of Busacker-Gowan (possibly too close for
comfort).
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Outline
Flow networks
The Ford-Fulkerson algorithm
Matching
Reducing the bill
Third-year projects
Summary
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Let (V ,E ) be a directed graph. A back-loop is a pair of edges{(u, v), (v , u)}.• A flow network is a quintuple (V ,E , s, t, c) where (V ,E ) is a
directed graph with no back-loops, s, t ∈ V are distinct, andc : E → N.
5
14
1
139
8
2
11
94
• We assume s has no incoming edges and t no outgoing edges.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Let N = (V ,E , s, t, c) be a flow network. A flow in N is afunction f : E → R+ with the following properties:• The net flow out of any node u ∈ (V \ {s, t}) is zero:∑
v :(u,v)∈E
f (u, v)−∑
v :(v ,u)∈E
f (v , u) = 0.
• No edge (u, v) ∈ E exceeds its capacity:
f (u, v) ≤ c(u, v).
• The value of f is the quantity:∑v :(s,v)∈E
f (s, v)−∑
v :(v ,s)∈E
f (v , s) =∑
(v ,t)∈E
f (v , t)−∑
v :(t,v)∈E
f (t, v).
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Our problem: given a flow network N, compute a flow f for Nwhose value is maximum; such a flow will be called optimal.
• That is, we have the task:
MAX FLOWGiven: A flow network NReturn: An optimal flow f for N.
• It is a fact (though hardly obvious) that an optimal flowexists. However, it will not in general be unique.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Outline
Flow networks
The Ford-Fulkerson algorithm
Matching
Reducing the bill
Third-year projects
Summary
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Here is a flow (numbers in red) for the above flow network:
5 (5)
14 (9)
1 (0)
13 (5) 9 (8)8 (8)
2 (2)
11 (5)
9 (1) 4 (4)
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• The key idea: given a flow network N = (V ,E , s, t, c) and aflow f , we construct an auxiliary directed graph Nf ,G = (V ,Ef ), where Ef is the set of pairs:
{(u, v) ∈ E | f (u, v) < c(u, v)} ∪{(u, v) | (v , u) ∈ E and f (v , u) > 0}.
• That is, the edges of directed graph Nf are• those edges in E on which the capacity is not exhausted;• the reversal of those edges in E on which there is some flow.
• Note that Nf may contain back-loops.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Original network N with flow f :
5 (5)
14 (9)
1 (0)
13 (5) 9 (8)8 (8)
2 (2)
11 (5)
9 (1) 4 (4)
• Auxiliary directed graph Nf :
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Original network N with flow f :
5 (5)
14 (9)
1 (0)
13 (5) 9 (8)8 (8)
2 (2)
11 (5)
9 (1) 4 (4)
• Auxiliary directed graph Nf :
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Original network N with new flow f ′:
5 (5)
14 (10)
1 (1)
13 (4) 9 (9)8 (8)
2 (2)
11 (4)
9 (0) 4 (4)
• New auxiliary directed graph Nf ′ :
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Lemma (Min-cut, Max-flow)
Let N = (V ,E , s, t, c) be a flow network and f a flow in N. Thenthere is a path in Nf from s to t if and only if f is not optimal.
Proof.The only-if direction is trivial. Suppose, conversely, there is nopath from s to t in Nf . Let S be the set of nodes V reachablefrom s in Nf , and T = V \ S .
S T t
f (e) = c(e)
0
s
There is no backflow from T to S , and no spare capacity from Sto T .
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Here is the algorithm:
begin flowMax(V ,E ,s,t,c)set f (e) = 0 for all e ∈ Ewhile reachable(Nf ,s,t)
let e1, . . . , em be the edges on a path from s to tfor 1 ≤ i ≤ m
if ei ∈ E , increment f (ei )
else decrement f (e−1i )return f
• This algorithm is sometimes known as the Ford-Fulkersonalgorithm.
• It has various refinements, which we shall not describe indetail.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• We have just shown
TheoremIf N = (V ,E , s, t, c) is a flow network with integralcapacity-function c , then flowMax(V ,E ,s,t,c) returns anoptimal flow for N.• This theorem has the following corollary:
Corollary
If a flow network N has integral capacities, then there existsan optimal flow for N; in fact, there exists an optimal flow forN which is integer-valued.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• What about complexity?
• suppose |V | = n and c(e) ≤ C .
• We know that REACHABILITY can be solved inTime(O(n + m)) where n is the number of nodes and m thenumber of edges.
• The maximum flow is nC (s is connected to < n other nodes,with capacity ≤ C , so there are at most nC cycles of the mainloop).
• Hence the operating time is at O(n(n + m)C ).
• (Actually, we can do a bit better in regard to C . . . )
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Outline
Flow networks
The Ford-Fulkerson algorithm
Matching
Reducing the bill
Third-year projects
Summary
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Consider the following (slightly idealized) matchmakingproblem.• We are given:
• a set of n boys and n girls;• a specification of who is prepared to marry whom.
• We want to compute:• a 1–1 pairing of boys with girls producing the maximum
number of couples.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• For example:
boys girls
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• A bipartite graph is a triple G = (U,V ,E ) where U and Vare disjoint sets and E ⊆ U × V .
U V
• Note that G = (U ∪ V ,E ) can be regarded as a (directed)graph in the sense of the previous lecture.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Let G = (V ,W ,E ) be a bipartite graph. A matching is asubset E ′ ⊆ E such that E such that for all v ∈ V , there is atmost one w ∈W with (v ,w) ∈ E ′, and, for all w ∈W , thereis at most one v ∈ V with (v ,w) ∈ E ′.
• The matching is perfect if every node in V and W is incidentto some e ∈ E ′.
• We then have the problem:
MATCHINGGiven: A bipartite Graph GReturn: Yes if G has a perfect matching, and No otherwise.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Here is a naıve algorithm for solving this problem
begin naiveMatch(V ,W ,E )if V = ∅
if W = ∅ return Yeselse return No
pick v ∈ Vfor each {w | (v ,w) ∈ E}
Let (V ′,W ′,E ′) be result of removing v , w from (V ,W ,E )if naiveMatch(V ′,W ′,E ′), return Yes
return No
• It is pretty clear that this will in general take exponential timein n = |V |.
• Maybe we can do better . . .
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• MATCHING can be thought of as a flow optimization problem
⇒ source sink
• We take the links in the graph to represent flow capacities:• Let n be the number of boys (girls) and m the number of
possible matches.• all links have capacity 1;• an perfect matching corresponds to a flow from source to sink
with value n, where n is the number of boys/girls.
• But we have just seen how to solve this problem onO(n(n + m)) time!
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Outline
Flow networks
The Ford-Fulkerson algorithm
Matching
Reducing the bill
Third-year projects
Summary
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• A flow network with costs is a sextuple N = (V ,E , s, t, c , γ)where (V ,E , s, t, c) is a flow network and γ : E → N.
• We think of γ as a cost function, where γ(e) is the price perunit of flow along e.
• We can draw a flow network with each edge e labelledc(e)/γ(e).
5/1
14/2
1/10
13/19/0
8/1
2/11
11/11
9/14/3
• The total cost of a flow f in N is∑
e∈E f (e) · γ(e).
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• This suggests another interesting computation task:
MIN COST MAX FLOWGiven: A flow network with cost function, NReturn: An optimal flow f for N having the minimum
cost among all optimal flows.
• It is again obvious that such a minimum cost optimal flowexists. Again, it will not in general be unique.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• In fact, a slight modification of the above algorithm will solvethis problem beautifully.• We regard the cost function as giving distances in the derived
graph:• if (u, v) is an edge of Nf and (u, v) ∈ E , the distance d(u, v)
is γ(e);• if (u, v) is an edge of Nf and (v , u) ∈ E , the distance d(u, v)
is −γ(e).
• Notice that there these ‘distances’ may be negative.
• However, we may assume that there are no cycles of totalnegative length. (This is actually rather tricky to see . . . )
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
LemmaLet N be a flow network with cost function, and suppose f is aflow of value v through N, such that f has minimal cost among allflows of value v . Suppose π is a path in Nf from s to t of minimallength, and let f ′ be obtained from f by augmenting along π (inthe usual way). Then f ′ has minimal cost among all flows of valuev + 1.
• A rigorous proof is quite involved, and we will not give one inthis course.
• See Jungnickel Graphs, Networks and Algorithms, 3rd ed.Springer, 2008, p. 299 ff. Warning: this is hard.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Here is the resulting algorithm for computing minimum costoptimal flows:
begin flowMaxCost(V ,E ,s,t,c ,γ)set f (e) = 0 for all e ∈ Ewhile reachable(Nf ,s,t)
let e1, . . . , em be the edges on a path from s to tof minimal length according to γ
for 1 ≤ i ≤ mif ei ∈ E , increment f (ei )
else decrement f (e−1i )return f
• It is sometimes known as the Busacker-Gowen algorithm.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Outline
Flow networks
The Ford-Fulkerson algorithm
Matching
Reducing the bill
Third-year projects
Summary
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• The task:• Students choose projects, and each project is associated with
one or more supervisors;• supervisors have maximum loads;• projects have maximum numbers of participants;• The Lab Manager must assign students to projects so as to
maximize student satisfaction.
• The general set-up of (unranked) choices, projects andsupervisors can still be modelled as a flow network:
sourcesink
2 (0)
2 (1)
1 (1)
1 (1)
2 (0)
2 (1)
3 (2)
2 (1)
2 (1)
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• To take account of rankings, we introduce costs into the flownetwork:• choice rankings (for Student 1 only) are shown in blue;• we model these rankings as costs per unit of flow.
source
Student 1
sink
1
2
3
2 (0)
2 (1)
1 (1)
1 (1)
2 (0)
2 (1)
3 (2)
2 (1)
2 (1)
• Here, an optimal assignment of students to projectscorresponds to a least cost maximal flow in the network.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• So, we have a mathematical model of our problem:• the structure of students, projects, supervisors, choices,
loadings . . . is represented as a flow network with costs;• an optimal assignment of m students corresponds to a least
cost integral flow of size m
• Now use the Busacker-Gowen algorithm to solve this problem.• Remember:
• this algorithm runs incrementally, gradually increasing the flowfrom the source to the sink by one unit on each pass;
• after m passes, a least-cost flow of size m has been found;• at the (m + 1)st pass, the current least-cost flow of size m is
revised to yield a least-cost flow of size m + 1;• it runs in polynomial time.
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• How the program is used:• students input their choices (by a certain deadline) using a web
form;• once the deadline is passed the Third Year Project
Administrator runs the assignment software;• the assignment software computes the optimum assignment,
asks for verification, and writes the assignment to the CSadministration databases;
• the assignment is then published on the internet.
• Important considerations:• all students should be treated fairly (everyone equally likely to
get his first choice);• all supervisors should be treated fairly (loads should be as even
as possible).
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
• Program structure
Flow network
Busacker-Gowenalgorithm
Assignments
RandomizerData entrywebpage
VDU/fileoutput
Userinput
ACSOdatabase
Studentchoices
• And here is a run . . .
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
p:\programs\java\...\ProjAssignV4>java ProjAssign
Safe flag is set to true
maxNumberOfStudentsAssigned= 20
Number 1st 2nd 3rd 4th 5th Cost
1 1 0 0 0 0 3
2 2 0 0 0 0 5
3 2 1 0 0 0 9
4 2 2 0 0 0 13
5 2 2 1 0 0 18
.....
19 4 9 4 2 0 98
20 4 8 4 3 1 108
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Outline
Flow networks
The Ford-Fulkerson algorithm
Matching
Reducing the bill
Third-year projects
Summary
Flow networks Ford-Fulkerson Matching Reducing the bill Third-year projects Summary
Summary
• In this lecture, we have considered:• a pseudo-polynomial-time algorithm for determining optimal
flows in flow networks;• the reduction of MATCHING to flow optimization (with
bounded capacities);• a pseudo-polynomial-time algorithm for determining minimal
cost optimal flows in flow networks with costs;• a practical application of this algorithm;
• Reading:• G+T Ch 16.