简体   繁体   中英

Interpreting Dijkstra's Algorithm

图形

I understand how to find the shortest path from start to finish as explained by the Dijkstra's Algorithm, what I do not understand is the interpretation. Here, from the graph in the picture, order added to my known set from A to E is A,C,B,D,F,H,G,E what I do not get is, how to get the path from A to E as shown in the picture (the mathematical aspect)

Every node has a parent node. When you reach 'E' , you simply look at its parent and so on until you find 'A' . This way you'll find the list in reverse order. Reverse the list it to find the path from 'A' to 'E' .

Your parent list will be 'E' 'G' 'H' 'F' 'B' 'A' if you append in order.

NOTE: The "parent node" is the node indicated in the table's "path" column

Consider a minimum priority queue containing paths to be traversed where the path's priority on the queue is the cost of traversing the edges in the path from the root up to and including that edge. At each step of the algorithm pop the lowest cost path from the queue and, considering each of its incident edges, extend the path with that incident edge and push the new path back onto the queue in priority order. Repeat until the first path reaches the destination.

For example:

  1. Start with an empty queue <>
  2. Then, starting from the root A , put all the incident edges ( A->B , A->C and A->D with costs 2, 1 and 4 respectively) into the queue and order by priority: <(A->C,1),(A->B,2),(A->D,4)>
  3. Pop the minimum priority path A->C from the front of the queue and then consider the edges incident to the end of the path C and push those paths back onto the queue (maintaining the priority order): <(A->B,2),(A->D,4),(A->C->A,10),(A->C->E,12)>
  4. Repeat, popping the minimum priority path A->B off and extending the paths with incident edges: <(A->D,4),(A->B->F,4),(A->B->C,7),(A->C->A,10),(A->B->E,12),(A->C->E,12)>
  5. Keep going... pop A->D and push more incident edges: <(A->B->F,4),(A->D->C,6),(A->B->C,7),(A->C->A,10),(A->B->E,12),(A->C->E,12)>
  6. pop A->B->F and push more incident edges: <(A->D->C,6),(A->B->C,7),(A->B->F->H,7),(A->C->A,10),(A->B->E,12),(A->C->E,12)>
  7. pop A->D->C - however, we've already reached C with a lower cost path so can ignore it.
  8. pop A->B->C and ignore it.
  9. pop A->B->F->H and push more incident edges: <(A->B->F->H->G,8),(A->C->A,10),(A->B->E,12),(A->C->E,12)>
  10. pop A->B->F->H and push more incident edges: <(A->B->F->H->G->F,10),(A->C->A,10),(A->B->F->H->G->E,11),(A->B->E,12),(A->C->E,12)>
  11. pop A->B->F->H->G->F and ignore it.
  12. pop A->C->A and ignore it.
  13. pop A->B->F->H->G->E and we've reached the goal (with a cost of 11)!

Beginning with the start node, a cumulative weight calculation is made when traversing the graph to available nodes. The cumulative weight from one node to an adjacent node is compared against any previous results (ie, possible traversals to that node). The route with the lowest cumulative weight is then selected as the "winner". The process repeats until a path from the start to terminal node is found and evaluated to be the lowest cumulative weight.

So in the example you have shown:

  • ACE = 12
  • ADCE = 17
  • ABE = 12

ABFHGE is the only path left (within the directed graph) with the lowest cumulative weight of 11 (and also the longest path) from start to end.

As you calculate from the start some paths may appear to initially be shorter (AC), but as the algorithm progresses these choices are abandoned as all paths from A to E are explored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM