Page 1 of 1

From A Lookup Grid to Database

Posted: Thu Jun 11, 2009 1:58 pm
by SpecialK
I have the following table

Code: Select all

 
x A B C D
A 0 2 1 1
B 2 0 1 1
C 1 1 0 1
D 1 1 1 0
 
Think of this similar to the travelling salesman with point to point, although the algorithm isn't complicated. More like a lookup from A->B

What would be the best way to store in a database, since the time is the same both directions. A to B is 2, and B to A is 2

Code: Select all

 
Start End Time
A      B     2
A      C     1
B      A     2  
etc
 
Doing this seems like it will be duplicating all the data which wouldn't be a good design.

Any thoughts which would be the best way implement this?

Re: From A Lookup Grid to Database

Posted: Fri Jun 12, 2009 2:10 pm
by Tobey
You can sort the way points before you look up the distance.

Code: Select all

$waypoints = array("B", "A");
sort($waypoints);
$sql = "SELECT distance FROM map WHERE start = '" . $waypoints[0] . "' AND end = '" . $waypoints[1] . "';";
Something like that...