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

Shortest Path

Uploaded by

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

Shortest Path

Uploaded by

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

Shortest Paths in Graphs

Finding the shortest path is a classical problem in graph theory, and a large number of
different solutions have been proposed. Edges are assigned certain weights representing, for
example, distances between cities, times separating the execution of certain tasks, costs of
transmitting information between locations, amounts of some substance transported from one
place to another, and so on. When determining the shortest path from vertex v to vertex u,
information about distances between intermediate vertices w has to be recorded. This
information can be recorded as a label associated with these vertices, where the label is only
the distance from v to w or the distance along with the predecessor of w in this path. The
methods of finding the shortest path rely on these labels. Depending on how many times
these labels are updated, the methods solving the shortest path problem are divided in two
classes: label-setting methods and labelcorrecting methods.
Types of Shortest Path Algorithms
1. Label-setting methods:
o Set the shortest distance for each city as we go.
o Works only when all road lengths (weights) are positive.
2. Label-correcting methods:
o Adjust the distances as needed, even after setting them.
o Can handle roads with negative weights but not cycles with negative total
weight (negative cycles).
Both approaches use labels (distance and predecessor city) to keep track of the shortest paths.
Label Setting Method

Generic Shortest Path Algorithm

Working of Algorithm (Generic Process)


1. Initialize:
o Set the distance to the starting city as 0.
o Set distances to all other cities as infinity (∞), meaning they are initially
unreachable.
2. Explore:
o Pick a city, check all its neighbors.
o Update the distance to each neighbor if using this city gives a shorter path.
o Keep a record of which city leads to which (predecessors).
3. Repeat:
o Continue until all cities are processed.
4. Result:
o You now know the shortest distance to each city and the route to follow.
Example of Algorithm
Suppose we have 4 cities (A, B, C, D) connected as follows:
 A → B (weight 1)
 A → C (weight 4)
 B → C (weight 2)
 B → D (weight 6)
 C → D (weight 3)

Our Objective: We want to find the shortest paths from A to all other cities.
Step 1: Initialize
 Start at city A:
o Distance to A: 0 (itself)
o Distance to B, C, D: ∞ (not reachable yet)
 Predecessors: None set initially.
Cit Distance Predecessor
y
A 0 -
B ∞ -
C ∞ -
D ∞ -

