12

Graph

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)

i_use_the_internet
  • 604
  • 1
  • 9
  • 28
  • 2
    Hint: first, find the path from E to A. Then reverse it. – Kevin Apr 20 '15 at 18:34
  • 5
    @Kevin: This is a directed graph, so the path from E to A is not actually the reverse of the path from A to E (and the calculations shown in the problem don't help in constructing the path from E to A). So your hint, as presented, is not correct. – ruakh Apr 20 '15 at 18:44

3 Answers3

11

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

Thellimist
  • 3,757
  • 5
  • 31
  • 49
  • 3
    Plus one. Note that the "parent node" is the node indicated in the table's "path" column. – ruakh Apr 20 '15 at 18:40
  • 1
    As noted by @ruakh in the comments, this is a directed graph so the path from E to A is not the same as the path from A to E. In your example, there actually is no path from G to H so that does not work. Also the weight from E to G, for example, is different than the weight from G to E. – mstbaum Apr 20 '15 at 18:52
1

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)!
MT0
  • 143,790
  • 11
  • 59
  • 117
0

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 (i.e., 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.

jhenderson2099
  • 956
  • 8
  • 17