Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch25c Title: Shortest Path Algorithm

Similar presentations


Presentation on theme: "Podcast Ch25c Title: Shortest Path Algorithm"— Presentation transcript:

1 Podcast Ch25c Title: Shortest Path Algorithm
Description: Overview of shortest path algorithm; shortestPath method; path method Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 Graph Optimization Algorithms
Dijkstra's algorithm computes minimum path weight from a specified vertex to all other vertices in the graph. The breadth‑first search can be used to find the shortest path from a specific vertex to all the other vertices in the graph. Minimum spanning tree for a connected, undirected graph is the set of edges that connect all vertices in the graph with the smallest total weight.

3 The Shortest Path Algorithm
The breadth-first search can be modified to find shortest paths. Begin at vertex sVertex and visit its neighbors (path length 1) followed by vertices with successively larger path lengths. The algorithm fans out from sVertex along paths of adjacent vertices until it visits all vertices reachable from sVertex.

4 Determine a shortest path
(minimum distance) connecting two vertices. (a) Path of minimum distance from A to E. P = [ _____________________________________ ] Identify a path from A to E of greater distance. P = [ _____________________________________ ] (b) Locate all paths of minimum distance from C to B. P = [ ____________________________ ]

5 Shortest Path Algorithm (cont)
Each vertex must maintain a record of its parent and its path length from sVertex. The DiGraph class provides methods that allow the programmer to associate two fields of information with a vertex. One field identifies the parent of a vertex and the other field is an integer dataValue associated with the vertex. The method initData() assigns a representation for  to each dataValue field of the graph vertices.

6 Shortest Path Algorithm (cont)

7 Shortest Path Algorithm (cont)
A breadth‑first search visit to a vertex defines a path of shortest length from the starting vertex. The parent field of the vertex determines the order of vertices from the starting vertex.

8 Shortest Path Algorithm (cont)
Start the algorithm with sVertex = C. In the vertex, set the dataValue (path length) to 0 and make sVertex its own parent. Initialize the queue by adding C as the first element.

9 Shortest Path Algorithm (cont)
At each step, the algorithm updates the data value (path length from starting vertex) and the parent reference before a vertex enters the queue. At the conclusion of the algorithm (queue becomes empty), the data value for a vertex is the shortest path length from the starting vertex.

10 Shortest Path Algorithm (cont)
shortestPath() finds the shortest path length from vs to all of the vertices in the graph. The value is  if the vertex is not reachable from vs.

11 shortestPath() // use the breadth-first traversal algorithm to
// determine the minimum number of edges in any // path from sVertex to all vertices in the graph // reachable from sVertex; upon return, the dataValue // field of each vertex in g is either the shortest // path length to the vertex or is INFINITY if the // vertex was not reachable from sVertex; call // path(g, sVertex, v) to find the shortest path // from sVertex to v public static <T> void shortestPath(DiGraph<T> g, T sVertex) { // BFS uses a queue to store adjacent vertices LinkedQueue<T> visitQueue = new LinkedQueue<T>(); Set<T> edgeSet; Iterator<T> edgeIter; T currVertex = null, neighborVertex = null; int currentPathLength;

12 shortestPath() (continued)
if (!g.containsVertex(sVertex)) throw new IllegalArgumentException( "shortestPath(): starting vertex not " + "in the graph"); // set each vertex data value to INFINITY g.initData(); // sVertex is its own parent and the shortest path // to itself has length 0 g.setParent(sVertex, sVertex); g.setData(sVertex, 0); // insert starting vertex into the queue visitQueue.push(sVertex); // process vertices until the queue is empty while (!visitQueue.isEmpty()) { // delete a queue entry currVertex = visitQueue.pop();

13 shortestPath() (concluded)
edgeSet = g.getNeighbors(currVertex); // sequence through the edge set and look // for vertices that have not been visited; // assign each such vertex a dataValue of // currentPathLength + 1 currentPathLength = g.getData(currVertex); edgeIter = edgeSet.iterator(); while (edgeIter.hasNext()) { neighborVertex = edgeIter.next(); if (g.getData(neighborVertex) == INFINITY) g.setData(neighborVertex, currentPathLength + 1); g.setParent(neighborVertex, currVertex); visitQueue.push(neighborVertex); }

14 path() // returns the path computed by a graph algorithm
// from sVertex to eVertex public static <T> LinkedList<T> path(DiGraph<T> g, T sVertex, T eVertex) { T currVertex = eVertex; LinkedList<T> path = new LinkedList<T>(); if (g.getData(eVertex) == DiGraphs.INFINITY) return path; while (!currVertex.equals(sVertex)) path.addFirst(currVertex); currVertex = g.getParent(currVertex); } path.addFirst(sVertex);

15 Running Time for shortestPath()
The shortest-path algorithm simply uses the breadth-first search. There is additional O(V) overhead to initialize the dateValue for each vertex. With the breadth-first search having running time O(V + E), the total running time for the shortest-path is O(V + E).

16 Determine a shortest path (minimum distance) connecting two vertices.
Path of minimum distance from A to G. P = [ _____________________________________ ] Path of minimum distance from E to D. Path of minimum distance from H to F.


Download ppt "Podcast Ch25c Title: Shortest Path Algorithm"

Similar presentations


Ads by Google