File - SECTION A 6 SEMESTER

DIJKSTRA'S
ALGORITHM
THE AUTHOR: EDSGER WYBE
DIJKSTRA
"Computer Science is no more
about computers than astronomy
is about telescopes."
http://www.cs.utexas.edu/~EWD/
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│)
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:
Single-source (single-destination).
Find a shortest path from a given
source (vertex s) to each of the
vertices.
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?
SINGLE-SOURCE SHORTEST-PATHS
PROBLEM
Important Observation: Any subpath of a shortest
path must also be a shortest path. Why?
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.
THE ROUGH IDEA OF
DIJKSTRA’S ALGORITHM
RELAXATION
RELAXATION
Relaxing an edge (u,v) means testing whether we can improve the
shortest path to v found so far by going through u
FINDING NEW PATHS
SELECTION
REVIEW OF PRIORITY QUEUES
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
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ANALYSIS
O(V)
O(1)
O(V)
O(V)
O((V+E)log V)
O(logV)
O(E)
O(logV)
O(E log V)
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.
25
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
DIJKSTRA ANIMATED EXAMPLE
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
36
APPLICATIONS OF DIJKSTRA'S
ALGORITHM
- Traffic Information Systems are most prominent use
- Mapping (Map Quest, Google Maps)
-Routing Systems
-robot motion planning