Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dijkstra's algorithm.

Similar presentations


Presentation on theme: "Dijkstra's algorithm."— Presentation transcript:

1 Dijkstra's algorithm

2 The author: Edsger Wybe Dijkstra
"Computer Science is no more about computers than astronomy is about telescopes."

3 Shortest Path Problem for Graphs
Let G: (V,E) be a (di)graph. The shortest path between two vertices is a path with the shortest length (least number of edges). Call this the link-distance. Breadth-first-search is an algorithm for finding shortest (link-distance) paths from a single source vertex to all other vertices. BFS processes vertices in increasing order of their distance from the root vertex. BFS has running time O(│V│+│E│)

4 Shortest Path Problem for Weighted Graphs
In a shortest-paths problem, we are given a weighted, directed graph G :(V,E), with weight function w: E → R mapping edges to real-valued weights. The weight w(p) of path p= < v0, v1,……, vk> is the sum of the weights of its constituent edges:

5 Single-source (single-destination)
Single-source (single-destination). Find a shortest path from a given source (vertex s) to each of the vertices.

6 Single-Source Shortest-Paths Problem
The Problem: Given a graph with non-negative edge weights G :(V,E) and a distinguished source vertex s Є V determine the distance and a shortest path from the source vertex to every vertex in the digraph. Question: How do you design an efficient algorithm for this problem?

7 Single-Source Shortest-Paths Problem
Important Observation: Any subpath of a shortest path must also be a shortest path. Why?

8 Intuition behind Dijkstra’s Algorithm
Report the vertices in increasing order of their distance from the source vertex. Construct the shortest path tree edge by edge; at each step adding one new edge, corresponding to construction of shortest path to the current new vertex.

9 The Rough Idea of Dijkstra’s Algorithm

10 relaxation

11 relaxation Relaxing an edge (u,v) means testing whether we can improve the shortest path to v found so far by going through u

12 Finding new paths

13 Selection

14 Review of Priority Queues

15 Dijkstra's algorithm Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory.  Works on both directed and undirected graphs. However, all edges must have nonnegative edge weights. Approach: Greedy, similar to Prim's algorithm for MST Like breadth-first search (if all weights = 1, one can simply use BFS) Basic idea: maintain a set S of solved vertices at each step select "closest" vertex u, add it to S, and relax all edges from u Input: Weighted graph G={E,V} and source vertex v ∈ V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v∈V to all other vertices

16 EXAMPLE

17 EXAMPLE

18 EXAMPLE

19 EXAMPLE

20 EXAMPLE

21 EXAMPLE

22 EXAMPLE

23 Dijkstra’s Algorithm

24 Dijkstra’s Analysis O(V) O(1) O(V) O(V) O((V+E)log V) O(logV) O(E)
O(E log V) O(logV)

25 Time Complexity For sparse graphs, (i.e. graphs with much less than |V2| edges) Dijkstra's implemented more efficiently by priority queue (binary minheap) Initialization O(|V|) using O(|V|) buildHeap While loop O(|V|) Find and remove min distance vertices O(log |V|) using O(log |V|) deleteMin Potentially |E| updates Update costs O(log |V|) using decreaseKey Each EXTRACT-MIN operation then takes time O(lg V). There are │V│ such operations. Each DECREASE-KEY operation takes time O(lgV), and there are still at most│E│ such operations. The total running time is therefore O((V+E)lgV), which is O(E lgV) if all vertices are reachable from the source.

26 Dijkstra Animated Example

27 Dijkstra Animated Example

28 Dijkstra Animated Example

29 Dijkstra Animated Example

30 Dijkstra Animated Example

31 Dijkstra Animated Example

32 Dijkstra Animated Example

33 Dijkstra Animated Example

34 Dijkstra Animated Example

35 Dijkstra Animated Example

36 Correctness Dijkstra’s algorithm is a greedy algorithm
make choices that currently seem the best locally optimal does not always mean globally optimal Correct because maintains following two properties: for every known vertex, recorded distance is shortest distance to that vertex from source vertex for every unknown vertex v, its recorded distance is shortest path distance to v from source vertex, considering only currently known vertices and v

37 Applications of Dijkstra's Algorithm
- Traffic Information Systems are most prominent use  - Mapping (Map Quest, Google Maps) Routing Systems robot motion planning


Download ppt "Dijkstra's algorithm."

Similar presentations


Ads by Google