Newsgroups: comp.robotics
Path: brunix!cat.cis.Brown.EDU!agate!howland.reston.ans.net!swrinde!elroy.jpl.nasa.gov!usc!nic-nac.CSU.net!charnel.ecst.csuchico.edu!csusac!csus.edu!netcom.com!bakul
From: bakul@netcom.com (Bakul Shah)
Subject: Re: Shortest path in a matrix
Message-ID: <bakulCuB2Jw.KpB@netcom.com>
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
References: <3273nv$9ol@schema.fiu.edu> <Cu9qnq.13wM@eclipse.torolab.ibm.com>
Date: Wed, 10 Aug 1994 06:11:55 GMT
Lines: 69

dchapman@toc.torolab.ibm.com (Dale Chapman Tic) writes:

>I don't want to sound really sarcastic, but what you have asked for is 
>the solution to the "shortest path in a graph" problem, which is known
>to be NP complete (for a definition, see a text book on discrete mathematics
>or graph theory). The long and the short of it is there is no known way to
>locate the shortest path without checking all possible paths. If you can solve
>this problem and prove it, I am sure that there is a university somewhere who
>will give you a PhD. This problem has been around for quite a while.
>Sorry that I can't help you beyond that.

Prof. Dijkstra came up with a polynomial algorithm for the shortest
path in a graph in the late 50s or early 60s (sorry, my books are
800 miles away to give a precise reference).  See any book on
Graph Algorithms.  One solution to Jay's problem, which is a
special case of the general problem, is by ``Lee's algorithm'',
commonly used in PCB routing.  The idea is to start at one
endpoint.  Label it with number 0.  Next label all of its *valid*
neighbors with number 1.  Next label all of their valid neighbors
and so on.  When the other endpoint is reached, we have found the
shortest path.  A valid neighbor of cell m[i,j] is a cell m[x,y]
where either x == i or y == j and m[x,y] == 1.  Labelling
involves choosing the lowest possible number for a cell.  It can
be shown that given a matrix of size MxN, we will have to perform
O(MxN) operations.  Now, if you want a `PhD' level problem, come
up with a *parallel* algorithm that simulteneously finds as many
shortest paths as there are processors!

Bakul Shah

PS: Solution in pseudo code is on the next page.  Hit n now if
you don't want to see it!

Note that with clever encoding you need only two bits per
cell for labelling regarless of the size of the matrix.



find-shortest-path(Matrix matrix, Point p0, Point pn) {
	int m = number-of-rows(matrix);
	int n = number-of-columns(matrix);
	Matrix(m,n) label, active;
	List active-list <- p0;

	for i in 1..m, j in 1..n {
		label(i,j) = m * n + 1 
		active(i,j) = false;
	}
	label(p0) = 0;
	active-list <- p0;
	active(p0) = true;
	for p in active-list {
		new-label = label(p) + 1;
		for p1 in north(p), south(p), east(p), west(p) {
			if matrix(p1) == 1 &&
			   label(p1) > new-label {
				if active(p1) == false {
					active-list <- p1;
					active(p1) = true;
				}
				label(p1) = new-label;
			}
		}
		if (p == pn) {
			retrace(p0,pn);
			return;
		}
	}
}
