Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.

Similar presentations


Presentation on theme: "Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem."— Presentation transcript:

1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem for a directed graph with nonnegative edge weights.

2 Dijkstras Algorithm It should be noted that distance between nodes can also be referred to as weight. Create a distance list, a previous vertex list, a visited list, and a current vertex. All the values in the distance list are set to infinity except the starting vertex which is set to zero. All values in visited list are set to false. All values in the previous list are set to a special value signifying that they are undefined, such as null. Current vertex is set as the starting vertex. Mark the current vertex as visited. Update distance and previous lists based on those vertices which can be immediately reached from the current vertex. Update the current vertex to the unvisited vertex that can be reached by the shortest path from the starting vertex. Repeat (from step 6) until all nodes are visited.

3 Dijkstra’s It works by keeping for each vertex v the cost d[v] of the shortest path found so far between s and v. Initially, this value is 0 for the source vertex s (d[s]=0), and infinity for all other vertices, representing the fact that we do not know any path leading to those vertices (d[v]=∞ for every v in V, except s). When the algorithm finishes, d[v] will be the cost of the shortest path from s to v — or infinity, if no such path exists.

4 Dijkstra’s The basic operation of Dijkstra's algorithm is edge relaxation: if there is an edge from u to v, then the shortest known path from s to u (d[u]) can be extended to a path from s to v by adding edge (u,v) at the end. This path will have length d[u]+w(u,v). If this is less than the current d[v], we can replace the current value of d[v] with the new value. Edge relaxation is applied until all values d[v] represent the cost of the shortest path from s to v. The algorithm is organized so that each edge (u,v) is relaxed only once, when d[u] has reached its final value.

5 Dijkstra’s The algorithm maintains two sets of vertices S and Q.
Set S contains all vertices for which we know that the value d[v] is already the cost of the shortest path and set Q contains all other vertices. Set S starts empty, and in each step one vertex is moved from Q to S. This vertex is chosen as the vertex with lowest value of d[u]. When a vertex u is moved to S, the algorithm relaxes every outgoing edge (u,v).

6 Dijkstra's Shortest Path Algorithm
Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44

7 Dijkstra's Shortest Path Algorithm
null 2 3 4 5 6 7 t S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } s 2 3 4 5 6 7 t pi d 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label

8 Dijkstra's Shortest Path Algorithm
Q = { s, 2, 3, 4, 5, 6, 7, t } Extract_Min 2 24 3 9 s 18 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 distance label

9 Dijkstra's Shortest Path Algorithm
Q = { 2, 3, 4, 5, 6, 7, t } decrease key X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X

10 Dijkstra's Shortest Path Algorithm
Q = { 2, 3, 4, 5, 6, 7, t } Extract_Min X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X

11 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 6, 7, t } X 9 2 24 3 9 s 18 14 X 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 15 X

12 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 6, 7, t } decrease key X 33 X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X

13 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 6, 7, t } X 33 X 9 2 24 3 9 Extract_Min s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X

14 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 7, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 X

15 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 7, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 Extract_Min X

16 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 4 30 19 11 15 5 5 6 20 16 t 7 44 59 15 X X

17 Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, t } Extract_Min 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 4 30 X 19 11 15 5 5 6 20 16 t 7 44 59 15 X X

18 Dijkstra's Shortest Path Algorithm
Q = { 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 4 30 X 19 11 15 5 5 6 20 16 t 7 44 51 59 15 X X X

19 Dijkstra's Shortest Path Algorithm
Q = { 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 30 X 4 19 11 15 5 5 6 20 Extract_Min 16 t 7 44 51 59 15 X X X

20 Dijkstra's Shortest Path Algorithm
Q = { 4, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X

21 Dijkstra's Shortest Path Algorithm
Q = { 4, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 Extract_Min 5 6 20 16 t 7 44 50 51 X 59 15 X X X

22 Dijkstra's Shortest Path Algorithm
Q = { t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X

23 Dijkstra's Shortest Path Algorithm
Q = { t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 Extract_Min 50 51 X 59 15 X X X

24 Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X

25 Dijkstra’s Pseudo Code
u := Extract_Min(Q) searches for the vertex u in the vertex set Q that has the least d[u] value. That vertex is removed from the set Q and returned to the user. If we are only interested in a shortest path between vertices s and t, we can terminate the search at line 9 if u = t.

26 Dijkstra’s Shortest Path
Through iteration, we can find the shortest path

27 Dijkstra Complexity We can express the running time of Dijkstra's algorithm on a graph with E edges and V vertices as a function of |E| and |V| using the Big-O notation. The simplest implementation of the Dijkstra's algorithm stores vertices of set Q in an ordinary linked list or array, and operation Extract-Min(Q) is simply a linear search through all vertices in Q. In this case, the running time is O(V2). For sparse graphs, that is, graphs with much less than V2 edges, Dijkstra's algorithm can be implemented more efficiently by storing the graph in the form of adjacency lists and using a: Binary heap Fibonacci heap as a priority queue to implement the Extract-Min function. With a binary heap, the algorithm requires O((E+V) lg V) time, and the Fibonacci heap improves this to O(E + V lg V).


Download ppt "Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem."

Similar presentations


Ads by Google