Dijkstra's Algorithm


















Who is Dijkstra? (wikipedia)

Dijkstra quotes

Object-oriented programming is an exceptionally bad idea
which could only have originated in California. 

If debugging is the process of removing bugs. Then
programming must be the process of putting them in.


















Shortest Paths

What's the length of a path in a weighted graph?

	The sum of the labels on the edges of the path.

What's the shortest path between two nodes in a weighted graph?

	The path between the nodes that has the smallest length.

How do you find the shortest path between two nodes in a weighted graph?
Would you use Floyd's algorithm?

	You could, but Floyd's does more work than is needed.
	Floyd's finds the distance between all pairs of nodes.


















Dijkstra's Algorithm Example

Find the length of the shortest path
from node 'a' to node 'f' in the directed graph.
(graph11.pdf)

	edge	weight
	(a,b)	  2
	(a,d)	  5
	(b,c)	  5
	(b,d)	  2
	(b,e)	  7
	(b,f)	  9
	(c,f)	  2
	(d,e)	  4
	(e,f)	  3

	a---b---c
	| / | \ |
	d---e---f

The array d[] (indexed by node) stores the shortest
distance found so far for each node in the graph.
How do you initialize the d[] array?

The node with smallest distance is marked (or settled).
Which node do you mark first?

When a node is marked, update the distances of adjacent nodes.
Which distances are updated?

What are the remaining iterations for the example?

What do you do on each iteration of Dijkstra's Algorithm?

	1. Find the unmarked node x with smallest distance and mark it.
	2. For each edge from node x to node y with label w
		if d[x]+w is less than d[y], change d[y] to d[x]+w



					

					
					











Classwork

You may work with a partner.

Simulate running Dijkstra's Algorithm on the undirected graph.
Use node 'd' as the source node.
For each iteration indicate which node is marked
and give the distance values assigned to each node.
(graph12.pdf)

	edge	weight
	{a,b}	  5
	{a,d}	  4
	{a,e}	  1
	{b,c}	  6
	{b,e}	  8
	{b,f}	  2
	{c,f}	  3
	{d,e}	  2
	{e,f}	  10

	a---b---c
	| \ | \ |
	d---e---f


















Dijkstra's Algorithm Code

	Dijkstra(Node s)

	   initialize the d[] array to infinity for each node
	   d[s] = 0

	   while at least one node is not marked

	      find the unmarked node x with the smallest d[] value
	      mark node x

	      for each node y that is adjacent to node x

	         w = weight on edge x,y

	         if (d[x] + w < d[y])
	      	    d[y] = d[x] + w
	         end if

	      end for

	   end while

	end Dijkstra

Which graph implementation is a better fit for Dijkstra's Algorithm,
Adjacency Matrix or Adjacency List?

What's the Big-Oh for the running time of Dijkstra's Algorithm?

	1. The main loop repeats O(n) times.
	2. Finding the node with smallest distance is O(n).
	3. Updating d-array is O(e) over all iterations of the main loop.
	4. Dijkstra is O(n^2 + e) which is O(n^2) (since e <= n^2)

How does Dijkstra's Algorithm compare to Floyd's Algorithm?

	1. Floyd's is O(n^3) but finds the shortest path for all pairs.
	2. Dijkstra's can also do all pairs in O(n^3) time.
	3. Floyd's is simpler (has a smaller constant).