| 24 |
|
|
| 25 |
$out = array(); |
$out = array(); |
| 26 |
$prev = $to; |
$prev = $to; |
| 27 |
|
$watch_infinite_loop = 0; |
| 28 |
while ($result['prev'][$prev] != '-') { |
while ($result['prev'][$prev] != '-') { |
| 29 |
$out[] = array('n' => $prev, 'd' => $result['dist'][$prev]); |
$out[] = array('n' => $prev, 'd' => $result['dist'][$prev]); |
| 30 |
$prev = $result['prev'][$prev]; |
$prev = $result['prev'][$prev]; |
| 31 |
|
if (++$watch_infinite_loop === 5000) { |
| 32 |
|
// It must be an infinite loop. This means that something terrible happens in the code elsewhere |
| 33 |
|
return array(); |
| 34 |
|
} |
| 35 |
} |
} |
| 36 |
return $out; |
return $out; |
| 37 |
} |
} |
| 54 |
else { |
else { |
| 55 |
$min_tree = breadth_first_walk($edges, $from, -1); |
$min_tree = breadth_first_walk($edges, $from, -1); |
| 56 |
} |
} |
|
/*print "eredm\n"; |
|
|
print_r($min_tree); |
|
|
print "eredmvege\n"; |
|
|
ob_flush();*/ |
|
| 57 |
if (isset($min_tree['dist'][$to]) && $min_tree['dist'][$to] !== '-') { |
if (isset($min_tree['dist'][$to]) && $min_tree['dist'][$to] !== '-') { |
| 58 |
$route = rollback($min_tree, $to); |
$route = rollback($min_tree, $to); |
| 59 |
$route[] = array('n' => $from, 'd' => 0); |
$route[] = array('n' => $from, 'd' => 0); |
| 111 |
if ($uid1 !== $uid2) { |
if ($uid1 !== $uid2) { |
| 112 |
$dist = n_step_distance($edges, $uid1, $uid2); |
$dist = n_step_distance($edges, $uid1, $uid2); |
| 113 |
if ($dist !== FALSE) { |
if ($dist !== FALSE) { |
|
//print "F:" . $uid1 . " T:" . $uid2 . " D:" . $dist . "\n"; |
|
| 114 |
$sum += $dist; |
$sum += $dist; |
| 115 |
++$num; |
++$num; |
| 116 |
} |
} |
| 117 |
} |
} |
| 118 |
} |
} |
|
//print $uid1 . " is done\n"; |
|
| 119 |
} |
} |
| 120 |
if ($num != 0) { // Avoid division by zero! |
if ($num != 0) { // Avoid division by zero! |
| 121 |
return $sum / $num; |
return $sum / $num; |
| 200 |
* @param array $edges The adjacentcy list of the graph |
* @param array $edges The adjacentcy list of the graph |
| 201 |
*/ |
*/ |
| 202 |
function depth_first_walk($edges) { |
function depth_first_walk($edges) { |
| 203 |
foreach (array_keys($edges) as $u) { |
$all_vertices = get_all_vertices(); |
| 204 |
|
foreach ($all_vertices as $u) { |
| 205 |
$c[$u] = 0; |
$c[$u] = 0; |
| 206 |
/* Color of point. 0 - white, 1 - grey, 2 - black |
/* Color of point. 0 - white, 1 - grey, 2 - black |
| 207 |
white - not reached |
white - not reached |
| 211 |
$p[$u] = '-'; // Previous vertex |
$p[$u] = '-'; // Previous vertex |
| 212 |
} |
} |
| 213 |
$time = 0; |
$time = 0; |
| 214 |
foreach (array_keys($edges) as $u) { |
foreach ($all_vertices as $u) { |
| 215 |
if ($c[$u] === 0) { |
if ($c[$u] === 0) { |
| 216 |
|
|
| 217 |
walk($u, $edges, $time, $c, $p, $f, $d); |
walk($u, $edges, $time, $c, $p, $f, $d); |
| 230 |
function mod_depth_first_walk($edges, $prev_res) { |
function mod_depth_first_walk($edges, $prev_res) { |
| 231 |
$prev_f = $prev_res[0]; |
$prev_f = $prev_res[0]; |
| 232 |
$p = $prev_res[1]; |
$p = $prev_res[1]; |
| 233 |
foreach (array_keys($edges) as $u) { |
$all_vertices = get_all_vertices(); |
| 234 |
|
foreach ($all_vertices as $u) { |
| 235 |
$c[$u] = 0; |
$c[$u] = 0; |
| 236 |
/* Color of point. 0 - white, 1 - grey, 2 - black |
/* Color of point. 0 - white, 1 - grey, 2 - black |
| 237 |
white - not reached |
white - not reached |
| 260 |
|
|
| 261 |
$c[$u] = 1; |
$c[$u] = 1; |
| 262 |
$d[$u] = $time++; |
$d[$u] = $time++; |
| 263 |
foreach (array_keys($edges[$u]) as $v) { |
if (isset($edges[$u])) { |
| 264 |
if ($c[$v] === 0) { |
foreach (array_keys($edges[$u]) as $v) { |
| 265 |
$p[$v] = $u; |
if ($c[$v] === 0) { |
| 266 |
walk($v, $edges, $time, $c, $p, $f, $d); |
$p[$v] = $u; |
| 267 |
|
walk($v, $edges, $time, $c, $p, $f, $d); |
| 268 |
|
} |
| 269 |
} |
} |
| 270 |
} |
} |
| 271 |
$c[$u] = 2; |
$c[$u] = 2; |
| 318 |
* @return array $new_graph The transposed graph |
* @return array $new_graph The transposed graph |
| 319 |
*/ |
*/ |
| 320 |
function transpose($edges) { |
function transpose($edges) { |
| 321 |
$new_graph = array(); |
$transposed_graph = array(); |
| 322 |
foreach (array_keys($edges) as $A) { |
foreach (array_keys($edges) as $A) { |
| 323 |
foreach (array_keys($edges[$A]) as $B) { |
foreach (array_keys($edges[$A]) as $B) { |
| 324 |
$new_graph[$B][$A] = $edges[$A][$B]; |
$transposed_graph[$B][$A] = $edges[$A][$B]; |
| 325 |
} |
} |
| 326 |
} |
} |
| 327 |
return $new_graph; |
return $transposed_graph; |
| 328 |
} |
} |
| 329 |
|
|
| 330 |
/** |
/** |
| 357 |
$groups[$index][] = $key; |
$groups[$index][] = $key; |
| 358 |
unset($prev[$key]); |
unset($prev[$key]); |
| 359 |
} |
} |
|
//$vert_prev = $prev[$vert_prev]; |
|
| 360 |
$time++; |
$time++; |
| 361 |
} |
} |
| 362 |
$index++; |
$index++; |
| 398 |
} |
} |
| 399 |
|
|
| 400 |
/** |
/** |
| 401 |
* Draw a distribution of edges picture in SVG format. (should be replaced with imagemagick integration module?) |
* Draw a distribution of edges picture in SVG format. |
| 402 |
* |
* |
| 403 |
* @param array $distribution |
* @param array $distribution |
| 404 |
* @return string The SVG picture file |
* @return string The SVG picture file |
| 445 |
} |
} |
| 446 |
/* Count the edges between the */ |
/* Count the edges between the */ |
| 447 |
$num_conn = 0; |
$num_conn = 0; |
|
//print_r($edges); |
|
| 448 |
$neighbours = array_keys($edges[$vertex]); |
$neighbours = array_keys($edges[$vertex]); |
| 449 |
foreach ($neighbours as $vert) { |
foreach ($neighbours as $vert) { |
| 450 |
if (is_array($edges[$vert])) { |
if (is_array($edges[$vert])) { |