Data Structures and Algorithms: Finding Shortest Way
Data Structures and Algorithms: Finding Shortest Way
2
Policies for students
These contents are only used for students
PERSONALLY.
Students are NOT allowed to modify or
deliver these contents to anywhere or anyone
for any purpose.
3
Recording of modifications
Currently, there are no modification on these
contents.
4
Outline
VisuAlgo: http://visualgo.net/sssp.html
Important note:
• For SSSP on unweighted
graph, we can only use
BFS
• For SSSP on tree, we can
use either DFS/BFS
• You can try this on PS5
Subtask A+B
Try in VisuAlgo!
This graph is unweighted (i.e. all edge weight = 1)
Why it works?
• More details later in the introductory lecture on
Dynamic Programming (Week 10)
Try in VisuAlgo!
Topological Sort of this DAG is {0, 2, 1, 3, 4, 5}
• Try relaxing the outgoing edges of vertices
listed in the topological order above
– With just one pass, all vertices will have the
correct dist[v]
• (This will be revisited in Lecture 10)
Special Case 4a:
The graph has no negative weight
Bellman Ford’s algorithm works fine for all cases of SSSP
on weighted graphs, but it runs in O(VE)…
• For a “reasonably sized” weighted graphs with
V ~ 1000 and E ~ 100000 (recall that E = O(V2) in a
complete simple graph), Bellman Ford’s is (really) “slow”…
For many practical cases, the SSSP problem is performed
on a graph where all its edges have non‐negative weight
• Example: Traveling between two cities on a map (graph)
usually takes positive amount of time units
Fortunately, there is a faster SSSP algorithm
that exploits this property: The Dijkstra’s algorithm
The ‘original version’
DIJKSTRA’S ALGORITHM
Key Ideas of (the original)
Dijkstra’s Algorithm
Formal assumption:
• For each edge(u, v) E, we assume w(u, v) ≥ 0 (non‐negative)
Proof by contradiction: p p p
• Let the shortest path p = v0 0i
vi ij
vj jk
vk pij'
• If pij is not the shortest path, the we have another pij’ that is
shorter than pij. We can then cut out pij and replace it with
pij’, which result in a shorter path from v0 to vk
• But p is the shortest path from v0 to vk contradiction!
• Thus pij must be a shortest path between i and j
Why This Greedy Strategy Works? (2)
i.e. why is it sufficient to only process each vertex just once?
DIJKSTRA’S ALGORITHM
Special Case 4b:
The graph has no negative weight cycle
For many practical cases, the SSSP problem is performed
on a graph where its edges may have negative weight
but it has no negative cycle
• Example: Traveling between two cities on a map (graph)
using electric car with battery to minimize battery usage:
– We take (+) energy from the battery if the road is flat or go uphill
– We recharge the battery (i.e. take ‐energy) if the road goes
downhill
– But we cannot keep cycling around to recharge the battery forever
due to kinetic energy loss, etc
Key ideas:
• We use a built‐in priority queue in C++ STL/Java Collections
to order the next vertex u to be processed based on its dist[u]
– This vertex information is stored as IntegerPair (dist[u], u)
• But with modification: We use “Lazy Data Structure” strategy
to avoid implementing “DecreaseKey()” in C++/Java PQ
library
Modified Implementation (2)
of Dijkstra’s Algorithm (CP3, Section 4.4.3)
Lazy DS: Get pair (d, u) in front of the priority queue PQ
with the minimum shortest path estimate so far
• if d = dist[u], we relax all edges out of u,
else if d > dist[u], we have to delete this inferior (d, u) pair
– See below to understand that we do not delete the wrong (d, u) pair
immediately, but instead, we wait until the last possible moment (lazy)
• If dist[v] of a neighbor v of u decreases, enqueue (dist[v], v)
to
PQ again for future propagation of shortest path distance info
– Here we adopt a lazy approach not to delete the “wrong (d, u) pair”
at this point of time. Q: Why?
• Because C++/Java PriorityQueue (Binary Heap) does not have feature
to efficiently search for certain entries other than the minimum one!
Modified Dijkstra’s Algorithm
initSSSP(s)