Discrete Wiskunde II, 2009 Lecture 5: Shortest Paths ...uetzm/dw/DWII_Lecture5.pdfShortest Path...
Transcript of Discrete Wiskunde II, 2009 Lecture 5: Shortest Paths ...uetzm/dw/DWII_Lecture5.pdfShortest Path...
Discrete Wiskunde II, 2009Lecture 5: Shortest Paths & Spanning Trees
Marc UetzUniversity of Twente
wwwhome.math.utwente.nl/~uetzm/dw/
Marc Uetz Discrete Wiskunde II
Shortest Path Problem
Given
directed graph (digraph) G = (V ,E ), |V | = n and |E | = m,arc lengths c : E → R, two vertices s, t ∈ V
want: shortest path from s to t (from s to all nodes v ∈ V )
!"#$%&'%()*%"()$#+,&-
! !"#$%&'()*+,%+('-*.#/'01234564'.*,'7+"-%/8',&'5!"4'84%#3
! 9$%#$%&
! :/;*%+8%'#.%/'<84% =*;>'8'%;'%?
! :/;*%+8%'#.%/8'<84@ ."('()8%.",+8'(2@6'=*;>'8'%;'.77';%/+*'@
! "
#$
%&'(&)
! "
#$
%&'(&)
!
!
)
$
$
##
)
"
Assumption: all vertices are reachable from s
Marc Uetz Discrete Wiskunde II
Three main cases
Arbitrary digraphs
includes negative cycle detection
Label correcting algorithms
O( nm ) computation time
Nonnegative edge lengths (cvw ≥ 0 for all (v ,w) ∈ E )
Dijkstra’s Algorithm
O( n2 ) computation time
Acyclic graphs (6 ∃ directed cycle)
Dynamic programming
O( n + m ) computation time
Marc Uetz Discrete Wiskunde II
Shortest Path Optimality
Let d(v), v ∈ V be arbitrary node labels such thatd(v) ≥ shortest path length from s to v , v ∈ V
Theorem
Labels d(v) are shortest (s, v)-path lengths if and only if the4-inequality holds for all edges (v ,w) ∈ E
d(w) ≤ d(v) + cvw
!"#$%&'%()*%"'(+,%-.*/-%0(1#23-%-#2
! !"#$%&'(!"#$%&'%(%$)(*+,#-'. )*(&!"%(+#$
! /+-(0123(12#43(5+(.67+(%$5"-$%$8(,60+(*%5+*.%$5"-$%$8(,60+(*%5+*. .9)'(-'%-(0123(".(%,(9&&+$(9&&+$(569,0569,0 :6$(-'+(.'6$-+.-(&%-'(:$67(.(-6(2(12#4;.3<
! ='+(,60+(*%5+*.(0123(%$+(.'6$-+.-(&%-'(*+,#-'.(":(%,0(6,*8(":":(%,0(6,*8(":
01>3(! 01,3 ? ),%- :6$(%**(12@>3#A
! "
#$
%&'(&)
!
!
)
$
$
#
"
)
#
!
*
!
"
"
+ 2
>)2>
Marc Uetz Discrete Wiskunde II
Proof
Necessity
trivial, for otherwise there is a shortcut to w via v
Sufficiency
Let `(w) be the (true) shortest path length to w , `(w) ≤ d(w).pick shortest (s,w)-path P = (s, . . . u, v ,w), length `(w)use 4-inequality along this path
d(w) ≤ d(v) + cvw
≤ d(u) + cuv + cvw
...
≤ d(s) + length(P) = length(P)
= `(w)
Marc Uetz Discrete Wiskunde II
Dijkstra’s Algorithm (1959)
all arc lengths cvw are nonnegative
Idea
Start with distance labels d(v) ≥ shortest path lengths, thencorrect violated 4-inequalities in a clever way
Algorithm
d(s) = 0, d(v) =∞ and pred(v) = ∅ ∀ v ∈ V \ s, S = ∅while (S 6= V )
pick v ∈ S̄ := V \ S with smallest label d(v)
S = S ∪ v /*d(v) will remain unchanged!*/
For all w with (v ,w) ∈ E , if (d(w) > d(v) + cvw )
d(w) = d(v) + cvw
pred(w) = v
pred(v) = predecessor of v on a shortest (s, v)-path
Marc Uetz Discrete Wiskunde II
Example: Dijkstra !"#$%&'(
2 5
4 3
t=6 s=1
2
1
1
5
3
5 4
1
5
2 5
4 3
t=6 s=1
2
1
1
5
3
5 4
1
5
2
1 5
6
9 0
note: set S already has the correct distance labels!
Marc Uetz Discrete Wiskunde II
Correctness Dijkstra I
We prove by induction on # iterations
Claim 1: d(u) ≤ d(w) for all u ∈ S , w 6∈ S
Iteration 1: S = s, d(s) = 0, other d(w) ≥ 0√
Iteration where S = S ∪ v :First argue about v : d(v) ≤ d(w) ∀w 6∈ S , by our choice of v
Next, argue about any other u ∈ S :If d(w), w 6∈ S , was not updated, d(u) ≤ d(w) for all u ∈ S byinduction hypothesis
If d(w), w 6∈ S , has been updated,d(w) = d(v) + cvw ≥ d(v), as cvw ≥ 0and d(u) ≤ d(v) ∀u ∈ S by induction hypothesisso d(u) ≤ d(w) ∀ u ∈ S also after this update
√
Marc Uetz Discrete Wiskunde II
Correctness Dijkstra II
We prove by induction on # iterations
Claim 2: labels within S are the correct shortest path lengths
Iteration 1: S = s, d(s) = 0√
Iteration where S = S ∪ v :By induction hypothesis, only need to check that d(v) is correct.Theorem ⇒ only need to verify 4-inequality for edges into v
Case 1: consider any (u, v), u ∈ S :From iteration S = S ∪ u (which was before), d(v) ≤ d(u) + cuv
Case 2: consider any (w , v), w 6∈ S :In iteration S = S ∪ w (which is later), our claim 1 yieldsd(v) ≤ d(w) ≤ d(w) + cwu (d(v) remains unchanged)
√
Upon termination, S = V , so Dijkstra works correctly
Marc Uetz Discrete Wiskunde II
Computation Time
Simple
Initialization O( n )
n iterations of while-loop, in each need to find smallest labelin V \ S , which is doable in O( n ) O( n2 )
in total, at most m updates of d(v), O( 1 ) each O( m )
Which gives O( n ) + n · O( n ) + O( m ) ∈ O( n2 ) time
Less Simple
An O( m log n ) implementation, which uses priority queue (alsocalled heap) to manage finding minimal d(v) in O( 1 ) time.(log n comes from overhead in organization of the heap)
Marc Uetz Discrete Wiskunde II
BFS - breadth-first-search
Let all arc lengths cvw = 1. What about Dijkstra’s algorithm?
distance labels d(v) = minimal # arcs to reach v from s(shortest path tree = BFS tree)
d(v) =∞ for non-reachable v (“reachability algorithm”)
Computation time?
once assigned a finite value, no node needs relabeling(otherwise contradiction to choice of v = argmin{d(v)})hence organize such nodes as a FIFO list Q (queue):when d(w) is assigned a (finite) value, w → end of Q
then minimal d(v) is found in O( 1 ) time, at top of Q
Total time O( n + m )
Marc Uetz Discrete Wiskunde II
Minimum Spanning Trees (MST)
MST
Given an edge-weighted, connected, undirected graphG = (V ,E , c), with |V | = n and |E | = m, find a MST.(cheapest connected acyclic subgraph)
!"#"$%$&'()##"#* +,-- .,/01-$
!"#$%&'()*+& !",#$%-.&&&'! % /0(12&)&()*++,+-'./00()*++,+-'./00 "/&,#$&-&"2&)&3($$
,"4$4.&50%%$53$6&)%6&)5758"5-
9%*:3;&
! !()*+&!'"',#$% -
! $6'$&50232 10$'0" %
<:3*:3;
! & ! % 2:5+&3+)3&&,#$&'-&"2&) 3($$& ="3+&1"%"1)8&303)8&5023&
1,&- "'!$ " > 1,0-4
! "
#$
%$&!%
#%
"&
$%
!&
$&
! "
#$
%$&!%
#%
"&
$%
!&
$&
Marc Uetz Discrete Wiskunde II
Spanning Trees and Cuts
Definitions
For subset of nodes W ⊆ V , δ(W ) denotes the cut inducedby W : all edges “leaving” W [δ(W ) = δ(V \W )]
Given a spanning tree T of G , let C (e) be the cut induced bydeleting edge e from T
!"#$$%$&'()**+'#$,'-./+
!"#$%%&'()'$'*+"" *,"+" -. $'/)-0/" 1$*, 2"*3"") $)4 *35 )56". ! $)6'"
!"#$%%&'()'$'*+"" #7'#$ % ,$. *35'#5815)")*.'9"$#,'%! & -.'$'2+-6:";
<"=-)-*-5).&
! >5+'$)4 ./2."* 5=')56".'" ()*!9';" & -.'#$%%"6'*," +,-+,- -)6/#"6 24
'
98"$)-): $%%'"6:". -)'?'*,$* @%"$A" BC7'-D"D7'#5))"#*' $)6'(!';
! ?-A") $'.1$))-): *+""'5='?7'3" /." *,"')5*$*-5) .9%;
=5+ *," #/* -)'/ *,$* -. -)6/#"6 24 6"%"*-): "6:"'%*-) #
!
"
#
"$
!%
#$
&
'
(
"#
#!
#)
"%
!'
#'
!
"
#
"$
!%
#$
&
'
(
"$
#!
#)
"%
!'
#'
Marc Uetz Discrete Wiskunde II
The Cut Condition
Theorem
Given a graph G = (V ,E ) with edge costs ce , e ∈ E , then aspanning tree T ⊆ E is an MST if and only if
ce ≤ cf for all edges e ∈ T and all edges f ∈ C (e) .
(i.e., T has the cheapest connection that crosses the cut)
!"#$%&'$#()*+,#*-./0$#$./
!"#$%#&'()*+#, -(.%-/" !"0#$%1 2*3" #4.#(5$636 &' $('! %$
3"#, -(6/-,,*,. 3%## !" 7(*6(-,(89! *: -,4($,;< *:
# #4.#6 #! !(-,4(# #4.#6 :! =0#1'((((50#1(! 50:1
!
"
#
"$
!%
#$
&
'
(
"#
#!
#)
"%
!'
#'
Marc Uetz Discrete Wiskunde II
The Path Condition
In a tree T , denote by PT (v ,w) the (unique) path from v to w
Theorem
Given a graph G = (V ,E ) with edge costs ce , e ∈ E , then aspanning tree T ⊆ E is an MST if and only if
∀ edges f = {v ,w} ∈ E \ T : cf ≥ ce ∀ edges e ∈ PT (v ,w)
!"#$%&'$#()*+&#,*-./0$#$./
!"#$%#&'()*+#, -(.%-/" !"0#$%1 2*3" #4.#(5$636 &'$('! %$
3"#, -(6/-,,*,. 3%## !" 7(*6(-(89! *: -,4($,;< *:
# #4.#6 :=>?@+A! 7!! -,4(# #4.#6 #! B!0?@+1'((((50#1(" 50:1
C#:*,# *,(-(3%## !@((B!0?@+1(-6(3"# 0?,*D?#1(/-3" :%$& ?(3$(+E
!
"
"#
!$
%#
&
'
"%
%!
%(
"$
%'
!'% )
Marc Uetz Discrete Wiskunde II
Kruskal’s Greedy Algorithm (1957)!"##$%&'()*"+,-. / 01"2345(6&/789:
!"#$%&$'#%&%()&)*+)$ $,-(&%(.%
-)/01 !-)/21 ! 3&3&3&3&3&! -)/41
56!7
8'# /"60&%'&41&9
"8 /! " "#$%&"$&.-:-;"-19
5# 5" )/"17
<
<
#)%,#= 57
>=?.#".=%@
03 /AB51 .-:-;"-
23 5&$.%"$8")$&%()&C.%(&
-'=*"%"'=
D'&"8 %() .;+'#"%(4 #)%,#=$
.&-'==)-%)* +#.C(B&"% "$
=)-)$$.#";: .=&ED53
!"#$%&'%#(()%*'+,-&"&.'/%&.+(%'0%12-#3"45#%"46'2.&7+%.# 89/+:;
<'##.=4(%>.&7%=(&&(2%?"&"%#&2-@&-2(#)%89+%4'6%+:;
Theorem
Kruskals algorithm solves MST problem in time O( m log m + n2 ).
Marc Uetz Discrete Wiskunde II
Kruskal’s Algorithm: Correctness
Proof 2nd Invariant (Path Condition)
any edge f = {v ,w} not added to T creates cycle, namely{v ,w} ∪ PT (v ,w),in particular, all edges e ∈ PT (v ,w) are already in T
by the sorting of the edges ⇒ cf ≥ ce ∀e ∈ PT (v ,w)
T connected
for any cut δ(W ) of the graph (V ,E ), the cheapest (so atleast one!) edge e ∈ δ(W ) must be in T
thus (V ,T ) must be connected(connected :⇔ graph contains at least one edge from any cutδ(W ), W ⊆ V )
Marc Uetz Discrete Wiskunde II
Kruskal’s Algorithm: Computation Time
To start with
O( m log m ) for sorting the ce values (MergeSort)
need to do m times: Is T ∪ e acyclic?, and if so, add e to T
We need a clever data structure!
Store & update to which component any node belongs:
Initialize c(v) = v ∀ v ∈ V [n components] O( n )
m times we do for an edge {v ,w}:Check (T ∪ {v ,w} acyclic) ⇔ c(v) 6= c(w) O( 1 )
If so, add {v ,w} to T [merge 2 components, n − 1 times]:For all u ∈ V : If c(u) == c(w) let c(u) = c(v) O( n )
O( m log m ) + O( n ) + mO( 1 ) + (n− 1)O( n ) ∈ O( m log m + n2 )
Marc Uetz Discrete Wiskunde II
More about MSTs
With a little more care (data structure & analysis), Kruskal’salgorithm can be implemented to run in O( m log m ) time
Prim’s algorithm (1959)
Start with W = {v0}, any v0 ∈ V , T = ∅For (i = 1, . . . , n − 1)
Let edge ei = {v ,w} be the cheapest edge in δ(W )(where v ∈W and w 6∈W )Let T = T ∪ ei and W = W ∪ {w}
Correctness follows directly from Cut Condition
Computation time can be shown in O( n2 )
Marc Uetz Discrete Wiskunde II