0% found this document useful (0 votes)
11 views23 pages

W-11 - L-2 - Single Source Shortest-Path (SSSP) Dijkstra's Algorithm

The document discusses Dijkstra's Algorithm for solving the Single Source Shortest Path (SSSP) problem in graphs, highlighting its application in finding the shortest route between two points. It outlines the initialization process, the algorithm's steps, and its running time based on different data structures for priority queues. Additionally, it covers various shortest path variants and provides examples and pseudo code for better understanding.

Uploaded by

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

W-11 - L-2 - Single Source Shortest-Path (SSSP) Dijkstra's Algorithm

The document discusses Dijkstra's Algorithm for solving the Single Source Shortest Path (SSSP) problem in graphs, highlighting its application in finding the shortest route between two points. It outlines the initialization process, the algorithm's steps, and its running time based on different data structures for priority queues. Additionally, it covers various shortest path variants and provides examples and pseudo code for better understanding.

Uploaded by

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

Single Source Shortest

Path(SSSP) Dijkstra’s Algorithm


Week-11, Lecture-02

Course Code: CSE221 Course Teacher: Tanzina Afroz Rimi


Course Title: Algorithms Designation: Lecturer
Program: B.Sc. in CSE Email: [email protected]
Shortest Path Problems
• What is shortest path ?
▪ shortest length between two vertices for an unweighted
graph:
▪ smallest cost between two vertices for a weighted graph:

B 210 B

A A
450
60 190

C unweighted C weighted
graph graph
200 130
D D
E E
Shortest Path Problems
• How can we find the shortest route between two points
on a map?
• Model the problem as a graph problem:
• Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
• Goal: find a shortest path between two vertices (cities)

3
Shortest Path Problems
• Input: t x
6
• Directed graph G = (V, E) 3 9
3
4
• Weight function w : E → R 2 1
s 0
2 7
• Weight of path p = 〈v0, v1, . . . , vk〉 5 3
1
5
6 1
y z

• Shortest-path weight from u to v:

δ(u, v) = min w(p) : u pv if there exists a path from u to v

∞ otherwise

• Shortest path u to v is any path p such that w(p) = δ(u, v)


4
Variants of Shortest Paths
• Single-source shortest path
• G = (V, E) ⇒ find a shortest path from a given source vertex s to each vertex v ∈ V
• Single-destination shortest path
• Find a shortest path to a given destination vertex t from each vertex v
• Reverse the direction of each edge ⇒ single-source
• Single-pair shortest path
• Find a shortest path from u to v for given vertices u and v
• Solve the single-source problem
• All-pairs shortest-paths
• Find a shortest path from u to v for every pair of vertices u and v

5
Shortest-Path Representation
For each vertex v ∈ V:
t x
• d[v] = δ(s, v): a shortest-path estimate 3
6
9
• Initially, d[v]=∞ 3
4
2 1
• Reduces as algorithms progress s 0
2 7
3
• π[v] = predecessor of v on a shortest path 5
1
5
from s y
6 1
z
• If no predecessor, π[v] = NIL
• π induces a tree—shortest-path tree
• Shortest paths & shortest path trees are not
unique

6
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v ∈ V
2. do d[v] ← ∞
3. π[v] ← NIL
4. d[s] ← 0

• All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE

7
Dijkstra’s Algorithm
• Single-source shortest path problem:
• No negative-weight edges: w(u, v) > 0 ∀ (u, v) ∈ E

• Maintains two sets of vertices:


• S = vertices whose final shortest-path weights have already been determined
• Q = vertices in V – S: min-priority queue
• Keys in Q are estimates of shortest-path weights (d[v])

• Repeatedly select a vertex u ∈ V – S, with the minimum


shortest-path estimate d[v]
8
Dijkstra (G, w, s)
t 1 x
1. INITIALIZE-SINGLE-SOURCE(V, s) ∞ ∞
10 9
2. S← ∅ 2 3 4 6
s 0
3. Q ← V[G] 5 7
∞ ∞
4. while Q ≠ ∅ y
2
z
t x
5. do u ← EXTRACT-MIN(Q) 1
1
∞ ∞
0
6. S ← S ∪ {u} 10 9
2 3 4 6
s 0
7. for each vertex v ∈ Adj[u] 5 7

8. do RELAX(u, v, w) ∞
5 2

y z

9
Example t x t x
1 1
1 1 11
8 ∞ 8
0 4 43
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7
∞ 5 7
2 2
y z y z

t x t 1 x
1
1 8 9
8 9
9
3 10 9
10
2 4 2 3 4 6
s 0 3 6 s 0

7 5 7
5
5 7 5 7
2 2
y z y z
10
Dijkstra’s Pseudo Code
• Graph G, weight function w, root s

relaxing
edges
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) Θ(V)
2. S← ∅
3. Q ← V[G] O(V) build min-heap
4. while Q ≠ ∅ Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S ∪ {u}
7. for each vertex v ∈ Adj[u]
8. do RELAX(u, v, w) O(E) times; O(lgV)
Running time: O(VlgV + ElgV) = O(ElgV)
12
Dijkstra’s Running Time
• Extract-Min executed |V| time
• Decrease-Key executed |E| time
• Time = |V| TExtract-Min + |E| TDecrease-Key
• T depends on different Q implementations

Q T(Extract T(Decrease-K Total


-Min) ey)
array Ο(V) Ο(1) Ο(V 2)
binary heap Ο(lg V) Ο(lg V) Ο(E lg V)
Fibonacci heap Ο(lg V) Ο(1) (amort.) Ο(V lgV + E)
Textbooks & Web References
• Text Book (Chapter 24)
• www.geeksforgeeks.org
Thank you
&
Any question?

You might also like