0% found this document useful (0 votes)
4 views17 pages

Chapter9 Part1 Weighted Shortest Path

The document discusses the concept of finding the shortest path in a weighted graph using Dijkstra's algorithm, which was invented by Edsger W. Dijkstra in 1956. It explains the algorithm's steps, including initializing distances, updating paths, and the use of a min heap for efficiency. Additionally, it introduces the Label Correcting Algorithm and the Floyd-Warshall algorithm for finding all-pairs shortest paths.

Uploaded by

alphamudassir689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Chapter9 Part1 Weighted Shortest Path

The document discusses the concept of finding the shortest path in a weighted graph using Dijkstra's algorithm, which was invented by Edsger W. Dijkstra in 1956. It explains the algorithm's steps, including initializing distances, updating paths, and the use of a min heap for efficiency. Additionally, it introduces the Label Correcting Algorithm and the Floyd-Warshall algorithm for finding all-pairs shortest paths.

Uploaded by

alphamudassir689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

COS 212

Weighted Shortest
Paths
Weighted Shortest Path
 Suppose you are travelling
from Pretoria to Durban

N
13
9
10
P L Z

2 3 3
11
J H D
15

 What route is the best?


 Let’s find the shortest path!
Weighted Shortest Path
 The shortest path will have a starting point: P (for Pretoria)
 We will also have an endpoint: D (for Durban)

N
13
9
10
P L Z

2 3 3
11
J H D
15

 We’ll use Dijkstra’s algorithm to find shortest path from P to


D
 Invented by computer scientist Edsger W. Dijkstra in 1956
 One of the best-known shortest path algorithms out there!
Dijkstra’s Algorithm
 For the shortest path, every vertex
will maintain a number variable
to store its distance from the
start vertex (null,
 At the beginning of the N )
algorithm 13
 Set start.dist = 0; 9 (null,
10
 For the rest of the vertices, P L Z )
dist = Infinity (i.e. unknown) (null,
(null, 3
 Remembering the distance 0)
2 ) 3 11
is not enough! D
J H
 To be able to reconstruct the 15
(null, (null, (null,
shortest path, every vertex must also
) ) )
remember its “shortest path” predecessor
Dijkstra’s Algorithm
1. Add all vertices to an “unknown vertex” pool
2. Set current = vertex with smallest distance (initially, start
vertex)
(P, 13)
(null,
3. For every unknown adjacent vertex n N )
of current, calculate its distance 13
from the start, through current 9 (null,
 newdist(n) = current.dist + 10
P L Z )
newdist(n) = edge(current, n)
 “If we travel to n through (null, (null, 3
current, how long will it take?” 0)
2 ) 3 11
 For current = P
J H D
 newdist(J) = dist(P) + edge(P,J) = 0 + 2 = 2 15
(null,
 newdist(N) = dist(P) + edge(P,N) = 0 + 13 = 13 (P, 2) (null, (null,
) ) )
4. We are looking for the shortest path: If current makes
distance to n smaller, current is on the shortest path to n
found so far
 if newdist(n) < n.dist, set n.dist = newdist(n), n.path = current

5. All neighbours considered, so remove current from “unknown”


pool, set current = vertex with min dist, repeat from step 2!
while (!unknown.empty()) {
Dijkstra’s curr = unknown vertex with min dist
curr.known = true;
Algorithm for (each unknown neighbour w of curr) {
newDist = curr.dist + length(curr, w);
if (newDist < w.dist) {
w.dist = newDist;
w.path = curr;
}
}
}

Iteratio Ini 1 2 3 4 5 6 (P, 13)


(null,
n: t P J N H L Z )
N
curr: 13
9
P 0 (null,
(H, 28)
2 10
 P L Z )
J
13 13 (null, (null,
(N,
(H, 22)
20)
N  2 3 3
  22 20 0) ) 11
L 
 17 17 J H D
H  15
   28 28 (null,
(P, 2) 17) (Z,
(null,
(J, 31)
(null,
Z  ) ) )
     31
