algorithmpathbreadth-first-searchshortest

Shortest path in matrix with obstacles with cheat paths


First of all this is an assingment and I am not looking for direct answers but instead the complexity of the best solution as you might be thinking it .

This is the known problem of shortest path between 2 points in a matrix (Start and End) while having obstacles in the way. Moves acceptables is up,down,left and right . Lets say when moving i carry sth and the cost of each movement is 2 . There are points in the matrix (lets name them B points) where I can leave this sth in one B point and pick it up from another B point . Cost of dumping sth in B point is 1 and cost of picking sth up from a B point is 1 again . Whenever I move without this sth , my cost of moving now is 1 . What I think of the solution is transform the matrix into a tree and have a BFS applied . However that works without the B points .

Whenever i take into account the B points complexity comes to a worst case scenario N^2. Here is an example :

S - - -
- - - -
B - - B
- - O E

S = Start , E = End , B = B point to drop sth, O = obstacle So i start with S move down down to the B point (2*2=4 points) leave sth in the B point (1 point ) move right right (2*1= 2 points ) , pick it up (1 point ) , move down 2 points = total of 10 points .

What i thought was build the tree with nodes every B point , however this would create a very dense cyclic graph of almost (V-1)*(V-1) edges which takes the algortithm in N^2 boundaries just to create the graph . That is the worst case scenario as above :

S b b b
b b b b
b b b b 
b b b E

Another option I thought was that of first calculating shortest paths withouth B points . Then have iterations where at each iteration : First have bfs on S and closest B have BFS on E and closest B Then see if there is a path between B of closest to S and B closest to E . If there is then I would see if the path is smaller than that of regular shortest path with obstacles . If that is bigger then there is no shortest path (no greedy test). If there is no path between the 2 B points , try second closest to S and try again . If no path again , the second closest to E and closest to S . However I am not able to calculate the complexity in this one in the worst case scenario plus there is no greedy test that evaluates that. Any help on calculating the complexity or even pointing out the best complexity solution (not the solution but just the complexity ) would be greatly appreciated


Solution

  • Your matrix is a representation of a graph. Without the cheat paths it is quite easy to implement a nice BFS. Implementing the cheat paths is not a big deal. Just add the same matrix as another 'layer' on top of the first one. bottom layer is 'carry', top layer is 'no carry'. You can move to the other layer only at B-points for the given cost. This is the same BFS with a third dimension.

    You have n^2 nodes and (n-1)^2 edges per layer and additionally a maximum of n^2 eges connecting the layers. That's O(n^2).