0% found this document useful (0 votes)
59 views

Lecture 12 - Graphs P2 PDF

The document summarizes algorithms for finding shortest paths in graphs, including Dijkstra's algorithm. It begins with an overview of pathfinding on graphs and the shortest path problem. It then provides details on Dijkstra's single-source shortest path algorithm, including how it uses distance labels and edge relaxation to iteratively build up the shortest path distances from the source vertex. Pseudocode for Dijkstra's algorithm is presented. An example run of Dijkstra's algorithm on a sample weighted graph is shown step-by-step to illustrate the technique.

Uploaded by

minhthang_hanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Lecture 12 - Graphs P2 PDF

The document summarizes algorithms for finding shortest paths in graphs, including Dijkstra's algorithm. It begins with an overview of pathfinding on graphs and the shortest path problem. It then provides details on Dijkstra's single-source shortest path algorithm, including how it uses distance labels and edge relaxation to iteratively build up the shortest path distances from the source vertex. Pseudocode for Dijkstra's algorithm is presented. An example run of Dijkstra's algorithm on a sample weighted graph is shown step-by-step to illustrate the technique.

Uploaded by

minhthang_hanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

DATA STRUCTURE AND

ALGORITHMS FIT324
Summer 2014
Lecture #12
Nguyen Xuan Thang
[email protected]
1
Pathf!"!#
! a #ra$h
2
Content
Pathfinding on a graph
Shortest path proble
!i"#stra algorith
$ellan%&or algorith
&loyd%'arshall algorith
3
Pathf!"!#%! a #ra$h
Pathfinding problem
(n a graph )*+,-./- find a path fro vertex s to
vertex t.
&ind a path
&ind all possible paths
)raph traversal algoriths can be used
BFS
DFS
4
v
1
(s)
v
2
v
4
v
3
v
5
(t)
Pathf!"!#u&!# '(S )1*
5
Algorithm BFS(V,E,s,t): Boolean
Input: A graph with V & E are the set of vertices and
edges, s is the source vertex, t is the destination vertex.
Output: A path from s to t if exist, otherwise return false.
For each v in V do
Color[v]Black
Path[v] -1
Color[ s]Yellow
Q new empty queue
Q.Enqueue(s)

