1 / 7

Dijkstra’s Algorithm

Dijkstra’s Algorithm. Dijkstra’s Algorithm. This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative edge weights. 5. 10. 2. a. b. d. 2. 3. 8. 4. 3. 4. 12. e. c. 16. 13. Dijkstra’s Algorithm.

suki-reid
Download Presentation

Dijkstra’s Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dijkstra’s Algorithm

  2. Dijkstra’s Algorithm • This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative edge weights. 5 10 2 a b d 2 3 8 4 3 4 12 e c 16 13

  3. Dijkstra’s Algorithm • For a Graph G • Vertices V = {v1, … vn} • And edge weights wij, for edge connecting vi to vj. • Let the source be v1. • Initialize a Set S = . • Keep track of the vertices for which we’ve already computed their shortest path from the source. • Initialize an array D of estimates of shortest distances. • Initialize D[source]=0, everything else D[i] = . • This says our estimate from the source to the source is 0, everything else is  initially. • While S!=V (or while we haven’t considered all vertices): • Find the vertex with the minimum dist (not already in S). • Add this vertex, vi to S • Recompute all estimates based on vi . • Specifically compute D[i]+wij. If this < D[j] then set D[j] = D[i]+wij

  4. Dijkstra’s Algorithm • Dijkstra’s algorithm relies on: • Knowing that all shortest paths contain subpaths that are also shortest paths. (Convince yourself of this) • The shortest path from a to b is 10, • so the shortest path from a to d has to go through b • so it will also contain the shortest path from a to b which is 10, • plus the additional 2 = 12. 10 2 a b d 8 4 c

  5. Greedy Algorithms • Greedy algorithms work in phases. • In each phase, a decision is made that appears to be good, without regard for future consequences. • “Take what you can get now” • When the algorithm terminates, we hope that the local optimum is equal to the global optimum. • That’s why Dijkstra’s works out well as a greedy algorithm • It’s greedy because we assume we have a shortest distance to a vertex before we ever examine all the edges that even lead into that vertex. • It works since all shortest paths contain subpaths that are also shortest paths. • This also works because we assume no negative edge weights.

  6. Example on the board

  7. References • Slides adapted from Arup Guha’s Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/ • Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss • Additional images: www.wikipedia.com xkcd.com

More Related