|
function display_all($array, $data, $template){
echo "<table border=1>";
echo "<tr><td></td><td></td>";
foreach($data as $spotdeux){
echo "<td align=center>".$spotdeux."</td>";
}
echo "</tr>";
$i=-1;
foreach($array as $spot){
echo "<tr><td>";
if ($i > -1) echo $template[$i];
echo "</td>";
foreach($spot as $spotdeux){
echo "<td>".$spotdeux."</td>";
}
echo "</tr>";
$i++;
}
echo "</table>";
}
function find_best($x, $y, $array, $matrix, $data, $template, $new_data, $new_template){
if (($x == 0) && ($y == 0)){
echo "<br>".$array."<br>".count($array)."<br>";
$size_best=count($array);
$array_final = array_reverse($array);
echo "<table border=1><tr>";
for ($s = 0; $s < $size_best; $s++){
echo "<td>".$array_final[$s]."</td>";
}
echo "</tr><tr><td></td>";
$data_final = array_reverse($new_data);
for ($s = 0; $s < count($new_data); $s++){
echo "<td>".$data_final[$s]."</td>";
}
echo "</tr><tr><td></td>";
$template_final = array_reverse($new_template);
for ($s = 0; $s < count($new_template); $s++){
echo "<td>".$template_final[$s]."</td>";
}
echo "</tr></table>";
return $array;
}
else{
if (($x != 0) && ($y != 0)) $moves[]=$matrix[$x-1][$y-1];
if ($y != 0) $moves[]=$matrix[$x][$y-1];
if ($x != 0) $moves[]=$matrix[$x-1][$y];
echo "<br>".$x." ".$y." ".$array;
$best_move=min($moves);
if ($best_move == $matrix[$x-1][$y-1]){
$array[] = $matrix[$x-1][$y-1];
$new_template[]=$template[$x-1];
$new_data[]=$data[$y-1];
find_best($x-1, $y-1, $array, $matrix, $data, $template, $new_data, $new_template);}
elseif ($best_move == $matrix[$x][$y-1]){
$array[]=$matrix[$x][$y-1];
$new_template[]='-';
$new_data[]=$data[$y-1];
find_best($x, $y-1, $array, $matrix, $data, $template, $new_data, $new_template);}
elseif ($best_move == $matrix[$x-1][$y]){
$array[]=$matrix[$x-1][$y];
$new_template[]=$template[$x-1];
$new_data[]='-';
find_best($x-1, $y, $array, $matrix, $data, $template, $new_data, $new_template);}
else{ echo "Error";}
}
}
$data=array('chk', 'wt', 'chng', 'chk', 'tt', 'trha', 'htrha',
'rnst', 'trha', 'sdsur', 'sdsur', 'sdsun', 'sdsur', 'sdsun',
'sdsur', 'rwt', 'rwt', 'exam', 'exam', 'exam', 'hall', 'trha',
'htrha', 'trha', 'tt', 'tr', 'htrha', 'sdsur', 'sdsun');
$template=array('chk', 'wt', 'chng', 'sdsur', 'hall', 'exam',
'hall', 'trha', 'tr', 'sdsur');
$size_data=count($data);
$size_template=count($template);
$g=1;
$d=1;
$matrix[0][0]=0;
for($x = 1; $x < ($size_data + 1); $x++){
$matrix[0][$x]=$matrix[0][$x-1] + $g;
echo "matrix[0][".$x."] = ".$matrix[0][$x]."<br>";
}
for($y = 1; $y < ($size_template + 1); $y++){
$matrix[$y][0]=$matrix[$y-1][0] + $g;
echo "matrix[".$y."][0] = ".$matrix[$y][0]."<br>";
}
for($w = 1; $w < ($size_template + 1); $w++){
for ($v = 1; $v < ($size_data + 1); $v++){
if($template[$w-1] == $data[$v-1]){
echo "template[".($w-1)."] = ".$template[$w-1]." and data[".($v-1)."] = ".$data[$v-1]."<br>";
echo "Compare ".$matrix[$w-1][$v-1]." ".($matrix[$w-1][$v] + $g)." ".($matrix[$w][$v-1] + $g)."<br>";
$matrix[$w][$v]=min($matrix[$w-1][$v-1], ($matrix[$w-1][$v] + $g), ($matrix[$w][$v-1] + $g));
}
else{
echo "template[".($w-1)."] = ".$template[$w-1]." and data[".($v-1)."] = ".$data[$v-1]."<br>";
echo "Compare ".($matrix[$w-1][$v-1] + $d)." ".($matrix[$w-1][$v] + $g)." ".($matrix[$w][$v-1] + $g)."<br>";
$matrix[$w][$v]=min(($matrix[$w-1][$v-1] + $d), ($matrix[$w-1][$v] + $g), ($matrix[$w][$v-1] + $g));
}
}
}
display_all($matrix, $data, $template);
$initial=array($matrix[$size_template-1][$size_data-1]);
$best_sequence = find_best($size_template, $size_data, $initial, $matrix, $data, $template, array(), array());
$size_best=count($best_sequence);
echo $best_sequence."<br>";
echo $size_best."<br>";
for ($s = 0; $s < $size_best; $s++){
echo $s." ";
}
| |
|