Location: PHPKode > scripts > Combinatorics > combinatorics/Geo.php
<? 
require "Combinatorics.php";

class Geo extends Combinatorics {
var $nodes;
var $distances;


/***  Add a node   ***/
function AddNode($key, $value){
$insert="yes";
$insert=$this->isUnique($key);
if ($insert=="no"){
echo "Node key must be unique. " ;
}
else
$this->nodes[$key]=$value;
}
/***  Check if the node name is unique  ***/
function isUnique($nodekey){
$UNIQUE="yes";
if (is_Array($this->nodes)) {
while (list($key14,$value14 )=each($this->nodes)){
if (trim($key14)==trim($nodekey))
$UNIQUE="no";
return $UNIQUE ;
}
$d=reset($this->nodes);
}
}



/*** Add distances Array ***/

function addDistArray($arraykey, $distarray){
ksort($distarray);
reset($distarray);

$this->distances[$arraykey]=$distarray;

}


/* Print Distances */

function printDistances(){

echo "<CENTER>";

$start_table="<TABLE BORDER='1' CLASS=TABLE>";
$end_table="</TABLE>";
$start_row="<TR>";
$end_row="</TR>";
$start_col1="<TD CLASS=FCOL>";
$start_col="<TD align='RIGHT'>";
$end_col="</TD>";
$start_head="<TH>";
$end_head="</TH>";

ksort($this->nodes);
reset($this->nodes);

echo $start_table;
//echo $start_row;
//echo $start_col;
echo $start_head;
echo "FROM/TO" ;
echo $end_head;
//echo $end_col;
while (list($keynode, $namenode)=each($this->nodes))
{
//echo $start_col;
echo $start_head;
echo $namenode;
echo $end_head;
//echo $end_col;
}

//echo $end_row;


reset($this->nodes);
ksort($this->distances);
$d=reset($this->distances);
while (list($cityfrom, $cityarray)=each($this->distances))
{

echo $start_row;
echo $start_col1;
echo  $this->nodes["$cityfrom"]  ;
echo $end_col;
$d=reset($cityarray);
while (list($cityto, $citydist)=each($cityarray))
{
echo $start_col;
echo $citydist  ;
echo $end_col;
}

echo $end_row;

}

echo $end_table;

echo "</CENTER>";
}

/*** Print Node Name **/
function printNode(){
$d=reset($this->nodes);
if (is_Array($this->nodes)) {
while (list($key1,$value1 )=each($this->nodes)){
echo $value1 ."<br></br>";
}
}
}

/**  Returns all dispositions without repetitions **/

function allDisp($numOfElements){
$d=reset($this->nodes);
$id_el=array_keys($this->nodes);
$Disp=$this->makeDispositionWoR ($id_el, $numOfElements);

return $Disp;
}


function getCityName($idCity){



while (list($key, $myarr)=each($idCity))
{
$temp=array();
$d=reset($myarr);
while (list($arrkey, $arrvalue)=each($myarr))
{

$temp[]=$this->nodes[$arrvalue];
}

$cityNames[]=$temp;

}

return $cityNames;
}


Function calcDistance($iter){

while (list($key, $myarr)=each($iter))
{

$d=reset($myarr);
$nOfEl=count($myarr);
$distk=0;
$counter= 0;
$prev="nuovo";
while (list($arrkey, $arrvalue)=each($myarr))
{

$counter+=1;

if ($counter==1){
$prev=$arrvalue;
}

else {
//print "citta" . $prev .  "<BR></BR>";
$distk+=$this->distances[$prev][$arrvalue];

$prev=$arrvalue;
}



}
echo "Km:" . $distk . "<BR></BR>";
}



}


function addDistanceToIter($iter){


while (list($key, $myarr)=each($iter))
{

$d=reset($myarr);
$nOfEl=count($myarr);
$distk=0;
$counter= 0;
$prev="nuovo";
while (list($arrkey, $arrvalue)=each($myarr))
{

$counter+=1;

if ($counter==1){
$prev=$arrvalue;
}

else {

$distk+=$this->distances[$prev][$arrvalue];

$prev=$arrvalue;
}

}
$iter[$key]["distance"]=$distk;
}


return $iter;

}


function cityPlusDistance($idCity){



while (list($key, $myarr)=each($idCity))
{
$temp=array();
$d=reset($myarr);
$distk=0;
$counter= 0;
$prev="nuovo";
while (list($arrkey, $arrvalue)=each($myarr))
{

$counter+=1;

if ($counter==1){
$prev=$arrvalue;
}

else {

$distk+=$this->distances[$prev][$arrvalue];

$prev=$arrvalue;
}



$temp[]=$this->nodes[$arrvalue];

}
$temp["distance"]=$distk;
$cityWithDistance[]=$temp;

}

return $cityWithDistance;
}


function minDistance($cityAndDistance){
$min_arr=array();
$minval=10000000000000;
while (list($key, $myarr)=each($cityAndDistance))
{

if ($myarr["distance"] < $minval)
$minval=$myarr["distance"] ;

}

$d=reset($cityAndDistance);


while (list($key, $myarr)=each($cityAndDistance))
{

if ($myarr["distance"] == $minval)
$min_arr[]=$myarr ;

}


return $min_arr;

}

}
Return current item: Combinatorics