Step 2: Process A
From A, we check its neighbors:
 A → B: 0 + 1 = 1 (update B's distance to 1, predecessor = A).
 A → C: 0 + 4 = 4 (update C's distance to 4, predecessor = A).
Cit Distance Predecessor
y
A 0 -
B 1 A
C 4 A
D ∞ -
Add B and C to the "to be checked" list.

Step 3: Process B
From B, check its neighbors:
 B → C: 1 + 2 = 3 (shorter than 4, so update C's distance to 3, predecessor = B).
 B → D: 1 + 6 = 7 (update D's distance to 7, predecessor = B).
Cit Distance Predecessor
y
A 0 -
B 1 A
C 3 B
D 7 B
Add D to the "to be checked" list.

Step 4: Process C
From C, check its neighbors:
 C → D: 3 + 3 = 6 (shorter than 7, so update D's distance to 6, predecessor = C).
Cit Distance Predecessor
y
A 0 -
B 1 A
C 3 B
D 6 C

Step 5: Process D
No further updates since D have no outgoing edges.

Final Result
The shortest distances from A are:
 A → B: 1
 A → C: 3
 A → D: 6
And The paths are:
 A→B
 A→B→C
 A→B→C→D
Objective Completed when all shortest paths are found.
Dijkstra Algorithm
Dijkstra’s algorithm is a label-setting method for finding the shortest path from one starting
vertex (source) to all other vertices in a graph with non-negative weights. It works by:
1. Maintaining a set of distances from the source to all vertices.
2. Finalizing the shortest distance to one vertex at a time, ensuring it never changes after
being finalized.
Key Features
 Label-Setting: Once the shortest distance for a vertex is determined, it is fixed and
not updated again.
 Priority-Based Selection: At each step, the algorithm processes the vertex with the
smallest known distance from the source.
In Dijkstra's Algorithm, the shortest path to a vertex is guaranteed to be finalized when it is
removed from the "to be checked" list or priority queue. This property is a direct result of:
 Non-negative edge weights.
 The greedy selection of the smallest current distance.
This is why Dijkstra's is efficient and correct for graphs with non-negative weights.
Algorithm
Example
Main solution is to create a table and interpret all the shortest paths from it.

Another General Solution like Generic Algorithm process.


Vertices: a, b, c, d, e, f, g, h, i, j
Edges with weights:
 a to e: 1
 a to h: 10
 b to c: 2
 f to b: 1
 f to c: 3
 d to a: 4
 d to h: 1
 e to f: 3
 f to g: 7
 f to i: 1
 j to g: 1
 h to i: 9
 h to e: 5
 i to j: 2
Dijkstra's Algorithm (Starting from Vertex d)
Step 1: Initialization
 Start at vertex d:
o Distance to d: 0 (itself)
o Distance to other vertices (a, b, c, e, f, g, h, i, j): ∞ (not reachable yet)
o Predecessors: None set initially.
Cit Distance Predecessor
y
a ∞ -
b ∞ -
c ∞ -
d 0 -
e ∞ -
f ∞ -
g ∞ -
h ∞ -
i ∞ -
j ∞ -

Step 2: Process Vertex d


 Why pick d: d has the smallest current distance (0).
 Update neighbors from d:
o d → a: 0 + 4 = 4 (update a's distance to 4, predecessor = d).
o d → h: 0 + 1 = 1 (update h's distance to 1, predecessor = d).
Cit Distance Predecessor
y
a 4 d
b ∞ -
c ∞ -
d 0 -
e ∞ -
f ∞ -
g ∞ -
h 1 d
i ∞ -
j ∞ -
Step 3: Process Vertex h
 Why pick h: h has the smallest current distance (1).
 Update neighbors from h:
o h → e: 1 + 5 = 6 (update e's distance to 6, predecessor = h).
o h → i: 1 + 9 = 10 (update i's distance to 10, predecessor = h).
Cit Distance Predecessor
y
a 4 d
b ∞ -
c ∞ -
d 0 -
e 6 h
f ∞ -
g ∞ -
h 1 d
i 10 h
j ∞ -

Step 4: Process Vertex a


 Why pick a: a has the smallest current distance (4).
 Update neighbors from a:
o a → e: 4 + 1 = 5 (update e's distance to 5, predecessor = a).
o a → h: 4 + 10 = 14 (no update for h as its distance is already 1).
Cit Distance Predecessor
y
a 4 d
b ∞ -
c ∞ -
d 0 -
e 5 a
f ∞ -
g ∞ -
h 1 d
i 10 h
j ∞ -

Step 5: Process Vertex e


 Why pick e: e has the smallest current distance (5).
 Update neighbors from e:
o e → f: 5 + 3 = 8 (update f's distance to 8, predecessor = e).
Cit Distance Predecessor
y
a 4 d
b ∞ -
c ∞ -
d 0 -
e 5 a
f 8 e
g ∞ -
h 1 d
i 10 h
j ∞ -

Step 6: Process Vertex f


 Why pick f: f has the smallest current distance (8).
 Update neighbors from f:
o f → b: 8 + 1 = 9 (update b's distance to 9, predecessor = f).
o f → c: 8 + 3 = 11 (update c's distance to 11, predecessor = f).
o f → g: 8 + 7 = 15 (update g's distance to 15, predecessor = f).
o f → i: 8 + 1 = 9 (update i's distance to 9, predecessor = f).
Cit Distance Predecessor
y
a 4 d
b 9 f
c 11 f
d 0 -
e 5 a
f 8 e
g 15 f
h 1 d
i 9 f
j ∞ -

Step 7: Process Vertex b


 Why pick b: b has the smallest current distance (9).
 Update neighbors from b:
o b → c: 9 + 2 = 11 (no update for c as its distance is already 11).
Cit Distance Predecessor
y
a 4 d
b 9 f
c 11 f
d 0 -
e 5 a
f 8 e
g 15 f
h 1 d
i 9 f
j ∞ -

Step 8: Process Vertex i


 Why pick i: i has the smallest current distance (9).
 Update neighbors from i:
o i → j: 9 + 2 = 11 (update j's distance to 11, predecessor = i).
Cit Distance Predecessor
y
a 4 d
b 9 f
c 11 f
d 0 -
e 5 a
f 8 e
g 15 f
h 1 d
i 9 f
j 11 i

Step 9: Process Vertex j


 Why pick j: j has the smallest current distance (11).
 No updates because j has no outgoing edges.
Cit Distance Predecessor
y
a 4 d
b 9 f
c 11 f
d 0 -
e 5 a
f 8 e
g 15 f
h 1 d
i 9 f
j 11 i

Step 10: Process Vertex g


 Why pick g: g has the smallest current distance (15).
 No updates because g has no outgoing edges.

Final Result: Shortest Distances


Cit Distance Predecessor
y
a 4 d
b 9 f
c 11 f
d 0 -
e 5 a
f 8 e
g 15 f
h 1 d
i 9 f
j 11 i
Shortest Paths from d:
 d → a: 4
 d → h: 1
 d → a → e: 5
 d → a → e → f: 8
 d → a → e → f → i: 9
 d → a → e → f → i → j: 11
 d → a → e → f → g: 15
Assignment# 4
Write down complete process for finding shortest paths
through generic shortest path algorithm and also create
Dijkstra algorithm shortest paths table for those graphs
below. (Note: Pick a starting vertex and find the
shortest paths to all the other vertices in the graph)

You might also like