Session 13 - Single Source Shortest Path Method
Session 13 - Single Source Shortest Path Method
Algorithms
Session -13
1
Insights
• Introduction
• Example
• Dijkstra’s Algorithm – Pseudocode
• Dijkstra’s Algorithm – Time Complexity
Single Source Shortest Path Problem (SSSP) – An Introduction
Miami
4 -4
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1
800 1000 250
2 3 2
Denver 3
300 6 New York
1000 4
1400
5 0
1 900
8 6
1700 7
Los Angeles
New Orleans 1000 7 8
Miami
5 is a Source Vertex
4 -5
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1500
1400
5 0
1 900
8 6 250
1700 7 inf
Los Angeles
New Orleans 1000 7 8 inf
Miami
Updated 4 & 6 Vertices
4 -6
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1500
1400
5 0
1 900
8 6 250
1700 7 inf
Los Angeles
New Orleans 1000 7 8 inf
Miami
Vertex 6 selected
4 -7
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 inf
Los Angeles
New Orleans 1000 7 8 inf
Miami
Updated Vertex 4
4 -8
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 inf
Los Angeles
New Orleans 1000 7 8 1650
Miami
Updated Vertex 8
4 -9
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Updated Vertex 7
4 -10
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Vertex 7 selected
4 -11
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
No changes in Vertex 4 & 8
4 -12
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 inf
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Vertex 4 selected
4 -13
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Updated Vertex 3
4 -14
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 inf
800 1000 250
2 3 2 inf
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Vertex 8 selected
4 -15
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 3350
800 1000 250
2 3 2 inf
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Updated Vertex 1
4 -16
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 3350
800 1000 250
2 3 2 inf
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Vertex 3 selected
4 -17
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 3350
800 1000 250
2 3 2 3250
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Updated Vertex 2
4 -18
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 3350
800 1000 250
2 3 2 3250
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Vertex 2 selected
4 -19
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 3350
800 1000 250
2 3 2 3250
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
No changes in Vertex 1
4 -20
Single Source Shortest Path Problem (SSSP)
Boston
Chicago 1500 5
City Dist
1200 4
San Francisco 1 3350
800 1000 250
2 3 2 3250
Denver 3 2450
300 6 New York
1000 4 1250
1400
5 0
1 900
8 6 250
1700 7 1150
Los Angeles
New Orleans 1000 7 8 1650
Miami
Vertex 1 selected
4 -21
Algorithm ShortestPaths (v, cost, dist, n)
// dist[j], 1<= j <=n, is set to the length of the shortest path from vertex v to vertex j in a
// di-graph G with n vertices. dist[v] is set to zero. G is represented by its cost adjacency
// matrix cost[l :n, 1:n].
{
for i :=1 to n do { // Initialize S.
S[i]:=false; dist[i] := cost[v, i];
}
S[v]:=true; dist[v] :=0.0; // Put v in S.
for num := 2 to n -1 do { // Determine n-1 paths from v.
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=true; // Put u in S.
for (each w adjacent to u with S[w]= false) do // Update distances.
if (dist[w] >(dist[u]+cost[u, w,])) then
dist[w] :=dist[u]+cost[u, w];
}// end of for loop
} 22
Algorithm ShortestPaths (v, cost, dist, n)
{
for i :=1 to n do { // Initialize S.
S[i]:=false; dist[i] := cost[v, i];
}
S[v]:=true; dist[v] :=0.0; // Put v in S.
for num := 2 to n -1 do { // Determine n-1 paths from v.
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=true; // Put u in S.
for (each w adjacent to u with S[w]= false) do // Update distances.
if (dist[w] >(dist[u]+cost[u, w,])) then
dist[w] :=dist[u]+cost[u, w];
}// end of for loop
}
23
Algorithm ShortestPaths (v, cost, dist, n)
{
for i :=1 to n do { // Initialize S.
S[i]:=false; dist[i] := cost[v, i];
}
S[v]:=true; dist[v] :=0.0; // Put v in S.
for num := 2 to n -1 do { // Determine n-1 paths from v.
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=true; // Put u in S.
for (each w adjacent to u with S[w]= false) do // Update distances.
if (dist[w] >(dist[u]+cost[u, w,])) then
dist[w] :=dist[u]+cost[u, w];
}// end of for loop
}
24
Algorithm ShortestPaths (v, cost, dist, n)
{
for i :=1 to n do { // Initialize S.
S[i]:=false; dist[i] := cost[v, i];
}
S[v]:=true; dist[v] :=0.0; // Put v in S.
for num := 2 to n -1 do { // Determine n-1 paths from v.
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=true; // Put u in S.
for (each w adjacent to u with S[w]= false) do // Update distances.
if (dist[w] >(dist[u]+cost[u, w,])) then
dist[w] :=dist[u]+cost[u, w];
}// end of for loop
}
25
Algorithm ShortestPaths (v, cost, dist, n)
{
for i :=1 to n do { // Initialize S.
S[i]:=false; dist[i] := cost[v, i];
}
S[v]:=true; dist[v] :=0.0; // Put v in S.
for num := 2 to n -1 do { // Determine n-1 paths from v.
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=true; // Put u in S.
for (each w adjacent to u with S[w]= false) do // Update distances.
if (dist[w] >(dist[u]+cost[u, w,])) then
dist[w] :=dist[u]+cost[u, w];
}// end of for loop
}
26
Algorithm ShortestPaths (v, cost, dist, n)
{
for i :=1 to n do { // Initialize S.
S[i]:=false; dist[i] := cost[v, i];
}
S[v]:=true; dist[v] :=0.0; // Put v in S.
for num := 2 to n -1 do { // Determine n-1 paths from v.
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=true; // Put u in S.
for (each w adjacent to u with S[w]= false) do // Update distances.
if (dist[w] >(dist[u]+cost[u, w,])) then
dist[w] :=dist[u]+cost[u, w];
}// end of for loop
}
27
Thank You
28