Upload
janel-montgomery
View
216
Download
0
Embed Size (px)
Citation preview
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
1
ALG0183 Algorithms & Data Structures
Lecture 21d Dijkstra´s algorithm
8/25/2009
…as implemented in Graph.java by Weiss
Chapter 14 Weiss
Finding shortest paths from a single source vertex.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
2
www.mapsofworld.com
8/25/2009
What route would you take between Sacramento and Providence?
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
38/25/2009
Dijkstra´s algorithmhttp://www.itl.nist.gov/div897/sqg/dads/HTML/dijkstraalgo.html
Definition:An algorithm to find the shortest paths from a single source vertex to all other vertices in a weighted, directed graph. All weights must be nonnegative.
Aggregate child (... is a part of or used in me.)priority queue, greedy algorithm.
Note: A naive implementation of the priority queue gives a run time complexity O(V²), where V is the number of vertices. Implementing the priority queue with a Fibonacci heap makes the time complexity O(E + V log V), where E is the number of edges.
All weights must be nonnegative.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
4
Pseuodcode implementationhttp://en.wikipedia.org/wiki/Dijkstra’s_algorithm 2.11.2009
8/25/2009
1. Assign to every node a distance value. Set it to zero for our initial node and to infinity for all other nodes.
2. Mark all nodes as unvisited. Set initial node as current.
3. For current node, consider all its unvisited neighbours and calculate their distance (from the initial node). For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
5
Pseuodcode implementationhttp://en.wikipedia.org/wiki/Dijkstra’s_algorithm 2.11.2009
8/25/2009
4. When we are done considering all neighbours of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal.
5. Set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3.
Notes:(i) If the unvisited nodes are kept in a priority queue of nodes ordered in
increasing distance from the start vertex, then the head of the queue can be taken as the next “current node”.
(ii) The strategy of taking the unvisited node with the smallest distance is sometimes described as the “greedy” part.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
6
Fig 18.9 Shortest-paths exampleSahni © Addison-Wesley
8/25/2009
At vertex 1, the shortest-path is to itself of length 0.Update the distances to vertices 2, 3, and 5 and place 2,3, and 5 in the priority queue.The next vertex to be considered is vertex 3 (cost 2 beats cost 4 and 8).Update the distance to vertex 4 and place 4 in priority queue.The next vertex to be considered is vertex 4 (cost 3 beats cost 4 and 8).Update the distance to vertex 5 and place 5 in priority queue.The next vertext to be considered is vertex 2 (cost 4 beats cost 6 and 8).Update the distance to vertices 4 and 5? No, cost 8/cost 9 does not beat cost 3/cost6.The last vertex to be removed from the priority queue is vertex 5 (with cost 6).
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
7
Features of algorithm d by Weiss.
• “In solving the unweighted shortest-path problem, if Dw = ∞, we set Dw = Dv +1...”– The cost of getting to w, which is adjacent to v, is the cost of getting
to v plus 1.
• “If we apply this logic to the weighted case, we should set Dw = Dv + cv,w if this new value of Dw is better than the original value. However, we are no longer guaranteed that Dw is altered only once.”– The cost of getting to w from v is cv,w.
• “Consequently, Dw should be altered if its current value is larger than Dv + cv,w (rather than merely testing against ∞).”
8/25/2009
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
8
Java API entry for PriorityQueue http://java.sun.com/javase/6/docs/api/ PriorityQueue<Path> pq = new PriorityQueue<Path>( );
8/25/2009
An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements.
The head of this queue is the least element with respect to the specified ordering.
public E remove() Retrieves and removes the head of this queue.(note: inherited from AbstractQueue)public boolean add(E e) Inserts the specified element into this priority queue.
If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
98/25/2009
// Represents an entry in the priority queue for Dijkstra's algorithm.class Path implements Comparable<Path> { public Vertex dest; // w public double cost; // d(w) public Path( Vertex d, double c ) { constructor dest = d; cost = c; } public int compareTo( Path rhs ) { to provide the ordering relationship double otherCost = rhs.cost; return cost < otherCost ? -1 : cost > otherCost ? 1 : 0; returns -1,+1, or 0 }}
entries in the priority queue
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
108/25/2009
public void dijkstra( String startName ) { PriorityQueue<Path> pq = new PriorityQueue<Path>( ); make priority queue
Vertex start = vertexMap.get( startName ); get start vertex from the map if( start == null ) throw new NoSuchElementException( "Start vertex not found" );
clearAll( ); call to the reset method for each vertex pq.add( new Path( start, 0 ) ); add the start to the priority queue start.dist = 0; start to itself has distance zero int nodesSeen = 0; while( !pq.isEmpty( ) && nodesSeen < vertexMap.size( ) ) { Path vrec = pq.remove( ); remove the head of the priority queue Vertex v = vrec.dest; if( v.scratch != 0 ) // already processed v go to next node in priority queue continue; v.scratch = 1; nodesSeen++; count up the number of nodes visited
startName from user
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
118/25/2009
for( Edge e : v.adj ) { iterate over a vertex´s edges Vertex w = e.dest; double cvw = e.cost; if( cvw < 0 ) throw new GraphException( "Graph has negative edges" ); if( w.dist > v.dist + cvw ) { if a better path has been found w.dist = v.dist +cvw; w.prev = v; note of previous vertext (v) on the current shortest path pq.add( new Path( w, w.dist ) ); add to priority queue } } }}
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
12
class Vertexpublic int scratch;// Extra variable used in algorithm
• Depending on the topology of a graph, the same node might appear more than once in the priority queue. When such a node becomes the head of the priority queue and is removed (“visited”), its shortest path has been found. Later occurences of this same node in the priority queue should be skipped over.– The variable scratch records when a node has been visited.
• Depending on the topology of the graph, there may be many several later occurences of nodes on the priority queue. Rather than repeatedly skipping over these nodes, the algorithm can terminate when all the nodes have been seen (“visited”). – The variable nodesSeen is used to count the number of nodes seen
(“visited”).
8/25/2009
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
138/25/2009
http://www.ifors.ms.unimelb.edu.au/tutorial/dijkstra_new/index.html
The start vertex is in red.
The start vertex has distance zero to itself.
The edges from the start vertex are checked and the distances of nearby vertices are updated.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
148/25/2009
http://www.ifors.ms.unimelb.edu.au/tutorial/dijkstra_new/index.html
The vertex at distance 3 is nearest so it will be at the head of the priority queue and removed “visited”. The shortest path to the green vertex is now known to be 3 (show as blue line).
The edges from the green vertex are checked and the distances of nearby vertices are updated if appropriate. The yellow vertex at distance 4 is not updated since 3+3>4.
The vertex at distance 4 is nearest so it will be at the head of the priority queue and removed “visited”. The shortest path to the green vertex is now known to be 4 (show as blue line).
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
158/25/2009
http://www.ifors.ms.unimelb.edu.au/tutorial/dijkstra_new/index.html
The edges from the green vertex are checked and the distances of nearby vertices are updated if appropriate.
The vertex at distance 6 is nearest so it will be at the head of the priority queue and removed “visited”. The shortest path to the green vertex is now known to be 6 (show as blue lines).
The edges from the green vertex are checked and the distances of nearby vertices are updated if appropriate. The top yellow vertex at distance 7 is not updated since 3+3+2>7.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
168/25/2009
http://www.ifors.ms.unimelb.edu.au/tutorial/dijkstra_new/index.html
The top vertex at distance 7 is nearest so it will be at the head of the priority queue and removed “visited”. The shortest path to the green vertex is now known to be 7 (show as blue lines). *** There are two distance 7s. Different implementations might yield different ordering of events.
The edges from the green vertex are checked and the distances of nearby vertices are updated if appropriate. The yellow vertex at distance 7 is not updated since 4+3+2>7. The blue vertex at distance 3 is not updated since 4+3+2>3.
The vertex at distance 7 is nearest so it will be at the head of the priority queue and removed “visited”. The shortest path to the green vertex is now known to be 7 (show as blue lines).
Important note: for assessment purposes, students should be able to describe the the operation of Weiss´s algorithm d on any small graph as shown above.