Preface :
Transportation companies and communication networks use graphs to describe links between hubs in their systems. To optimize customer service and the flow of cargo or data, these system often need to determine the shortest-path length between two points (vertices). An algorithm, called the shortest-path algorithm, takes a specified vs. For each vertex v in the graph that is reachable from vs, the algorithm determines the length of a shortest path connecting vs to v. Vertices that are not reachable from vs have path length infinity (∞). For each reachable vertex, the algorithm provides both the length of the path and parent references so that the path of vertices can be constructed.
We can use the design of the breadth-first search to find shortest paths. The algorithm begins at a starting vertex sVertex and then proceeds to visit its neighbors (path length 1 from sVertex) followed by vertices with successively larger path lengths. it fans out from sVertex along paths of adjacent vertices until it visits all vertices reachable from sVertex. In order to determine the path length from sVertex to a vertex v, we need to modify bfs() so each vertex maintains a record of its parent and its path length from sVertex. The JGraph 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() prepares a graph for the application of an optimization algorithm by assigning a representation for ∞ to each dataValue field of the graph vertices.
The shortest-path algorithm is an iterative process that uses a queue to store the vertices. At each step, the algorithm pops an element from the queue. This becomes the current vertex and provides access to an adjacency list that may include unvisited neighbors. Before pushing an unvisited adjacent vertex on the queue, the algorithm assigns values to parent and dataValue fields of the adjacent vertex. The current vertex is the parent and the dataValue is the path length from the starting vertex to the adjacent vertex. This is simply one more than the path length to the current vertex. The iterative process terminates when the queue is empty. The dataValue field of each vertex is the shortest-path length and its parent field allows us to backtrack along a path of parents to list the vertices in the shortest path. A proof that this algorithm does yield the shortest path is difficult and beyond the scope of the book. The interested reader should consult a book on the theory of algorithms.
Let us see how all of this plays out for the graph in below figure. We will find the shortest-path from vertex C to each of the reachable vertices in the graph :
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. In the figure, we represent a queue element with the name of the vertex and use subscripts for the pathLenght and parent. Columns in the table identify the order in which vertices are visited and the current path length from vertex C :
Step1:
Step2:
Step3:
Step4:
Step5:
At the conclusion of the algorithm, the data value for a vertex is the shortest-path length from starting vertex C. In addition, we can build the shortest-path. For instance, look at vertex B. The parent reference field indicates that the parent of B is D. We can backtrack to the starting vertex using the fact that P(B)=D, P(D)=E and P(E)=C. The shortest path from C to B is [C, E, D, E].
Implementing the shortestPath() Method :
The implementation of the static method shortestPath() includes arguments for the graph and the starting vertex. A queue holds the vertices that the algorithm discovers. After the pushing the starting vertex onto the queue, a look continues until the algorithm finds the shortest-path length to all vertices that are reachable from the starting vertex. This occurs when the queue is empty. The path length can be accessed using the getData() method.
The shortest-path algorithm stores parent references that identify the parent of each vertex on a path of shortest length. The method path() builds the path. The arguments includes the graph and two vertices specifying the starting and ending vertices. The method assumes that an optimum path method has been called and that appropriate parent references are defined. The return value is a linked list of vertices designating the path from the starting vertex to the ending vertex :
- Running-Time Analysis
The shortest-path algorithm simply uses the breadth-first search. There is additional O(V) overhead to initialize the dataValue 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).
Supplement :
* Wiki : Shortest path problem
* [ 資料結構 小學堂 ] 圖形結構 : 圖形最短路徑 (單點對全部頂點)
Transportation companies and communication networks use graphs to describe links between hubs in their systems. To optimize customer service and the flow of cargo or data, these system often need to determine the shortest-path length between two points (vertices). An algorithm, called the shortest-path algorithm, takes a specified vs. For each vertex v in the graph that is reachable from vs, the algorithm determines the length of a shortest path connecting vs to v. Vertices that are not reachable from vs have path length infinity (∞). For each reachable vertex, the algorithm provides both the length of the path and parent references so that the path of vertices can be constructed.
We can use the design of the breadth-first search to find shortest paths. The algorithm begins at a starting vertex sVertex and then proceeds to visit its neighbors (path length 1 from sVertex) followed by vertices with successively larger path lengths. it fans out from sVertex along paths of adjacent vertices until it visits all vertices reachable from sVertex. In order to determine the path length from sVertex to a vertex v, we need to modify bfs() so each vertex maintains a record of its parent and its path length from sVertex. The JGraph 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() prepares a graph for the application of an optimization algorithm by assigning a representation for ∞ to each dataValue field of the graph vertices.
The shortest-path algorithm is an iterative process that uses a queue to store the vertices. At each step, the algorithm pops an element from the queue. This becomes the current vertex and provides access to an adjacency list that may include unvisited neighbors. Before pushing an unvisited adjacent vertex on the queue, the algorithm assigns values to parent and dataValue fields of the adjacent vertex. The current vertex is the parent and the dataValue is the path length from the starting vertex to the adjacent vertex. This is simply one more than the path length to the current vertex. The iterative process terminates when the queue is empty. The dataValue field of each vertex is the shortest-path length and its parent field allows us to backtrack along a path of parents to list the vertices in the shortest path. A proof that this algorithm does yield the shortest path is difficult and beyond the scope of the book. The interested reader should consult a book on the theory of algorithms.
Let us see how all of this plays out for the graph in below figure. We will find the shortest-path from vertex C to each of the reachable vertices in the graph :
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. In the figure, we represent a queue element with the name of the vertex and use subscripts for the pathLenght and parent. Columns in the table identify the order in which vertices are visited and the current path length from vertex C :
Step1:
Step2:
Step3:
Step4:
Step5:
At the conclusion of the algorithm, the data value for a vertex is the shortest-path length from starting vertex C. In addition, we can build the shortest-path. For instance, look at vertex B. The parent reference field indicates that the parent of B is D. We can backtrack to the starting vertex using the fact that P(B)=D, P(D)=E and P(E)=C. The shortest path from C to B is [C, E, D, E].
Implementing the shortestPath() Method :
The implementation of the static method shortestPath() includes arguments for the graph and the starting vertex. A queue holds the vertices that the algorithm discovers. After the pushing the starting vertex onto the queue, a look continues until the algorithm finds the shortest-path length to all vertices that are reachable from the starting vertex. This occurs when the queue is empty. The path length can be accessed using the getData() method.
The shortest-path algorithm stores parent references that identify the parent of each vertex on a path of shortest length. The method path() builds the path. The arguments includes the graph and two vertices specifying the starting and ending vertices. The method assumes that an optimum path method has been called and that appropriate parent references are defined. The return value is a linked list of vertices designating the path from the starting vertex to the ending vertex :
- Running-Time Analysis
The shortest-path algorithm simply uses the breadth-first search. There is additional O(V) overhead to initialize the dataValue 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).
Supplement :
* Wiki : Shortest path problem
* [ 資料結構 小學堂 ] 圖形結構 : 圖形最短路徑 (單點對全部頂點)
This message was edited 16 times. Last update was at 15/03/2011 11:05:37
沒有留言:
張貼留言