Data Structures and Algorithms              Graphs  Single-Source Shortest Paths      (Dijkstra’s Algorithm)           PLS...
Graphs - Shortest Paths• Application  • In a graph in which edges have costs ..  • Find the shortest path from a source to...
Dijkstra’s Algorithm - Data Structures• For a graph,                   G = ( V, E )• Dijkstra’s algorithm keeps two sets o...
Predecessor Sub-graph• Array of vertex indices, π[j], j = 1 .. |V|   •   π[j] contains the pre-decessor for node j   •   j...
Dijkstra’s Algorithm - Operation• Initialise d and π   • For each vertex, j, in V       • dj = ∞                 Initial e...
Dijkstra’s Algorithm - Operation• Initialise d and π   • For each vertex, j, in V       • dj = ∞       • π j = nil        ...
Dijkstra’s Algorithm - Operation• The Relaxation processRelax the node vattached to node u   Edge cost matrix             ...
Dijkstra’s Algorithm - Full  • The Shortest Paths algorithmGiven a graph, g, and a source, sshortest_paths( Graph g, Node ...
Dijkstra’s Algorithm - Initialise  • The Shortest Paths algorithmGiven a graph, g,                                   Initi...
Dijkstra’s Algorithm - Loop  • The Shortest Paths algorithmGiven a graph, g,and a source, sshortest_paths( GraphthereNode ...
Dijkstra’s Algorithm - Relax neighbours  • The Shortest Paths algorithmGiven a graph, g,and a source, s     Update the    ...
Dijkstra’s Algorithm - Operation• Initial Graph SourceMark 0                    Distance to all                         no...
Dijkstra’s Algorithm - Operation • Initial GraphSource                        Relax vertices adjacent to                  ...
Dijkstra’s Algorithm - Operation • Initial GraphSource                         Red arrows show                          pr...
Dijkstra’s Algorithm - OperationSource is now in S        Sort vertices and                           choose closest
Dijkstra’s Algorithm - Operation                 Relax u because a                 shorter path via x                     ...
Dijkstra’s Algorithm - Operation                        Change u’s                     pre-decessor alsoSource is now in S...
Dijkstra’s Algorithm - Operation S is now { s, x }        Sort vertices and                           choose closest
Dijkstra’s Algorithm - Operation                                Relax v because a                                shorter p...
Dijkstra’s Algorithm - Operation S is now { s, x, y }      Sort vertices and                           choose closest, u
Dijkstra’s Algorithm - OperationS is now { s, x, y, u }   Finally add v
Dijkstra’s Algorithm - OperationS is now { s, x, y, u }     Pre-decessors show                          shortest paths sub...
Dijkstra’s Algorithm - Proof• Greedy Algorithm  • Proof by contradiction best• Lemma 1  • Shortest paths are composed of s...
Dijkstra’s Algorithm - Proof• Denote    δ(s,v) - the cost of the shortest path from s to v• Lemma 2  • If s→...→u→v is a s...
Dijkstra’s Algorithm - Proof• Using Lemma 2   • After running Dijkstra’s algorithm, we assert     d[v] = δ(s,v) for all v•...
Dijkstra’s Algorithm - Proof• Proof (by contradiction)   • Suppose that u is the first vertex added to S for which     d[u...
Dijkstra’s Algorithm - Proof• Proof (by contradiction)   • Edge x→y was relaxed at that time,     so d[y] = δ(s,y)        ...
Dijkstra’s Algorithm - Time Complexity• Dijkstra’s Algorithm   • Similar to MST algorithms   • Key step is sort on the edg...
Upcoming SlideShare
Loading in...5
×

Dijkstra c

964
-1

Published on

c

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
964
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dijkstra c

  1. 1. Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
  2. 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. 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 π Predecessors for each vertex
  4. 4. Predecessor Sub-graph• Array of vertex indices, π[j], j = 1 .. |V| • π[j] contains the pre-decessor for node j • j’s predecessor is in π[π[j]], and so on .... • The edges in the pre-decessor sub-graph are ( π[j], j )
  5. 5. Dijkstra’s Algorithm - Operation• Initialise d and π • For each vertex, j, in V • dj = ∞ Initial estimates are all ∞ • π j = nil No connections • Source distance, ds = 0• Set S to empty• While V-S is not empty • Sort V-S based on d Add s first! • Add u, the closest vertex in V-S, to S • Relax all the vertices still in V-S connected to u
  6. 6. Dijkstra’s Algorithm - Operation• Initialise d and π • For each vertex, j, in V • dj = ∞ • π j = nil Initial estimates are all ∞ No connections • 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 Add s first! • Relax all the vertices still in V-S connected to u
  7. 7. Dijkstra’s Algorithm - Operation• The Relaxation processRelax the node vattached to node u Edge cost matrix If the current bestrelax( Node u, Node v, double w[][] ) estimate to v is if d[v] > d[u] + w[u,v] then greater than the d[v] := d[u] + w[u,v] path through u .. pi[v] := u Update the Make v’s predecessor estimate to v point to u
  8. 8. Dijkstra’s Algorithm - Full • The Shortest Paths algorithmGiven a graph, g, and a source, sshortest_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. 9. Dijkstra’s Algorithm - Initialise • The Shortest Paths algorithmGiven a graph, g, Initialise d, π, S,and a source, s vertex Qshortest_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 )
  10. 10. Dijkstra’s Algorithm - Loop • The Shortest Paths algorithmGiven a graph, g,and a source, sshortest_paths( GraphthereNode s ) While g, are initialise_single_source( g, s ) still nodes in Q S := { 0 } /* Make S empty */ Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); Greedy! AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w )
  11. 11. Dijkstra’s Algorithm - Relax neighbours • The Shortest Paths algorithmGiven a graph, g,and a source, s Update the estimate of theshortest_paths( Graph g, paths to ) shortest Node s initialise_single_source( g, s ) all nodes S := { 0 } attached to u S empty */ /* Make Q := Vertices( g ) /* Put the vertices in a PQ */ while not Empty(Q) u := ExtractCheapest( Q ); Greedy! AddNode( S, u ); /* Add u to S */ for each vertex v in Adjacent( u ) relax( u, v, w )
  12. 12. Dijkstra’s Algorithm - Operation• Initial Graph SourceMark 0 Distance to all nodes marked ∞
  13. 13. Dijkstra’s Algorithm - Operation • Initial GraphSource Relax vertices adjacent to source
  14. 14. Dijkstra’s Algorithm - Operation • Initial GraphSource Red arrows show pre-decessors
  15. 15. Dijkstra’s Algorithm - OperationSource is now in S Sort vertices and choose closest
  16. 16. Dijkstra’s Algorithm - Operation Relax u because a shorter path via x existsSource is now in S Relax y because a shorter path via x exists
  17. 17. Dijkstra’s Algorithm - Operation Change u’s pre-decessor alsoSource is now in S Relax y because a shorter path via x exists
  18. 18. Dijkstra’s Algorithm - Operation S is now { s, x } Sort vertices and choose closest
  19. 19. Dijkstra’s Algorithm - Operation Relax v because a shorter path via y exists S is now { s, x } Sort vertices and choose closest
  20. 20. Dijkstra’s Algorithm - Operation S is now { s, x, y } Sort vertices and choose closest, u
  21. 21. Dijkstra’s Algorithm - OperationS is now { s, x, y, u } Finally add v
  22. 22. Dijkstra’s Algorithm - OperationS is now { s, x, y, u } Pre-decessors show shortest paths sub-graph
  23. 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. 24. Dijkstra’s Algorithm - Proof• Denote δ(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] = δ(s,v) and d[v] is not changed thereafter.• Proof • Follows from the fact that at all times d[v] ≥ δ(s,v) • See Cormen (or any other text) for the details
  25. 25. Dijkstra’s Algorithm - Proof• Using Lemma 2 • After running Dijkstra’s algorithm, we assert d[v] = δ(s,v) for all v• Proof (by contradiction) • Suppose that u is the first vertex added to S for which d[u] ≠ δ(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. 26. Dijkstra’s Algorithm - Proof• Proof (by contradiction) • Suppose that u is the first vertex added to S for which d[u] ≠ δ(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] = δ(s,x) • Edge x→y was relaxed at that time, so d[y] = δ(s,y)
  27. 27. Dijkstra’s Algorithm - Proof• Proof (by contradiction) • Edge x→y was relaxed at that time, so d[y] = δ(s,y) ≤ δ(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] = δ(s,y) = δ(s,u) = d[u] • And our hypothesis (d[u] ≠ δ(s,u)) is contradicted!
  28. 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|
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×