SlideShare a Scribd company logo
CSE 373: Data Structures and
Algorithms
Lecture 21: Graphs V
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
Ad

More Related Content

Similar to graph in Data Structures and Algorithm.ppt (20)

All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
P5 - Routing Protocols
P5 - Routing ProtocolsP5 - Routing Protocols
P5 - Routing Protocols
Kurniawan Dwi Irianto
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
SeethaDinesh
 
Lec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data StructureLec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data Structure
Anil Yadav
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptxSEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
bharatherltech
 
dms slide discrete mathematics sem 2 engineering
dms slide discrete mathematics sem 2 engineeringdms slide discrete mathematics sem 2 engineering
dms slide discrete mathematics sem 2 engineering
pranavstar99
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
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
 
Lecture 10 - Graph part 2.pptx,discrete mathemactics
Lecture 10 - Graph part 2.pptx,discrete mathemacticsLecture 10 - Graph part 2.pptx,discrete mathemactics
Lecture 10 - Graph part 2.pptx,discrete mathemactics
thuihue88
 
Algorithm Exam Help
Algorithm Exam Help Algorithm Exam Help
Algorithm Exam Help
Programming Exam Help
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
iftakhar8
 
graphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.pptgraphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.ppt
ssuser7b9bda1
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
Venkat Sai Sharath Mudhigonda
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
SeethaDinesh
 
Lec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data StructureLec-35Graph - Graph - Copy in Data Structure
Lec-35Graph - Graph - Copy in Data Structure
Anil Yadav
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptxSEMINAR ON SHORTEST PATH ALGORITHMS.pptx
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
bharatherltech
 
dms slide discrete mathematics sem 2 engineering
dms slide discrete mathematics sem 2 engineeringdms slide discrete mathematics sem 2 engineering
dms slide discrete mathematics sem 2 engineering
pranavstar99
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
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
 
Lecture 10 - Graph part 2.pptx,discrete mathemactics
Lecture 10 - Graph part 2.pptx,discrete mathemacticsLecture 10 - Graph part 2.pptx,discrete mathemactics
Lecture 10 - Graph part 2.pptx,discrete mathemactics
thuihue88
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
iftakhar8
 
graphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.pptgraphass1-23022111180722548-1ba6b00a.ppt
graphass1-23022111180722548-1ba6b00a.ppt
ssuser7b9bda1
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal
 

More from RAJASEKARAN G (20)

PREDICTION BRAIN STROKE using Machine LEarning.pptx
PREDICTION BRAIN STROKE using Machine LEarning.pptxPREDICTION BRAIN STROKE using Machine LEarning.pptx
PREDICTION BRAIN STROKE using Machine LEarning.pptx
RAJASEKARAN G
 
A Novel Network Intrusion Detection Sysy.pptx
A Novel Network Intrusion Detection Sysy.pptxA Novel Network Intrusion Detection Sysy.pptx
A Novel Network Intrusion Detection Sysy.pptx
RAJASEKARAN G
 
Frequent Item Set Mining by Rajasekaran.ppt
Frequent Item Set Mining by Rajasekaran.pptFrequent Item Set Mining by Rajasekaran.ppt
Frequent Item Set Mining by Rajasekaran.ppt
RAJASEKARAN G
 
_Introduction, comparison, and application of deep learning - Dr. Gopalakrish...
_Introduction, comparison, and application of deep learning - Dr. Gopalakrish..._Introduction, comparison, and application of deep learning - Dr. Gopalakrish...
_Introduction, comparison, and application of deep learning - Dr. Gopalakrish...
RAJASEKARAN G
 
engineergradprojectpiotrowskiword03-1232494818285266-3.ppt
engineergradprojectpiotrowskiword03-1232494818285266-3.pptengineergradprojectpiotrowskiword03-1232494818285266-3.ppt
engineergradprojectpiotrowskiword03-1232494818285266-3.ppt
RAJASEKARAN G
 
