SlideShare a Scribd company logo
Dijkstra's algorithm
1
Dijkstra's algorithm
• Dijkstra's algorithm: finds shortest (minimum weight) path between a
particular pair of vertices in a weighted directed graph with nonnegative
edge weights
– solves the "one vertex, shortest path" problem
– basic algorithm concept: create a table of information about the
currently known best way to reach each vertex (distance, previous
vertex) and improve it until it reaches the best solution
• in a graph where:
– vertices represent cities,
– edge weights represent driving distances between pairs of cities
connected by a direct road,
Dijkstra's algorithm can be used to find the shortest route between
one city and any other
2
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.
3
4
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) = 
5
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
6
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
 
7
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
8
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
9
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
10
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
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
6 5
Distance(F) = min (8, 5+1) = 6
Previous distance
Pick vertex List with minimum distance (G) and update neighbors
12
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
13
Correctness
• Dijkstra’s algorithm is a greedy algorithm
– make choices that currently seem the best
– locally optimal does not always mean globally optimal
• Correct because maintains following two properties:
– for every known vertex, recorded distance is shortest
distance to that vertex from source vertex
– for every unknown vertex v, its recorded distance is shortest
path distance to v from source vertex, considering only
currently known vertices and v
14
THE KNOWN
CLOUD
v
Next shortest path from
inside the known cloud
v'
“Cloudy” Proof: The Idea
• 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
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.
15
16
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)
• Reconstruct path O(|E|)
Total time O(|V2| + |E|) = O(|V2| )
17
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
• Reconstruct path O(|E|)
Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|)
• |V| = O(|E|) assuming a connected graph
18
Dijkstra's Exercise
A
G
F
B
E
C
D
20
10
50
40
20
80
50
20
30
20
90
H
10
10
10
• Use Dijkstra's algorithm to determine the lowest cost path from vertex A
to all of the other vertices in the graph. Keep track of previous vertices so
that you can reconstruct the path later.
Minimum spanning tree
• tree: a connected, directed acyclic graph
• spanning tree: a subgraph of a graph, which meets
the constraints to be a tree (connected, acyclic) and
connects every vertex of the original graph
• minimum spanning tree: a spanning tree with
weight less than or equal to any other spanning tree
for the given graph
19
Min. span. tree applications
• Consider a cable TV company laying cable to a new
neighborhood...
– If it is constrained to bury the cable only along certain paths, then
there would be a graph representing which points are connected by
those paths.
– Some of those paths might be more expensive, because they are
longer, or require the cable to be buried deeper.
• These paths would be represented by edges with larger weights.
– A spanning tree for that graph would be a subset of those paths that
has no cycles but still connects to every house.
• There might be several spanning trees possible. A minimum spanning tree
would be one with the lowest total cost.
20

More Related Content

What's hot

Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
ArijitDhali
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
sana younas
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
Rajendran
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
Yosuke Mizutani
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
Akash Sethiya
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
Pankaj Thakur
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims AlgorithmSriram Raj
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
Acad
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
Ankit Kumar Singh
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 

What's hot (20)

Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims Algorithm
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 

Similar to Dijkstra.ppt

Dijesktra 1.ppt
Dijesktra 1.pptDijesktra 1.ppt
Dijesktra 1.ppt
DEEPAK948083
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithmAAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
SantoshDood
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
SeethaDinesh
 
P5 - Routing Protocols
P5 - Routing ProtocolsP5 - Routing Protocols
P5 - Routing Protocols
Kurniawan Dwi Irianto
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
WahyuAde4
 
Algorithm Exam Help
Algorithm Exam Help Algorithm Exam Help
Algorithm Exam Help
Programming Exam Help
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.ppt
SaimaShaheen14
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
Khoa Mac Tu
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
Venkat Sai Sharath Mudhigonda
 

Similar to Dijkstra.ppt (20)

Dijesktra 1.ppt
Dijesktra 1.pptDijesktra 1.ppt
Dijesktra 1.ppt
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithmAAD_Lec-3-B-ShortestPaths.ppt  of design and analysis of algorithm
AAD_Lec-3-B-ShortestPaths.ppt of design and analysis of algorithm
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
P5 - Routing Protocols
P5 - Routing ProtocolsP5 - Routing Protocols
P5 - Routing Protocols
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 
Algorithm Exam Help
Algorithm Exam Help Algorithm Exam Help
Algorithm Exam Help
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
bellman-ford Theorem.ppt
bellman-ford Theorem.pptbellman-ford Theorem.ppt
bellman-ford Theorem.ppt
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
 

More from Ruchika Sinha

Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Clipping
ClippingClipping
Clipping
Ruchika Sinha
 
