1 / 30

Podcast Ch25d

Podcast Ch25d. Title : Minimum Path Algorithm Description : Overview of Dijkstra’s minimum path algorithm; minimumPath method; minimum path in acyclic graphs Participants : Barry Kurtz (instructor); John Helfert and Tobie Williams (students)

matap
Download Presentation

Podcast Ch25d

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Podcast Ch25d • Title: Minimum Path Algorithm • Description: Overview of Dijkstra’s minimum path algorithm; minimumPath method; minimum path in acyclic graphs • Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) • Textbook: Data Structures for Java; William H. Ford and William R. Topp

  2. Dijkstra's Minimum-Path Algorithm • The minimum path problem is to determine a path of minimum weight from a starting vertex vs to each reachable vertex in the graph. The path may contain more vertices than the shortest path from vs.

  3. Dijkstra's Minimum-Path Algorithm (continued) Three paths, A-B-E, A-C-E, and A-D-E, have path length 2, with weights 15, 17, and 13 respectively. The minimum path is A-C-D-E, with weight 11but path length 3.

  4. Determine a path of minimum weight connecting two vertices. • Minimum weight and path from A to E. Weight = ____ P = [ __________________________ ] Identify weight and path from A to E of greater weight. Weight = _____ P = [ __________________ ] (b) Identify all paths and their weights from C to B. Weight = _____ P = [ __________________ ] Weight = _____ P = [ __________________ ] • Minimum weight and path from E to A. Weight = ____ P = [ __________________________ ]

  5. Dijkstra's Minimum-Path Algorithm (continued) • Dijkstra's algorithm uses a priority queue with elements that store the minimum path weight from the starting vertex. • The DiGraphs static inner class MinInfo contains a vertex reference and the path weight as variables. The vertex is the ending vertex on a path from the starting vertex and pathWeight is sum of the weights for the edges of the path. The class implements the Comparable interface using the pathWeight attribute.

  6. MinInfo Class // priority queue data used by minimumPath() // andminSpanningTree() algorithms private static class MinInfo<T> implements Comparable<MinInfo<T>> { public T endV; public int pathWeight; public int compareTo(MinInfo<T> item) { if (pathWeight < item.pathWeight) return -1; else if (pathWeight == item.pathWeight) return 0; else return 1; } }

  7. Dijkstra's Minimum-Path Algorithm (continued) • Each step in the algorithm removes a MinInfo object from the priority queue and identifies the vertex. Since no subsequent step could find a new path to the vertex with a smaller weight, we have the minimum path weight and can color the vertex BLACK (visited).

  8. Dijkstra's Minimum-Path Algorithm (continued) • For each neighbor that is not BLACK, check whether adding the edge to the minimum path from the starting vertex to the current vertex will create a "better" path from the starting vertex. If the new path is better, create a MinInfo object and insert it into the priority queue. Update the data value and parent reference for the neighbor.

  9. Dijkstra's Minimum-Path Algorithm (continued) • The algorithm terminates whenever the priority queue becomes empty or when the number of visited vertices matches the vertex size of the graph.

  10. Dijkstra's Minimum-Path Algorithm (continued)

  11. Apply Dijkstra's Algorithm to find the minimum weight in going from A to E. Show the initial value in the priorityqueue and the values in the queue after each pop() operation and subsequent edge insertion. Use the format (Vertex, weight) for eachpriority queue entry. Start: PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ]

  12. Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Pop(): _________PQ: [ _________________________________________________ ] Push(): ________PQ: [ _________________________________________________ ] Minimum Weight = ____________Path: [ ___________________________________ ]

  13. Dijkstra's Minimum-Path Algorithm (continued) • Assume a better path than the one found by Dijkstra's Algorithm exists. • Assume this second path and the Dijkstra path are the same up to vertex u and that the weight of the path P(vs, ve) found by Dijkstra's algorithm is W. The better (second) path has an intermediate vertex x that is in the priority queue but is not marked. The weight to x is w = weight for P (vs, x) = weight of P(vs,u) + weight(u,x)

  14. Dijkstra's Minimum-Path Algorithm (continued)

  15. Dijkstra's Minimum-Path Algorithm (continued) • The weight w must be less than W, so the MinInfo(x, w) object will come out of the priority queue before the path found by the algorithm. Continuing in this way, all the vertices on the better path will come out of the priority queue, and the algorithm will find the optimal path, contradicting our assumption that the algorithm does not find the minimum path.

  16. minimumPath() // find the path with minimum total weight from // sVertex to each vertex in the graph reachable // from sVertex; upon return, the dataValue field // of each vertex in g is either the minimum path // weight to the vertex or is INFINITY if the // vertex was not reachable from sVertex; call // path(g, sVertex, v) to find the minimum path // from sVertex to v public static <T> void minimumPath(DiGraph<T> g, T sVertex) { T currVertex = null, neighborVertex = null; // heap (PQ) that stores MinInfo objects HeapPQueue<MinInfo<T>> minPathPQ = new HeapPQueue<MinInfo<T>>(new Less<MinInfo<T>>()); // used when inserting MinInfo entries into the // priority queue or erasing entries MinInfo<T> vertexData = null;

  17. minimumPath()(cont) // edgeSet is edge set of vertex we are // visiting; edgeIter is used to traverse edgeSet Set<T> edgeSet; Iterator<T> edgeIter; // computed minimum weight int newMinWeight, numVisited = 0, numVertices; ... } // terminate on an empty priority queue or when // the number of vertices visited is numVertices while (numVisited < numVertices && !minPathPQ.isEmpty()){ // delete a priority queue entry and record vertex vertexData = minPathPQ.pop(); currVertex = vertexData.endV;

  18. MinimumPath() (cont) // if currVertex is BLACK, we have already found // the optimal path from sVertex to currVertex if (g.getColor(currVertex) != VertexColor.BLACK){ // mark the vertex so we don't look at it // again and increment numVisited g.setColor(currVertex, VertexColor.BLACK); numVisited++; // find all neighbors of the current vertex; // for each neighbor that has not been visited, // generate a MinInfo object and insert it into // the priority queue provided the total weight // to get to the neighbor is better than the // current dataValue of neighborVertex edgeSet = g.getNeighbors(currVertex); edgeIter = edgeSet.iterator();

  19. minimumPath() (cont) while (edgeIter.hasNext()){ neighborVertex = edgeIter.next(); if (g.getColor(neighborVertex) == VertexColor.WHITE){ newMinWeight = g.getData(currVertex) + g.getWeight(currVertex, neighborVertex); // if we have found a better path to // neighborVertex, create a new MinInfo // object for neighborVertex and push it // onto the priority queue; update the // dataValue and parent of neighborVertex; // NOTE: if neighborVertex is WHITE, its // data value is INFINITY and a new MinInfo // object will enter the priority queue

  20. minimumPath() if (newMinWeight < g.getData(neighborVertex)) { vertexData = new MinInfo<T>(); vertexData.endV = neighborVertex; vertexData.pathWeight = newMinWeight; minPathPQ.push(vertexData); g.setData(neighborVertex, newMinWeight); g.setParent(neighborVertex, currVertex); } } } } } • Dijkstra's algorithm has running time O(V + E log2V).

  21. Determine a path of minimum weight connecting two vertices. • Minimum weight and path from A to H. Weight = ____ P = [ __________________________ ] • Minimum weight and path from B to G. Weight = ____ P = [ __________________________ ]

  22. Minimum Path in AcyclicGraphs • When the weighted digraph is acyclic, the problem of finding minimum paths is greatly simplified. • The depth-first search creates a list of vertices in topolocial order. dfsList : [v0, v1, . . .,vi, . . ., vn-1] • Assume vi is the starting vertex for the minimum-path problem. Vertices in the list v0 to vi-1 are not reachable from vi.

  23. Minimum Path in AcyclicGraphs (continued) • After initializing the data value for all of the vertices to , set the data value for vi to 0 and its parent reference to vi. A scan of the vertices in dfsList will find that v0 through vi-1 have value  and will not be considered. The algorithm discovers vi and iteratively scans the tail of dfsList, proceeding much like Dijkstra's algorithm.

  24. dagMinimumPath() // in the directed acyclic graph, find the path with // minimum total weight from sVertex to each vertex // reachable from sVertex; upon return, the dataValue // field of each vertex in g is either the minimum // path weight to the vertex or is INFINITY if the // vertex was not reachable from sVertex; call // path(g, sVertex, v) to find the minimum path from // sVertex to v public static <T> void dagMinimumPath( DiGraph<T> g, T sVertex) { LinkedList<T> tlist = new LinkedList<T>(); Iterator<T> topSortIter, setIter; T currVertex, neighborVertex; int w, dataValue; // perform a topological sort of g topologicalSort(g, tlist);

  25. dagMinimumPath()(cont) // set all dataValues to INFINITY g.initData(); // set dataValue and parent for sVertex g.setData(sVertex, 0); g.setParent(sVertex, sVertex); topSortIter = tlist.iterator(); // sequence through the topological sort and // update the distance to each neighbor while (topSortIter.hasNext()) { // get the next vertex in the topological sort currVertex = topSortIter.next();

  26. dagMinimumPath() (cont) // get the dataValue of currVertex; if it is // INFINITY, the vertex is not reachable // from sVertex if ((dataValue = g.getData( currVertex)) != INFINITY) { // obtain an iterator for the neighbors // of currVertex setIter = g.getNeighbors(currVertex).iterator(); while (setIter.hasNext()) { // get the next neighbor of currVertex neighborVertex = setIter.next();

  27. dagMinimumPath()(concluded) // reset dataValue and parent if adding the // edge (currVertex, neighborVertex) to the // path sVertex --> currVertex provides a // better path to neighborVertex w = dataValue + g.getWeight(currVertex, neighborVertex); if (w < g.getData(neighborVertex)) { g.setData(neighborVertex, w); g.setParent(neighborVertex, currVertex); } } } } }

  28. Minimum Path in AcyclicGraphs (continued) • At a vertex v in the sequential scan of dfsList, its current data value is the minimum path weight from vi to v. For there to be a better path, there must be an unvisited vertex, v', reachable from vi that has an edge to v. This is not possible, since topological order guarantees that v' will come earlier in dfsList.

  29. Minimum Path in AcyclicGraphs (continued)

  30. Running Time for MinimumPath in Acyclic Graphs • The algorithm first creates a topological sort of the vertices with running time O(V+E). A loop visits all of the vertices in the graph once and examines edges that eminate from each vertex only once. Access to all of the vertices and edges has running time O(V+E) and so the total running time for the algorithm is O(V+E).

More Related