SlideShare a Scribd company logo
1 of 17
Analysis and Design of Algorithm
B. Tech. (CSE), VII Semester
Roshan Tailor
Amity University Rajasthan
2
This Lecture
• Single-source shortest paths in weighted graphs
– Shortest-Path Problems
– Properties of Shortest Paths, Relaxation
– Dijkstra’s Algorithm
– Bellman-Ford Algorithm
3
Shortest Path
• Generalize distance to weighted setting
• Digraph G = (V,E) with weight function W: E →
R (assigning real values to edges)
• Weight of path p = v1 → v2 → … → vk is
• Shortest path = a path of the minimum weight
• Applications
– static/dynamic network routing
– robot motion planning
– map/route generation in traffic
1
1
1
( ) ( , )
k
i i
i
w p w v v
−
+
=
= ∑
4
Shortest-Path Problems
• Shortest-Path problems
– Single-source (single-destination). Find a shortest
path from a given source (vertex s) to each of the
vertices.
– Single-pair. Given two vertices, find a shortest path
between them. Solution to single-source problem
solves this problem efficiently, too.
– All-pairs. Find shortest-paths for every pair of
vertices. Dynamic programming algorithm.
5
Negative Weights and Cycles?
• Negative edges are OK, as long as there are no
negative weight cycles (otherwise paths with
arbitrary small “lengths” would be possible)
• Shortest-paths can have no cycles (otherwise
we could improve them by removing cycles)
– Any shortest-path in graph G can be no longer than
n – 1 edges, where n is the number of vertices
6
Relaxation
• For each vertex v in the graph, we maintain
v.d(), the estimate of the shortest path from s,
initialized to ∞ at the start
• Relaxing an edge (u,v) means testing whether
we can improve the shortest path to v found so
far by going through u
5
u v
vu
2
2
9
5 7
Relax(u,v)
5
u v
vu
2
2
6
5 6
Relax(u,v)
Relax (u,v,G)
if v.d > u.d()+ w(u,v) then
v.d = (u.d()+ w(u,v))
v.setparent = u
7
Dijkstra's Algorithm
• Non-negative edge weights
• Greedy, similar to Prim's algorithm for MST
• Like breadth-first search (if all weights = 1, one
can simply use BFS)
• Use Q, a priority queue ADT keyed by v.d()
(BFS used FIFO queue, here we use a PQ,
which is re-organized whenever some d
decreases)
• Basic idea
– maintain a set S of solved vertices
– at each step select "closest" vertex u, add it to S, and
relax all edges from u
8
Dijkstra’s Pseudo Code
9
Dijkstra’s Example
∞ ∞
∞ ∞
0s
u v
yx
10
5
1
2 3
9
4 6
7
2
10 ∞
5 ∞
0s
u v
yx
10
5
1
2 3
9
4 6
7
2
10
Dijkstra’s Example (2)
u v
8 14
5 7
0s
yx
10
5
1
2 3
9
4 6
7
2
8 13
5 7
0s
u v
yx
10
5
1
2 3
9
4 6
7
2
11
Dijkstra’s Example (3)
8 9
5 7
0
u v
yx
10
5
1
2 3
9
4 6
7
2
8 9
5 7
0
u v
yx
10
5
1
2 3
9
4 6
7
2
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-
Min)
T(Decrease-Key) Total
array Ο(V) Ο(1) Ο(V 2
)
binary heap Ο(lg V) Ο(lg V) Ο(E lg V)
Fibonacci heap Ο(lg V) Ο(1) (amort.) Ο(V lgV + E)
13
Bellman-Ford Algorithm
• Dijkstra’s doesn’t work when there are negative
edges:
– Intuition – we can not be greedy any more on the
assumption that the lengths of paths will only increase
in the future
• Bellman-Ford algorithm detects negative cycles
(returns false) or returns the shortest path-tree
14
Bellman-Ford Algorithm
15
Bellman-Ford Example
5
∞ ∞
∞ ∞
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
6 ∞
7 ∞
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
6 4
7 2
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
2 4
7 2
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
16
Bellman-Ford Example
2 4
7 −2
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
• Bellman-Ford running time:
– (|V|-1)|E| + |E| = Θ(VE)
5
Bellman-Ford Example
17

