forked from rdmpage/geojson-phylogeny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossing.php
84 lines (69 loc) · 1.36 KB
/
crossing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
Barth, W., Mutzel, P., & Jünger, M. (2004). Simple and Efficient Bilayer Cross Counting.
Journal of Graph Algorithms and Applications, 8(2), 179-194.
doi:10.7155/jgaa.00088
*/
// Layer is an array of edges, where each edge is a pair of integers.
function count_crossings($layer)
{
// 1. Sort layer lexically
asort($layer);
// 2. Get positions of "southern nodes"
$southsequence = array();
foreach ($layer as $edge)
{
$southsequence[] = $edge[1];
}
// 3. count crossings
$firstindex = 1;
$q = count(array_unique($southsequence));
while ($firstindex < $q)
{
$firstindex *= 2;
}
$treesize = 2 *$firstindex - 1;
$firstindex--;
$tree = array_fill(0, $treesize, 0);
$crosscount = 0;
$r = count($layer); // number of edges
for ($k = 0; $k < $r; $k++) // insert edge k
{
$index = $southsequence[$k] + $firstindex;
$tree[$index]++;
while ($index > 0)
{
if ($index % 2 != 0)
{
$crosscount += $tree[$index + 1];
}
$index = floor(($index - 1)/2);
$tree[$index]++;
}
}
return $crosscount;
}
/*
$layer = array(
array(0,1),
array(1,2),
array(1,0),
array(0,2)
);
$layer = array(
array(0,0),
array(1,1),
array(1,2),
array(2,0),
array(2,3),
array(2,4),
array(3,0),
array(3,2),
array(4,3),
array(5,2),
array(5,4)
);
$crossings = count_crossings($layer);
echo "Number of crossings: $crossings\n";
*/
?>