1 / 18

Dijkstra's algorithm

Dijkstra's algorithm. Shortest paths in edge-weighted digraph. Krasin Georgiev. Technical University of Sofia. g.krasin at gmail com. Assistant Professor. Table of Contents. Background The problem Properties and assumptions Applications Dijkstra's algorithm and Pseudocode

yoko
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 Shortest paths in edge-weighted digraph KrasinGeorgiev Technical University of Sofia g.krasin at gmail com Assistant Professor

  2. Table of Contents • Background • The problem • Properties and assumptions • Applications • Dijkstra'salgorithm and Pseudocode • C# Demo • Related problems and algorithms • Resources

  3. Background • Let G=(V,E)be a (di)graph. • In directed graphs, edges areone-way • An edge-weighted graph is a graphwhere we associate weights or costs with each edge (or other attributes). • A shortest path from vertex s to vertex t is a directed path from s to t with the property that no other such path has a lower weight.

  4. The Problem Single-Source Shortest Path Problem Find the shortest pathsfrom a source vertex to all other vertices in the graph

  5. Properties and Assumptions • Paths are directed. • The weights are not necessarily distances (could be time or cost). • Edge weights are positive (or zero) • Shortest paths are not necessarily unique • Not all vertices need be reachable • Parallel edges may be present

  6. Algorithm Applications

  7. Algorithm Description Dijkstra'salgorithm first initiates all vertex distances with preliminary values and puts all vertexes in a priority queue. Then picks the unvisited vertex with the lowest-distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited when done with neighbors.

  8. Algorithm Trace Trough

  9. Algorithm Trace Trough

  10. Algorithm Pseudocode function Dijkstra(Graph, source): for each vertex v in Graph: // Initializations dist[v] := infinity ; // Unknown distance function from // source to v previous[v] := undefined ; // Previous node in optimal path end for // from source dist[source] := 0 ; // Distance from source to source Q := the set of all nodes in Graph ; // All nodes in the graph are put in a // Priority Queue Q while Q is not empty: // The main loop u := vertex in Q with smallest distance in dist[] ; // Start node in first case remove u from Q ; if dist[u] = infinity: break ; // all remaining vertices are end if // inaccessible from source for each neighbor v of u in Q: // where v has not yet been // removed from Q. alt := dist[u] + dist_between(u, v) ; if alt < dist[v]: // Relax (u,v,a) dist[v] := alt ; previous[v] := u ; decrease-key v in Q; // Reorder v in the Queue end if end for end while return dist;

  11. 0 6 3 1 5 4 2 C# Demo

  12. Modifications • We can solve different problems using modified Dijkstra algorithm elements: • Graph –vertices, edges and weights meanings • Distance – definition • Priority Queue • Relaxation and Distance Initialization

  13. Some Theory • Dijkstra algorithm is based on the following Lemmas: • Shortest paths are composed of shortest paths. It is based on the fact that if there was a shorter path than any sub-path, then the shorter path should replace that sub-path to make the whole path shorter. • The sum of the lengths of any two sides of a triangle is greater than the length of the third side.

  14. Some Theory • Analysis of Dijkstra’s Algorithm: • The initialization uses only O(n) time. • Each vertex is processed exactly once. • The inner loop is called once for each edge in the graph. Each call of the inner loop does O(1) work plus, possibly, one Decrease-Key operation. • All of the priority queue operations require time • Finallyand we get time • If unsorted sequence is used instead of priority queue we get O(n2+ e)

  15. Related algorithms • Breadth-first search - special-case of Dijkstra's algorithm on unweighted graphs when all edge costs are positive and identical. The priority queue degenerates into a FIFO queue. • Uniform-cost search - the shortest path to a particular node • The A* algorithm - generalization of Dijkstra's algorithm that cuts down on the size of the subgraph that must be explored, if additional information is available that provides a lower bound on the "distance" to the target.

  16. Resources • http://algs4.cs.princeton.edu/44sp/ • http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm • http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/CourseHome/ • https://www.udacity.com/course/cs271 • Nakov’s book: Programming = ++Algorithms;

  17. Dijkstra's algorithm http://algoacademy.telerik.com

  18. Free Trainings @ Telerik Academy • “C# Programming @ Telerik Academy • csharpfundamentals.telerik.com • Telerik Software Academy • academy.telerik.com • Telerik Academy @ Facebook • facebook.com/TelerikAcademy • Telerik Software Academy Forums • forums.academy.telerik.com

More Related