SlideShare a Scribd company logo
Dijkstra’s Algorithm
Slide Courtesy: Uwash, UT
1
Single-Source Shortest Path Problem
Single-Source Shortest Path Problem - The
problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
Applications
- Maps (Map Quest, Google Maps)
- Routing Systems
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.
Works on both directed and undirected graphs. However,
all edges must have nonnegative weights.
Input: Weighted graph G={E,V} and source vertex v∈V,
such that all edge weights are nonnegative
Output: Lengths of shortest paths (or the shortest paths
themselves) from a given source vertex v∈V to all other
vertices
Approach
• The algorithm computes for each vertex u the distance to u from
the start vertex v, that is, the weight of a shortest path between
v and u.
• the algorithm keeps track of the set of vertices for which the
distance has been computed, called the cloud C
• Every vertex has a label D associated with it. For any vertex u,
D[u] stores an approximation of the distance between v and u.
The algorithm will update a D[u] value when it finds a shorter
path from v to u.
• When a vertex u is added to the cloud, its label D[u] is equal to
the actual (final) distance between the starting vertex v and
vertex u.
5
Dijkstra pseudocode
Dijkstra(v1, v2):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.
while List is not empty:
v := remove List vertex with minimum distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v, n)'s weight.
if dist is smaller than n's distance:
n's distance := dist.
n's previous := v.
reconstruct path from v2 back to v1,
following previous pointers.
6
7
Example: Initialization
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 ∞
∞ ∞
∞
Pick vertex in List with minimum distance.
∞ ∞
Distance(source) = 0 Distance (all vertices
but source) = ∞
8
Example: Update neighbors'
distance
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
∞ ∞
1
∞ ∞
Distance(B) = 2
Distance(D) = 1
9
Example: Remove vertex with
minimum distance
Pick vertex in List with minimum distance, i.e., D
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
∞ ∞
1
∞ ∞
10
Example: Update neighbors
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
9 5
Distance(C) = 1 + 2 = 3
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9
Distance(G) = 1 + 4 = 5
11
Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
Pick vertex in List with minimum distance (B) and update neighbors
9 5
Note : distance(D) not
updated since D is
already known and
distance(E) not updated
since it is larger than
previously computed
12
Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
9 5
No updating
Pick vertex List with minimum distance (E) and update neighbors
13
Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
8 5
Pick vertex List with minimum distance (C) and update neighbors
Distance(F) = 3 + 5 = 8
14
Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
6 5
Distance(F) = min (8, 5+1) = 6
Previous distance
Pick vertex List with minimum distance (G) and update neighbors
15
Example (end)
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
Pick vertex not in S with lowest cost (F) and update neighbors
6 5
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
THE KNOWN
CLOUD
v
P: Next shortest path from
inside the known cloud
v'
Correctness :“Cloudy” Proof
• If the path to v is the next shortest path, the path to v' must be at
least as long. Therefore, any path through v' to v cannot be shorter!
Source
Least cost node
competitor
When a vertex is added to the cloud, it has shortest
distance to source.
26
Dijkstra’s Correctness
• We will prove that whenever u is added to S, d[u] =
(s,u), i.e., that d[u] is minimum, and that equality is
maintained thereafter
• Proof
– Note that v, d[v] ≥ (s,v)
– Let u be the first vertex picked such that there is a shorter
path than d[u], i.e., that d[u]  (s,u)
– We will show that this assumption leads to a contradiction
27
Dijkstra Correctness (2)
• Let y be the first vertex V – S on the actual
shortest path from s to u, then it must be that d[y] =
(s,y) because
– d[x] is set correctly for y's predecessor x S on the
shortest path (by choice of u as the first vertex for which d
is set incorrectly)
– when the algorithm inserted x into S, it relaxed the edge
(x,y), assigning d[y] the correct value
28
• But if d[u] > d[y], the algorithm would have chosen y
(from the Q) to process next, not u  Contradiction
• Thus d[u] = (s,u) at time of insertion of u into S, and
Dijkstra's algorithm is correct
Dijkstra Correctness (3)
[ ] ( , ) (initial assumption)
( , ) ( , ) (optimal substructure)
[ ] ( , ) (correctness of [ ])
[ ] (no negative weights)
d u s u
s y y u
d y y u d y
d y
 
  
 