About Computer Science Engineering Course.ppt
About Computer Science Engineering Course.pptAbout Computer Science Engineering Course.ppt
About Computer Science Engineering Course.ppt
RAJASEKARAN G
 
session faculty development programme.pptx
session faculty development programme.pptxsession faculty development programme.pptx
session faculty development programme.pptx
RAJASEKARAN G
 
Power point Presentation FOR 1ST REVIEW.pptx
Power point Presentation FOR 1ST REVIEW.pptxPower point Presentation FOR 1ST REVIEW.pptx
Power point Presentation FOR 1ST REVIEW.pptx
RAJASEKARAN G
 
quantumComputers in Application of Qunatum Physics.ppt
quantumComputers in Application of Qunatum Physics.pptquantumComputers in Application of Qunatum Physics.ppt
quantumComputers in Application of Qunatum Physics.ppt
RAJASEKARAN G
 
Quantum Computing in Application of Qunatum Physics.pptx
Quantum Computing in Application of Qunatum Physics.pptxQuantum Computing in Application of Qunatum Physics.pptx
Quantum Computing in Application of Qunatum Physics.pptx
RAJASEKARAN G
 
Software Project Management UNIT 2.pptx
Software Project Management  UNIT 2.pptxSoftware Project Management  UNIT 2.pptx
Software Project Management UNIT 2.pptx
RAJASEKARAN G
 
Software Project Management UNIT 4.pptx
Software Project Management  UNIT 4.pptxSoftware Project Management  UNIT 4.pptx
Software Project Management UNIT 4.pptx
RAJASEKARAN G
 
bellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.pptbellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.ppt
RAJASEKARAN G
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
Warshalls Floyds Algorithm in Data Structure.ppt
Warshalls Floyds Algorithm in Data Structure.pptWarshalls Floyds Algorithm in Data Structure.ppt
Warshalls Floyds Algorithm in Data Structure.ppt
RAJASEKARAN G
 
Switching Technology_in_Computer_Networks.ppt
Switching Technology_in_Computer_Networks.pptSwitching Technology_in_Computer_Networks.ppt
Switching Technology_in_Computer_Networks.ppt
RAJASEKARAN G
 
data input technique Digitization and GPS.pptx
data input technique Digitization and GPS.pptxdata input technique Digitization and GPS.pptx
data input technique Digitization and GPS.pptx
RAJASEKARAN G
 
Addressing mapping protocol_ARPandRARP.ppt
Addressing mapping protocol_ARPandRARP.pptAddressing mapping protocol_ARPandRARP.ppt
Addressing mapping protocol_ARPandRARP.ppt
RAJASEKARAN G
 
Internet Protocol Version-4 Addressing.ppt
Internet Protocol Version-4 Addressing.pptInternet Protocol Version-4 Addressing.ppt
Internet Protocol Version-4 Addressing.ppt
RAJASEKARAN G
 
Types_of_Switching_Technique_in_Networks.ppt
Types_of_Switching_Technique_in_Networks.pptTypes_of_Switching_Technique_in_Networks.ppt
Types_of_Switching_Technique_in_Networks.ppt
RAJASEKARAN G
 
PREDICTION BRAIN STROKE using Machine LEarning.pptx
PREDICTION BRAIN STROKE using Machine LEarning.pptxPREDICTION BRAIN STROKE using Machine LEarning.pptx
PREDICTION BRAIN STROKE using Machine LEarning.pptx
RAJASEKARAN G
 
A Novel Network Intrusion Detection Sysy.pptx
A Novel Network Intrusion Detection Sysy.pptxA Novel Network Intrusion Detection Sysy.pptx
A Novel Network Intrusion Detection Sysy.pptx
RAJASEKARAN G
 
Frequent Item Set Mining by Rajasekaran.ppt
Frequent Item Set Mining by Rajasekaran.pptFrequent Item Set Mining by Rajasekaran.ppt
Frequent Item Set Mining by Rajasekaran.ppt
RAJASEKARAN G
 
