6

I've stampled upon a curious problem.

I've got an unbounded chessboard, N knights starting locations and N target locations.

The task is to find minimal number of moves for all knights to reach all target locations.

I know that shortest path problem for a single knight can be solved using breadth-first search, but how can it be solved for multiple knights?

Sorry for my english, I use it seldom.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
mrjames
  • 87
  • 1
  • 7

4 Answers4

2

I assume you know how to do it for one Knigt .

You can reformulate your problem as a linear program:

I will use the following notations :

We have N knights and N en locations.

xij = 1 if you chose knight i to go to location j and 0 otherwise

cij is the min length of moving knight i to location j

Then you have the following linear program :

variables:

xij for i j in [0,N]

Cost function :

C= SUM(cij.xij for (i,j) in [0,N]x[0,N])

constraints:

SUM(xij for j in [1,N]) = 1 //exactly one knigt goes from i to j

SUM(xij for i in [1,N] ) = 1

(The matrix (xij) is a stochastic matrix)

if X is the matrix of (xij) you have n! possible matrix. This problem can be NP-Hard (there is no easy solution to this system, solving the system is pretty similar than testing all possible solutions).


EDIT:

This problem is called the assignment problem and there exist multiple algorithms to solve it in polynomial time . (check out @purav answer for an example)

As mentionned by @Purav even though this kind of problems can be NP-hard, in this case it can be solve in O(n^3)




About the problem @j_random_hacker raised :

Problem

If a knight is at a endpoint, the next knights should not be able to go through this endpoint. So the Cij might need to be updated after each knight is moved.

Remarks :

1. multiple optimal paths :

As there is no constraint on the side of the chessboard (ilimited chessboard), the order in which you do your move for achiveing the shortest path is not relevant, so there is always a lot a different shortest path (I won't do the combinatorics here).

Example with 2 knights

Let say you have 2 K and 2 endpoints ('x'), the optimal path are drawned.

    -x
   |
   | 
   x
   |
K-- --K

you move the right K to the first point (1 move) the second cannot use the optimal path.

    -x
   |
   |  
   K
   |
K-- --:

But I can easily create a new optimal path, instead of moving 2 right 1 up then 2 up 1 right. 1 can move 2 up 1 right the 1 up 2 right (just inverse)

   --K
  |
 -    
|   K
|   |
:    --:

and any combination of path works :

1 U 2 R then 2 U 1 R etc... as long as I keep the same number of move UP LEFT DOWN and RIGHT and that they are valid.

2. order in which knights are moved :

The second thing is that I can chose the order of my move.

example:

with the previous example if I chose to start with the left knight and go to the upper endpoint, dont have anymore endpoint constraint.

    -K
   |
   | 
   x
   |
:-- --K


    -K
   |
   | 
   K
   |
:-- --:

With these 2 remarks it might be possible to prove that there is no situation in which the lower bound calculated is not optimal .

Ricky Bobby
  • 7,490
  • 7
  • 46
  • 63
  • IP is NP-hard, but not every problem that can be expressed as an IP problem is NP-hard. This one isn't -- as Purav points out, it can be solved in O(n^3) after calculating all c_ij. – j_random_hacker Oct 20 '11 at 04:43
  • Also it'd be worth mentioning that the apparent problem of knights needing to move to already-occupied spaces isn't a problem in practice, because for any solution (sequence of moves) in which a knight is forced by an occupied square to detour from its shortest path to its final destination, there is another solution (possibly with a different permutation of knights on end-squares) that uses the same or fewer moves, and we only care about finding a minimal solution. – j_random_hacker Oct 20 '11 at 04:52
  • @j_random_hacker, I totally agree with your second point, I assumed that you can use a endpoint location in your path, even if it's occupied by a knight. But as the chessboard is infinite, I have the intuition that for most of the path you can pretty much inverse the order of the moves, so it should be easy to find a identical length path which doesn't go through the end point.(is that understandable?) – Ricky Bobby Oct 20 '11 at 07:07
  • 1
    @j_random_hacker about your first objection, I wrote "in general" which is I think true. But in this case it might not be, I didn't say it couldn't be solved in polynomial time. I should have dig this up more. thanks for your comments, I am gonna edit my post. – Ricky Bobby Oct 20 '11 at 07:19
  • 1
    It's an improvement :) But there are still a few problems. The algorithms for the Assignment Problem aren't linear time as you claim -- according to Wikipedia the best known is O(n^3) (as you say yourself). To see why it's a mistake to say that every IP problem is NP-hard, consider that you can formulate even very simple problems (such as selecting the maximum of n numbers) as an IP problem -- obviously this one can be solved in linear time! Also it's not really good enough to say that it's "unlikely" that one knight will have to go around another -- you need to prove it can't happen... – j_random_hacker Oct 20 '11 at 13:05
  • 1
    ... since otherwise all your approach gives you is a lower bound on the cost of a shortest path (i.e. it might report a cost that is lower than any actual shortest path). In this case I'm sure it can be proved. Here's an outline: given a solution, "rewind" the moves from the end until you see a move of a knight that does not correspond to some shortest path between its starting and ending points. Suppose this knight is x and the move is from a to b, and that x's final resting place is p. Then the reason x has gone "out of its way" must be because all other squares on shortest paths... – j_random_hacker Oct 20 '11 at 13:17
  • 1
    ... between a and p are occupied by other knights. Pick one of these other knights arbitrarily (call it y). Now we can build a different solution in which y goes on to p instead of x, because we know y is "in the clear" and we don't care about which knight actually ends up at p, so long as some knight does. So in this new solution, y moves on towards p, leaving a square free that x can now move to, as it heads towards y's original resting place (i.e. the position of knights x and y will be swapped in the final solution). You can continue rewinding and repeating this as necessary until... – j_random_hacker Oct 20 '11 at 13:24
  • 1
    ... you wind up with a solution in which every knight moves along a shortest path to some end square. Actually there are some gaps in this proof sketch (such as whether y is really "in the clear") but I think they could be worked out (or maybe someone can think of a much simpler proof strategy!) My final complaint is that I can't parse your last sentence at all I'm afraid. OK, sorry for the wall of text! – j_random_hacker Oct 20 '11 at 13:27
  • 1
    @j_random_hacker :) with all that text you could have edited my post or add a new answer. I am gonna try to reformulate my last sentence so it's understandable. Because I am sure it's possible to work something from it. – Ricky Bobby Oct 20 '11 at 13:35
  • 1
    Yeah I probably should have added my own answer, but my proof sketch isn't close enough to being a proof for my taste. A couple more things (I know, it never ends! :-P) You still have the word "linear" in there; and I think in your sentence beginning "But I can easily create a new optimal path" you need to change every "left" to "right". – j_random_hacker Oct 21 '11 at 11:02
