SlideShare a Scribd company logo
CS 332: Algorithms S-S Shortest Path:  Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis
Review:  Single-Source Shortest Path Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v “Shortest-path” = minimum weight  Weight of path is sum of edges E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?
Review: Shortest Path Properties Optimal substructure : the shortest path consists of shortest subpaths Let   (u,v) be the weight of the shortest path from u to v.  Shortest paths satisfy the  triangle inequality :   (u,v)      (u,x) +   (x,v) In graphs with negative weight cycles, some shortest paths will not exist
Review: Relaxation Key technique:  relaxation Maintain upper bound d[ v ] on   (s,v): Relax(u,v,w) {  if (d[v] > d[u]+w) then d[v]=d[u]+w; } 9 5 2 7 5 2 Relax 6 5 2 6 5 2 Relax
Review: Bellman-Ford Algorithm BellmanFord() for each v    V d[v] =   ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)    E Relax(u,v, w(u,v)); for each edge (u,v)    E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w Initialize d[], which will converge to  shortest-path value   Relaxation:  Make |V|-1 passes,  relaxing each edge Test for solution: have we converged yet? Ie,    negative cycle?
Review: Bellman-Ford Algorithm BellmanFord() for each v    V d[v] =   ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)    E Relax(u,v, w(u,v)); for each edge (u,v)    E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w What will be the  running time?
Review: Bellman-Ford Running time: O(VE) Not so good for large dense graphs But a very practical algorithm in many ways Note that order in which edges are processed affects how quickly it converges (show example)
DAG Shortest Paths Problem: finding shortest paths in DAG Bellman-Ford takes O(VE) time.  How can we do better? Idea: use topological sort.  How does it work again? If were lucky and processes vertices on each shortest path from left to right, would be done in one pass Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert).  Thus: just one pass.  What will be the running time?
Dijkstra’s Algorithm If no negative edge weights, we can beat BF Similar to breadth-first search Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim’s algorithm for MST Use a priority queue keyed on d[v]
Dijkstra’s Algorithm Dijkstra(G) for each v    V d[v] =   ; d[s] = 0; S =   ; Q = V; while (Q      ) u = ExtractMin(Q); S = S  U  {u}; for each v    u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Relaxation Step Note: this is really a  call to Q->DecreaseKey() B C D A 10 4 3 2 1 5 Ex: run the algorithm
The End

More Related Content

PPT
lecture 22
PPT
lecture 20
PDF
Longest common subsequence
PPT
Inroduction_To_Algorithms_Lect14
PDF
Bellman ford
PDF
Single source shortes path in dag
PDF
Shortest path algorithms
PDF
Johnson's algorithm
lecture 22
lecture 20
Longest common subsequence
Inroduction_To_Algorithms_Lect14
Bellman ford
Single source shortes path in dag
Shortest path algorithms
Johnson's algorithm

What's hot (20)

PPT
chapter24.ppt
PDF
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
PPT
Algorithm Design and Complexity - Course 9
PDF
Dynamics Kinematics Curvilinear Motion
PPT
Bellman ford algorithm
PPTX
Prims and kruskal algorithms
PPT
SINGLE-SOURCE SHORTEST PATHS
PPTX
Prim's algorithm
PPTX
Proyecto parcial iii_ proyecciones lineales
PPT
Single source stortest path bellman ford and dijkstra
PPTX
Minimum spanning tree algorithms by ibrahim_alfayoumi
PDF
Topological sorting
PPT
Shortest path
PPTX
Prims & kruskal algorithms
PDF
Shortest Path in Graph
PPT
Algorithm Design and Complexity - Course 8
PPTX
Shortest path algorithm
PDF
21 All Pairs Shortest Path
PPT
2.3 shortest path dijkstra’s
PPTX
Kruskal's algorithm
chapter24.ppt
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Algorithm Design and Complexity - Course 9
Dynamics Kinematics Curvilinear Motion
Bellman ford algorithm
Prims and kruskal algorithms
SINGLE-SOURCE SHORTEST PATHS
Prim's algorithm
Proyecto parcial iii_ proyecciones lineales
Single source stortest path bellman ford and dijkstra
Minimum spanning tree algorithms by ibrahim_alfayoumi
Topological sorting
Shortest path
Prims & kruskal algorithms
Shortest Path in Graph
Algorithm Design and Complexity - Course 8
Shortest path algorithm
21 All Pairs Shortest Path
2.3 shortest path dijkstra’s
Kruskal's algorithm
Ad

Viewers also liked (20)

