$requestResult; $startPointID = $requestData[startPointID]; $endPointID = $requestData[endPointID]; $pointsIDbyIndexList = getPointsList(); $pointsIndexbyIDList = getPointsIndexByIDList($pointsIDbyIndexList); $startPointIndex = $pointsIndexbyIDList[$startPointID][Index]; $endPointIndex = $pointsIndexbyIDList[$endPointID][Index]; $routes = getRoutesArray(); $mapWidth = count($pointsIDbyIndexList); $timeMap = initTimeMap($routes, $pointsIndexbyIDList, $mapWidth); $distanceMap = initDistanceMap($routes, $pointsIndexbyIDList, $mapWidth); $dijkstra = new Dijkstra($distanceMap, 1000); $dijkstra->findShortestPath($startPointIndex, $endPointIndex); $resultByDistance = $dijkstra->getRoute($endPointIndex); $dijkstra = new Dijkstra($timeMap, 1000); $dijkstra->findShortestPath($startPointIndex, $endPointIndex); $resultByTime = $dijkstra->getRoute($endPointIndex); $resultByDistance = cahngeRouteindex($resultByDistance, $pointsIDbyIndexList); $resultByTime = cahngeRouteindex($resultByTime, $pointsIDbyIndexList); $requestResult[path] = true; $requestResult[routeByDistance] = $resultByDistance; $requestResult[routeByTime] = $resultByTime; echo json_encode($requestResult); function cahngeRouteindex($findRouteRes, $pointsIDbyIndexList) { $path = $findRouteRes[path]; $count = $findRouteRes[nodeCount]; for ($index = 0; $index < $count; $index++) { $pointIndex = $path[$index]; $path[$index] = $pointsIDbyIndexList[$pointIndex]; }
$points = array(array(1, 3, 1), array(1, 2, 4), array(2, 3, 1), array(3, 4, 2)); $ourMap = array(); // Read in the points and push them into the map for ($i = 0, $m = count($points); $i < $m; $i++) { $x = $points[$i][0]; $y = $points[$i][1]; $c = $points[$i][2]; $ourMap[$x][$y] = $c; $ourMap[$y][$x] = $c; } // ensure that the distance from a node to itself is always zero // Purists may want to edit this bit out. for ($i = 0; $i < $matrixWidth; $i++) { for ($k = 0; $k < $matrixWidth; $k++) { if ($i == $k) { $ourMap[$i][$k] = 0; } } } // initialize the algorithm class $dijkstra = new Dijkstra($ourMap, I, $matrixWidth); $dijkstra->findShortestPath(1, 3); //to find only path from field 0 to field 13... //$fromClass = $_POST['fromClass']; //$toClass = $_POST['toClass']; //$dijkstra->findShortestPath($fromClass, $toClass); // Display the results echo '<pre>'; echo "\n\n the shortest route from class 1 to 3 is :\n"; echo $dijkstra->getResults(3); echo '</pre>';
$matrixWidth = 20; // $points is an array in the following format: (router1,router2,distance-between-them) $points = array(array(0, 1, 4), array(0, 2, I), array(1, 2, 5), array(1, 3, 5), array(2, 3, 5), array(3, 4, 5), array(4, 5, 5), array(4, 5, 5), array(2, 10, 30), array(2, 11, 40), array(5, 19, 20), array(10, 11, 20), array(12, 13, 20)); $ourMap = array(); // Read in the points and push them into the map for ($i = 0, $m = count($points); $i < $m; $i++) { $x = $points[$i][0]; $y = $points[$i][1]; $c = $points[$i][2]; $ourMap[$x][$y] = $c; $ourMap[$y][$x] = $c; } // ensure that the distance from a node to itself is always zero // Purists may want to edit this bit out. for ($i = 0; $i < $matrixWidth; $i++) { for ($k = 0; $k < $matrixWidth; $k++) { if ($i == $k) { $ourMap[$i][$k] = 0; } } } // initialize the algorithm class $dijkstra = new Dijkstra($ourMap, I, $matrixWidth); // $dijkstra->findShortestPath(0,13); to find only path from field 0 to field 13... $dijkstra->findShortestPath(0); // Display the results echo '<pre>'; echo "the map looks like:\n\n"; echo $dijkstra->printMap($ourMap); echo "\n\nthe shortest paths from point 0:\n"; echo $dijkstra->getResults();