-1

I am trying to implement the Floyd–Warshall algorithm on a maze to calculate the distance from one point to all of the other points inside the maze. For some reason, when I use a k which equals the maximum between the columns and the rows, I get an incorrect answer.

Is there a way to solve this with a value of k which would be correct for all lengths of a given maze?

In other words, is there a way to use the Floyd-Warshall algorithm for a non-n×n matrix? That is, for a m×n matrix where m != n?

Pops
  • 30,199
  • 37
  • 136
  • 151
Nadav
  • 2,589
  • 9
  • 44
  • 63
  • 1
    Can you elaborate on what is going on? Right now you haven't provided enough detail for us to give a meaningful answer. – templatetypedef Dec 19 '11 at 19:32
  • sorry about it, my question is like i wrote down , if there is a way to use the known Floyd warshall on a non MXM matrix, but on MXN matrix when M!=N – Nadav Dec 19 '11 at 19:39
  • i think the question is reasonable, people probably aren't aware of floyd-warshall. for their benefit, floyd-warshall is a graph algorithm which computes shortest path for all pairs. – necromancer Dec 19 '11 at 19:39
  • 2
    I do not see how calculating Floyd-Warshall on a non nXn matrix makes sense. The adjacency matrix A stores the direct distance between nodes. I.e. the value at (A)_{i,j} is the weight of the (directed) edge between nodes i and j. If you have a nXm matrix the nodes you come from and the nodes you go to with each edge seem to come from different domains (because these domains have a different size). The only way this could make sense would be, if you somehow "packed" your matrix, i.e. leave out nodes with in-/out-degree of zero. In that case, just expand it first. – LiKao Dec 19 '11 at 19:45
  • 2
    Or better question: Please clarify what the value of (A)_{i,j} for the nXm matrix A means. Maybe then someone could help you better. – LiKao Dec 19 '11 at 19:47
  • i am working on a maze graph which is a MXN graph atm i have only the walls of the maze - a MXM matrix which has a value of True on the walls points. i want to build a new graph that would calculate the distance from a given s node which would have the cordinates (1,1) for example to any other points on the matrix (the distance from anode to himself is ofc 0 ... each edge equals to 1) – Nadav Dec 19 '11 at 19:53

4 Answers4

1

No.

You seem to be confused about the purpose of the matrix in Floyd-Warshall algorithm: for two locations i,j in the maze the matrix A[i,j] stores weight of edge i -> j (perhaps infinity, if there is no edge). Both columns and rows indicate locations, it is nonsensical for a nonsquare matrix.

If you have a rectangular maze of size M x N, and all possible places are possible, then you need (M*N) x (M*N) matrix for Floyd-Warshall algorithm. Assuming you can only go in 4 directions this is a real waste of space.

If you want shortest paths from one vertex, use Dijkstra's algorithm, it's much faster. If the edges do not have weight, even better, use plain BFS.

sdcvvc
  • 25,343
  • 4
  • 66
  • 102
  • as mentioned in my answer, (M*N)x(M*N) is not the right model if the maze is the narrow passage sort. You end up with way too many unnecessary vertices and all paths have length 1. – necromancer Dec 19 '11 at 20:11
1

If the maze is of the narrow passage sort (which is quite common and probably the case here) then having a vertex for each cell does not make sense because it would add absolutely unnecessary vertices with the cost to each path being the same (unweighted).

The right way to model your graph is to assign a vertex to each intersection (not corner). I. e. if at any point the choices is between 3 or 4 directions place a vertex. If you can go only forward or backward then do not assign a vertex.

This would yield a fairly compact number of vertices even for a large maze.

Next, the weight of the path between a pair of vertices is simply the number of squares on the solitary direct path between the two vertices. This can be easily computed by going in each of the maximum four directions of the vertex and counting the number of hops.

Thus you start out with vertices and path weights and I am sure Floyd-Warshall will give you shortest path lengths between each pair with no problem.

The matrix will be NxN (and not MxN etc.)

Edit: Additionally, if your maze is not of the "narrow passage" sort and you can usually go in all four directions, then instead of Floyd-Warshall or graph algorithms, use A*-search or simulated annealing or that set of global optimization algorithms. (A*-search is what I would recommend)

necromancer
  • 23,916
  • 22
  • 68
  • 115
  • the thing is that for Astar i would need a heuristic , which would consider the walls and i wanted to find all of the distances between the corners to each point on the matrix and return it as a heuristic – Nadav Dec 19 '11 at 20:35
  • the heuristic would not consider the walls -- the thing about Astar is you have a search space constricted by the walls and you search the space guided by a non-accurate extremely-simple-to-compute metric/heuristic. in your case it would be the distance between 2 points IGNORING the walls – necromancer Dec 19 '11 at 20:50
  • think of it another way -- the difficulty of computing an accurate heuristic is equal to the difficulty of doing the A* search. so if you already have an accurate metric you don't need to do the A* search because you would have already done so just to compute the accurate metric :-) – necromancer Dec 19 '11 at 20:52
0

From the Wikipedia article, it would seem that it is possible to use Floyd-Warshall on a non-square matrix. I'm not familiar enough with the details of the algorithm to explain how, but I'll keep looking at it.

Based on what I know of mazes, though, I think a first step would be to generate a graph representing the maze with each non-wall "edge" of a point represented by 1 and each wall represented by either infinity or some very large number.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • Now that I've looked at this for a few minutes, I think if you still have issues, you'll need to post some code to get more help. – Pops Dec 19 '11 at 19:58
  • As herby above explains, the right way to model a maze is to construct a list of vertices of each place (perhaps each corner) and the cost is the length of the path from each place to an adjacent place. then you construct an NxN matrix where N is the number of corners/places. i doubt if your way of generating a graph from the maze is useful in any way for computing shortest-paths. – necromancer Dec 19 '11 at 20:01
  • That runs contrary to the very first thing I learned in my algorithms/data structures class in college, @agksmehx. The entire first lecture was spent proving that representing a maze with a matrix is both intuitive and wrong. Why do you think a list of vertices is superior to a graph? – Pops Dec 19 '11 at 20:10
  • hi lord torgamus, please see my own answer esp the last part. i think the reason for people seeing it different ways is what they think about a maze. is it the kind where you have narrow passages and you can only go forward or backward? or is it the kind where you can go in 4 directions but somtimes encounter walls. your representation is reasonable for the latter case which did not occur to me when i first saw it. my claim is that in the latter case graph algorithms are not the best way to model it. instead you should use A* search type stuff. (A* is pretty cool if you haven't encountered it) – necromancer Dec 19 '11 at 20:17
0

I don't see a reason for your problem in this algorithm. To my knowledge, that algorithm does not have any limitation in terms of size of the matrix (note: you can use any graph!).

A quick overview at wikipedia confirms my guess.

I can only think that you might have a bug in your implementation or in the definition of the graph. Also, could you have any negative cycles?

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • no i do not have any negative cycles and my implementation seems to be okey the only problem is if i use the starting point -> S node to be (n,m) by chosing k to be max(n,m) i get a wrong answer and i need to multiply max(n,m) by 2 to make it correct – Nadav Dec 19 '11 at 19:59