29
Dijkstra’s Pseudo Code
• Graph G, weight function w, root s
relaxing
edges
Time Complexity: Using List
The simplest implementation of the Dijkstra's algorithm stores
vertices in an ordinary linked list or array
– Good for dense graphs (many edges)
• |V| vertices and |E| edges
• Initialization O(|V|)
• While loop O(|V|)
– Find and remove min distance vertices O(|V|)
• Potentially |E| updates
• Update costs O(1)
Total time O(|V2
| + |E|) = O(|V2
| )
31
Time Complexity: Priority Queue
For sparse graphs, (i.e. graphs with much less than |V2| edges)
Dijkstra's implemented more efficiently by priority queue
• Initialization O(|V|) using O(|V|) buildHeap
• While loop O(|V|)
• Find and remove min distance vertices O(log |V|) using O(log |V|)
deleteMin
• Potentially |E| updates
• Update costs O(log |V|) using decreaseKey
Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|)
• |V| = O(|E|) assuming a connected graph

More Related Content

PPT
Dijkstra.ppt
PPT
graph in Data Structures and Algorithm.ppt
PPT
Bellman ford algorithm
PPT
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
PPT
Unit26 shortest pathalgorithm
PPT
Lec-35Graph - Graph - Copy in Data Structure
PPT
Dijesktra 1.ppt
PPT
Algorithm Design and Complexity - Course 10
Dijkstra.ppt
graph in Data Structures and Algorithm.ppt
Bellman ford algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
Unit26 shortest pathalgorithm
Lec-35Graph - Graph - Copy in Data Structure
Dijesktra 1.ppt
Algorithm Design and Complexity - Course 10

Similar to Dijkstra algorithm ds 57612334t4t44.ppt (20)

PPT
2.6 all pairsshortestpath
PPT
Inroduction_To_Algorithms_Lect14
PPT
Dijkstra c
PPT
Dijksatra
PPT
Single Source Shortest Path Algorithm.ppt
PPT
Dijkstra's algorithm for computer science
PDF
All pairs shortest path algorithm
PPT
dijkstraC.ppt
PDF
Daa chpater14
PDF
04 greedyalgorithmsii
PPT
lecture 21
DOC
BFS, Breadth first search | Search Traversal Algorithm
PDF
Single source shortes path in dag
PPT
Algorithm to count number of disjoint paths
PDF
04 greedyalgorithmsii 2x2
PDF
White Grey Minimalist Geometric Project Presentation.pdf
PPTX
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
PDF
14 chapter9 graph_algorithmstopologicalsort_shortestpath
DOCX
Shortest Path Problem.docx
PPT
Weighted graphs
2.6 all pairsshortestpath
Inroduction_To_Algorithms_Lect14
Dijkstra c
Dijksatra
Single Source Shortest Path Algorithm.ppt
Dijkstra's algorithm for computer science
All pairs shortest path algorithm
dijkstraC.ppt
Daa chpater14
04 greedyalgorithmsii
lecture 21
BFS, Breadth first search | Search Traversal Algorithm
Single source shortes path in dag
Algorithm to count number of disjoint paths
04 greedyalgorithmsii 2x2
White Grey Minimalist Geometric Project Presentation.pdf
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
14 chapter9 graph_algorithmstopologicalsort_shortestpath
Shortest Path Problem.docx
Weighted graphs
Ad

More from ssuser7b9bda1 (14)

