You are on page 1of 3

Single-Source Shortest Path problem

Input: a weighted connected graph G=(V,E), and a node s designated as a source node !he weights are represented b" the #$ arra" (matri%) &'(:n,(:n), where &'i,*) is the weight o+ edge (i,*) I+ (i,*) is not an edge, &'i,*)=in+init" ,ote: &'i i)=- +or all i Problem: .ind the distance between s and e/er" node in the graph !he greed" method here will ta0e the de+initions o+ some concept be+ore it can be +ormulated 1et 2 be a set, initiall" containg the single source node s $e+inition: 3 path +rom s to a node % outside 2 is called special i+ e/er" intemediar" node on the path belongs to 2 1et $IS!'(:n) be a real arra" where $IS!'i)=the length o+ the shortest special path +rom s to i

Greed" selection polic": choose +rom all the nodes still outside 2 the node o+ minimum $IS! /alue, and add it to 2 !he claim, which will pro/ed later, is that e/er" node in 2 has is $IS! /alue e4ual to the distance +rom it to s the SSSP algorithm:

Procedure SSSP(in W[1:n,1:n], s;out DIST[1:n]); begin for i =1 to n do DIST[i] := W[s,i]; endfor /* i !"e ent # is $oo"e%n %rr%& #[1:n] */ /* #[i]= 1 if i be"ong to set #, ' ot(er)ise */ $oo"e%n #[1:n]; /* initi%"i*ed to '*/ #[s] := 1; /* %dd s to set # */ for nu =+ to n,1 do c(oose % node u fro out of # suc( t(%t DIST[u] = in-DIST[i] . #[i] = '/; #[u] := 1; /* 0dd u to #;*/ /*u!d%te t(e DIST 1%"ues of t(e ot(er nodes*/ for %"" node ) )(ere #[)] = ' do DIST[)]= in(DIST[)],DIST[u]2W[u,)]); endfor endfor

end

!ime 5omple%it" o+ the SSSP algorithm: o the (st +or-loop clearl" ta0es 6(n) time
o

5hoosing u ta0es 6(n) time, because it in/ol/es +inding a minimum in an arra" !he innermost +or-loop +or updating $IS! has a contant-time bod", and iterates at most n times, thus ta0es 6(n) time !here+ore, the +or-loop iterating o/er num ta0es 6(n7n) time !hus, SSSp ta0es 6(n8#) time

o o

!heorem: &hen a node u enters 2, we ha/e $IS!'u) = distance(s,u)

Proo+:
o o

!he proo+ is b" induction on the number 0 o+ elements in 2 9asis: :=( !hat is, 2 has onl" node s &ell $IS!'s)=&'s,s)=-; also, distance(s,s)=- !hus, $IS!'s)=distance(s,s) Induction: assume the theorem holds +or e/er" node / that had entered 2 be+ore u Pro/e that the theorem holds +or u which is selected b" the algorithm to be the ne%t node to enter 2 &e do so b" contradiction

3ssume that $IS!'u) <= distance(s,u) !hat is, $IS!'u) = distance(s,u) !his means the shortest path +rom s to u (call that path P) is not a special path !his implies that at some point, P e%its 2 going through some intermediar" node(s) be+ore reaching u 1et > be the +irst node that P goes through right when P e%its 2 !hen, the portion o+ P +rom s to >, which we?ll call @, is a special path +rom s to > &e now ha/e $IS!'>) A= length(@) A= length(P) = distance(s,u) A $IS!'u) !hat is, $IS!'>) A $IS!'u)

!his contradicts the +act that the algorithm choose the min-$IS! node u to enter 2 !here+ore, $IS!'u) = distance(s,u)

You might also like