2

You can compute the cost matrix as suggested by Ricky using breadth first search. so now, cost[i][j] denotes the cost of choosing knight i to goto end location j. Then you can use the Hungarian algorithm to find the final answer, which can be computed in O(N^3) complexity.

Purav Shah
  • 523
  • 1
  • 4
  • 13
1

BFS can still work here. You need to adjust your states a bit, but it will still work:

let S be the set of possible states:
S={((x1,y1),(x2,y2),...,(xn,yn))|knight i is in (xi,yi)}

For each s in S, define:
Successors(s)={all possible states, moving 1 knight on the board}

Your target states are of course all permutations of your target points [you don't actually need to develop these permutations, just check if you reached a state where all the squares are "filled", which is simple to check]

start=(start_1,start_2,...,start_n) where start_i is the start location of knight i.

A run of BFS, from start [the initial position of each knight], is guaranteed to find a solution if one exists [because BFS is complete]. It is also guaranteed to be the shortest possible solution.

(*) Note that the case for single knight is a private instance of this solution, with n=1.

Though BFS will work, it will take a lot of time! the branch factor in here is 4n, so the algorithm will need to develop O((4n)^d) vertices, where n is the number of knights and d is the number of steps needed for a solution.

Possible optimizations:

  1. Space: Note that because BFS uses a lot of memory [O((4n)^d)] you might want to use Iterative Deepening DFS, which behaves like a BFS, but consumes much less memory [O(nd)], but takes more time to run.
  2. Time: To accelerate the solution search, you can use A Star with an admissible heuristic function. It is also guarenteed to find a solution if one exists, and also ensures the solution found is optimal, and will probably [with good heuristic] need to develop less vertices then BFS.
amit
  • 175,853
  • 27
  • 231
  • 333
0

So, I've found the solution.

BFS won't work well on an unlimited chessboard. There is no point in using any shortest path algorithm -- number of knight's moves from location a to location b can be computed in O(1) time -- M. Deza, Dictionary of Distances, p. 251

http://www.scribd.com/doc/53001767/Dictionary-of-Distances-M-Deza-E-Deza-Elsevier-2006-WW

The assignment problem can be solved using mincost-maxflow algorithm (eg. Edmonds-Karp):

http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm

mrjames
  • 87
  • 1
  • 7