SlideShare a Scribd company logo
Single-Source Shortest Paths in a
Directed Acyclic Graph
Dr. Kiran K
Assistant Professor
Department of CSE
UVCE
Bengaluru, India.
Algorithm
Given a Weighted, Directed Acyclic Graph (DAG) G = (V, E), the Shortest Paths
from a single source is computed by Relaxing the edges according to a Topological
Sort of its vertices.
TOPOLOGICAL-SORT (G)
Call DFS (G) to compute finishing times
v.f for each vertex v.
As each vertex is finished, insert it onto
the front of a linked list.
Return the linked list of vertices.
DAG-SHORTEST-PATHS (G, w, s)
Topologically Sort the vertices of G
INITIALIZE-SINGLE-SOURCE (G, s)
For each vertex u, taken in
Topologically Sorted order
For each vertex v є G.Adj [u]
RELAX (u, v, w)
Algorithm…
Running Time:
Topological Sort - Ө (V + E)
INITIALIZE-SINGLE-SOURCE - Ө (V)
Relax - Ө (1)
DAG-SHORTEST-PATHS - Ө (V + E)
(The algorithm relaxes all the edges
exactly once in the for loop for all the
vertices in the graph.)
INITIALIZE-SINGLE-SOURCE (G, s)
For (Each Vertex v є G.V)
v.d = ꝏ
v.π = NIL
s.d = 0
RELAX (u, v, w)
If (v.d > (u.d + w (u, v))
v.d = u.d + w (u, v)
v.π = u
• v.d : Shortest Path Estimate; Estimate of the shortest path from the source s to a
vertex v.
• v.π : Predecessor of v in the path from s to v.
• v.f : Timestamp recording when the search finishes examining v’s adjacency list
• δ (s, v) : Shortest path from the source s to vertex v.
• Relax (u, v) : Process of testing whether the shortest path to v (from s) found so far
can be improved by going through u and if so, updating v.d and v.π accordingly.
Note:
• Relax (u, v) is equivalent to finding whether the shortest path to v found so far is
minimum or the path through u is minimum. i.e,
v.d = min (v.d, u.d + w (u, v))
Algorithm…
Example
Vertex: r
Relax (r, s): s.d = min (s.d, r.d + w (r, s)) = min (0, ꝏ + 5) = 0
Relax (r, t): t.d = min (t.d, r.d + w (r, t)) = min (ꝏ, ꝏ + 3) = ꝏ
Example…
Vertex: s
Relax (s, t): t.d = min (t.d, s.d + w (s, t)) = min (ꝏ, 0 + 2) = 2; t.π = s
Relax (s, x): x.d = min (x.d, s.d + w (s, x)) = min (ꝏ, 0 + 6) = 6; x.π = s
Example…
Vertex: t
Relax (t, x): x.d = min (x.d, t.d + w (t, x)) = min (6, 2 + 7) = 6
Relax (t, y): y.d = min (y.d, t.d + w (t, y)) = min (ꝏ, 2 + 4) = 6; y.π = t
Relax (t, z): z.d = min (z.d, t.d + w (t, z)) = min (ꝏ, 2 + 2) = 4; z.π = t
Example…
Vertex: x
Relax (x, y): y.d = min (y.d, x.d + w (x, y)) = min (6, 6 + (-1)) = 5; y.π = x
Relax (x, z): z.d = min (z.d, x.d + w (x, z)) = min (4, 6 + 1) = 4
Example…
Vertex: y
Relax (y, z): v.z = min (v.z, v.y + w (y, z)) = min (4, 5 + (-2)) = 3; z.π = y
Example…
Example…
The Shortest Paths from source s to all the vertices is:
Destination Path Cost
r No Path
r.π = NIL
ꝏ
s - 0
t s → t
t.π = s
2
x s → x
x.π = s
6
y s → x → y
x.π = s, y.π = x
5
(6 + (-1))
z s → x → y → z
x.π = s, y.π = x, z.π = y
3
(6 + (-1) + (-2))
If a weighted, directed graph G = (V, E) has source vertex s and no cycles, then at the
termination of the DAG-SHORTEST-PATHS procedure, v.d = δ (s, v) for all vertices v є V,
and the predecessor subgraph Gπ is a Shortest-Paths Tree.
Proof:
(1) v.d = δ (s, v):
(i) If v is Not Reachable from s
v.d = δ (s, v) = ꝏ (No-Path Property) (T1)
(ii) If v Reachable from s
Let p = <v0, v1, . . . , vk>, v0 = s and vk = v be the Shortest Path from s to v.
The algorithm processes the vertices in Topologically Sorted Order (T2)
(T2) → The edges are relaxed in the order (v0, v1), (v1, v2), . . . , (vk - 1, vk) (T3)
(T3) → vi.d = δ (s, vi) for i = 1, 2, . . . , k (Path-Relaxation Property) (T4)
(T1) & (T4) → v.d = δ (s, v) (T5)
(2) Gπ is a Shortest-Paths Tree:
(T5) → Gπ is a shortest-paths tree rooted at s (Predecessor-Subgraph Property)
Theorem
Determining Critical Paths in PERT (Program Evaluation and Review Technique) chart
analysis.
• Edges represent Jobs to be performed.
• Edge Weights represent the Times required to perform particular jobs.
• If edge (u, v) enters vertex v and edge (v, x) leaves v, then job (u, v) must be performed
before job (v, x).
• A Path through this dag represents a Sequence of Jobs that must be performed in a particular
order.
• Critical path
 Longest path through the dag, corresponding to the longest time to perform any sequence of jobs.
 Thus, the weight of a critical path provides a lower bound on the total time to perform all the
jobs.
 Finding a critical path:
(1) Negating the edge weights and running DAG-SHORTEST-PATHS, or
(2) Replacing ꝏ by - ꝏ in INITIALIZE-SINGLE-SOURCE, > by < in RELAX and running
DAG-SHORTEST-PATHS
Application
u v x
Appendix
No-path property
If there is no path from s to v, then v.d = δ (s, v) = ꝏ always.
Path-relaxation property
If p = <v0, v1, . . . , vk> is a shortest path from s = v0 to vk , then vk.d = δ (s, vk) if the
edges of p are Relaxed in the order (v0, v1), (v1, v2), . . . , (vk - 1, vk).
Predecessor-subgraph property
Once v.d = δ (s, v) for all v є V , the predecessor subgraph is a shortest-paths tree
rooted at s.
References:
• Thomas H Cormen. Charles E Leiserson, Ronald L Rivest, Clifford Stein,
Introduction to Algorithms, Third Edition, The MIT Press Cambridge,
Massachusetts London, England.

More Related Content

What's hot

lecture 22
lecture 22lecture 22
lecture 22sajinsc
 
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 20
lecture 20lecture 20
lecture 20sajinsc
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpathCarlostheran
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Traian Rebedea
 
Introduccio al calculo vectorial
Introduccio  al calculo vectorialIntroduccio  al calculo vectorial
Introduccio al calculo vectorial
EDESMITCRUZ1
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Graph for Coulomb damped oscillation
Graph for Coulomb damped oscillationGraph for Coulomb damped oscillation
Graph for Coulomb damped oscillation
phanhung20
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
Dhaval Shukla
 
Linear transformation.ppt
Linear transformation.pptLinear transformation.ppt
Linear transformation.ppt
Raj Parekh
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
Ibrahim Alfayoumi
 
Algorithm Design and Complexity - Course 11
Algorithm Design and Complexity - Course 11Algorithm Design and Complexity - Course 11
Algorithm Design and Complexity - Course 11Traian Rebedea
 
linear transformation and rank nullity theorem
linear transformation and rank nullity theorem linear transformation and rank nullity theorem
linear transformation and rank nullity theorem
Manthan Chavda
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Traian Rebedea
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
Amit Kumar Rathi
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
Linear transformations and matrices
Linear transformations and matricesLinear transformations and matrices
Linear transformations and matrices
EasyStudy3
 
Isomorphism
IsomorphismIsomorphism
Isomorphism
Raj Parekh
 

What's hot (20)

lecture 22
lecture 22lecture 22
lecture 22
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
lecture 20
lecture 20lecture 20
lecture 20
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
Introduccio al calculo vectorial
Introduccio  al calculo vectorialIntroduccio  al calculo vectorial
Introduccio al calculo vectorial
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Graph for Coulomb damped oscillation
Graph for Coulomb damped oscillationGraph for Coulomb damped oscillation
Graph for Coulomb damped oscillation
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
 
Linear transformation.ppt
Linear transformation.pptLinear transformation.ppt
Linear transformation.ppt
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 
Algorithm Design and Complexity - Course 11
Algorithm Design and Complexity - Course 11Algorithm Design and Complexity - Course 11
Algorithm Design and Complexity - Course 11
 
linear transformation and rank nullity theorem
linear transformation and rank nullity theorem linear transformation and rank nullity theorem
linear transformation and rank nullity theorem
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Linear transformations and matrices
Linear transformations and matricesLinear transformations and matrices
Linear transformations and matrices
 
Isomorphism
IsomorphismIsomorphism
Isomorphism
 

Similar to Single source shortes path in dag

Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
Temporal graph
Temporal graphTemporal graph
Temporal graph
Vinay Sarda
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint paths
Sujith Jay Nair
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
Hanif Durad
 
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
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
Roshan Tailor
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
Shortest path
Shortest pathShortest path
Shortest path
Ruchika Sinha
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
SeethaDinesh
 
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
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
Nv Thejaswini
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 

Similar to Single source shortes path in dag (20)

Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Temporal graph
Temporal graphTemporal graph
Temporal graph
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint paths
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
 
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
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Shortest path
Shortest pathShortest path
Shortest path
 
Compactrouting
CompactroutingCompactrouting
Compactrouting
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
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
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Dijksatra
DijksatraDijksatra
Dijksatra
 

Recently uploaded

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Single source shortes path in dag

  • 1. Single-Source Shortest Paths in a Directed Acyclic Graph Dr. Kiran K Assistant Professor Department of CSE UVCE Bengaluru, India.
  • 2. Algorithm Given a Weighted, Directed Acyclic Graph (DAG) G = (V, E), the Shortest Paths from a single source is computed by Relaxing the edges according to a Topological Sort of its vertices. TOPOLOGICAL-SORT (G) Call DFS (G) to compute finishing times v.f for each vertex v. As each vertex is finished, insert it onto the front of a linked list. Return the linked list of vertices. DAG-SHORTEST-PATHS (G, w, s) Topologically Sort the vertices of G INITIALIZE-SINGLE-SOURCE (G, s) For each vertex u, taken in Topologically Sorted order For each vertex v є G.Adj [u] RELAX (u, v, w)
  • 3. Algorithm… Running Time: Topological Sort - Ө (V + E) INITIALIZE-SINGLE-SOURCE - Ө (V) Relax - Ө (1) DAG-SHORTEST-PATHS - Ө (V + E) (The algorithm relaxes all the edges exactly once in the for loop for all the vertices in the graph.) INITIALIZE-SINGLE-SOURCE (G, s) For (Each Vertex v є G.V) v.d = ꝏ v.π = NIL s.d = 0 RELAX (u, v, w) If (v.d > (u.d + w (u, v)) v.d = u.d + w (u, v) v.π = u
  • 4. • v.d : Shortest Path Estimate; Estimate of the shortest path from the source s to a vertex v. • v.π : Predecessor of v in the path from s to v. • v.f : Timestamp recording when the search finishes examining v’s adjacency list • δ (s, v) : Shortest path from the source s to vertex v. • Relax (u, v) : Process of testing whether the shortest path to v (from s) found so far can be improved by going through u and if so, updating v.d and v.π accordingly. Note: • Relax (u, v) is equivalent to finding whether the shortest path to v found so far is minimum or the path through u is minimum. i.e, v.d = min (v.d, u.d + w (u, v)) Algorithm…
  • 5. Example Vertex: r Relax (r, s): s.d = min (s.d, r.d + w (r, s)) = min (0, ꝏ + 5) = 0 Relax (r, t): t.d = min (t.d, r.d + w (r, t)) = min (ꝏ, ꝏ + 3) = ꝏ
  • 6. Example… Vertex: s Relax (s, t): t.d = min (t.d, s.d + w (s, t)) = min (ꝏ, 0 + 2) = 2; t.π = s Relax (s, x): x.d = min (x.d, s.d + w (s, x)) = min (ꝏ, 0 + 6) = 6; x.π = s
  • 7. Example… Vertex: t Relax (t, x): x.d = min (x.d, t.d + w (t, x)) = min (6, 2 + 7) = 6 Relax (t, y): y.d = min (y.d, t.d + w (t, y)) = min (ꝏ, 2 + 4) = 6; y.π = t Relax (t, z): z.d = min (z.d, t.d + w (t, z)) = min (ꝏ, 2 + 2) = 4; z.π = t
  • 8. Example… Vertex: x Relax (x, y): y.d = min (y.d, x.d + w (x, y)) = min (6, 6 + (-1)) = 5; y.π = x Relax (x, z): z.d = min (z.d, x.d + w (x, z)) = min (4, 6 + 1) = 4
  • 9. Example… Vertex: y Relax (y, z): v.z = min (v.z, v.y + w (y, z)) = min (4, 5 + (-2)) = 3; z.π = y
  • 11. Example… The Shortest Paths from source s to all the vertices is: Destination Path Cost r No Path r.π = NIL ꝏ s - 0 t s → t t.π = s 2 x s → x x.π = s 6 y s → x → y x.π = s, y.π = x 5 (6 + (-1)) z s → x → y → z x.π = s, y.π = x, z.π = y 3 (6 + (-1) + (-2))
  • 12. If a weighted, directed graph G = (V, E) has source vertex s and no cycles, then at the termination of the DAG-SHORTEST-PATHS procedure, v.d = δ (s, v) for all vertices v є V, and the predecessor subgraph Gπ is a Shortest-Paths Tree. Proof: (1) v.d = δ (s, v): (i) If v is Not Reachable from s v.d = δ (s, v) = ꝏ (No-Path Property) (T1) (ii) If v Reachable from s Let p = <v0, v1, . . . , vk>, v0 = s and vk = v be the Shortest Path from s to v. The algorithm processes the vertices in Topologically Sorted Order (T2) (T2) → The edges are relaxed in the order (v0, v1), (v1, v2), . . . , (vk - 1, vk) (T3) (T3) → vi.d = δ (s, vi) for i = 1, 2, . . . , k (Path-Relaxation Property) (T4) (T1) & (T4) → v.d = δ (s, v) (T5) (2) Gπ is a Shortest-Paths Tree: (T5) → Gπ is a shortest-paths tree rooted at s (Predecessor-Subgraph Property) Theorem
  • 13. Determining Critical Paths in PERT (Program Evaluation and Review Technique) chart analysis. • Edges represent Jobs to be performed. • Edge Weights represent the Times required to perform particular jobs. • If edge (u, v) enters vertex v and edge (v, x) leaves v, then job (u, v) must be performed before job (v, x). • A Path through this dag represents a Sequence of Jobs that must be performed in a particular order. • Critical path  Longest path through the dag, corresponding to the longest time to perform any sequence of jobs.  Thus, the weight of a critical path provides a lower bound on the total time to perform all the jobs.  Finding a critical path: (1) Negating the edge weights and running DAG-SHORTEST-PATHS, or (2) Replacing ꝏ by - ꝏ in INITIALIZE-SINGLE-SOURCE, > by < in RELAX and running DAG-SHORTEST-PATHS Application u v x
  • 14. Appendix No-path property If there is no path from s to v, then v.d = δ (s, v) = ꝏ always. Path-relaxation property If p = <v0, v1, . . . , vk> is a shortest path from s = v0 to vk , then vk.d = δ (s, vk) if the edges of p are Relaxed in the order (v0, v1), (v1, v2), . . . , (vk - 1, vk). Predecessor-subgraph property Once v.d = δ (s, v) for all v є V , the predecessor subgraph is a shortest-paths tree rooted at s.
  • 15. References: • Thomas H Cormen. Charles E Leiserson, Ronald L Rivest, Clifford Stein, Introduction to Algorithms, Third Edition, The MIT Press Cambridge, Massachusetts London, England.