4

Given a grid of width W and height H containing 5 type of symbols :

'S' means starting position
'E' means ending position
'C' means checkpoints
'.' means open position and player can pass through it
'#' means closed block that player cant pass through.

The aim of the player is to reach end from start in minimum distance but he need to pass all the checkpoints in the grid .

We need to find this minimum distance.

Some moves allowed to player are :

  1. Player can move only by one step in horizontal or vertical(up,down,left,right) direction.
  2. Diagonal movement is NOT permitted.
  3. Distance is defined as number of movements to the different blocks.
  4. Player can pass opened blocks,checkpoints,start and end points more than once if necessary.
  5. If its not possible to arrive at goal from start then we need to tell that its not possible.

Example :

Let W=5 and H=5

# # # # #
# . C C #
# S # # #
# . . E #
# # # # #

Then here answer is 9 as path is :

(1,2)=>(1,1)=>(2,1)=>(3,1)=>(2,1)=>(1,1,)=>(1,2)=>(1,3)=>(2,3)=>(3,3)

Now W and H can go up to 100 and maximum number of checkpoints can be at max 18

One of a basic approach can be to use all pair shortest path in here.But As their can be 10000 nodes so complexity of O(N^3) by using alogrithms like Floyd-Warshall Or Dijkstra's will not serve the purpose.

So is their an better approach for this question?

lennon310
  • 12,503
  • 11
  • 43
  • 61