More Related Content

What's hot

Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithmami_01
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmArijitDhali
 
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 treeoneous
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
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
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithmgsp1294
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approachAkash Sethiya
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's AlgorithmTanmay Baranwal
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmA. S. M. Shafi
 

What's hot (20)

Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
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
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's 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)
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 

Viewers also liked

Bellman ford algorithm -Shortest Path
Bellman ford algorithm -Shortest PathBellman ford algorithm -Shortest Path
Bellman ford algorithm -Shortest PathSharmilaChidaravalli
 
Bellman ford (part-ii)
Bellman ford (part-ii)Bellman ford (part-ii)
Bellman ford (part-ii)Zain Zahid
 
Bellman ford (part-i)
Bellman ford (part-i)Bellman ford (part-i)
Bellman ford (part-i)Zain Zahid
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqmontaser185
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithmguest862df4e
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Jaimin chp-5 - network layer- 2011 batch
Jaimin   chp-5 - network layer- 2011 batchJaimin   chp-5 - network layer- 2011 batch
Jaimin chp-5 - network layer- 2011 batchJaimin Jani
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaMalinga Perera
 
Shortest path problem
Shortest path problemShortest path problem
Shortest path problemIfra Ilyas
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 
Computer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithmComputer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithmRAJEEV KUMAR SINGH
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
Shortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma DijkstraShortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma DijkstraOnggo Wiryawan
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to PseudocodeDamian T. Gordon
 
Bellman ford
Bellman fordBellman ford
Bellman fordbidil
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoJay Patel
 

Viewers also liked (20)

Bellman ford algorithm -Shortest Path
Bellman ford algorithm -Shortest PathBellman ford algorithm -Shortest Path
Bellman ford algorithm -Shortest Path
 
Bellman ford (part-ii)
Bellman ford (part-ii)Bellman ford (part-ii)
Bellman ford (part-ii)
 
Bellman ford (part-i)
Bellman ford (part-i)Bellman ford (part-i)
Bellman ford (part-i)
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Jaimin chp-5 - network layer- 2011 batch
Jaimin   chp-5 - network layer- 2011 batchJaimin   chp-5 - network layer- 2011 batch
Jaimin chp-5 - network layer- 2011 batch
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - Malinga
 
Bellmanford
BellmanfordBellmanford
Bellmanford
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
Shortest path problem
Shortest path problemShortest path problem
Shortest path problem
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Computer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithmComputer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithm
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Shortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma DijkstraShortest Path Problem: Algoritma Dijkstra
Shortest Path Problem: Algoritma Dijkstra
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algo
 

Similar to Single source stortest path bellman ford and dijkstra

04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Naor Ami
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
Design and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problemDesign and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problempooja saini
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 

Similar to Single source stortest path bellman ford and dijkstra (20)

Shortest path
Shortest pathShortest path
Shortest path
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Graph 3
Graph 3Graph 3
Graph 3
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
(148065320) dijistra algo
(148065320) dijistra algo(148065320) dijistra algo
(148065320) dijistra algo
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
lecture 21
lecture 21lecture 21
lecture 21
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
dijkstra algo.ppt
dijkstra algo.pptdijkstra algo.ppt
dijkstra algo.ppt
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Design and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problemDesign and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problem
 
dijkstraC.ppt
dijkstraC.pptdijkstraC.ppt
dijkstraC.ppt
 
CSE633
CSE633CSE633
CSE633
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 

Recently uploaded

Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 

