Example #1
1
File: d4.php Project: epiii/latihan
$weightArr[1][2] = 4;
$weightArr[1][3] = 5;
$weightArr[2][3] = 8;
$weightArr[2][3] = 8;
$weightArr[4][3] = 9;
$nn = 5;
initMatrix($nn);
echo 'init matrix ' . $nn . ' x ' . $nn . '<br>';
vwMatrix();
inputNode($weightArr);
echo '<br>update matrix : <br>';
vwMatrix();
echo '<br>input source : 0';
echo '<br>input tujuan : 4';
echo '<br>hasilnya <br>';
dijkstra(0, 3);
// vd($nodeSolu);
echo 'visited: ';
// print_r($nodeVisited);
echo '<br>solu: ';
print_r($nodeSolu);
echo '<br>weight: ';
print_r($nodeWeight);
vd($edgeArr);
/*// node
		{
			'node':[{
				'label':'sby',
				'isVisited':false,
			},{
				'label':'',
    //print_r($route[$end]);
    return $route[$end];
}
function next_start($used, $cost)
{
    $tmp = array();
    $min = INFI;
    $prov;
    foreach ($cost as $key => $value) {
        if (!in_array($key, $used) && $value !== INFI) {
            $tmp[$key] = $value;
        }
    }
    foreach ($tmp as $key => $value) {
        if ($min > $value) {
            $prov = $key;
            $min = $value;
        }
    }
    return $prov;
}
$waypoints = dijkstra($nodeinfo, $start, $end);
//最小コストルートを実際の緯度経度情報に置き換える
for ($i = 0; $i < count($waypoints); $i++) {
    $sql = "select X(Latlon),Y(Latlon) from Node where NodeNo = {$waypoints[$i]}";
    $stmt = $dbh->query($sql);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $waypoints[$i] = array($result['X(Latlon)'], $result['Y(Latlon)']);
}
$waypoints = json_safe_encode($waypoints);
//print_r($waypoints);
Example #3
0
                    $previous[$arr["end"]] = $u;
                }
            }
        }
    }
    $path = array();
    $u = $target;
    while (isset($previous[$u])) {
        array_unshift($path, $u);
        $u = $previous[$u];
    }
    array_unshift($path, $u);
    return $path;
}
$graph_array = array(array("a", "b", 7), array("a", "c", 9), array("a", "f", 14), array("b", "c", 10), array("b", "d", 15), array("c", "d", 11), array("c", "f", 2), array("d", "e", 6), array("e", "f", 9));
$path = dijkstra($graph_array, "a", "e");
echo "path is: " . implode(", ", $path) . "\n";
?>
		</div>
		<script>
			<?php 
$graph_array = array(array("a", "b", 7), array("a", "c", 9), array("a", "f", 14), array("b", "c", 10), array("b", "d", 15), array("c", "d", 11), array("c", "f", 2), array("d", "e", 6), array("e", "f", 9));
$neighbours = array();
$vertices = array();
foreach ($graph_array as $edge) {
    array_push($vertices, $edge[0], $edge[1]);
    $neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
    $neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
}
?>