Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.

Similar presentations


Presentation on theme: "ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss."— Presentation transcript:

1 ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss Finding shortest paths from a single source vertex.

2 www.mapsofworld.com 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 What route would you take between Sacramento and Providence?

3 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 Dijkstra´s algorithm http://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.shortest paths sourcevertexweighted, directed graph Aggregate child (... is a part of or used in me.) priority queue, greedy algorithm. priority queuegreedy 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.priority queueO(V²)Fibonacci heap O(E + V log V)edges

4 Pseuodcode implementation http://en.wikipedia.org/wiki/Dijkstra’s_algorithm 2.11.2009 http://en.wikipedia.org/wiki/Dijkstra’s_algorithm 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 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.

5 Pseuodcode implementation http://en.wikipedia.org/wiki/Dijkstra’s_algorithm 2.11.2009 http://en.wikipedia.org/wiki/Dijkstra’s_algorithm 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5 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.

6 Fig 18.9 Shortest-paths example Sahni © Addison-Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 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).

7 Features of algorithm d by Weiss. “In solving the unweighted shortest-path problem, if D w = ∞, we set D w = D v +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 D w = D v + c v,w if this new value of D w is better than the original value. However, we are no longer guaranteed that D w is altered only once.” – The cost of getting to w from v is c v,w. “Consequently, D w should be altered if its current value is larger than D v + c v,w (rather than merely testing against ∞).” 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7

8 Java API entry for PriorityQueue http://java.sun.com/javase/6/docs/api/ PriorityQueue pq = new PriorityQueue ( ); 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 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.queuenatural ordering Comparator 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.E (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.E

9 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 // Represents an entry in the priority queue for Dijkstra's algorithm. class Path implements Comparable { 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 : 0; returns -1,+1, or 0 }

10 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10 public void dijkstra( String startName ) { PriorityQueue pq = new PriorityQueue ( ); 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

11 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 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 }

12 class Vertex public 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 12

13 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 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.

14 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 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).

15 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 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.

16 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 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.


Download ppt "ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss."

Similar presentations


Ads by Google