Code
Pathf!"!#u&!# '(S )2*
6
while Q is not empty do
u Q.Dequeue()
If u equals to t then
showPath(t)
return true
Color[u] Red
For each v adjacent to u do
If Color[v] is Black then
Color[v] Yellow
Q.Enqueue(v)
path[v] s
endwhile
return false
Code
Pathf!"!#u&!# '(S )3*
&ind path fro 0 to !
Start at 0. Put 0 to the 1ueue +ar#ed 2ello3/
4ueue5 0
7
F
A
B
G C
E
D
0
0
0 0
0
0
0
Pathf!"!#u&!# '(S )4*
Ta#e 0 fro 4ueue- visit 0 +ar#ed 6ed/- 0#!
&ound t3o vertices $ and .- that are ad"acent to 0
Put $ 7 . to 1ueue- update path8$9 7 path8.9
4ueue5 0 $ .
8
A
B
G
C
E
D
F
0
A
0 0
A
0
0
Pathf!"!#u&!# '(S )+*
Ta#e $ fro 4ueue- visit $ +$#!/- put ) and : to
1ueue
;pdate path8)9 and path8:9
4ueue5 0 $ . ) :
9
A
B
G
C
E
D
F
0
A
B B
A
0
0
Pathf!"!#u&!# '(S ),*
Ta#e . fro 1ueue- visit . +.#!/. Put !7& to the
1ueue
;pdate path8!9 7 path8&9
4ueue5 0 $ . ) : ! &
10
A
B
G
C
E
D
F
0
A
B B
A
E
E
Pathf!"!#u&!# '(S )-*
Ta#e ) fro the 1ueue- visit )- nothing putted to the
1ueue.
Ta#e : fro the 1ueue- visit :. & is an ad"acent node to
:- but & is ar#ed as 2ello3 already.
'e 3on<t put & to the 1ueue.
4ueue5 0 $ . ) : ! &
11
A
B
G
C
E
D
F
0
A
B B
A
E
E
Pathf!"!#u&!# '(S ).*
Ta#e ! fro the 1ueue- visit !. ! is the destination
vertex. Sho3 path and return true.
4ueue5 0 $ . ) : ! &
12
A
B
G
C
E
D
F
0
A
B B
A
E
E
Pathf!"!#u&!# '(S )/*
13
Algorithm showPath(t)
Input: Destination vertex t.
Output: print the path from s to t if exist.
u t
while u !=0 do
Print u
u path[u]
Code
(!" m0 1%ffee #ame )1*
Introduction
Start fro a position in a ap
&ind the cup of coffee
14
(!" m0 1%ffee #ame )2*
Introduction
$&S is a solution
15
(!" m0 1%ffee #ame )3*
Data representation
The ap
;sing a atrix =xN += ro3s- N coluns/
16
0 1 2 3 4
0
G X X G G
1
Y G X G C
2
G G G G G
(!" m0 1%ffee #ame )4*
Data representation
The graph
.ach cell in the ap is a node of the graph. Nodes
are indexed fro > to =?N @ A.
'ith node v- the corresponding ro3 and colun are5
ro3 * v B NC colun*v D NC
17
0 1 2 3 4
0
0 1 2 3 4
1
5 6 7 8 9
2
10 11 12 13 14
(!" m0 1%ffee #ame )+*
Data representation
.ach node has a list of ad"acent nodes.
Node >5
EFG
Node F5
E>- H- A>G
Node I5
EJ- K- AKG
18
0 1 2 3 4
0
G X X G G
1
Y G X G C
2
G G G G G
0 1 2 3 4
0
0 1 2 3 4
1
5 6 7 8 9
2
10 11 12 13 14
(!" m0 1%ffee #ame ),*
erte! "D#
19
$erte!
% inde! & int ''Position in the (ertices list
% ro)* column & int ''+hich cell in the map
% mar,ed & boolean ''-sed in BFS
% num.f"d/erte! & int
% ad/erte! & int0 1 ''2ist of ad/acent (ertices
LLLLLLLLLLLLLLLLLLLLL
3 $erte!4inde!* map* m* n5 & (oid
3 getInde!45 & int
3 get6o)45 & int
3 getColumn45 & int
3 777
(!" m0 1%ffee #ame )-*
.ther (ariables
char8989 ap +The ap/
),ertex 89 list,ertex +0rray contains all =?N vertices/
),ertex start,ertex- end,ertex
2our position and position of the coffee cup.
0rray4ueue 1
,ertex 1ueue used in $&S +each 1ueue<s ite is a
vertex/
),ertex 89 path
Path inforation
20
(!" m0 1%ffee #ame ).*
Implementation
See 3ee#A2LtutorialLinstruction for the detail.
21
0 1 2 3 4 8 9 :
0 $ $ $ $ $ ; $ $
1 $ $ $ $ $ ; $ $
2 $ $ $ $ $ ; $ C
3 $ $ ; $ $ $ $ $
4 < $ ; $ $ $ $ $
(4,0) (3,0) (2,0) (2,1) (2,2) (2,3) (2,4)
(3,4) (3,5) (3,6) (2,6) (2,7)
Sh%rte&t $ath
22
Re1a22 a 3e#hte" #ra$h )1*
(n a 3eighted graph- each
edge has an associated
nuerical value- called
the 3eight of the edge.
.dge 3eights ay represent
distances- costs- etc.
.xaple5
(n a train route graph-
the 3eight of an edge
represents the distance
bet3een the endpoint
cities
23
Re1a22 a 3e#hte" #ra$h )2*
24
S#- F6" ="> C.P B?6 2?I
S#- 0 210 4@0
F6" 210 0 380 848 3A8
="> 380 0 4:8 2A0
C.P 4:8 0 438
B?6 848 2A0 438 0 1A0
2?I 4@0 3A8 1A0 0
w[i][i]=0
w[i][j]=w[j][i] if G is un-direced
w[i][j]= if !ere is n" ed#e
c"nnec $ere% i " $ere% j
Sh%rte&t $ath $r%42em )1*
Definition
(n a graph $B4*?5- consider to a path from s to t.
P
,
BC
1

2

3
D
,
E.
Let +4P
,
5 is the )eight of the path P
#
3e have5
(f ) is an un%)eighted graph- then5
+4P
,
5*The nuber of vertices in P
#
*,
(f ) is a )eighted graph- then5
w P
k
= w(I

, I
+1
)
k-1
=1
&ind a P
#
so that +4P
,
5 is minimum.
25
Sh%rte&t $ath $r%42em )2*
Shortest path properties
0 sub%path of a shortest path is itself a shortest
path.
&or exaple5
(f P
sp
*E,
A
,
M
,
H
,
K
,
F
,
N
G is the shortest
path fro ,
A
to ,
N
- then the path E,
M
,
H
,
K
G is
the shortest path fro ,
M
to ,
K
.
There is a tree of shortest path fro a vertex to all
other vertices that connect to this vertex.
26
Sh%rte&t $ath $r%42em )3*
Shortest path in an un%3eighted graph
BFS is a solution
Shortest path in a 3eighted graph
.very edge has positi(e )eight
.dge ay have negati(e )eight
)raph ay have negati(e )eight cFcle
27
A B s d
-3
1
8
W(s, d)=6
A B s d
-3
1
8
W(s, d)= -
1
Sh%rte&t $ath $r%42em )4*
Shortest path algorith types5
Single%pair shortest path +SPSP/
&ind shortest paths fro a given vertex to a given
vertex.
Single%source shortest path 4SSSP5
&ind shortest paths fro a given vertex to all other
vertices.
"ll%pairs shortest path 4"PSP5
&ind shortest paths for every pair of vertices.
28
D56&tra a2#%rthm )1*
0 SSSP algorithm
&ind shortest paths fro (erte! s to all other
vertices.
"ssumption
The graph is connected.
The edge )eights are non%negati(e.
!efinition of the distance fro ( to s
!8v9 is the total 3eight of the shortest path fro s to
v.
'ith a given vertex s- !i"#stra algorith 3ill
copute the !8v9 for all v in ,.
29
D56&tra a2#%rthm )2*
Di/,stra algorithm idea
'e gro3 a GcloudH of (ertices- beginning 3ith s
and eventually covering all the vertices.
'e store 3ith each vertex ( a label d4(5
representing the distance of ( from s in the sub%
graph consisting of the cloud and its ad"acent
vertices
0t each step5
'e add to the cloud the vertex u outside the cloud
3ith the sallest distance label- d+u/
'e update the labels of the vertices ad"acent to u
30
D56&tra a2#%rthm )3*
-pdate 4?dge rela!ation5
:onsider an edge
e B +u*I/ such that5
u is the vertex ost
recently added to the cloud
I is not in the cloud
The relaxation of edge e
updates distance d+I/
as follo3s5
d+I/ inEd+I/*d+u/ O )eight+u*I/G
31
d(z) = = = = 75
d(u) = = = = 50
z
s
u
d(z) = = = = 60
d(u) = = = = 50
z
s
u
e
e
D56&tra a2#%rthm )4*
?!ample
32
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
0
8 2 4

C B
A
E
D
F
4
8
7
2 5
2
3 9
1
0
8 2 3
5
11
D56&tra a2#%rthm )+*
?!ample
33
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
0
7 2 3
5
8
0
7 2 3
5
8
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
0
8 2 3
5
8
C B
A
E
D
F
4
8
7
2 5
2
3 9
1
0
7 2 3
5
8
D56&tra a2#%rthm ),*
Di/,stra algorithm
34
Algorithm Dijkstra(V,E,s):
Input: A graph G=(V,E), w is the weighted matrix, s is the
source vertex.
Output: The shortest paths from s to other vertices.
For each v in V do
D[v]
path[v] -1
D[s] 0
Create empty S set //The cloud set
Do
Find vertex u in V \ S so that D[u] is minimum
Move u from V to S
For each z in V \ S do
D[z] min{D[z], D[u]+w[u][z]}
path[z] u //Only update if D[u]+w[u][z]<D[z]
While could not find any u
Code
D56&tra a2#%rthm )-*
Di/,stra algorithm demonstration
35
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6







0
distance D
& = ' (
) = ' s* 2* 3* 4* 5* 6* 7* 8(
+ = '0* * , , , , , (
D56&tra a2#%rthm ).*
Di/,stra algorithm demonstration
36
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6

15
9



14
0
& = 's(
) = ' -* 2* 3* 4* 5* 6* 7* 8(
+ = '0* 9* , , , 14, 15, (



D56&tra a2#%rthm )/*
Di/,stra algorithm demonstration
37
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6

15
9
33


14
0
& = 's* 2(
) = ' -* -* 3* 4* 5* 6* 7* 8(
+ = '0* 9* 33, , , 14, 15, (



D56&tra a2#%rthm )10*
Di/,stra algorithm demonstration
38
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6

15
9
32

44
14
0
& = 's* 2* 6(
) = ' -* -* 3* 4* 5* -* 7* 8(
+ = '0* 9* 32, , 44, 14, 15, , (


33



D56&tra a2#%rthm )11*
Di/,stra algorithm demonstration
39
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6

15
9
32
59
35
14
0
& = 's* 2* 6* 7(
) = ' -* -* 3* 4* 5* -* -* 8(
+ = '0* 9* 32, , 35, 14, 15, 59(


33



44

D56&tra a2#%rthm )12*
Di/,stra algorithm demonstration
40
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6

15
9
32
51
34
14
0
& = 's* 2* 6* 7* 3(
) = ' -* -* -* 4* 5* -* -* 8(
+ = '0* 9* 32, , 34, 14, 15, 51(


33



44

35
59
D56&tra a2#%rthm )13*
Di/,stra algorithm demonstration
41
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
45
15
9
32
50
34
14
0
& = 's* 2* 6* 7* 3* 5(
) = ' -* -* -* 4* -* -* -* 8(
+ = '0* 9* 32, 45, 34, 14, 15, 50(


33



44

35
59

51
D56&tra a2#%rthm )14*
Di/,stra algorithm demonstration
42
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
45
15
9
32
50
34
14
0
& = 's* 2* 6* 7* 3* 5* 4(
) = ' -* -* -* -* -* -* -* 8(
+ = '0* 9* 32, 45, 34, 14, 15, 50(


33



44

35
59

51
D56&tra a2#%rthm )1+*
Di/,stra algorithm demonstration
43
s
3
8
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
45
15
9
32
50
34
14
0
& = 's* 2* 6* 7* 3* 5* 4* 8(
) = ' -* -* -* -* -* -* -* -(
+ = '0* 9* 32, 45, 34, 14, 15, 50(


33



44

35
59

51
D56&tra a2#%rthm )1,*
Di/,stra algorithm analFsis
Textboo# page 3:A- suppleentary textboo# page
98@.
The tie coplexity of !i"#stra algorith is5
.4J?J 3 JJ
2
5 bF using arraF to store and D0(17
.4J?Jlog 3 JJlog5 bF using a prioritF Kueue to store
and D0(17
!i"#stra algorith doesn<t 3or# 3ith negative%
3eighted edge.
)raph can be directed or un%directed.
44
'e22ma!7(%r" a2#%rthm )1*
0 SSSP algorithm
&ind shortest paths fro (erte! s to all other
vertices.
'or#s even 3ith negati(e%)eight edges.
:an detect the existence of negati(e%)eight cFcle
reachable fro s.
"ssumption
The graph is connected.
The edges are directed.
45
'e22ma!7(%r" a2#%rthm )2*
!efinition of D0i10(15
!8i98v9 is the total 3eight of the shortest path that
use i or fe)er edges- fro s to (.
6ecall that the distance fro vertex u to vertex v is
the total 3eight of the shortest path fro u to v.
Then !8i98v9 is the distance fro s to ( using i or
fe)er edges.
!8i98s9*>
!8i98v9*if you can<t get to v 3ithin i edges.
$ellan%&ord algorith computes the D0i10(1 for
anF ( in and iB1 77 JJ.
46
'e22ma!7(%r" a2#%rthm )3*
"lgorithm idea
To copute !8i98v9- 3e calculate i
th
ro) from i%1
th
ro).
:onsider a vertex u so that there is an edge fro u
to v.
'e #no3 the shortest path fro s to u using i%A or
fe3er edges- !8i%A98u9.
Then5
D | u = mtn
w u |u]=
{D | 1 u +w|u]|u]]
47
'e22ma!7(%r" a2#%rthm )4*
?!ample5
48
4
S
3
1
5
2
30
-20
10
50
30
15
30
& 1 2 3 4 5
0 0
1 0 50 15
2 0 50 80 15 45
3 0 25 80 15 45 75
4 0 25 55 15 45 75
5 0 25 55 15 45 65
D | u = mtn
w u |u]=
{D | 1 u + w|u]|u]]
'e22ma!7(%r" a2#%rthm )+*
Bellman%Ford algorithm
49
Algorithm BellmanFord(V,E,s): boolean
Input: A graph G=(V,E), w is the weighted matrix, s is the
source vertex.
Output: The shortest paths from s to other vertices or
false if G has a negative-weighted cycle.
For i 0 to |V| do
For each v in V do
D[i][v]
path[v] -1
D[i][s] 0
For i 1 to |V| do
For each v in V do
For each e=(u,v) in E do
D[i][v] Min{D[i-1][u]+w[u][v]}
path[v] u
min
// D[i][v]=D[i-1][u
min
]+w[u
min
][v]
Code
'e22ma!7(%r" a2#%rthm ),*
"lgorithm analFsis
(f the graph contains no negative%3eight cycles
reachable fro the source vertex s- after JJ % 1
iterations all distance estiates represent shortest
paths.
(f the graph contains a negative%3eight cycles
reachable fro s.
50
4
S
3
1
5
2
30
-100
10
50
30
15
30
'e22ma!7(%r" a2#%rthm )-*
"lgorithm analFsis
51
4
S
3
1
5
2
30
-100
10
50
30
15
30
& 1 2 3 4 5
0 0
1 0 50 15
2 0 50 80 15 45
3 0 -55 80 15 45 75
4 -5 -55 -25 15 45 75
5 -5 -55 -25 10 45 -15
'e22ma!7(%r" a2#%rthm ).*
Bellman%Ford algorithm
52
Algorithm BellmanFord(V,E,s):
Input: A graph G=(V,E), w is the weighted matrix, s is the
source vertex.
Output: The shortest paths from s to other vertices or
false if G has a negative-weighted cycle.

For i 1 to |V| do
For each v in V do
For each e=(u,v) in E do
If D[i][v] > D[i][u] + w[u][v] then
return false //There is a negative-weighted cycle
return true
Code
'e22ma!7(%r" a2#%rthm )/*
"lgorithm analFsis
Tie coplexity
.4JJ7J?J5
53
(2%0"78ar&ha22&a2#%rthm )1*
0 "PSP 4all%pairs shortest path5 algorithm
&ind shortest paths fro e(erF pair of (ertices u
i
* u
/
+i-"*A..P,P and i # "/
"ssumption
The graph is connected.
The edges are non%negati(e )eighted.
54
(2%0"78ar&ha22&a2#%rthm )2*
"lgorithm idea
:onstruct a atrix D
4,5
0i10/1 that is the 3eight of the
shortest path fro u
i
to u
/
3ith all interediate
vertices in an initial subset
,
BCu
1
* u
2
* D u
,
E
55
u
i
u
j
p
V
k
(2%0"78ar&ha22&a2#%rthm )3*
"lgorithm idea
D
405
0i10/1 is the direct%path from u
i
to u
/
D
405
0i10/1 B )0i10/1
D
415
0i10/1 is the shortest path fro u
i
to u
/
through
one intermediate vertex.
D
415
0i10/1BminCD
405
0i10/1*D
405
0i10,13D
405
0,10/1E
for each , in JJ
D4,50i10/1 is the shortest path fro u
i
to u
/
through ,
intermediate vertices.
D
4,5
0i10/1BminCD
4,%15
0i10/1*D
4,%15
0i10,13D
4,%15
0,10/1E for
each , in JJ
56
(2%0"78ar&ha22&a2#%rthm )4*
FloFd%+arshalls algorithm
57
Algorithm FloydWarshalls(V,E):
Input: A graph G=(V,E), w is the weighted matrix.
Output: The shortest paths of any pair u,v vertices in G.
For (i,j 1 to |V| ) do
!8i98"9*38i98"9
path8i98"9*%A
For (k 1 to |V|)
For (i,j 1 to |V|)
!8i98"9 inE!8i98"9- !8i98#9O!8#98"9G
path8i98"9 # BBQnly update if
BB !8i98"9*!8i98#9O!8#98"9
Code
Tut%ra2 9 !e:t t%$1
Preparing for the tutorial&
Practice 3ith exaples and exercises in
3ee#A2LtutorialLinstruction
Preparing for ne!t topic&
:ourse revie3
58
59
Any
Questions?

You might also like