0

I have used the Dijkstra algorithm for finding the shortest path between two stations on a map. The cost of going from one station to another is the same at every link.

However, the problem is that Dijkstra tries to find the least cost path from the source to all the stations. I want the search to stop once the least cost path to the destination has been found.

So, I decided to use an A* algorithm for this. But I am not able to think of a good heuristic in this case. What can I possibly use as a heuristic?

thor
  • 21,418
  • 31
  • 87
  • 173
Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • [See here](http://en.wikipedia.org/wiki/Admissible_heuristic) for description and examples. – M.M Jul 13 '14 at 03:50

1 Answers1

1

If you only

want the search to stop once the least cost path to the destination has been found

, Dijkstra's algorithm can already do that efficiently. You can have the algorithm to return once the target node's status is changed from "grey" to "final". Quoting wikipedia

If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target.

 1  function Dijkstra(Graph, source):
 2      dist[source]  := 0                     // Distance from source to source
 3      for each vertex v in Graph:            // Initializations
 4          if v ≠ source
 5              dist[v]  := infinity           // Unknown distance function from source to v
 6              previous[v]  := undefined      // Previous node in optimal path from source
 7          end if 
 8          add v to Q                         // All nodes initially in Q
 9      end for
10      
11      while Q is not empty:                  // The main loop
12          u := vertex in Q with min dist[u]  // Source node in first case
13          remove u from Q 
14          
15          for each neighbor v of u:           // where v has not yet been removed from Q.
16              alt := dist[u] + length(u, v)
17              if alt < dist[v]:               // A shorter path to v has been found
18                  dist[v]  := alt 
19                  previous[v]  := u 
20              end if
21          end for
22      end while
23      return dist[], previous[]
24  end function

A* solves a different aspect, and is only useful when you have a meaningful heuristic estimate of how close you are to the target node.

Also, if

The cost of going from one station to another is the same at every link

,i.e. if path length is the number of links from origin to destination, then shortest path reduces to a depth first search. Using a DFS algorithm may be more efficient.


Additional Note:

In Dijkstra's algorithm, when a node is extracted from the top element u of the priority queue in line 12, its distance label is fixed, and it's impossible to find a smaller distance label than what u currently has. That is why u can be removed in line 13. You can prove this via techniques similar to mathematical induction. If other words, after u is removed from Q, it is not possible for Dijkstra to find a shorter path.

thor
  • 21,418
  • 31
  • 87
  • 173
  • Yeah you are right about the fact that we can stop the search when the target node is found from the set of unsettled nodes. A* is jut another variant of djkistra algorithm. Why do you say that it solves a different issue and can there be no meaningful heuristic in this case? – Ashwin Jul 13 '14 at 05:41
  • I mean A* addresses a different aspect, not the stop-promptly aspect of the SP. I don't mean there is no meaningful heuristic. For example, if Euclidean distance to target is available, you can use it. But it's purpose is to hopefully accelerate solution with that additional information. – thor Jul 13 '14 at 05:46
  • I edited my answer to remove the impression that there is no meaningful heuristics here. – thor Jul 13 '14 at 05:49
  • hmmm ok. But there would be no euclidean disatance for a map with only vertices and edges. So, is there any other heuristic that you can suggest? – Ashwin Jul 13 '14 at 05:51
  • As far as I know, some form of planar distance is the most common one. I can't think of a heuristic on pure abstract graphs. Maybe someone more knowledgeable on this can answer that part. – thor Jul 13 '14 at 05:54
  • You said "If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target". What if there exists a shorter path to that node? That is the purpose of having lines 16-20 right? – Ashwin Jul 14 '14 at 17:31
  • @Ashwin It's impossible to find a shorter path to `u` after 13. Please see my added note. – thor Jul 14 '14 at 21:25
  • If you are there right now, shall we continue this on chat – Ashwin Jul 15 '14 at 00:10
  • Sorry I wasn't around. You can find a proof about the correctness of the termination in algorithm books that cover sp. I have also personally adapted the Dijkstra's implementation in boost graph library and let it return once the target is met. – thor Jul 15 '14 at 03:06