Single source stortest path bellman ford and dijkstra

  • 1. Analysis and Design of Algorithm B. Tech. (CSE), VII Semester Roshan Tailor Amity University Rajasthan
  • 2. 2 This Lecture • Single-source shortest paths in weighted graphs – Shortest-Path Problems – Properties of Shortest Paths, Relaxation – Dijkstra’s Algorithm – Bellman-Ford Algorithm
  • 3. 3 Shortest Path • Generalize distance to weighted setting • Digraph G = (V,E) with weight function W: E → R (assigning real values to edges) • Weight of path p = v1 → v2 → … → vk is • Shortest path = a path of the minimum weight • Applications – static/dynamic network routing – robot motion planning – map/route generation in traffic 1 1 1 ( ) ( , ) k i i i w p w v v − + = = ∑
  • 4. 4 Shortest-Path Problems • Shortest-Path problems – Single-source (single-destination). Find a shortest path from a given source (vertex s) to each of the vertices. – Single-pair. Given two vertices, find a shortest path between them. Solution to single-source problem solves this problem efficiently, too. – All-pairs. Find shortest-paths for every pair of vertices. Dynamic programming algorithm.
  • 5. 5 Negative Weights and Cycles? • Negative edges are OK, as long as there are no negative weight cycles (otherwise paths with arbitrary small “lengths” would be possible) • Shortest-paths can have no cycles (otherwise we could improve them by removing cycles) – Any shortest-path in graph G can be no longer than n – 1 edges, where n is the number of vertices
  • 6. 6 Relaxation • For each vertex v in the graph, we maintain v.d(), the estimate of the shortest path from s, initialized to ∞ at the start • Relaxing an edge (u,v) means testing whether we can improve the shortest path to v found so far by going through u 5 u v vu 2 2 9 5 7 Relax(u,v) 5 u v vu 2 2 6 5 6 Relax(u,v) Relax (u,v,G) if v.d > u.d()+ w(u,v) then v.d = (u.d()+ w(u,v)) v.setparent = u
  • 7. 7 Dijkstra's Algorithm • Non-negative edge weights • Greedy, similar to Prim's algorithm for MST • Like breadth-first search (if all weights = 1, one can simply use BFS) • Use Q, a priority queue ADT keyed by v.d() (BFS used FIFO queue, here we use a PQ, which is re-organized whenever some d decreases) • Basic idea – maintain a set S of solved vertices – at each step select "closest" vertex u, add it to S, and relax all edges from u
  • 9. 9 Dijkstra’s Example ∞ ∞ ∞ ∞ 0s u v yx 10 5 1 2 3 9 4 6 7 2 10 ∞ 5 ∞ 0s u v yx 10 5 1 2 3 9 4 6 7 2
  • 10. 10 Dijkstra’s Example (2) u v 8 14 5 7 0s yx 10 5 1 2 3 9 4 6 7 2 8 13 5 7 0s u v yx 10 5 1 2 3 9 4 6 7 2
  • 11. 11 Dijkstra’s Example (3) 8 9 5 7 0 u v yx 10 5 1 2 3 9 4 6 7 2 8 9 5 7 0 u v yx 10 5 1 2 3 9 4 6 7 2
  • 12. 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- Min) T(Decrease-Key) Total array Ο(V) Ο(1) Ο(V 2 ) binary heap Ο(lg V) Ο(lg V) Ο(E lg V) Fibonacci heap Ο(lg V) Ο(1) (amort.) Ο(V lgV + E)
  • 13. 13 Bellman-Ford Algorithm • Dijkstra’s doesn’t work when there are negative edges: – Intuition – we can not be greedy any more on the assumption that the lengths of paths will only increase in the future • Bellman-Ford algorithm detects negative cycles (returns false) or returns the shortest path-tree
  • 15. 15 Bellman-Ford Example 5 ∞ ∞ ∞ ∞ 0s zy 6 7 8 -3 7 2 9 -2 xt -4 6 ∞ 7 ∞ 0s zy 6 7 8 -3 7 2 9 -2 xt -4 5 6 4 7 2 0s zy 6 7 8 -3 7 2 9 -2 xt -4 5 2 4 7 2 0s zy 6 7 8 -3 7 2 9 -2 xt -4 5
  • 16. 16 Bellman-Ford Example 2 4 7 −2 0s zy 6 7 8 -3 7 2 9 -2 xt -4 • Bellman-Ford running time: – (|V|-1)|E| + |E| = Θ(VE) 5