I efteråret 2007 og foråret 2008 underviste jeg på ITU-kurset Social Software: Design og implementering.
Se http://mortengade.dk/2009/social-software-et-semesters-undervisning-i-en-post
Se http://social08.pbwiki.com
1. Sanders: Flows 1 Sanders: Flows
Ford Fulkerson Algorithm INFORMATIK A Bad Example for Ford Fulkerson INFORMATIK
Ô
[U. Zwick, TCS 148, p. 165–170, 1995]
½. a
Function FFMaxFlow( ´Î µ × Ø Ô: Ê) : Ê Let Ö
¾ 4 r 4
:= ¼ Consider the graph b
while path Ô ´× µ
Ø in do s 4 1 4 t
c
augment along Ô And the augmenting paths
4 1 4
return Ô¼ × Ø
d
time Ç´ÑÚ Ð ´ µµ Ô½ × Ø
Ô¾ × Ø
Ô¿ × Ø
The sequence of augmenting paths Ô¼ Ô½ Ô¾ Ô½ Ô¿ ´ µ£ is an infinite
sequence of positive flow augmentations.
The flow value does not converge to the maximum value .
Sanders: Flows 3 Sanders: Flows
Blocking Flows INFORMATIK Dinitz Algorithm INFORMATIK
is a blocking flow in À if Function DinitzMaxFlow( ´Î µ × Ø Ô: Ê) : Ê
paths Ô × Ø ¾Ô ´µ Ô´ µ := ¼
while path Ô ´×
µ do Ø in
1/1 1/0 Ö Ú Ö× Ë ´Øµ : Î Æ
s t
Ä ´Î ´Ù Úµ ¾ ´Úµ ´Ùµ ½ µ // layer graph
1/1
find a blocking flow in Ä
1/0 1/1 augment +=
return
2. Sanders: Flows 5 Sanders: Flows
Function blockingFlow(À ´Î µ) : Ê INFORMATIK Blocking Flows Analysis 1 INFORMATIK
Ô × : Path
¾ running time is ÜØ Ò × · Ö ØÖ Ø× · Ò ¡ Ö Ø ÖÓÙ ×
Ú NodeRef : Ô last ´µ
:= ¼ ¾ Ö Ø ÖÓÙ × Ñ, since at least one edge is saturated
loop // Round ¾ Ö ØÖ Ø× Ñ, since one edge is removed
if Ú Ø then // breakthrough
¾ ÜØ Ò × Ö ØÖ Ø× · Ò ¡ Ö Ø ÖÓÙ ×, since a retreat cancels
Æ := ÑÒ Ô´ µ ´ µ ¾Ô one extend and a breakthrough cancels Ò extends
foreach ¾ Ô do
´ µ+=Æ ´ · Òѵ
time is Ç Ñ ´ µ
Ç ÒÑ
if ´ µ Ô´ µ then remove from
Ô:= ×
elsif ´Ú Ûµ ¾ then Ô.pushBack Û ´ µ // extend
elsif Ú × then return // done
else delete the last edge from Ô in Ô and // retreat
Sanders: Flows 7 Sanders: Flows
Blocking Flows Analysis 2 INFORMATIK Blocking Flows Analysis 3 INFORMATIK
Unit capacities: Dynamic trees: breakthrough (!), retreat, extend in time Ç´ÐÓ Òµ
breakthroughs saturates all edges on Ô, i.e., amortized constant cost per
time Ç Ñ´´ · ÒµÐÓ Òµ
edge. Theory alert: In practice, this seems to be slower (few breakthroughs, many
time Ç Ñ ´ · Òµ retreat, extend ops.)
4. Sanders: Flows 13 Sanders: Flows
Preflow-Push Algorithms INFORMATIK Level Function INFORMATIK
Idea: make progress by pushing towards Ø
Preflow : a flow where the flow conservation constraint is relaxed to
excess Ú´ µ ¼. Maintain
Procedure push ´ ´Ú Ûµ Ƶ an approximation ´Úµ of the BFS distance from Ú to Ø in .
assert Æ ¼ invariant ´Øµ ¼
assert residual capacity of Æ invariant ´×µ Ò
assert excess Ú ´µ Æ invariant ´Ú Ûµ ¾ ´Úµ ´Ûµ · ½ // no steep edges
´ µ Æ
excess Ú
if ´ µ ¼ then ´ µ· Æ Edge directions of ´Ú Ûµ
else ´reverse´ µµ Æ steep: ´Û µ ´Úµ ½
downward: ´Û µ ´Úµ
horizontal: ´Û µ ´Úµ
upward: ´Û µ ´Úµ
Sanders: Flows 15 Sanders: Flows
INFORMATIK Lemma 3. INFORMATIK
Procedure genericPreflowPush(G=(V,E), var f)
forall ´× Úµ ¾ do push ´ Ô´ µµ // saturate
active nodes Ú Ü ×× ´Ú µ ¼µ path Ú × ¾
´×µ:= Ò Intuition: what got there can always go back.
´Úµ:= ¼ for all other nodes
while Ú ¾Î Ò × Ø Ü ×× ´Ú µ
¼ do // select active node Proof. Ë Ù ¾Î path Ú Ù ¾ ,Ì Î Ò Ë . Then
if ´Ú Ûµ ¾ ´Ûµ ´Úµ then // select eligible edge
choose some Æ Ñ Ò Ü ×× ´Úµ Ö × Ô ´ µ Ü ×× ´Ùµ ´ µ ´µ
push´ Æ µ // no new steep edges since reverse edge goes upward Ù¾Ë ¾ ´Ì ¢Ë µ ¾ ´Ë ¢Ì µ
else ´Ú µ·· // relabel. Cannot introduce steep edges !
´Ú Ûµ ¾ ¾Ë Û¾Ë
Ú by Def. of ,Ë
Obvious choice for Æ Æ Ñ Ò Ü ×× ´Úµ Ö × Ô ´ µ Thus ´Ù Ûµ ¾ ´Ì ¢ Ë µ ´ µµ ¼ Otherwise Û Ù ´ µ¾
Ö × Ô´ µ
È
Saturating push: Æ Hence, Ü ×× ´Ùµ ¼
Ù¾Ë
Ö × Ô´ µ
nonsaturating push: Æ
One the negative excess of × can outweigh excess Ú ´ µ ¼.
To be filled in: Hence × ¾ Ë.
How to select active nodes and eligible edges?
5. Sanders: Flows 17 Sanders: Flows
Lemma 4. INFORMATIK Partial Correctness INFORMATIK
Ú ¾Î ´Úµ ¾Ò
Lemma 5. When Ò Ö ÈÖ ÓÛÈÙ× terminates is a maximal flow.
Proof. Suppose Ú is lifted to ´Úµ ¾Ò.
By Lemma 3, there is a (simple) path Ô to × in . Proof.
Ô has at most Ò ½ nodes is a flow since Ú ¾Î Ò × Ø Ü ×× ´Ú µ ¼.
´×µ Ò.
Hence ´Úµ ¾Ò. Contradiction. To show that is maximal, it suffices to show that
path Ô × Ø ¾ (Max-Flow Min-Cut Theorem):
Since ´×µ Ò, ´Øµ ¼, Ô would have to contain steep edges.
That would be a contradiction.
Sanders: Flows 19 Sanders: Flows
Lemma 6. # Relabels ¾Ò ¾ INFORMATIK Lemma 7. # saturating pushes ÒÑ INFORMATIK
Proof. ´Úµ ¾Ò, i.e., Ú is relabeled at most ¾Ò time. Proof.
Hence, at most Î ¡ ¾Ò ¾Ò relabels.
¾ We show that there are at most Ò sat. pushes over any edge ´Ú Ûµ.
´ Ƶ removes from
A saturating push .
Only a push on ´Û Ú µ can reinsert into .
For this to happen, Û must be lifted at least two levels.
Hence, at most Ò ¾ ¾ Ò saturating pushes over Ú Û´ µ
6. Sanders: Flows 21 Sanders: Flows
¡
Lemma 8. # nonsaturating pushes Ç Ò¾ Ñ INFORMATIK Searching for Eligible Edges INFORMATIK
if Æ ÑÒ Ü ×× ´Ú µ Ö × Ô´ µ
for arbitrary node and edge selection rules. (arbitrary-preflow-push)
Every node Ú maintains a ÙÖÖ ÒØ pointer to its sequence of outgoing
edges in .
Proof. ¨ ´Úµ. (Potential)
¨ ¼ initially
Ú Ú is active
invariant no edge ´Ú Ûµ to the left of ÙÖÖ ÒØ is eligible
relabel increases ¨ by one. ( ¾Ò ¢)
¾
Reset ÙÖÖ ÒØ at a relabel ( ¾Ò¢)
saturating push increases ¨ by at most ¾Ò ( ÒÑ ) ¢ Invariant cannot be violated by a push over a reverse edge ¼ ´Û Úµ since
µ total increase at most ¾Ò · ¾Ò Ñ ¾ ¾
this only happens when ¼ is downward,
nonsaturating push decreases ¨ by at least one i.e., is upward and hence not eligible.
¨ ¼ always.
Lemma 9.
Total cost for searching ¾Ò ¡ Ö ´Úµ ÒÑ Ç´Òѵ
Ú ¾Î
Sanders: Flows 23 Sanders: Flows
INFORMATIK Highest Level Preflow Push INFORMATIK
¡
Ç Ò¾ Ñ .
Theorem 10. Arbitrary Preflow Push finds a maximum flow in time
Always select active nodes that maximize ´Úµ
Proof. Use bucket priority queue (insert, increaseKey, deleteMax)
Lemma 5: partial correcntess not monotone (!) but relabels “pay” for scan operations
Initialization in time Ç´Ò · ѵ. ÔÑ nonsaturating pushes.
Maintain set (e.g., stack, FIFO) of active nodes. Lemma 11. At most Ò¾
Use reverse edge pointers to implement push.
¾
Lemma 6: Ò¾ relabels
Proof. later
Lemma 7: ÒÑ saturating pushes
Ç Ò ÔÑ
Theorem 12. Highest Level Preflow Push finds a maximum flow in time
¡ ¡
Lemma 8: Ç ¾
Ò Ñ nonsaturating pushes ¾
Lemma 9: Ç ´Òѵ search time for eligible edges
———————————————————————–
¡
Total time Ç Ò¾ Ñ
7. Sanders: Flows 25 Sanders: Flows
Proof of Lemma 11 INFORMATIK Claims: INFORMATIK
Ã
ÔÑ tuning parameter 1. Ò¾ Ã nonsaturating pushes in all cheap phases together
¼ ´Ú µ Û ´Ûµ ´Úµ scaled number of dominated nodes 2. ¨ ¼ always, ¨ Ò¾ Ã initially (obvious)
Ã
¨ ´µ
¼Ú. (Potential) ¨ by at most Ò Ã .
3. a relabel or saturating push increases
Ú Ú is active
£ Ñ Ü ´Úµ Ú is active (highest level) 4. a nonsaturating push does not increase ¨.
phase:= all pushes between two consecutive changes of £ 5. an expensive phase with É Ã nonsaturating pushes decreases ¨ by
expensive phase: more than à pushes at least É.
cheap phase: otherwise
· · · · µ total decrease ´¾Ò · Òѵ à · Ò¾
Lemma 6 Lemma 7 2. 3. 4.: Ò
Ã
¾
· ´¾Ò · Ò · ÑÒ µ Ã nonsat. pushes in expensive phases
¿ ¾ ¾
´¾Ò · Ò · ÑÒ µ Ã · Ò Ã Ç Ò ÔÑ¡
This
This ·½ ¿ ¾ ¾ ¾ ¾
ÔÑ
nonsaturating pushes overall for
Ã
Sanders: Flows 27 Sanders: Flows
¾ INFORMATIK INFORMATIK
Claim 1: Ò Ã nonsat. pushes in cheap phases
Claim 3: A relabel or saturating push increases ¨ by at most Ò Ã
¾
We first show that there are at most Ò phases
£ Ñ Ü Ú Ú is active Let Ú denote the relabeled or activated node.
´Ûµ ´Úµ
(changes of ).
£ ¼ initially, £ ¼ always. ¼ ´Ú µ Û Ò
£ , i.e., Ã Ã
A relabel of Ú can increas only the ¼ -value of Ú .
Only relabels increase
¾Ò ¾
increases by Lemma 6 and hence
A saturating push on ´Ù Û µ only changes the ¼ -value of Û .
¾Ò ¾
decreases
——————————————————
Ò¾ changes overall
Claim 4: a nonsaturating push across ´Ú Û µ does not increase ¨
By Definition of a cheap phase, it has at most à pushes.
Ú is deactivated
Û may be activated
but ¼ ´Ûµ ¼ ´Ú µ
8. Sanders: Flows 29 Sanders: Flows
Claim 5: an expensive phase INFORMATIK Heuristic Improvements INFORMATIK
¨ by at least
ª Ò ¡. Why?
with É Ã nonsaturating pushes decreases É.
¾
Naive algorithm has best case
During a phase £ remains constant We can do better.
£
Each nonsat. push decreases the number of nodes at level aggressive local relabeling: ´Úµ:= ½ · Ñ Ò ´Ûµ ´Ú Ûµ ¾
Hence, Û ´Ûµ £ Ã during an expensive phase (like a sequence of relabels)
Each nonsat. push across Ú Û decreases´ µ ¨ by
¼ ´Ú µ ¼ ´Ûµ Û ´Ûµ £ à à à ½ global relabeling: (initially and every Ǵѵ edge inspections):
´Úµ Ö Ú Ö× Ë ´Øµ for nodes that can reach Ø in .
Special treatment of nodes with ´Úµ Ò. (Returning flow is easy)
Gap Heuristics. Nobody can reach Ø across an empty level:
if Ú ´Úµ then foreach Ú with ´Úµ do ´Úµ:= Ò
Sanders: Flows 31 Sanders: Flows
Timings: Random Graphs INFORMATIK Timings: CG1 INFORMATIK
Gen Rule BASIC HL LRH GRH GAP LEDA Gen Rule BASIC HL LRH GRH GAP LEDA
rand FF 5.84 6.02 4.75 0.07 0.07 — CG1 FF 3.46 3.62 2.87 0.9 1.01 —
33.32 33.88 26.63 0.16 0.17 — 15.44 16.08 12.63 3.64 4.07 —
HL 6.12 6.3 4.97 0.41 0.11 0.07 HL 20.43 20.61 20.51 1.19 1.33 0.8
27.03 27.61 22.22 1.14 0.22 0.16 192.8 191.5 193.7 4.87 5.34 3.28
MF 5.36 5.51 4.57 0.06 0.07 — MF 3.01 3.16 2.3 0.89 1.01 —
26.35 27.16 23.65 0.19 0.16 — 12.22 12.91 9.52 3.65 4.12 —
Ò ¾ ½¼¼¼ ¾¼¼¼ Ñ ¿Ò Ò ¾ ½¼¼¼ ¾¼¼¼ Ñ ¿Ò
FF FIFO node selection, HL hightest level, MF modified FIFO FF FIFO node selection, HL hightest level, MF modified FIFO
HL ´Úµ Ò is special, HL ´Úµ Ò is special,
LRH local relabeling heuristic, LRH local relabeling heuristic,
GRH global relabeling heuristics GRH global relabeling heuristics
9. Sanders: Flows 33 Sanders: Flows
Timings: CG2 INFORMATIK Timings: AMO INFORMATIK
Gen Rule BASIC HL LRH GRH GAP LEDA Gen Rule BASIC HL LRH GRH GAP LEDA
CG2 FF 50.06 47.12 37.58 1.76 1.96 — AMO FF 12.61 13.25 1.17 0.06 0.06 —
239 222.4 177.1 7.18 8 — 55.74 58.31 5.01 0.1399 0.1301 —
HL 42.95 41.5 30.1 0.17 0.14 0.08002 HL 15.14 15.8 1.49 0.13 0.13 0.07001
173.9 167.9 120.5 0.3599 0.28 0.1802 62.15 65.3 6.99 0.26 0.26 0.1399
MF 45.34 42.73 37.6 0.94 1.07 — MF 10.97 11.65 0.04999 0.06 0.06 —
198.2 186.8 165.7 4.11 4.55 — 46.74 49.48 0.1099 0.1301 0.1399 —
Ò ¾ ½¼¼¼ ¾¼¼¼ Ñ ¿Ò Ò ¾ ½¼¼¼ ¾¼¼¼ Ñ ¿Ò
FF FIFO node selection, HL hightest level, MF modified FIFO FF FIFO node selection, HL hightest level, MF modified FIFO
HL ´Úµ Ò is special, HL ´Úµ Ò is special,
LRH local relabeling heuristic, LRH local relabeling heuristic,
GRH global relabeling heuristics GRH global relabeling heuristics
Sanders: Flows 35 Sanders: Flows
Asymptotics, Ò ¾ ¼¼¼ ½¼¼¼¼ ¾¼¼¼¼ INFORMATIK Research Problem INFORMATIK
Gen Rule GRH GAP LEDA
Define a family of flow networks such that highest level preflow push with all
rand FF 0.16 0.41 1.16 0.15 0.42 1.05 — — —
the heuristics is forced into its worst case even if ties are broken randomly.
HL 1.47 4.67 18.81 0.23 0.57 1.38 0.16 0.45 1.09
MF 0.17 0.36 1.06 0.14 0.37 0.92 — — —
CG1 FF 3.6 16.06 69.3 3.62 16.97 71.29 — — —
HL 4.27 20.4 77.5 4.6 20.54 80.99 2.64 12.13 48.52
MF 3.55 15.97 68.45 3.66 16.5 70.23 — — —
CG2 FF 6.8 29.12 125.3 7.04 29.5 127.6 — — —
HL 0.33 0.65 1.36 0.26 0.52 1.05 0.15 0.3 0.63
MF 3.86 15.96 68.42 3.9 16.14 70.07 — — —
AMO FF 0.12 0.22 0.48 0.11 0.24 0.49 — — —
HL 0.25 0.48 0.99 0.24 0.48 0.99 0.12 0.24 0.52
MF 0.11 0.24 0.5 0.11 0.24 0.48 — — —
10. Sanders: Flows 37 Sanders: Flows
Minimum Cost Flows INFORMATIK The Cycle Canceling Algorithm for Min-Cost Flow INFORMATIK
Define ´Î µ, , Ü ×× , and Ô as for maximum flows. Residual cost: Let ´Ú Ûµ ¾ , ¼ ´Û Úµ.
Let Ê denote the edge costs.
È
´ µ ´ ¼µ if ¼ ¾ , ´ ¼µ ¼, ´ µ ´ µ otherwise.
Consider ×ÙÔÔÐÝ Î Ê with ھΠ×ÙÔÔÐÝ Ú ´ µ ¼. A negative supply Lemma 13. A feasible flow is optimal iff cycle ¾ ´ µ ¼
is called a demand.
Objective: minimize ´µ ´µ´µ Proof. not here
¾ A pseudopolynomial Algorithm:
subject to
Ú ¾Î Ü ×× ´Ú µ ×ÙÔÔÐÝ ´Úµ flow conservation constraints := any feasible flow // Exercise: solve this problem using maximum flows
¾ ´µ Ô´ µ capacity constraints invariant is feasible
while cycle ´ µ ¼ do augment flow around
Corollary 14 (Integrality Property:). If all edge capacities are integral then
there exists an integral minimum cost flow.
Sanders: Flows 39 Sanders: Flows
Finding a Feasible Flow INFORMATIK Better Algorithms INFORMATIK
set up a maximum flow network £ starting with the min cost flow problem : Theorem 15. The min-cost flow problem can be solved in time
¾ Add a vertex × Ç ÑÒ ÐÓ Ò · Ñ ÐÓ Ñ Ü ¾
¾
Ô´ µ¡ .
¾ Ú ¾ Î with ×ÙÔÔÐÝ ´Úµ ¼, add edge ´× Úµ with cap. ×ÙÔÔÐÝ ´Úµ For details take the courses in optimization or network flows.
¾ Add a vertex Ø
¾ Ú ¾ Î with ×ÙÔÔÐÝ ´Úµ ¼, add edge ´Ú ص with cap. ×ÙÔÔÐÝ ´Úµ
¾ find a maximum flow in £
saturates the edges leaving × µ is feasible for
otherwise there cannot be a feasible flow ¼ because ¼ could easily be
converted into a flow in £ with larger value.
11. Sanders: Flows 41 Sanders: Flows
Special Cases of Min Cost Flows INFORMATIK Maximum Weight Matching INFORMATIK
Transportation Problem: ¾ Ô´ µ ½ Generalization of maximum cardinality matching. Find a matching Å £
È
Minimum Cost Bipartite Perfect Matching:
such that Û Å £´ µ ¾Å £ Û´ µ is maximized
A transportation problem in a bipartite graph ´ ¢ µ Applications: Graph partitioning, selecting communication partners
with Theorem 16. A maximum weighted matching can be found in time
×ÙÔÔÐÝ ´Ú µ ½ for Ú ¾ , ¡
Ç ÒÑ · Ò ÐÓ
¾
Ò . [Gabow 1992]
×ÙÔÔÐÝ ´Ú µ ½ for Ú ¾ .
An integral flow defines a matching
Approximate Weighted Matching
Reminder: Å ´ µ
is a matching if Î Å has maximum degree one.
Theorem 17. There is an Ǵѵ time algorithm that finds a matching of
weight at leastÑ Ü Å Û´Å µ ¾. [Drake Hougardy 2002]
matching
The algorithm is a ½ ¾-approximation algorithm.
A rule of Thumb: If you have a combinatorial optimization problem. Try to
formulate it as a shortest path, flow, or matching problem. If this fails its likely
to be NP-hard.
Sanders: Flows 43 Sanders: Flows
Approximate Weighted Matching Algorithm INFORMATIK Proof of Approximation Ratio INFORMATIK
Å ¼ :=
invariant Å ¼ is a set of simple paths Let Å £ denote a maximum weight matching.
while do // find heavy simple paths ´ µ
It suffices to show that Û Å ¼ ´ µ
Û Å£ .
select any Ú ¾ Î with degree´Úµ ¼ // select a starting node Assign each edge to that incident node that is deleted first.
while degree Ú ´ µ ¼ do // extend path greedily £ ¾ Å £ are assigned to different nodes.
´Ú Ûµ:= heaviest edge leaving Ú
All
Consider any edge £ ¾ Å £ and assume it is assigned to node Ú .
// (*)
Å ¼ := Å ¼ ´Ú Ûµ Since £ is assigned to Ú , it was available in line (*).
remove Ú from the graph
Hence, there is an edge ¾Å ¼½ assigned to Ú with Û ´µ Û ´ £µ.
Ú := Û
return any matching Å ´ µ
Å ¼ with Û Å ´ µ¾
Û Å¼
// one path at a time, e.g., look at the two ways to take every other edge.