Upload
krishver2
View
171
Download
2
Embed Size (px)
Citation preview
INTRODUCTIONDijkstra’s algorithm calculates the least distancefrom a starting vertex to a destination vertex fromthe given weighted graph
Input:• A weighted graph• Starting vertex• Destination vertex
Output:A graph which connects all the vertices with least distance
Dijkstra's AlgorithmInput:
a weighted digraph G=(V,E) with positive edge weightsa source node s V∈
Initialization:d[s]=0for each vertex x V-s∈d[x]=infinityMark all the vertices as unprocessed
Iteration:for i=1 to |V|Choose an unprocessed vertex x from V with minimum d[x]Mark x as processedfor all y adj(x)∈if d[y] > d[x]+w(x,y)d[y] = d[x]+w(x,y)
Dijkstra's Algorithm With PATHInput:
a weighted digraph G=(V,E) with positive edge weightsa source node s V∈
Initialization:d[s]=0 predecessor [ s ] = undefinedfor each vertex x V-s∈
d[x]=infinityMark all the vertices as unprocessed
Iteration:for i=1 to |V|
Choose an unprocessed vertex x from V with minimum d[x]Mark x as processedfor all y adj(x)∈
if d[y] > d[x]+w(x,y)d[y] = d[x]+w(x,y)predecessor [ y ] = x
Printing the Distance and pathfor each vertex v{
print Distance d[v] print_path(v);
}
print_path(v){
if (v is undefined)return;
elseprint_path ( prdecessor[v] ); // note recursion
print v}
C Codefor(i=1; i<=numofvertices; i++){
printf("\nDistance : %d ::: Path: ",distance[i]); print_path(i);
}
void print_path(int i){
if (i==-1)return;
elseprint_path(prdecessor[i]);
printf("%d ",i);}
Assumes that vertices are numbered from 1.
distance array and predecessor array are computed by dijkstra algorithm
-1 is undefined vertex
Dijkstra's Shortest Path Algorithm
Find shortest path from s to t.
7
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
Dijkstra's Shortest Path Algorithm 8
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
0
distance label
S = { }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
Dijkstra's Shortest Path Algorithm 9
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
0
distance label
S = { }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin
Dijkstra's Shortest Path Algorithm10
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
distance label
S = { s }
PQ = { 2, 3, 4, 5, 6, 7, t }decrease key
X
X
X
Dijkstra's Shortest Path Algorithm 11
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
distance label
S = { s }
PQ = { 2, 3, 4, 5, 6, 7, t }
X
X
X
delmin
Dijkstra's Shortest Path Algorithm12
s
3
t
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 = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
X
X
X
Dijkstra's Shortest Path Algorithm 13
s
3
t
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 = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
X
X
X
decrease key
X 33
Dijkstra's Shortest Path Algorithm14
s
3
t
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 = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
X
X
X
X 33
delmin
Dijkstra's Shortest Path Algorithm 15
s
3
t
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 = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
X
X
X
X 33
44X
X
32
Dijkstra's Shortest Path Algorithm16
s
3
t
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 = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
X
X
X
44X
delmin
X 33X
32
Dijkstra's Shortest Path Algorithm 17
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
X
X
X
44X
35X
59 X
24
X 33X
32
Dijkstra's Shortest Path Algorithm 18
s
3
t
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 = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
X
X
X
44X
35X
59 X
delmin
X 33X
32
Dijkstra's Shortest Path Algorithm 19
s
3
t
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 = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
X
X
X
44X
35X
59 XX 51
X 34
X 33X
32
Dijkstra's Shortest Path Algorithm20
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
X
X
X
44X
35X
59 XX 51
X 34
delmin
X 33X
32
24
Dijkstra's Shortest Path Algorithm21
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
X
X
X
44X
35X
59 XX 51
X 34
24
X 50
X 45
X 33X
32
Dijkstra's Shortest Path Algorithm22
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
X
X
X
44X
35X
59 XX 51
X 34
24
X 50
X 45
delmin
X 33X
32
Dijkstra's Shortest Path Algorithm 23
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
X
X
X
44X
35X
59 XX 51
X 34
24
X 50
X 45
X 33X
32
Dijkstra's Shortest Path Algorithm24
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
X
X
X
44X
35X
59 XX 51
X 34
X 50
X 45
delmin
X 33X
32
24
Dijkstra's Shortest Path Algorithm 25
s
3
t
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 = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
X
X
X
44X
35X
59 XX 51
X 34
X 50
X 45
X 33X
32
Dijkstra's Shortest Path Algorithm 26
s
3
t
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 = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
X
X
X
44X
35X
59 XX 51
X 34
X 50
X 45
X 33X
32