290 likes | 590 Views
Algorithme de Dijkstra. R éférence : John W. Byers . Dijkstra Demo , Course notes CS 330 introduction to Algorithms , Doston Uniuversity , Spring 2013, [available at] http://www.cs.bu.edu/fac/byers/cs330.html Modifié par: Vincent Roberge. Pseudocode.
E N D
Algorithme de Dijkstra • Référence: • John W. Byers. DijkstraDemo, Course notes CS 330 introduction to Algorithms, DostonUniuversity, Spring 2013, [available at] http://www.cs.bu.edu/fac/byers/cs330.html • Modifié par: Vincent Roberge
Pseudocode dist[s] ←0 (distance to source vertex is zero)for all v ∈ V–{s} do dist[v] ←∞ (set all other distances to infinity) S←∅ (S, the set of visited vertices is initially empty) Q←V (Q, the queue initially contains all vertices) while Q ≠∅ (while the queue is not empty) do u ← mindistance(Q,dist) (select the element of Q with the min. distance) S←S∪{u} (add u to list of visited vertices) for all v ∈ neighbors[u] do if dist[v] > dist[u] + w(u, v) (if new shortest path found) then d[v] ←d[u] + w(u, v) (set new value of shortest path) (if desired, add traceback code) return dist
Dijkstra's Shortest Path Algorithm • Find shortest path from s to t. 24 2 3 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44
Dijkstra's Shortest Path Algorithm Visités= { } Non-visités= { s, 2, 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ ] 24 2 3 0 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label
Dijkstra's Shortest Path Algorithm Visités= { } Non-visités= { s, 2, 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ ] delmin 24 2 3 0 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label
Dijkstra's Shortest Path Algorithm Visités= { s } Non-visités= { 2, 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s ss ] decrease key 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label 15 X
Dijkstra's Shortest Path Algorithm Visités= { s } Non-visités= { 2, 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s ss ] delmin 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label 15 X
Dijkstra's Shortest Path Algorithm Visités= { s, 2 } Non-visités= { 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s ss ] 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm Visités= { s, 2 } Non-visités= { 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 2 s s ] decrease key 33 X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm Visités= { s, 2 } Non-visités= { 3, 4, 5, 6, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 2 s s ] 33 X 9 X 24 2 3 0 9 delmin s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm Visités= { s, 2, 6 } Non-visités= { 3, 4, 5, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 6 s s ] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 44 4 X 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 6 } Non-visités = { 3, 4, 5, 7, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 6 s s ] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 44 4 X 19 30 11 5 15 5 6 16 20 t 7 44 delmin 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 6, 7 } Non-visités = { 3, 4, 5, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 7 s s 7] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 35 44 X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 59 X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 6, 7 } Non-visités = { 3, 4, 5, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 7 s s 7] delmin 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 35 44 X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 59 X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 6, 7 } Non-visités = { 4, 5, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 3 s s 3] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 35 34 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 51 59 X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 6, 7 } Non-visités = { 4, 5, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 3 s s 3] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 35 34 44 X X 4 X 19 30 11 5 15 5 6 16 delmin 20 t 7 44 51 59 X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 5, 6, 7 } Non-visités = { 4, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 5 3 s s 5] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 5, 6, 7 } Non-visités = { 4, t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 5 3 s s 5] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 delmin 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 4, 5, 6, 7 } Non-visités = { t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 5 3 s s 5] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 4, 5, 6, 7 } Non-visités = { t } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 5 3 s s 5] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 delmin 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 4, 5, 6, 7, t } Non-visités = { } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 5 3 s s 5] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm Visités = { s, 2, 3, 4, 5, 6, 7, t } Non-visités = { } suivants = [s 2 3 4 5 6 7 t] precedents = [ s 6 5 3 s s 5] 32 33 X X 9 X 24 2 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X