Lec22 intel
Lec22 intelLec22 intel
Lec22 intel
Ruchika Sinha
 
Pc assembly
Pc assemblyPc assembly
Pc assembly
Ruchika Sinha
 
Casing
CasingCasing
Installation of motherboard
Installation of motherboardInstallation of motherboard
Installation of motherboard
Ruchika Sinha
 
Shortest path
Shortest pathShortest path
Shortest path
Ruchika Sinha
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Python material
Python materialPython material
Python material
Ruchika Sinha
 
Python3
Python3Python3
Python3
Ruchika Sinha
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
Ruchika Sinha
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
Ruchika Sinha
 
Software Teting
Software TetingSoftware Teting
Software Teting
Ruchika Sinha
 
Backtrack search-algorithm
Backtrack search-algorithmBacktrack search-algorithm
Backtrack search-algorithm
Ruchika Sinha
 
XML
XMLXML
Software for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentationSoftware for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentation
Ruchika Sinha
 

More from Ruchika Sinha (20)

Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Clipping
ClippingClipping
Clipping
 
Lec22 intel
Lec22 intelLec22 intel
Lec22 intel
 
Pc assembly
Pc assemblyPc assembly
Pc assembly
 
Casing
CasingCasing
Casing
 
Installation of motherboard
Installation of motherboardInstallation of motherboard
Installation of motherboard
 
Shortest path
Shortest pathShortest path
Shortest path
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Python material
Python materialPython material
Python material
 
Python3
Python3Python3
Python3
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
 
Software Teting
Software TetingSoftware Teting
Software Teting
 
Backtrack search-algorithm
Backtrack search-algorithmBacktrack search-algorithm
Backtrack search-algorithm
 
XML
XMLXML
XML
 
Software for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentationSoftware for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentation
 

Recently uploaded

J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 

Recently uploaded (20)

J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 

Dijkstra.ppt

  • 2. Dijkstra's algorithm • Dijkstra's algorithm: finds shortest (minimum weight) path between a particular pair of vertices in a weighted directed graph with nonnegative edge weights – solves the "one vertex, shortest path" problem – basic algorithm concept: create a table of information about the currently known best way to reach each vertex (distance, previous vertex) and improve it until it reaches the best solution • in a graph where: – vertices represent cities, – edge weights represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and any other 2
  • 3. 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. 3
  • 4. 4 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) = 
  • 5. 5 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
  • 6. 6 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  
  • 7. 7 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
  • 8. 8 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
  • 9. 9 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
  • 10. 10 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
  • 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 6 5 Distance(F) = min (8, 5+1) = 6 Previous distance Pick vertex List with minimum distance (G) and update neighbors
  • 12. 12 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
  • 13. 13 Correctness • Dijkstra’s algorithm is a greedy algorithm – make choices that currently seem the best – locally optimal does not always mean globally optimal • Correct because maintains following two properties: – for every known vertex, recorded distance is shortest distance to that vertex from source vertex – for every unknown vertex v, its recorded distance is shortest path distance to v from source vertex, considering only currently known vertices and v
  • 14. 14 THE KNOWN CLOUD v Next shortest path from inside the known cloud v' “Cloudy” Proof: The Idea • 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
  • 15. 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. 15
  • 16. 16 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) • Reconstruct path O(|E|) Total time O(|V2| + |E|) = O(|V2| )
  • 17. 17 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 • Reconstruct path O(|E|) Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|) • |V| = O(|E|) assuming a connected graph
  • 18. 18 Dijkstra's Exercise A G F B E C D 20 10 50 40 20 80 50 20 30 20 90 H 10 10 10 • Use Dijkstra's algorithm to determine the lowest cost path from vertex A to all of the other vertices in the graph. Keep track of previous vertices so that you can reconstruct the path later.
  • 19. Minimum spanning tree • tree: a connected, directed acyclic graph • spanning tree: a subgraph of a graph, which meets the constraints to be a tree (connected, acyclic) and connects every vertex of the original graph • minimum spanning tree: a spanning tree with weight less than or equal to any other spanning tree for the given graph 19
  • 20. Min. span. tree applications • Consider a cable TV company laying cable to a new neighborhood... – If it is constrained to bury the cable only along certain paths, then there would be a graph representing which points are connected by those paths. – Some of those paths might be more expensive, because they are longer, or require the cable to be buried deeper. • These paths would be represented by edges with larger weights. – A spanning tree for that graph would be a subset of those paths that has no cycles but still connects to every house. • There might be several spanning trees possible. A minimum spanning tree would be one with the lowest total cost. 20