COS 226 Programming Assignment 9 - Fix


Updated April 30: Oops...even the corrected version is not yet correct. For those who are interested and struggled with this, we have updated the following yet again to get the new correct algorithm. (Oh please let this be correct).

The two-way search, as described in the handout, does not necessarily give back the true shortest path in all instances (this is a problem using the crow-flies distance as well as the grid-to-grid distance). At this point, we are not going to change the assignment as it is too close to the deadline, however for those interested we will offer one point of extra credit for implementing the correct method described below.

The bug in the algorithm is the exit condition which we described for when the algorithm ends. Originally, we said that as soon as you find a vertex which has been added to both trees, return the path which goes from s to this vertex concatenated with the path from this vertex to t. This certainly gives you a legitimate path connecting s to t. Unfortunately, there is no gurantee that this path is the true shortest path.

Corrected algorithm: [Updated April 30]:

  • Keep track of a variable bestlength, which is the length of the "shortest path you have seen" for connecting s to t (originally there is no such path).
  • When a node is added to one tree, and that node has previously been added to the other tree, do the following:
  • consider the concatenated path from s to t which goes through this vertex, and check whether this is a shorter path then that of bestlength. If so, update bestlength and remember this intermediate vertex.
  • In the search which just added the vertex, do not update the priorities for the neighbors of this vertex. (The search still continues, just not along this branch of the tree)
  • Each time you pick a node with minimum priority from the fringe, compare that node's priority to the bestlength variable. If the priority is greater than bestlength, do not bother adding the node to the tree and immediately stop that half of the search for good.

  • As soon as both halves of the search have been stopped for good, you may return the best path you have seen, as it is guaranteed to be the true shortest path.