Shortest Path Algorithms ♢ COMP2521 ♢ (23T1)

COMP2521(23T1) ♢ Shortest Path ♢ [0/10]
❖ Shortest Path

Path = sequence of edges in graph G

cost(path) = sum of edge weights along path

Shortest path between vertices s and t

Assumptions: weighted digraph, no negative weights.

Applications:  navigation,  routing in data networks,  …

COMP2521(23T1) ♢ Shortest Path ♢ [1/10]
❖ ... Shortest Path


Some variations on shortest path (SP) ...

Source-target SP problem

Single-source SP problem

All-pairs SP problems
COMP2521(23T1) ♢ Shortest Path ♢ [2/10]
❖ Single-source Shortest Path (SSSP)

Shortest paths from s  to all other vertices

Example:

[Diagram:Pics/sssp.png]

COMP2521(23T1) ♢ Shortest Path ♢ [3/10]
❖ Edge Relaxation

Assume:  dist[] and pred[] as above

If we have ...

Relaxation updates data for w  if we find a shorter path from s  to w :

COMP2521(23T1) ♢ Shortest Path ♢ [4/10]
❖ ... Edge Relaxation

Relaxation along edge e = (v,w,3) :

[Diagram:Pics/relaxation.png]

COMP2521(23T1) ♢ Shortest Path ♢ [5/10]
❖ Dijkstra's Algorithm

One approach to solving single-source shortest path …

dist[]  // array of cost of shortest path from s
pred[]  // array of predecessor in shortest path from s
vSet    // vertices whose shortest path from s is unknown

dijkstraSSSP(G,source):
|  Input graph G, source node
|
|  initialise all dist[] to ∞
|  dist[source]=0
|  initialise all pred[] to -1
|  vSet=all vertices of G
|  while vSet ≠ ∅ do
|  |  find v ∈ vSet with minimum dist[v]
|  |  for each (v,w,weight) ∈ edges(G) do
|  |     relax along (v,w,weight)
|  |  end for
|  |  vSet=vSet \ {v}
|  end while

COMP2521(23T1) ♢ Shortest Path ♢ [6/10]
❖ Tracing Dijkstra's Algorithm

How Dijkstra's algorithm runs when source = 0:

[Diagram:Pics/dijkstra-trace.png]

COMP2521(23T1) ♢ Shortest Path ♢ [7/10]
❖ Analysis of Dijkstra's Algorithm


Why Dijkstra's algorithm is correct ...

Hypothesis:

(a) for visited s, dist[s] is shortest distance from source

(b) for unvisited t, dist[t] is shortest distance from source via visited nodes

Ultimately, all nodes are visited, so ...

COMP2521(23T1) ♢ Shortest Path ♢ [8/10]
❖ ... Analysis of Dijkstra's Algorithm

Proof:

Base case: no visited nodes, dist[source]=0, dist[s]=∞ for all other nodes

Induction step:

  1. If s  is unvisited node with minimum dist[s], then dist[s]  is shortest distance from source to s:
    • if ∃ shorter path via only visited nodes, then dist[s]  would have been updated when processing the predecessor of s on this path
    • if ∃ shorter path via an unvisited node u, then dist[u]<dist[s], which is impossible if s  has min distance of all unvisited nodes
  2. This implies that (a) holds for s  after processing s
  3. (b) still holds for all unvisited nodes t  after processing s:
    • if ∃ shorter path via s  we would have just updated dist[t]
    • if ∃ shorter path without s  we would have found it previously
COMP2521(23T1) ♢ Shortest Path ♢ [9/10]
❖ ... Analysis of Dijkstra's Algorithm

Time complexity analysis …

Each edge needs to be considered once ⇒ O(E).

Outer loop has O(V) iterations.

Implementing "find s ∈ vSet with minimum dist[s]"

  1. try all svSet ⇒ cost = O(V) ⇒ overall cost = O(E + V2) = O(V2)
  2. using a PQueue to implement extracting minimum
    • can improve overall cost to O(E + V·log V)
COMP2521(23T1) ♢ Shortest Path ♢ [10/10]


Produced: 26 Mar 2023