_Introduction, comparison, and application of deep learning - Dr. Gopalakrish...
_Introduction, comparison, and application of deep learning - Dr. Gopalakrish..._Introduction, comparison, and application of deep learning - Dr. Gopalakrish...
_Introduction, comparison, and application of deep learning - Dr. Gopalakrish...
RAJASEKARAN G
 
engineergradprojectpiotrowskiword03-1232494818285266-3.ppt
engineergradprojectpiotrowskiword03-1232494818285266-3.pptengineergradprojectpiotrowskiword03-1232494818285266-3.ppt
engineergradprojectpiotrowskiword03-1232494818285266-3.ppt
RAJASEKARAN G
 
About Computer Science Engineering Course.ppt
About Computer Science Engineering Course.pptAbout Computer Science Engineering Course.ppt
About Computer Science Engineering Course.ppt
RAJASEKARAN G
 
session faculty development programme.pptx
session faculty development programme.pptxsession faculty development programme.pptx
session faculty development programme.pptx
RAJASEKARAN G
 
Power point Presentation FOR 1ST REVIEW.pptx
Power point Presentation FOR 1ST REVIEW.pptxPower point Presentation FOR 1ST REVIEW.pptx
Power point Presentation FOR 1ST REVIEW.pptx
RAJASEKARAN G
 
quantumComputers in Application of Qunatum Physics.ppt
quantumComputers in Application of Qunatum Physics.pptquantumComputers in Application of Qunatum Physics.ppt
quantumComputers in Application of Qunatum Physics.ppt
RAJASEKARAN G
 
Quantum Computing in Application of Qunatum Physics.pptx
Quantum Computing in Application of Qunatum Physics.pptxQuantum Computing in Application of Qunatum Physics.pptx
Quantum Computing in Application of Qunatum Physics.pptx
RAJASEKARAN G
 
Software Project Management UNIT 2.pptx
Software Project Management  UNIT 2.pptxSoftware Project Management  UNIT 2.pptx
Software Project Management UNIT 2.pptx
RAJASEKARAN G
 
Software Project Management UNIT 4.pptx
Software Project Management  UNIT 4.pptxSoftware Project Management  UNIT 4.pptx
Software Project Management UNIT 4.pptx
RAJASEKARAN G
 
bellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.pptbellman-ford dynamic algorithm in data structures.ppt
bellman-ford dynamic algorithm in data structures.ppt
RAJASEKARAN G
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
Warshalls Floyds Algorithm in Data Structure.ppt
Warshalls Floyds Algorithm in Data Structure.pptWarshalls Floyds Algorithm in Data Structure.ppt
Warshalls Floyds Algorithm in Data Structure.ppt
RAJASEKARAN G
 
Switching Technology_in_Computer_Networks.ppt
Switching Technology_in_Computer_Networks.pptSwitching Technology_in_Computer_Networks.ppt
Switching Technology_in_Computer_Networks.ppt
RAJASEKARAN G
 
data input technique Digitization and GPS.pptx
data input technique Digitization and GPS.pptxdata input technique Digitization and GPS.pptx
data input technique Digitization and GPS.pptx
RAJASEKARAN G
 
Addressing mapping protocol_ARPandRARP.ppt
Addressing mapping protocol_ARPandRARP.pptAddressing mapping protocol_ARPandRARP.ppt
Addressing mapping protocol_ARPandRARP.ppt
RAJASEKARAN G
 
Internet Protocol Version-4 Addressing.ppt
Internet Protocol Version-4 Addressing.pptInternet Protocol Version-4 Addressing.ppt
Internet Protocol Version-4 Addressing.ppt
RAJASEKARAN G
 
Types_of_Switching_Technique_in_Networks.ppt
Types_of_Switching_Technique_in_Networks.pptTypes_of_Switching_Technique_in_Networks.ppt
Types_of_Switching_Technique_in_Networks.ppt
RAJASEKARAN G
 
Ad

Recently uploaded (20)

"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Ad

graph in Data Structures and Algorithm.ppt

  • 1. CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1
  • 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