SlideShare a Scribd company logo
1 of 28
Data Structures and Algorithms
Graphs
Single-Source Shortest Paths
(Dijkstra’s Algorithm)
PLSD210(ii)
Graphs - Shortest Paths
• Application
• In a graph in which edges have costs ..
• Find the shortest path from a source to a destination
• Surprisingly ..
• While finding the shortest path from a source to one
destination,
• we can find the shortest paths to all over destinations
as well!
• Common algorithm for
single-source shortest paths
is due to Edsger Dijkstra
Dijkstra’s Algorithm - Data Structures
• For a graph,
G = ( V, E )
• Dijkstra’s algorithm keeps two sets of vertices:
S Vertices whose shortest paths have already been
determined
V-S Remainder
• Also
d Best estimates of shortest path to each vertex
p Predecessors for each vertex
Predecessor Sub-graph
• Array of vertex indices, p[j], j = 1 .. |V|
• p[j] contains the pre-decessor for node j
• j’s predecessor is in p[p[j]], and so on ....
• The edges in the pre-decessor sub-graph are
( p[j], j )
Dijkstra’s Algorithm - Operation
• Initialise d and p
• For each vertex, j, in V
• dj = 
 pj = nil
• Source distance, ds = 0
• Set S to empty
• While V-S is not empty
• Sort V-S based on d
• Add u, the closest vertex in V-S, to S
• Relax all the vertices still in V-S connected to u
Initial estimates are all 
No connections
Add s first!
Dijkstra’s Algorithm - Operation
• Initialise d and p
• For each vertex, j, in V
• dj = 
 pj = nil
• Source distance, ds = 0
• Set S to empty
• While V-S is not empty
• Sort V-S based on d
• Add u, the closest vertex in V-S, to S
• Relax all the vertices still in V-S connected to u
Initial estimates are all 
No connections
Add s first!
Dijkstra’s Algorithm - Operation
• The Relaxation process
Relax the node v
attached to node u
relax( Node u, Node v, double w[][] )
if d[v] > d[u] + w[u,v] then
d[v] := d[u] + w[u,v]
pi[v] := u
If the current best
estimate to v is
greater than the
path through u ..
Edge cost matrix
Update the
estimate to v
Make v’s predecessor
point to u
Dijkstra’s Algorithm - Full
• The Shortest Paths algorithm
Given a graph, g, and a source, s
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Dijkstra’s Algorithm - Initialise
• The Shortest Paths algorithm
Given a graph, g,
and a source, s
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Initialise d, p, S,
vertex Q
Dijkstra’s Algorithm - Loop
• The Shortest Paths algorithm
Given a graph, g,
and a source, s
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Greedy!
While there are
still nodes in Q
Dijkstra’s Algorithm - Relax neighbours
• The Shortest Paths algorithm
Given a graph, g,
and a source, s
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Greedy!
Update the
estimate of the
shortest paths to
all nodes
attached to u
Dijkstra’s Algorithm - Operation
• Initial Graph
Distance to all
nodes marked 
Source
Mark 0
Dijkstra’s Algorithm - Operation
• Initial Graph
Source
Relax vertices adjacent to
source
Dijkstra’s Algorithm - Operation
• Initial Graph
Source
Red arrows show
pre-decessors
Dijkstra’s Algorithm - Operation
Source is now in S Sort vertices and
choose closest
Dijkstra’s Algorithm - Operation
Source is now in S
Relax u because a
shorter path via x
exists
Relax y because a
shorter path via x
exists
Dijkstra’s Algorithm - Operation
Source is now in S
Change u’s
pre-decessor also
Relax y because a
shorter path via x
exists
Dijkstra’s Algorithm - Operation
S is now { s, x } Sort vertices and
choose closest
Dijkstra’s Algorithm - Operation
S is now { s, x }
Sort vertices and
choose closest
Relax v because a
shorter path via y
exists
Dijkstra’s Algorithm - Operation
S is now { s, x, y } Sort vertices and
choose closest, u
Dijkstra’s Algorithm - Operation
S is now { s, x, y, u } Finally add v
Dijkstra’s Algorithm - Operation
S is now { s, x, y, u } Pre-decessors show
shortest paths sub-graph
Dijkstra’s Algorithm - Proof
• Greedy Algorithm
• Proof by contradiction best
• Lemma 1
• Shortest paths are composed of shortest paths
• Proof
• If there was a shorter path than any sub-path, then
substitution of that path would make the whole path
shorter
Dijkstra’s Algorithm - Proof
• Denote
d(s,v) - the cost of the shortest path from s to v
• Lemma 2
• If s...uv is a shortest path from s to v,
then after u has been added to S and relax(u,v,w) called,
d[v] = d(s,v) and d[v] is not changed thereafter.
• Proof
• Follows from the fact that at all times d[v]  d(s,v)
• See Cormen (or any other text) for the details
Dijkstra’s Algorithm - Proof
• Using Lemma 2
• After running Dijkstra’s algorithm, we assert
d[v] = d(s,v) for all v
• Proof (by contradiction)
• Suppose that u is the first vertex added to S for which
d[u]  d(s,u)
• Note
• v is not s because d[s] = 0
• There must be a path s...u,
otherwise d[u] would be 
• Since there’s a path, there must be a shortest path
Dijkstra’s Algorithm - Proof
• Proof (by contradiction)
• Suppose that u is the first vertex added to S for which
d[u]  d(s,u)
• Let sxyu be the shortest path
su,
where x is in S and y is the
first outside S
• When x was added to S, d[x] = d(s,x)
• Edge xy was relaxed at that time,
so d[y] = d(s,y)
Dijkstra’s Algorithm - Proof
• Proof (by contradiction)
• Edge xy was relaxed at that time,
so d[y] = d(s,y)
 d(s,u)  d[u]
