forked from EisenIn/DiscreteOpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflows.tex
2226 lines (1750 loc) · 80 KB
/
flows.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Paths, cycles and flows in graphs}
\label{cha:short-paths-graphs}
Suppose you want to find a shortest path from a given starting point
to a given destination. This is a common scenario in driver assistance
systems (GPS) and can be modeled as one of the most basic
combinatorial optimization problems, the \emph{shortest path problem}.
In this chapter, we introduce directed graphs, shortest paths and
flows in networks. We focus in particular on the maximum-flow problem,
which is a linear program that we solve with direct methods, versus
the simplex method, and analyze the running time of these direct
methods.
% \section{Growth of functions}
% \label{sec:growth-functions}
% In the analysis of algorithms, it is more appropriate to investigate
% the asymptotic running time
% of an algorithm depending on the input and not the
% precise running time itself. We review the $O,\Omega$ and $\Theta$-notation.
% \begin{definition}[$O,\Omega,\Theta$-notation]
% Let $T,f: \setN \to \setN$ be two functions
% \begin{itemize}
% \item $T(n)$ is in $O(f(n))$, if there exist positive constants $n_o\in \setN$ and $c\in\setR_{>0}$
% with $T(n) \leq c \cdot f(n)$ for all $n\geq n_0$.
% \item $T(n)$ is in $\Omega(f(n))$, if there exist constants $n_o\in \setN$ and $c\in\setR_{>0}$
% with $T(n) \geq c \cdot f(n)$ for all $n\geq n_0$.
% \item $T(n)$ is in $\Theta(f(n))$ if $T(n)$ is both in $O(f(n))$ and
% in $\Omega(f(n))$.
% \end{itemize}
% \end{definition}
% \begin{example}
% The function $T(n)=2n^2 + 3n +1$ is in $O(n^2)$, since for all
% $x\geq1$ one has $2n^2 + 3n + 1 \leq 6n^2$. Here $n_0 = 1$ and $c =
% 6$.
% Similarly $T(n) = \Omega(n^2)$, since for each $n\geq1$ one has $2n^2 + 3n
% +1\geq n^2$. Thus $T(n)$ is in $\Theta(n^2)$.
% \end{example}
\section{Graphs}
\label{sec:graphs}
\begin{definition}
\label{def:3}
A \emph{directed graph} is a tuple $G = (V,A)$, where $V$ is a finite set of elements, called the
\emph{vertices} of $G$ and $A\subseteq(V\times V)$ is the set of
\emph{arcs} of $G$. We denote an arc by its two defining nodes $(u,v) \in
A$. The nodes $u$ and $v$ are called \emph{tail} and \emph{head}
of the arc $(u,v)$ respectively.
\end{definition}
\begin{figure}
\centering
\tikzstyle{vertex}=[circle,fill=black!25,minimum size=15pt,inner sep=0pt]
\tikzstyle{selected vertex} = [circle,fill=black!25,minimum size=15pt,inner sep=0pt]
\tikzstyle{edge} = [draw,thick,->]
\tikzstyle{weight} = [font=\small]
\tikzstyle{selected edge} = [draw,thick, ->,blue!50]
\tikzstyle{ignored edge} = [draw,line width=5pt,-,black!20]
\begin{tikzpicture}[scale=1.5, auto,swap]
\foreach \pos/\name in {{(2,1)/u}, {(4,1)/v},
{(3,0)/x}, {(2,-1)/y}, {(4,-1)/z}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \source/ \dest in { u/y, x/u, y/x, z/x, x/y}
\path[edge] (\source) -- (\dest);
\path (u) edge [bend right, ->, thick] (v);
\path (v) edge [bend right, ->, thick] (u);
\end{tikzpicture}
\caption{Example of a directed graph with $5$ nodes and $7$ arcs.}
\label{ex:graph}
\end{figure}
\begin{definition}[Walk, path, distance]
\label{f:def:6}
A \emph{walk} is a sequence of the form
$$P=(v_0,a_1,v_1,\ldots,v_{m-1},a_m,v_m),$$ where $a_i =
(v_{i-1},v_i)\in A$ for $i=1,\ldots,m$. If the nodes $v_0,\ldots,v_m$ are all
different, then $P$ is a \emph{path}. The \emph{length } of $P$ is
$m$. The \emph{distance} of two nodes $u$ and $v$ is the length of
a shortest path from $u$ to $v$. It is denoted by $d(u,v)$.
\end{definition}
\begin{example}
The following is a walk and a path of the graph in
Figure~\ref{ex:graph}.
\begin{displaymath}
\begin{array}{c}
z,(z,x),x,(x,u),u,(u,v),v,(v,u),u,(u,y),y,(y,x),x\\
y,(y,x),x,(x,u),u,(u,v),v\\
\end{array}
\end{displaymath}
\end{example}
% \begin{figure}
% \centering
% $\psmatrix[colsep=0.2cm,mnode=circle]
% v_0&v_1 & v_3 & v_4 & v_5 & v_6 & & v_0&v_1 & v_3 & v_4 & v_5 & v_6 \\
% \ncline{->}{1,1}{1,2}
% \ncline{->}{1,2}{1,3}
% \ncarc[arcangle=30]{->}{1,3}{1,2}
% \ncarc[arcangle=30]{->}{1,5}{1,1}
% \ncline{->}{1,3}{1,4}
% \ncline{->}{1,4}{1,5}
% \ncline{->}{1,5}{1,6}
% \ncline{->}{1,8}{1,9}
% \ncline{->}{1,9}{1,10}
% \ncline{->}{1,10}{1,11}
% \ncline{->}{1,11}{1,12}
% \ncline{->}{1,12}{1,13}
% \endpsmatrix
% $
% \caption{Example of a walk and a path. }
% \end{figure} Das Beispiel
\section{Representing graphs and computing the distance of two nodes}
\label{sec:repr-graphs-comp}
We represent a graph with $n$ vertices $v_1,\ldots,v_n$ as an array $A[v_1,\ldots,v_n]$,
where the entry $A[v_i]$ is a pointer to a linked list of vertices,
the \emph{neighbours of $v_i$}. $N(v_i) = \{ u \in V \colon (v_i,u) \in A\}$.
\begin{figure}
\centering
\includegraphics{figures/adjacency.pdf}
\caption{Adjacency list representation of the graph in
Figure~\ref{ex:graph}. }
\end{figure}
\subsection{Breadth-first search}
\label{subsec:BFs}
We next describe a very basic algorithm that computes the distances
from a designated node $s \in V$ to all other nodes.
The \emph{distance} from $s$ to $v$ is denoted by $d(s,v)$. It is the
smallest integer $i$ such that there exists a path from $s$ to $v$ of
length $i$. If there does not exist such a path, then $s$ and $v$
are \emph{not connected} and we define $d(s,v) = \infty$. For $ i \in
\setN_0$, $V_i\subseteq V$ denotes the set of vertices that have distance $i$
from $s$. Notice that $V_0 = \{s\}$.
\begin{lemma}
\label{lem:15}
For $i=1,\ldots,n-1$, the set $V_{i}$ is equal to the set of
vertices $v \in V \backslash (V_0\cup\cdots\cup V_{i-1})$ such that there exists an arc
$(u,v) \in A$ with $u \in V_{i-1}$.
\end{lemma}
\begin{proof}
Suppose that $v \notin V_0\cup\cdots\cup V_{i-1}$ and there exists an arc $uv\in A$
with $u \in V_{i-1}$. Since $u \in V_{i-1}$, there exists a path
$s,a_1,v_1,a_2,v_2,\ldots,a_{i-1},u$ of length $i-1$ from $s$ to $u$. The
sequence $s,a_1,v_1,a_2,v_2,\ldots,a_i,u,uv,v$ is a path of length
$i$ from $s$ to $v$ and thus $v \in V_{i}$.
If, on the other hand, $v \in V_{i}$, then there exists a path
\begin{displaymath}
s,a_1,v_1,\ldots,a_{i-1},u,a_{i},v
\end{displaymath}
of length $i$ from $s$ to $v$. We need to show that $u \in V_{i-1}$
holds. Clearly, since there exists a path of length $i-1$ from $s$ to
$u$, one has $u \in V_j$ with $j\leq i-1$. If $j<i-1$, then there exists
a path $s,a_1',v_1',\ldots,a_j',u$ of length $j$ which can be extended
to a path of length $j+1<i$ from $s$ to $v$
\begin{displaymath}
s,a_1',v_1',\ldots,a_j',u,a_{i},v
\end{displaymath}
which contradicts $v\in V_i$. \qed
\end{proof}
The \emph{breadth-first search algorithm} is an implementation of
Lemma~\ref{lem:15}.
The algorithm maintains arrays
\begin{displaymath}
\begin{array}{c}
D[v_1=s,v_2,\ldots,v_n]\\
\pi[v_1=s,v_2,\ldots,v_n]\\
\end{array}
\end{displaymath}
and a queue $Q$ that contains only $s$ in the beginning. The array
$D$ contains at termination of the algorithm the distances from $s$ to
all other nodes and is initialized with $[0,\infty,\ldots,\infty]$.
The array $\pi$ contains predecessor information for
shortest paths, in other words, when the algorithm terminates, $\pi[v]
= u$, where $uv$ is an arc and $D[u]+1 = D[v]$. The array $\pi$ is
initialized with $[0,\ldots,0]$.
After this initialization, the algorithm proceeds as follows.
\begin{tabbing}
{\bf while} \= $Q \neq \emptyset$ \\
\> $u := head(Q)$ \\
\> {\bf for} \= each $v \in \delta^+(u)$ \\
\> \> {\bf if} \= ($D[v]=\infty$) \\
\> \> \> $\pi[v]:=u$ \\
\> \> \> $D[v]:=D[u]+1$ \\
\> \> \> $enqueue(Q,v)$ \\
\> $dequeue(Q)$
\end{tabbing}
Here the function $head(Q)$ returns the next element in the queue and
$dequeue(Q)$ removes the first element of $Q$, while $enqueue(Q,v)$
adds $v$ to the queue $Q$ as last element.
\begin{lemma}
\label{lem:17}
The breadth-first search algorithm assigns \emph{distance labels} $D$
correctly.
\end{lemma}
\begin{proof}
We show the following claim by induction on $i \in \{0,\ldots,n-1\}$.
\begin{quote}
For each $i \in \{1,\ldots,n-1\}$ there exists a point in time where:
\begin{enumerate}[i)]
\item $Q$ contains precisely the elements of $V_i$
\label{item:1}
\item for each $v \in V_i$, $D[v] = d(s,v)$ \label{item:2}
\item for each $v \in V_i$ one has $\pi[v]v$
is an arc and $\pi[v] \in V_{i-1}$.
\end{enumerate}
\end{quote}
Once this claim is shown, the lemma follows, because the labels $D[v]$
and $\pi[v]$ are only changed once, if at all, from $\infty$ or $0$
to an integer
or a vertex respectively.
Since $V_0 = \{s\}$ and since $Q = [s]$ and $D[s] = 0$ after the initialization, the
claim holds for $ i = 0$. Suppose $i>0$. By the induction hypothesis,
there is a point in time, where $Q$ contains precisely $V_{i-1}$. By
Lemma~\ref{lem:15}, after the last element of $V_{i-1}$ is dequeued
$Q$ contains precisely the elements in $V_i$. Also, since $D[u] =
d(s,u) = i-1$ for all $u \in V_{i-1}$, we have for each $v \in V_i$ that $D[v]
= d(s,v)=i$. Also $\pi[v]v$ is an arc, by virtue of the algorithm, and
$\pi[v] \in V_{i-1}$. \qed
\end{proof}
% \begin{proof}
% Let $v \in V$. We show by induction on $d(s,v)$ that the labels are
% correctly assigned.
% If $d(s,v)=0$, then $s=v$ and $D[v]=0$. If $d(s,v)=1$, then $v$ is
% a neighbor of $s$ and $D[v]=1$ is set correctly in the first
% iteration of the {\bf while} loop.
% Let $d(s,v)>1$. Then there exists a $u\neq s,v$ with $d(s,u)=d(s,v)-1$
% and $uv \in A$. By induction, the label $D[u]=d(s,u)$ is set
% correctly by the breadth-first-search algorithm. Also, since the
% breadth-first-search algorithm computes for $v$ a path of length
% $D[v]$ from $s$ to $v$, the node $v$ receives a label which is
% greater than or equal to $d(s,v)$. If we consider the sequence (over
% time) of assigned labels, that breadth-first-search is assigning,
% then it is easy to see that this sequence is monotonously
% increasing, see exercise~\ref{item:16}. The node $v$ is thus
% explored at the latest, when $u$ is dequeued. This shows that the
% label of $v$, $D[v]$ is assigned correctly. \qed
% \end{proof}
\begin{definition}[Directed tree]
\label{f:def:6}
A \emph{directed tree} is a directed graph $T = (V,A)$ with
$|A| = |V| -1$ and containing a node $r \in V$ such that there
exists a path from $r$ to all other nodes of $T$.
\end{definition}
\begin{lemma}
\label{lem:18}
Consider the arrays $D$ and $\pi$ after the termination of the
breadth-first-search algorithm.
The graph $T = (V',A')$ with $V' = \{v \in V \colon D[v]<\infty\}$ and $A' =
\{ \pi(v) v \colon 1\leq D[v]<\infty\}$ is a tree.
\end{lemma}
\begin{proof}
Clearly, $|A'| = |V'| -1$. For any $i \in \{1,\ldots,n-1\}$, by
backtracking the $\pi$-labels from any $v \in V_i$, we will eventually
reach $s$.
\end{proof}
\begin{definition}
\label{f:def:7}
The tree $T$ from lemma~\ref{lem:18} is the \emph{shortest-path-tree}
of the (unweighted) directed graph $G = (V,A)$.
\end{definition}
% %% Adjacency matrix of graph
% %% \ a b c d e f g
% %% a x 7 5
% %% b 7 x 8 9 7
% %% c 8 x 5
% %% d 5 9 x 15 6
% %% e 7 5 15 x 8 9
% %% f 6 8 x 11
% %% g 9 11 x
\tikzstyle{vertex}=[circle,fill=black!25,minimum size=15pt,inner sep=0pt]
\tikzstyle{selected vertex} = [vertex, fill=red!24]
\tikzstyle{edge} = [draw,thick,->]
\tikzstyle{weight} = [font=\small]
\tikzstyle{selected edge} = [draw,thick, ->,blue!50]
\tikzstyle{ignored edge} = [draw,line width=5pt,-,black!20]
\begin{figure}
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest in { s/a, s/c,
b/s, b/d,d/e,
b/e,e/c, a/b}
\path[edge] (\source) -- (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{The breadth-first search algorithm starts with the queue
$Q=[s]$. The distance labels for $[s,a,b,c,d,e]$ are
$[0,\infty,\infty,\infty,\infty,\infty]$ respectively.}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in { {(4,1)/d},
{(3,0)/b}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(2,1)/a},{(2,-1)/c}, {(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest in { s/a, s/c,
b/s, b/d,d/e,
b/e,e/c, a/b}
\path[edge] (\source) -- (\dest);
\end{tikzpicture}
\caption{After the first iteration of the {\bf while} loop the queue
is $Q = [a,c]$ and the distance labels are $[0,1,\infty,1,\infty,\infty]$ respectively. }
\end{subfigure}
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
\foreach \pos/\name in { {(4,1)/d},
{(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(2,1)/a}, {(3,0)/b}, {(2,-1)/c}, {(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest in { s/a, s/c,
b/s, b/d,d/e,
b/e,e/c, a/b}
\path[edge] (\source) -- (\dest);
\end{tikzpicture}
\caption{After the second iteration of the {\bf while} loop the queue
is $Q = [c,b]$ and the distance labels are $[0,1,2,1,\infty,\infty]$ respectively. }
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in { {(4,1)/d},
{(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(2,1)/a}, {(3,0)/b}, {(2,-1)/c}, {(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest in { s/a, s/c,
b/s, b/d,d/e,
b/e,e/c, a/b}
\path[edge] (\source) -- (\dest);
\end{tikzpicture}
\caption{After the third iteration of the {\bf while} loop the queue
is $Q = [b]$ and the distance labels are unchanged, since $c$ does
not have any neighbors. }
\end{subfigure}
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
\foreach \pos/\name in {{(4,1)/d},
{(4,-1)/e}, {(2,1)/a}, {(3,0)/b}, {(2,-1)/c}, {(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest in { s/a, s/c,
b/s, b/d,d/e,
b/e,e/c, a/b}
\path[edge] (\source) -- (\dest);
\end{tikzpicture}
\caption{{After the fourth iteration of the {\bf while} loop the queue
is $Q = [d,e]$ and the distance labels are $[0,1,2,1,3,3]$ respectively. }}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
\foreach \pos/\name in {{(4,1)/d},
{(4,-1)/e}, {(2,1)/a}, {(3,0)/b}, {(2,-1)/c}, {(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest in {b/s, d/e, e/c}
\path[edge] (\source) -- (\dest);
\foreach \source/ \dest in { s/a, s/c, b/d, b/e, a/b}
\path[selected edge] (\source) -- (\dest);
\end{tikzpicture}
\caption{After the fifth and sixth iteration of the {\bf while} loop
the queue is empty $Q = []$ and the distance labels remain unchanged.
The blue edges denote the shortest path tree.}
\end{subfigure}
\caption{An example-run of breadth-first search}\label{g:fig:4}
\end{figure}
\begin{theorem}
\label{f:thr:26}
The breath-first-search algorithm runs in time $O( |V| + |A|)$.
\end{theorem}
\begin{proof}
Each vertex is queued and dequeued at most once. These queuing
operations take constant time each. Thus queuing and dequeuing costs
$O( |V| )$ in total.
When a vertex $u$ is dequeued, its neighbors are
inspected and the operations in the {\bf if} statement cost constant
time each. Thus one has an additional cost of $O( |A | )$, since
these constant-time operations are carried out for each arc $a \in
A$.
\qed
\end{proof}
\section{Shortest Paths}
\label{sec:shortest-paths}
\begin{definition}[Cycle]
A walk in which starting node and end-node agree is called a
\emph{cycle}.
\end{definition}
Suppose we are given a directed graph $D=(V,A)$ and a length function
$c:~A\longrightarrow\setR$. The \emph{length} of a walk $W$ is defined as
\begin{displaymath}
c(W) = \sum_{\substack{a \in A\\ a\in W}} c(a).
\end{displaymath}
%
We now study how to determine a shortest path in a weighted directed
graph
$G$ efficiently, in case of the absence of cycles of negative length (such cycles are called \emph{negative cycles}).
\begin{theorem}
\label{f:thr:1}
Suppose that each cycle in $D$ has non-negative length and suppose
there exists an $s-t$-walk in $D$. Then there exists a path
connecting $s$ with $t$ which has minimum length among all walks
connecting $s$ and $t$.
\end{theorem}
\begin{proof}
If there exists an $s-t$-walk, then there exists an $s-t$-path. Since
the number of arcs in a path is at most $|V| - 1 $, there must exist a
shortest \emph{path} $P$ connecting $s$ and $t$. We claim that
$c(P)\leq c(W)$ for all $s-t$-walks $W$. Suppose that there exists
an $s-t$-walk $W$ with $c(W)<c(P)$. Then let $W$ be such a walk with
a minimum number of arcs. Clearly $W$ contains a cycle $C$. Since the
cycle has non-negative length, then it can be removed from $W$ to
obtain a walk whose length is at most $c(W)$ and whose number of
arcs is strictly less than $|W|$. This is a contradiction to the
minimality of the number of arcs in $W$.
\qed
\end{proof}
We use the notation $|W|,|C|,|P|$ to denote the number of arcs in a
walk $W$, a cycle $C$ or a path $P$.
As a conclusion we can note here:
\begin{quote}
If there do not exist negative cycles in $D$, and $s$ and $t$ are
connected, then there exists a shortest walk traversing at most
$|V| - 1$ arcs.
\end{quote}
\subsection*{The Bellman-Ford algorithm}
Let $n=|V|$. We calculate functions $f_0,f_1,\ldots,f_n:V\longrightarrow\setR\cup\{\infty\}$
successively by the following rule.
\begin{enumerate}[i)]
\item $f_0(s) = 0$, $f_0(v) = \infty$ for all $v \neq s$
\item For $k<n$ if $f_k$ has been found, compute
\begin{displaymath}
\displaystyle f_{k+1}(v) = \min\{f_k(v), \min_{(u,v)\in A}\{f_k(u)+c(u,v)\} \}
\end{displaymath}
for all $v \in V$.
\end{enumerate}
% %% Adjacency matrix of graph
% %% \ a b c d e f g
% %% a x 7 5
% %% b 7 x 8 9 7
% %% c 8 x 5
% %% d 5 9 x 15 6
% %% e 7 5 15 x 8 9
% %% f 6 8 x 11
% %% g 9 11 x
\tikzstyle{vertex}=[circle,fill=black!25,minimum size=15pt,inner sep=0pt]
\tikzstyle{selected vertex} = [vertex, fill=red!24]
\tikzstyle{edge} = [draw,thick,->]
\tikzstyle{weight} = [font=\small]
\tikzstyle{selected edge} = [draw,thick, ->,blue!50]
\tikzstyle{ignored edge} = [draw,line width=5pt,-,black!20]
\begin{figure}
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest/ \weight in { s/a/3, s/c/4,
b/c/-2, b/d/3 ,d/e/-3,
b/e/2,e/c/2, a/b/1}
\path[edge] (\source) -- node[weight] {$\weight$} (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{
The algorithm is initialized with distance labels for
$s,a,b,c,d,e$ being $[0,\infty,\infty,\infty,\infty,\infty]$ respectively
}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest/ \weight in {b/c/-2, b/d/3 ,d/e/-3,b/e/2,e/c/2, a/b/1}
\path[edge] (\source) -- node[weight] {$\weight$} (\dest);
\foreach \source/ \dest/ \weight in { s/a/3, s/c/4 }
\path[selected edge] (\source) -- node[weight] {$\weight$} (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{
After the first iteration the labels are $[0,3,\infty,4,\infty,\infty]$
}
\end{subfigure}
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest/ \weight in {b/c/-2, b/d/3 ,d/e/-3,b/e/2,e/c/2}
\path[edge] (\source) -- node[weight] {$\weight$} (\dest);
\foreach \source/ \dest/ \weight in { s/a/3, s/c/4, a/b/1 }
\path[selected edge] (\source) -- node[weight] {$\weight$} (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{
After the second iteration the labels are $[0,3,4,4,\infty,\infty]$
}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest/ \weight in {d/e/-3,e/c/2, s/c/4}
\path[edge] (\source) -- node[weight] {$\weight$} (\dest);
\foreach \source/ \dest/ \weight in {b/e/2,b/c/-2, b/d/3 , s/a/3, a/b/1 }
\path[selected edge] (\source) -- node[weight] {$\weight$} (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{
After the third iteration the labels are $[0,3,4,2,7,6]$
}
\end{subfigure}
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest/ \weight in { b/e/2, e/c/2, s/c/4}
\path[edge] (\source) -- node[weight] {$\weight$} (\dest);
\foreach \source/ \dest/ \weight in {d/e/-3,b/c/-2, b/d/3 , s/a/3, a/b/1 }
\path[selected edge] (\source) -- node[weight] {$\weight$} (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{
After the fourth iteration the labels are $[0,3,4,2,7,4]$
}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.45\textwidth}
\begin{tikzpicture}[scale=1, auto,swap]
% Draw a 7,11 network
% First we draw the vertices
\foreach \pos/\name in {{(2,1)/a}, {(4,1)/d},
{(3,0)/b}, {(2,-1)/c}, {(4,-1)/e}}
\node[vertex] (\name) at \pos {$\name$};
\foreach \pos/\name in {{(0,0)/s}}
\node[selected vertex] (\name) at \pos {$\name$};
% Connect vertices with edges and draw weights
\foreach \source/ \dest/ \weight in { b/e/2, e/c/2, s/c/4}
\path[edge] (\source) -- node[weight] {$\weight$} (\dest);
\foreach \source/ \dest/ \weight in {d/e/-3,b/c/-2, b/d/3 , s/a/3, a/b/1 }
\path[selected edge] (\source) -- node[weight] {$\weight$} (\dest);
% Start animating the vertex and edge selection.
% \foreach \vertex / \fr in {d/1,a/2,f/3,b/4,e/5,c/6,g/7}
% \path<\fr-> node[selected vertex] at (\vertex) {$\vertex$};
% For convenience we use a background layer to highlight edges
% This way we don't have to worry about the highlighting covering
% weight labels.
% \begin{pgfonlayer}{background}
% \pause
% \foreach \source / \dest in {d/a,d/f,a/b,b/e,e/c,e/g}
% \path<+->[selected edge] (\source.center) -- (\dest.center);
% \foreach \source / \dest / \fr in {d/b/4,d/e/5,e/f/5,b/c/6,f/g/7}
% \path<\fr->[ignored edge] (\source.center) -- (\dest.center);
% \end{pgfonlayer}
\end{tikzpicture}
\caption{
After the fifth iteration the labels are unchanged. The shortest
path distances have been computed.
}
\end{subfigure}
\caption{An example-run of the Bellman-Ford algorithm. The blue
edges represent the tree whose paths have the corresponding lengths.} \label{f:fig:5}
\end{figure}
The following theorem shows us that the Bellman-Ford algorithm is a
method to compute minimum length walks.
\begin{theorem}
\label{f:thr:1}
For each $k=0,\ldots,n$ and for each $v \in V$
\begin{displaymath}
f_k(v) = \min\{ c(P) \colon P \text{ is an } s-v\text{-walk traversing
at most } k \text{ arcs}\}.
\end{displaymath}
\end{theorem}
\begin{theorem}
\label{bobo:f:thr:1}
Given a directed graph $D=(V,A)$, $s \in V$ and a length
function $c:A \to \setR$, one has $f_n = f_{n-1}$ if and only
if $D$ does not have a cycle
of negative length that is reachable from $s$.
\end{theorem}
\begin{proof}
$(\Leftarrow)$ Suppose there exists $t \in V$ such that
$f_n(t) < f_{n-1}(t) < \infty$. This implies that the shortest
$s-t$-walk traversing at most $n$ arcs (call it $W$) traverses
exactly $n$ arcs and thus contains a cycle (call it $C$). Consider
the $s-t$-walk $W'$ obtained by eliminating $C$ from $W$.
$W'$ traverses at most $n-1$ arcs and thus we have $c(W') > c(W)$.
Since $c(W) = c(W') + c(C)$, we have that $c(C) < 0$.
$(\Rightarrow)$ Let $C = v_0,v_1,\cdots,v_k,v_0$ be a cycle
reachable from $s$. Notice that
$f_{n-1}(v_i)<\infty$ $\forall 0 \leq i \leq k$.
We can show that $C$ is a non-negative cycle by these simple
calculations (notice that the node indices $i$ and $i+1$ are
considered modulo $k$ in the following sums).
\begin{displaymath}
0 = \sum_{i = 0}^k f_n(v_{i+1}) - f_n(v_i) \leq
\sum_{i = 0}^k c(v_i,v_{i+1}) = c(C)
\end{displaymath}
\end{proof}
Theorem~\ref{bobo:f:thr:1} can be generalized for every $n \geq |V|$.
This allows us to obtain the following corollary.
\begin{corollary}
\label{co:7}
If $D = (V,A) $ does not contain negative cycles w.r.t. $c$, then
$f_n(v)$ is equal to the length of a shortest $s-v$-path. The
numbers $f_n(v)$ can be computed in time $O( |V| \cdot |A| )$.
\end{corollary}
Notice that by using theorem~\ref{bobo:f:thr:1}
we can see if a directed graph $D(V,A)$ has negative cycles in the
following way. We obtain a new graph $D'=(V',A')$ add a node $s$ to
$D$ and we connect $s$ to all other vertices with an outgoing arc
of weight $0$. Then, we apply Bellman-Ford algorithm to $D'$ with
starting node $s$. Notice that $D$ contains a negative cycle, if
and only if $D'$ contains a negative cycle (since every cycle in $D$
is reachable from $s$ in $D'$). Thus $D$ contains a
negative cycle if and only if there exists $t \in V$ such that
$f_n(t) < f_{n-1}(t)$ in $D'$.
This gives us the following corollary.
\begin{corollary}
\label{co:9}
In time $O(|V| \cdot |A| )$ one can test whether $D = (V,A)$ has a
negative cycle w.r.t. $c$ and eventually return one.
\end{corollary}
\begin{proof}
By our previous words we know that we simply have to apply
Bellman-Ford algorithm to the graph $D'$. The corollary follows
by the fact that $|V'| = |V|+1$ and $|A'| = |A| + |V|$ and by
using corollary~\ref{co:7}.
\end{proof}
\section{Maximum $s-t$-flows}
\label{sec:maximum-s-t}
We now turn our attention to a linear programming problem which we
will solve by direct methods, motivated by the nature of the
problem. We often use the following notation. If $f:A\longrightarrow B$ denotes a
function and if $U\subseteq A$, then $f(U)$ is defined as $f(U)= \sum_{a\in U}
f(a)$.
\begin{definition}[Network, $s-t$-flow]
A network with capacities consists of a directed simple graph
$D=(V,A)$ and a \emph{capacity function} $u:A\to\setR_{\geq0}$.
We also require that if there is an arc $uv \in A$, then there is
no reverse arc $vu$, and we disallow self-loops.
A function $f:A\to\setR_{\geq0}$ is called an \emph{$s-t$-flow}, if
\begin{equation}
\sum_{e \in \delta^{out}(v)} f(e) = \sum_{e \in \delta^{in}(v)} f(e), \, \mbox{ for all } v \in V - \{s,t\},
\end{equation}
where $s,t\in V$. These two vertices are called \emph{source} and
\emph{sink} respectively.
The flow is \emph{feasible}, if $f(e) \leq u(e)$ for all
$e \in A$.
The \emph{value} of $f$ is defined as
$value(f) = \sum_{e \in \delta^{out}(s)} f(e) - \sum_{e \in \delta^{in}(s)} f(e)$.
The \emph{maximum $s-t$-flow problem} is the problem of determining
a maximum feasible $s-t$-flow.
\end{definition}
Here, for $U \subseteq V$, $\delta^{in}(U)$ denotes the arcs which are entering $U$
and $\delta^{out}(U)$ denotes the arcs which are leaving $U$. Arc sets
of the form $\delta^{out}(U)$ are called a \emph{cut} of $D$. The
\emph{capacity of a cut } $u(\delta^{out}(U))$ is the sum of the capacities of
its arcs.
Thus the maximum flow problem is a linear program of the form
\begin{eqnarray}
\max \sum_{e \in \delta^{out}(s)} x(e) & - & \sum_{e \in \delta^{in}(s)} x(e) \\
\sum_{e \in \delta^{out}(v)} x(e) & = & \sum_{e \in \delta^{in}(v)} x(e), \, \mbox{ for all
} v \in V-\{s,t\} \\
x(e) & \leq & u (e) , \, \mbox{ for all
} e \in A \\
x(e) & \geq & 0 , \, \mbox{ for all
} e \in A
\end{eqnarray}
\begin{definition}[excess function]
For any $f:A \to \setR$, the excess function is the function
$excess_f: 2^V \to \setR$ defined by $excess_f(U) = \sum_{e \in \delta^{in}(U)}
f(e) - \sum_{e \in \delta^{out}(U)} f(e)$.
\end{definition}
\begin{theorem}
\label{f:thr:6}
Let $D = (V,A)$ be a digraph, let $f:A\to\setR$ and let $U\subseteq V$, then
\begin{equation}
\label{f:eq:28}
excess_f(U) = \sum_{v \in U} excess_f(v).
\end{equation}
\end{theorem}
\begin{proof}
An arc which has both endpoints in $U$ is counted twice with
different parities on the right, and thus cancels out. An arc which
has its tail in $U$ is subtracted once on the right and once on the
left. An arc which has its head in $U$ is added once on the right
and once on the left. \qed
\end{proof}
A cut $\delta^{out}(U)$ with $s \in U$ and $t \notin U$ is called an $s-t$-cut.
\begin{theorem}[Weak duality]