PPT
03_sorting123456789454545454545444543.ppt
PPT
Ch02 algorithm 127836129812y12y823873893.ppt
PPT
Ch01-2 set theory 123873298738938r3387.ppt
PPT
Ch01 logic 7238248327433333722u393-1.ppt
PPT
lecture DS warshalls algo DFS BFS DS.ppt
PPTX
DS ppt 123445544434t33vdbdbdbfgfPPtt.pptx
PPT
graphass1-23022111180722548-1ba6b00a.ppt
PPTX
DSE LECTURE 1 -LRW_ Introduction_to_Computer.pptx
PPT
lecture07 dicrete mathematics relation .ppt
PPTX
skoekoe3748339828yrhjejh2989839jkej.pptx
PPTX
Exploring the Stack Stack Description.pptx
PPT
Lecture9_StackQueue operation on stacks .ppt
PPT
Exploring the Stack Stack operation.pptx
PDF
Agri Brochure sample for reference 123.pdf
03_sorting123456789454545454545444543.ppt
Ch02 algorithm 127836129812y12y823873893.ppt
Ch01-2 set theory 123873298738938r3387.ppt
Ch01 logic 7238248327433333722u393-1.ppt
lecture DS warshalls algo DFS BFS DS.ppt
DS ppt 123445544434t33vdbdbdbfgfPPtt.pptx
graphass1-23022111180722548-1ba6b00a.ppt
DSE LECTURE 1 -LRW_ Introduction_to_Computer.pptx
lecture07 dicrete mathematics relation .ppt
skoekoe3748339828yrhjejh2989839jkej.pptx
Exploring the Stack Stack Description.pptx
Lecture9_StackQueue operation on stacks .ppt
Exploring the Stack Stack operation.pptx
Agri Brochure sample for reference 123.pdf
Ad

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PPT on Performance Review to get promotions
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Well-logging-methods_new................
PPTX
Geodesy 1.pptx...............................................
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Welding lecture in detail for understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
DOCX
573137875-Attendance-Management-System-original
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT on Performance Review to get promotions
Lecture Notes Electrical Wiring System Components
OOP with Java - Java Introduction (Basics)
Well-logging-methods_new................
Geodesy 1.pptx...............................................
Internet of Things (IOT) - A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Welding lecture in detail for understanding
Operating System & Kernel Study Guide-1 - converted.pdf
Digital Logic Computer Design lecture notes
CYBER-CRIMES AND SECURITY A guide to understanding
573137875-Attendance-Management-System-original
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
bas. eng. economics group 4 presentation 1.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT 4 Total Quality Management .pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx

