Title: Dijkstras Algorithm
1Dijkstras Algorithm
- Matt Jurkoic
- CS566 Analysis of Algorithms
- April 10, 2007
2Introduction
- Developed in 1959 by Edsger W. Dijkstra (1930
-2002) a Dutch computer scientist - Addresses the Shortest Path Problem
- Given a connected graph G (V, E), a weight
dE-gtR and a fixed vertex s in V, find a
shortest path from s to each vertex v in V. - Specifically it solves the single source path
problem for a directed graph with non-negative
edge weights - It is a greedy algorithm.
3Description
- Start with a graph G (V, E) where V is the set
of all vertices and E is the set of all edges.
All edges have non-negative weights. - Dijkstra's algorithm keeps two sets of vertices
- SÂ the set of vertices whose shortest paths
from the source havealready been determined - This is initialized to empty.
- V-SÂ the remaining vertices.
- This is initialized to the set of all vertices
(V) in G - The other data structures needed are
- d array of best estimates of shortest path to
each vertex - pi an array of predecessors for each vertex
4Description (continued)
- The basic mode of operation is
- 1. Initialise d and pi,
- 2. Set S to empty,
- 3. While there are still vertices in V-S,
- i. Sort the vertices in V-S according to the
current best estimate of their distance from the
source, - ii. Add u, the closest vertex in V-S, to S,
- iii. Relax all the vertices still in V-S
connected to u
5Description (continued)
- The relaxation process updates the costs of all
the vertices, v, connected to a vertex, u, if we
could improve the best estimate of the shortest
path to v by including (u,v) in the path to v. - The relaxation procedure starts with the
following - initialize_single_source( Graph g, Node s )
- for each vertex v in Vertices( g )
- g.dv infinity
- g.piv nil
- g.ds 0
- This sets up the graph so that each node has no
predecessor (piv nil) and the estimates of
the cost (distance) of each node from the source
(dv) are infinite, except for the source node
itself (ds 0).
6Description (continued)
- The relaxation procedure checks whether the
current best estimate of the shortest distance to
v (dv) can be improved by going through u (i.e.
by making u the predecessor of v) - relax( Node u, Node v, double w )
- if dv gt du wu,v
- then dv du wu,v
- piv u
7Pseudocode
- The algorithm itself is now
- Dijkstra( Graph g, Node s )
- initialize_single_source( g, s )
- S 0 / Make S empty /
- V-S Vertices( G ) / Put the vertices in
a PQ / - while not Empty(V-S)
- u ExtractMin( V-S )
- AddNode( S, u ) / Add u to S /
- for each vertex v in Adjacent( u )
- relax( u, v, w )
- http//www.cs.auckland.ac.nz/software/AlgAnim/dijk
stra.html
8Step1. Given initial graph G(V, E). All nodes
have infinite cost except the source node, s,Â
which has 0 cost.
9Step 2. First we choose the node, which is
closest to the source node, s. We initialize
ds to 0. Add it to S. Relax all nodes adjacent
to source, s. Update predecessor (see red arrow
in diagram below) for all nodes updated.
10Step 3. Choose the closest node, x. Relax all
nodes adjacent to node x. Update predecessors for
nodes u, v and y (again notice red arrows in
diagram below).
11Step 4. Now, node y is the closest node, so add
it to S. Relax node v and adjust its predecessor
(red arrows remember!).
12Step 5. Now we have node u that is closest.
Choose this node and adjust its neighbor node v.
13Step 6. Finally, add node v. The predecessor
list now defines the shortest path from each node
to the source node, s.
14Running Time
- V-S as a linear array
- EXTRACT_MIN takes O(V) time and there are V
such operations. Therefore, a total time for
EXTRACT_MIN in while-loop is O(V2). Since the
total number of edges in all the adjacency list
is E. Therefore for-loop iterates E times
with each iteration taking O(1) time. Hence, the
running time of the algorithm with array
implementation is O(V2 E) O(V2). - V-S as a binary heap ( If G is sparse)
- In this case, EXTRACT_MIN operations takes O(lg
V) time and there are V such operations.The
binary heap can be build in O(V) time.Operation
DECREASE (in the RELAX) takes O(lg V) time and
there are at most such operations.Hence, the
running time of the algorithm with binary heap
provided given graph is sparse is O((V E) lg
V). Note that this time becomes O(ElgV) if all
vertices in the graph is reachable from the
source vertices.
15Applications
- Trucking/Freight Delivery Company
- A trucking company can use this algorithm to
determine the shortest route from city A to city
B (or multiple cities) when delivering its
freight. - Phone/Power Company
- A utility can use this algorithm to determine
the best route of a new line in its network to
get from pole A to pole B (or multiple poles). - Computer Network Design
- A computer network designer may use this
algorithm to find an optimal network path to get
from server A to server B (or multiple servers).
16Comparisons with Other Algorithms
- Prims Algorithm
- Both Dijkstras Algorithm and Prims Algorithm
use a min-priority queue to find the lightest
vertex outside a given set. In Dijkstras it is
the set S and in Prims it is the minimum
spanning tree that is being built. Both
algorithms then add this vertex into the set and
adjust the weights of the remaining vertices,
that are outside of this set, accordingly. - Bellman-Ford Algorithm
- The Bellman-Ford Algorithm computes single
source shortest paths in a weighted digraph where
some of the edge weights may be negative as long
as the graph contains no negative cycle reachable
from the source vertex s. The presence of such
cycles means there is no shortest path, since the
total weight becomes lover each time the cycle is
traversed. Dijkstras Algorithm accomplishes the
same problem with a lower running time, but
requires edge weights to be non-negative. Thus,
Bellman-Ford is only used when there are negative
edge weights.
17References
- Grimaldi, Discrete and Combinatorial Mathematics,
An Applie Introduction, 2000 - Cormen, Leiserson, Rivest, Stein, Introduction to
Algorithms Second Edition, 2001 - E.W. Dijkstra Archive
- http//www.cs.utexas.edu/users/EWD/
- http//www.personal.kent.edu/rmuhamma/Algorithms/
MyAlgorithms/GraphAlgor/dijkstraAlgor.htm - National Institute of Standards and Technology
- http//www.cs.auckland.ac.nz/software/AlgAnim/dij
kstra.html -
18References (continued)
- http//www.nist.gov/dads/HTML/greedyalgo.html
- Homepage of Kenji Ikada, Ph.D, The University of
Tokushima - http//www-b2.is.tokushima-u.ac.jp/ikeda/suu
ri/dijkstra/Dijkstra.shtml - Wikipedia entry for Dijkstras Algorithm
- http//en.wikipedia.org/wiki/Dijkstra27s_algorit
hm - Wikipedia entry for Greedy algorithm
- http//en.wikipedia.org/wiki/Greedy_algorithm