code test
  • 93
  • 5
  • 1
    Do you need the _best_ solution, or just 'good enough'? Have you tried greedy algorithm? – tobias_k Aug 19 '14 at 14:12
  • @tobias_k Best solution in what sense.Obviously the path should be minimum of all possible paths.And greedy in what sense.? – code test Aug 19 '14 at 14:14
  • I meant "best/good enough" in the sense of whether it's okay to find a path with length, say, 5% longer then needed if that gets you e.g. O(n) instead of O(n^3), and "greedy" in the sense of "always go to the nearest not yet visited intermediate node and finally go to end node". – tobias_k Aug 19 '14 at 14:17
  • @tobias_k No..It need to be exactly the minimum.Also i did think of greddy But this may lead to some wrong answers. – code test Aug 19 '14 at 14:18
  • 1
    Yes, "greedy" can be "wrong" if you need the absolutely best solution. That's why I asked. Also, why would you have to consider all the 10.000 cells in the grid for all-pairs-shortest-path? It seems that finding the shortest paths between all the checkpoints should be enough. – tobias_k Aug 19 '14 at 14:20
  • @tobias_k How ? Can you please explain – code test Aug 19 '14 at 14:21
  • 1
    Related question: http://stackoverflow.com/q/25368709/1639625 – tobias_k Aug 19 '14 at 14:22
  • @tobias_k Their they had reduced it to TSP.But can't it be done by all pair shortest path in a efficient way ? – code test Aug 19 '14 at 14:28
  • @tobias_k Though i cant comment their but the best solution their said that Find shortest paths between each pair of must-pass vertices, from the source to each must-pass vertex, and from each must-pass vertex to the sink.But what about distance between must pass point themseleves? – code test Aug 19 '14 at 14:31
  • Yes, that's how I understand it: find the shortest paths between all pairs of nodes from `S`, `E`, and `C`, create a new fully-connected graph using those as edges between the must-pass vertices, and then basically do TSP to find the best permutation starting at `S` and ending at `E`. (Although it's not really TSP because you could visit an intermediate node more than once.) – tobias_k Aug 19 '14 at 14:48
  • @tobias_k Yes exactly...Then how to improve its efficeincy ?Any ideas?Also how i should modify TSP even .? – code test Aug 19 '14 at 14:55
  • possible duplicate of [Construct a minimum spanning tree covering a specific subset of the vertices](http://stackoverflow.com/questions/7685291/construct-a-minimum-spanning-tree-covering-a-specific-subset-of-the-vertices) – mbeckish Aug 19 '14 at 15:15
  • 1
    @mbeckish This question has nothing to do with minimum spanning trees. – Sneftel Aug 19 '14 at 15:52
  • @mbeckish Hey i agree with Sneftel It has nothing to do with minimum spanning tree – code test Aug 19 '14 at 15:59
  • @Sneftel - I may have been too hasty with that. But wouldn't the minimal spanning tree end up being the same as the shortest path (allowing revisiting vertices)? I'm having trouble thinking of a case where the shortest path would include a vertex not on the minimal spanning tree. – mbeckish Aug 19 '14 at 16:05
  • @mbeckish Consider the start and end in the same place, and the checkpoints arranged in a semicircle with one end at the start and end. The spanning tree would include only edges along the semicircle; the shortest path would cut from one end of the semicircle to the other. – Sneftel Aug 19 '14 at 16:11
  • @Sneftel - I see - I was just considering the vertices that need to be included, but ignoring the edges that need to be included. – mbeckish Aug 19 '14 at 16:14
  • you are asking Works Application.. recruitment question... Answer given by @Vikram Bhat is the best. – CyberBoy Aug 25 '14 at 13:06

3 Answers3

2

The cost of finding paths between checkpoints is the least of your concerns. For N being the number of checkpoints, that'll grow to O(N*W*H), assuming you just do BFS out from each checkpoint (and the start and end nodes). Once you've got that easy part done, though, you still have to decide on an ordering for the checkpoints. As other commenters have pointed out, this is the travelling salesman problem, and you're not going to make it efficient -- it is unavoidably O(N!). For comparison, if we discard constant factors and use W=H=100, N=18, the cost of the shortest paths is 180000 "time units"... and the cost of finding the best ordering for the checkpoints is 6402373705728000 "time units". That, you may have noticed, is a bigger number.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • I dont think this answers the question.Its all discussed in comments i guess – code test Aug 19 '14 at 15:59
  • 1
    @codetest Your question is "Is there a better approach". The answer is "no, there is not". This is standard TSP, and its cost is nonnegotiable. – Sneftel Aug 19 '14 at 16:01
  • So please provide it in some pseudocode or so.Because like this am not getting what you want to say – code test Aug 19 '14 at 16:07
1

Use pathfinding algorithm like A* to find paths between two points then you have to decompose your problem into following :-

cost(S,E) = cost(S,C1)+cost(C2,C3)..cost(C3,C4)..cost(Ck,E)

There are k! sequence of k checkpoints so your algorithm will be O(k!*N^2) and there cannot be better algorithm as this problem is reduced to TSP.

Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19
  • @VikramBhatt And Why A* why not something like DFS then ? – code test Aug 20 '14 at 07:14
  • @codetest A* uses heuristics which help get shortest path faster than other algorithm which explore the whole grid while A* explores only valid positions. please check :- http://en.wikipedia.org/wiki/A*_search_algorithm – Vikram Bhat Aug 20 '14 at 07:17
  • @VikramBhatt Any better source to learn it where i can get some code or so ? – code test Aug 20 '14 at 08:39
  • @codetest search on google for code in your language , you will get that but it would be good to try to code by yourself at first and you might go for a library – Vikram Bhat Aug 20 '14 at 08:47
  • 1
    You can use dynamic programming to solve this in `O(k^2 * 2^k)` – avmohan Nov 11 '14 at 15:52
  • @v3ga good point , forget there is a DP algorithm for TSP so the actual time complexity would be O(2^k*k^2*N^2) with O(2^k) space complexity – Vikram Bhat Nov 11 '14 at 16:52
0

Well I encountered a same sort of problem in the past. I'll tell you the way I did it.

First I used Dijkstra's for finding out distances from S to C1....Ck

Then again for distances from C1....Ck then from C2...Ck and so on .

This will give you the shortest path from each check point to all the others including the start and end.

Now create an adjacency matrix between each of the checkpoints including start and goal.

In the end use travelling salesman to figure out the shortest route through the checkpoints.

If you don't know how to implement TSP then skip the last two steps i.e from Adjacency matrix and use Bruteforce to figure out the shortest path (will take more time ,but eventually does the job).

I hope it helps

PhoenixDD
  • 121
  • 1
  • 2
  • 12
  • Oh and the distance in the beginning to all the checkpoints will be the shortest through Dijkstra's just to clarify – PhoenixDD Oct 01 '14 at 21:45