D 
Dijkstra’s Algorithm
(P, 13)
N
13
9
(H, 28)
10
P L Z
(null, (H, 20) 3
2 3 11
0)
J H D
15
(P, 2) (J, 17) (Z, 31)
 So… What is the shortest path?
Complexity?
 The predecessor vertices can tell us!
 Two ways to use Dijkstra’s algorithm O()
 To find the shortest path between A and B
 Given A, to determine the shortest paths to every vertex in the
graph
 The latter is referred to as shortest-path tree
Shortest Paths: Dijkstra’s Algorithm
Dijkstra(Graph, start){
for (each vertex v in Graph) { // Initialization
v.dist = INFINITY; // Unknown distance from start to
v
v.path = null; // Previous node in shortest path
v.known = false; // All vertices initially
unvisited
}
start.dist = 0 // Distance from start to start (0)
while (vertex with known = false exists in Graph) {
curr = unknown vertex with min dist // will be start
curr.known = true;

for (each unknown neighbour w of curr) {


newDist = curr.dist + length(curr, w)
if (newDist < w.dist) {
w.dist = newDist;
w.path = curr; What data structure would
} you use for “unknown”?
}
} Heap Complexity? Using a min heap can
significantly improve the
O() performance
How Reliable is Dijkstra’s Algorithm?
6 6
(null, (A, 2) (B,
(A, 5)
6) (null, (A, 2) (B,
(A, 5)
6)
0) 2 3 0) 2 3
A B C A B C

100 1 100 -
D D 100

(A, (A,
100) 100)

Iteratio Ini 1 2 3 4 Iteratio Ini 1 2 3 4


n: t n: t A B C D
A B C D
curr: curr:
A 0 A 0 2 0
2
B  B 
6 5 6 5 3
C  C 
10 10 10 10 10 10 10 10
D  0 0 0 0 D  0 0 0 0
Dijkstra’s algorithm will never
realise that D offers a shorter
path!
Fixing Dijkstra’s Algorithm

 Can’t we fix Dijkstra’s algorithm?

while !unknown.empty() {
curr = unknown vertex with min dist
Should we really curr.known = true;
remove it?
Visit all
for (each unknown neighbour w of curr) {
neighbours?
newDist = curr.dist + length(curr, w)
if (newDist < w.dist) {
w.dist = newDist;
w.path = curr; Add vertex back to the
} heap if dist changes?
}
}
Shortest Paths: Label Correcting Algorithm
labelCorrecting(Graph, start){
create vertex pool toBeChecked
for (each vertex v in Graph) { // Initialization
v.dist = INFINITY; // Unknown distance
v.path = null; // Previous node in shortest
path
}
start.dist = 0
Use a queue for the
toBeChecked.enqueue(start); “toBeChecked” pool!
while (!toBeChecked.empty()) {
curr = toBeChecked.dequeue();
for (each neighbour w of curr) { // even the visited ones
newDist = curr.dist + length(curr, w);
if (newDist < v.dist) {
w.dist = newDist;
w.path = curr;
if (!toBeChecked.contains(w))
Put w in the queue if
toBeChecked.enqueue(w);
} w gets updated!
}
}
Complexity?
}
O()
Label Correcting Algorithm with Queue
(a,2) 5
2
a b c (b,7) Works on negative weights,
2 2 -3 9 but not if cycles are present!

4 5 (e,4)
(e,3)
d e f
(a,2) (a,2)
(g,-2)
(b,-1) Iter: Ini 1 2 3 4 5 6 7 8 9
1
-5 curr: t
g Queue a ba b d e c g f ef f
(d,3) :
d d e c g ef e
create vertex set toBeChecked
for (each vertex v in Graph) { e e
c cg gf f
v.dist = INFINITY;
v.path = null;
}
start.dist = 0;
toBeChecked.enqueue(start);
a 0
while (!toBeChecked.empty()) { 2
curr = toBeChecked.dequeue() b 
for (each neighbour w of curr) { 7
newDist = curr.dist + length(curr,w);
if (newDist < w.dist) { c 
w.dist = newDist; 2
w.path = curr; d 
if (!toBeChecked.contains(w)) 2 -1 -2
}
toBeChecked.enqueue(w);
e 
} 4 3
} f 
3

Shortest Paths: All-Pairs
 All the algorithms discussed so far discover the shortest paths from
vertex i to any other vertex in the graph
 Can we find all the shortest paths from any vertex to any other
vertex?
 We can calculate the adjacency matrix of shortest paths using the
Floyd–Warshall algorithm (named after R. Floyd and S. Warshall)
let dist be a |V| × |V| array of minimum distances
initialized to infinity
for each vertex v
dist[v][v] = 0
for each edge(u,v)
dist[u][v] = weight(u,v) // the weight of the edge(u,v)
FWalgorithm(Matrix dist)
for i = 1 to |V|
for j = 1 to |V|
for k = 1 to |V|
if dist[j][k] > dist[j][i] + dist[i][k]
dist[j][k] = dist[j][i] + dist[i][k]
A B C D
6
A 0 0 3 1
2 3 distance
A B C B  0 3  adjacency
   matrix
1 -1 C 0
D D  -1 2 0
All-Pairs: Adjacency Matrix
let dist be a |V| × |V| array of minimum distances initialized
to infinity
for each vertex v
dist[v][v] = 0
for each edge(u,v)
dist[u][v] = weight(u,v) // the weight of the edge (u,v)

6 A B C D A B C D
2 3 A 0    A 0 2 6 1
A B C
B  0   B  0 3 
1 -1 C   0  C   0 
D     
D 0 D -1 0

Initialise intersections
to
existing edge weights
6
All-Pairs: Distance Matrix 2 3
A B C
for i = 1 to |V|
for j = 1 to |V|
for k = 1 to |V| 1 -1
if dist[j][k] > dist[j][i] + dist[i][k]
dist[j][k] = dist[j][i] + dist[i][k] D

At every iteration, determine: Given i, j, k, will the path


from j to k be cheaper if it goes through i ?

A B C D A B C D
A 0 2 6 1 A 0 2 5 1
i = 1: A B  0 3  i = 2: B B  0 3 

C   0  C   0 

D  -1  0 D  -1 2 0

A B C D A B C D
A 0 2 5 1 A 0 0 3 1
B  0 3  i = 4: B  0 3 
i = 3: C
D
C   0  C   0 
D  -1 2 0 D  -1 2 0
All-Pairs: Modified FW Algorithm
public static void allPairs(int [ ][ ] a, int [ ][ ] dist, int [ ]
[ ] path)
{
int n = a.length;
// Initialize distance and path matrices
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
dist[i][j] = a[i][j];
path[i][j] = NOT_A_VERTEX;
}

for (int k = 0; k < n; k++)


// Consider each vertex as an intermediate Complexity?
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) O()
if(dist[i][k] + dist[k][j] < dist[i][j])
{
// Update shortest path and distance
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = k;
}
}
 Also allows for reconstruction of all shortest paths
6
All-Pairs: Path Matrix 2 3
A B C
for i = 1 to |V|
for j = 1 to |V|
for k = 1 to |V| 1 -1
if dist[j][k] > dist[j][i] + dist[i][k]
dist[j][k] = dist[j][i] + dist[i][k] D
path[j][k] = i

path adjacency distance adjacency


matrix matrix

A B C D A B C D
A -1 -1 2 -1 A 0 2 5 1
B=2 B -1 -1 -1 -1 i = 2: B B  0 3 

C -1 -1 -1 -1 C   0 

D -1 -1 2 -1 D  -1 2 0

A B C D A B C D
A -1 4 4 -1 A 0 0 3 1
B -1 -1 -1 -1 i = 4: B  0 3 
D=4
D
C -1 -1 -1 -1 C   0 
D -1 -1 2 -1 D  -1 2 0

You might also like