PDF
20 Single Source Shorthest Path
DOCX
Santosh Sahu_MTech_CSE
PDF
Lec1 Algorthm
PPTX
PPT
Year 2 Organic Chemistry Mechanism and Stereochemistry Lecture 3 Conformation...
PPT
Mk ppt chapter 5
PPTX
Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures.
PPT
Disjoint sets
PDF
Introduction to the Internet of Things
PPT
ODP
Chapter25
PDF
Lecture2
PDF
Substructure Search Face-off
PDF
Lec2 Algorth
PPT
Advance Network Technologies
PPT
Sets and disjoint sets union123
PPTX
Conformational analysis
PPTX
Glutamate receptor
DOCX
Big data lecture notes
PPTX
Neuropharmacology: GABA & Glutamate
20 Single Source Shorthest Path
Santosh Sahu_MTech_CSE
Lec1 Algorthm
Year 2 Organic Chemistry Mechanism and Stereochemistry Lecture 3 Conformation...
Mk ppt chapter 5
Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures.
Disjoint sets
Introduction to the Internet of Things
Chapter25
Lecture2
Substructure Search Face-off
Lec2 Algorth
Advance Network Technologies
Sets and disjoint sets union123
Conformational analysis
Glutamate receptor
Big data lecture notes
Neuropharmacology: GABA & Glutamate
Ad

Similar to lecture 21 (20)

PPT
Algorithm Design and Complexity - Course 10
PPT
Dijkstra algorithm ds 57612334t4t44.ppt
PPT
Dijkstra Shortest Path Algorithm in Network.ppt
PPT
Dijksatra
PPT
Single Source Shortest Path Algorithm.ppt
PPTX
lec6.pptx
PDF
Daa chpater14
PPT
Dijkstra c
PPT
Lec-35Graph - Graph - Copy in Data Structure
PPT
Shortest path
PDF
Shortest Paths Part 2: Negative Weights and All-pairs
PPTX
Bellman ford Algorithm
PPT
2.6 all pairsshortestpath
PPT
dijkstraC.ppt
DOCX
Shortest Path Problem.docx
PPT
SINGLE SOURCE SHORTEST PATH.ppt
PPT
bellman-ford Theorem.ppt
PPT
Unit 5 session 2 MinimumSpanningTrees.ppt
PPT
Unit26 shortest pathalgorithm
PPTX
All pair shortest path by Sania Nisar
Algorithm Design and Complexity - Course 10
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
Dijksatra
Single Source Shortest Path Algorithm.ppt
lec6.pptx
Daa chpater14
Dijkstra c
Lec-35Graph - Graph - Copy in Data Structure
Shortest path
Shortest Paths Part 2: Negative Weights and All-pairs
Bellman ford Algorithm
2.6 all pairsshortestpath
dijkstraC.ppt
Shortest Path Problem.docx
SINGLE SOURCE SHORTEST PATH.ppt
bellman-ford Theorem.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit26 shortest pathalgorithm
All pair shortest path by Sania Nisar

More from sajinsc (20)

PPT
lecture 30
PPT
lecture 29
PPT
lecture 28
PPT
lecture 27
PPT
lecture 26
PPT
lecture 25
PPT
lecture 24
PPT
lecture 23
PPT
lecture 19
PPT
lecture 18
PPT
lecture 17
PPT
lecture 16
PPT
lecture 15
PPT
lecture 14
PPT
lecture 13
PPT
lecture 12
PPT
lecture 11
PPT
lecture 10
PPT
lecture 9
PPT
lecture 8
lecture 30
lecture 29
lecture 28
lecture 27
lecture 26
lecture 25
lecture 24
lecture 23
lecture 19
lecture 18
lecture 17
lecture 16
lecture 15
lecture 14
lecture 13
lecture 12
lecture 11
lecture 10
lecture 9
lecture 8

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Insiders guide to clinical Medicine.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
TR - Agricultural Crops Production NC III.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Pre independence Education in Inndia.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Week 4 Term 3 Study Techniques revisited.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
Renaissance Architecture: A Journey from Faith to Humanism

lecture 21

  • 1. CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis
  • 2. Review: Single-Source Shortest Path Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v “Shortest-path” = minimum weight Weight of path is sum of edges E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?
  • 3. Review: Shortest Path Properties Optimal substructure : the shortest path consists of shortest subpaths Let  (u,v) be the weight of the shortest path from u to v. Shortest paths satisfy the triangle inequality :  (u,v)   (u,x) +  (x,v) In graphs with negative weight cycles, some shortest paths will not exist
  • 4. Review: Relaxation Key technique: relaxation Maintain upper bound d[ v ] on  (s,v): Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w; } 9 5 2 7 5 2 Relax 6 5 2 6 5 2 Relax
  • 5. Review: Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w Initialize d[], which will converge to shortest-path value  Relaxation: Make |V|-1 passes, relaxing each edge Test for solution: have we converged yet? Ie,  negative cycle?
  • 6. Review: Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w What will be the running time?
  • 7. Review: Bellman-Ford Running time: O(VE) Not so good for large dense graphs But a very practical algorithm in many ways Note that order in which edges are processed affects how quickly it converges (show example)
  • 8. DAG Shortest Paths Problem: finding shortest paths in DAG Bellman-Ford takes O(VE) time. How can we do better? Idea: use topological sort. How does it work again? If were lucky and processes vertices on each shortest path from left to right, would be done in one pass Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert). Thus: just one pass. What will be the running time?
  • 9. Dijkstra’s Algorithm If no negative edge weights, we can beat BF Similar to breadth-first search Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim’s algorithm for MST Use a priority queue keyed on d[v]
  • 10. Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Relaxation Step Note: this is really a call to Q->DecreaseKey() B C D A 10 4 3 2 1 5 Ex: run the algorithm