Shortest Path
Shortest Path
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
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.