Dijkstra algorithm ds 57612334t4t44.ppt

  • 2. Single-Source Shortest Path Problem Single-Source Shortest Path Problem - The problem of finding shortest paths from a source vertex v to all other vertices in the graph.
  • 3. Applications - Maps (Map Quest, Google Maps) - Routing Systems
  • 4. Dijkstra's algorithm Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v∈V to all other vertices
  • 5. Approach • The algorithm computes for each vertex u the distance to u from the start vertex v, that is, the weight of a shortest path between v and u. • the algorithm keeps track of the set of vertices for which the distance has been computed, called the cloud C • Every vertex has a label D associated with it. For any vertex u, D[u] stores an approximation of the distance between v and u. The algorithm will update a D[u] value when it finds a shorter path from v to u. • When a vertex u is added to the cloud, its label D[u] is equal to the actual (final) distance between the starting vertex v and vertex u. 5
  • 6. Dijkstra pseudocode Dijkstra(v1, v2): for each vertex v: // Initialization v's distance := infinity. v's previous := none. v1's distance := 0. List := {all vertices}. while List is not empty: v := remove List vertex with minimum distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers. 6
  • 7. 7 Example: Initialization A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 ∞ ∞ ∞ ∞ Pick vertex in List with minimum distance. ∞ ∞ Distance(source) = 0 Distance (all vertices but source) = ∞
  • 8. 8 Example: Update neighbors' distance A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 ∞ ∞ 1 ∞ ∞ Distance(B) = 2 Distance(D) = 1
  • 9. 9 Example: Remove vertex with minimum distance Pick vertex in List with minimum distance, i.e., D A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 ∞ ∞ 1 ∞ ∞
  • 10. 10 Example: Update neighbors A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 9 5 Distance(C) = 1 + 2 = 3 Distance(E) = 1 + 2 = 3 Distance(F) = 1 + 8 = 9 Distance(G) = 1 + 4 = 5
  • 11. 11 Example: Continued... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 Pick vertex in List with minimum distance (B) and update neighbors 9 5 Note : distance(D) not updated since D is already known and distance(E) not updated since it is larger than previously computed
  • 12. 12 Example: Continued... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 9 5 No updating Pick vertex List with minimum distance (E) and update neighbors
  • 13. 13 Example: Continued... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 8 5 Pick vertex List with minimum distance (C) and update neighbors Distance(F) = 3 + 5 = 8
  • 14. 14 Example: Continued... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 6 5 Distance(F) = min (8, 5+1) = 6 Previous distance Pick vertex List with minimum distance (G) and update neighbors
  • 15. 15 Example (end) A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 Pick vertex not in S with lowest cost (F) and update neighbors 6 5
  • 25. THE KNOWN CLOUD v P: Next shortest path from inside the known cloud v' Correctness :“Cloudy” Proof • If the path to v is the next shortest path, the path to v' must be at least as long. Therefore, any path through v' to v cannot be shorter! Source Least cost node competitor When a vertex is added to the cloud, it has shortest distance to source.
  • 26. 26 Dijkstra’s Correctness • We will prove that whenever u is added to S, d[u] = (s,u), i.e., that d[u] is minimum, and that equality is maintained thereafter • Proof – Note that v, d[v] ≥ (s,v) – Let u be the first vertex picked such that there is a shorter path than d[u], i.e., that d[u]  (s,u) – We will show that this assumption leads to a contradiction
  • 27. 27 Dijkstra Correctness (2) • Let y be the first vertex V – S on the actual shortest path from s to u, then it must be that d[y] = (s,y) because – d[x] is set correctly for y's predecessor x S on the shortest path (by choice of u as the first vertex for which d is set incorrectly) – when the algorithm inserted x into S, it relaxed the edge (x,y), assigning d[y] the correct value
  • 28. 28 • But if d[u] > d[y], the algorithm would have chosen y (from the Q) to process next, not u  Contradiction • Thus d[u] = (s,u) at time of insertion of u into S, and Dijkstra's algorithm is correct Dijkstra Correctness (3) [ ] ( , ) (initial assumption) ( , ) ( , ) (optimal substructure) [ ] ( , ) (correctness of [ ]) [ ] (no negative weights) d u s u s y y u d y y u d y d y        
  • 29. 29 Dijkstra’s Pseudo Code • Graph G, weight function w, root s relaxing edges
  • 30. Time Complexity: Using List The simplest implementation of the Dijkstra's algorithm stores vertices in an ordinary linked list or array – Good for dense graphs (many edges) • |V| vertices and |E| edges • Initialization O(|V|) • While loop O(|V|) – Find and remove min distance vertices O(|V|) • Potentially |E| updates • Update costs O(1) Total time O(|V2 | + |E|) = O(|V2 | )
  • 31. 31 Time Complexity: Priority Queue For sparse graphs, (i.e. graphs with much less than |V2| edges) Dijkstra's implemented more efficiently by priority queue • Initialization O(|V|) using O(|V|) buildHeap • While loop O(|V|) • Find and remove min distance vertices O(log |V|) using O(log |V|) deleteMin • Potentially |E| updates • Update costs O(log |V|) using decreaseKey Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|) • |V| = O(|E|) assuming a connected graph