• But, when we chose u,
both u and y where in V-S,
so d[u]  d[y]
(otherwise we would have chosen y)
• Thus the inequalities must be equalities
 d[y] = d(s,y) = d(s,u) = d[u]
• And our hypothesis (d[u]  d(s,u)) is contradicted!
Dijkstra’s Algorithm - Time Complexity
• Dijkstra’s Algorithm
• Similar to MST algorithms
• Key step is sort on the edges
• Complexity is
• O( (|E|+|V|)log|V| ) or
• O( n2 log n )
for a dense graph with n = |V|

More Related Content

Similar to dijkstraC.ppt

Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxsandeep54552
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
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 dijkstraRoshan Tailor
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
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 pathsSujith Jay Nair
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania NisarSania Nisar
 

Similar to dijkstraC.ppt (20)

Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptx
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path 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
 
lecture 21
lecture 21lecture 21
lecture 21
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Shortest path
Shortest pathShortest path
Shortest path
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
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
 
(148065320) dijistra algo
(148065320) dijistra algo(148065320) dijistra algo
(148065320) dijistra algo
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania Nisar
 

Recently uploaded

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 

dijkstraC.ppt

  • 1. Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
  • 2. Graphs - Shortest Paths • Application • In a graph in which edges have costs .. • Find the shortest path from a source to a destination • Surprisingly .. • While finding the shortest path from a source to one destination, • we can find the shortest paths to all over destinations as well! • Common algorithm for single-source shortest paths is due to Edsger Dijkstra
  • 3. Dijkstra’s Algorithm - Data Structures • For a graph, G = ( V, E ) • Dijkstra’s algorithm keeps two sets of vertices: S Vertices whose shortest paths have already been determined V-S Remainder • Also d Best estimates of shortest path to each vertex p Predecessors for each vertex
  • 4. Predecessor Sub-graph • Array of vertex indices, p[j], j = 1 .. |V| • p[j] contains the pre-decessor for node j • j’s predecessor is in p[p[j]], and so on .... • The edges in the pre-decessor sub-graph are ( p[j], j )
  • 5. Dijkstra’s Algorithm - Operation • Initialise d and p • For each vertex, j, in V • dj =   pj = nil • Source distance, ds = 0 • Set S to empty • While V-S is not empty • Sort V-S based on d • Add u, the closest vertex in V-S, to S • Relax all the vertices still in V-S connected to u Initial estimates are all  No connections Add s first!
  • 6. Dijkstra’s Algorithm - Operation • Initialise d and p • For each vertex, j, in V • dj =   pj = nil • Source distance, ds = 0 • Set S to empty • While V-S is not empty • Sort V-S based on d • Add u, the closest vertex in V-S, to S • Relax all the vertices still in V-S connected to u Initial estimates are all  No connections Add s first!
  • 7. Dijkstra’s Algorithm - Operation • The Relaxation process Relax the node v attached to node u relax( Node u, Node v, double w[][] ) if d[v] > d[u] + w[u,v] then d[v] := d[u] + w[u,v] pi[v] := u If the current best estimate to v is greater than the path through u .. Edge cost matrix Update the estimate to v Make v’s predecessor point to u
  • 8. Dijkstra’s Algorithm - Full • The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w )
  • 9. Dijkstra’s Algorithm - Initialise • The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w ) Initialise d, p, S, vertex Q
  • 10. Dijkstra’s Algorithm - Loop • The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w ) Greedy! While there are still nodes in Q
  • 11. Dijkstra’s Algorithm - Relax neighbours • The Shortest Paths algorithm Given a graph, g, and a source, s shortest_paths( Graph g, Node s ) initialise_single_source( g, s ) S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w ) Greedy! Update the estimate of the shortest paths to all nodes attached to u
  • 12. Dijkstra’s Algorithm - Operation • Initial Graph Distance to all nodes marked  Source Mark 0
  • 13. Dijkstra’s Algorithm - Operation • Initial Graph Source Relax vertices adjacent to source
  • 14. Dijkstra’s Algorithm - Operation • Initial Graph Source Red arrows show pre-decessors
  • 15. Dijkstra’s Algorithm - Operation Source is now in S Sort vertices and choose closest
  • 16. Dijkstra’s Algorithm - Operation Source is now in S Relax u because a shorter path via x exists Relax y because a shorter path via x exists
  • 17. Dijkstra’s Algorithm - Operation Source is now in S Change u’s pre-decessor also Relax y because a shorter path via x exists
  • 18. Dijkstra’s Algorithm - Operation S is now { s, x } Sort vertices and choose closest
  • 19. Dijkstra’s Algorithm - Operation S is now { s, x } Sort vertices and choose closest Relax v because a shorter path via y exists
  • 20. Dijkstra’s Algorithm - Operation S is now { s, x, y } Sort vertices and choose closest, u
  • 21. Dijkstra’s Algorithm - Operation S is now { s, x, y, u } Finally add v
  • 22. Dijkstra’s Algorithm - Operation S is now { s, x, y, u } Pre-decessors show shortest paths sub-graph
  • 23. Dijkstra’s Algorithm - Proof • Greedy Algorithm • Proof by contradiction best • Lemma 1 • Shortest paths are composed of shortest paths • Proof • If there was a shorter path than any sub-path, then substitution of that path would make the whole path shorter
  • 24. Dijkstra’s Algorithm - Proof • Denote d(s,v) - the cost of the shortest path from s to v • Lemma 2 • If s...uv is a shortest path from s to v, then after u has been added to S and relax(u,v,w) called, d[v] = d(s,v) and d[v] is not changed thereafter. • Proof • Follows from the fact that at all times d[v]  d(s,v) • See Cormen (or any other text) for the details
  • 25. Dijkstra’s Algorithm - Proof • Using Lemma 2 • After running Dijkstra’s algorithm, we assert d[v] = d(s,v) for all v • Proof (by contradiction) • Suppose that u is the first vertex added to S for which d[u]  d(s,u) • Note • v is not s because d[s] = 0 • There must be a path s...u, otherwise d[u] would be  • Since there’s a path, there must be a shortest path
  • 26. Dijkstra’s Algorithm - Proof • Proof (by contradiction) • Suppose that u is the first vertex added to S for which d[u]  d(s,u) • Let sxyu be the shortest path su, where x is in S and y is the first outside S • When x was added to S, d[x] = d(s,x) • Edge xy was relaxed at that time, so d[y] = d(s,y)
  • 27. Dijkstra’s Algorithm - Proof • Proof (by contradiction) • Edge xy was relaxed at that time, so d[y] = d(s,y)  d(s,u)  d[u] • But, when we chose u, both u and y where in V-S, so d[u]  d[y] (otherwise we would have chosen y) • Thus the inequalities must be equalities  d[y] = d(s,y) = d(s,u) = d[u] • And our hypothesis (d[u]  d(s,u)) is contradicted!
  • 28. Dijkstra’s Algorithm - Time Complexity • Dijkstra’s Algorithm • Similar to MST algorithms • Key step is sort on the edges • Complexity is • O( (|E|+|V|)log|V| ) or • O( n2 log n ) for a dense graph with n = |V|