line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Graph::Statistics; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
56639
|
use 5.008008; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
55
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2219
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
12
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
13
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# This allows declaration use Graph::Statistics ':all'; |
16
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
17
|
|
|
|
|
|
|
# will save memory. |
18
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
19
|
|
|
|
|
|
|
constraint |
20
|
|
|
|
|
|
|
c |
21
|
|
|
|
|
|
|
p |
22
|
|
|
|
|
|
|
CTdi |
23
|
|
|
|
|
|
|
flushCache |
24
|
|
|
|
|
|
|
) ] ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our @EXPORT = qw( |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Preloaded methods go here. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my %p = (); # cache for p() results |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub m { |
40
|
0
|
|
|
0
|
0
|
0
|
my $g = shift; |
41
|
0
|
|
|
|
|
0
|
my $i = shift; |
42
|
0
|
|
|
|
|
0
|
my $j = shift; |
43
|
0
|
|
|
|
|
0
|
my %g = %{$g}; |
|
0
|
|
|
|
|
0
|
|
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
0
|
return $g{$i}{$j} if defined $g{$i}{$j}; |
46
|
0
|
|
|
|
|
0
|
return 0; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub flushCache { |
50
|
1
|
|
|
1
|
1
|
5
|
%p = (); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub CTdi { |
54
|
|
|
|
|
|
|
#Coleman-Theil disorder index |
55
|
|
|
|
|
|
|
#a measure of diversity of contacts |
56
|
|
|
|
|
|
|
#0 = same for all contacts |
57
|
|
|
|
|
|
|
#1 = all constraint is measured in a single relationship |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
0
|
1
|
0
|
my $g = shift; |
60
|
0
|
|
|
|
|
0
|
my $i = shift; |
61
|
0
|
|
|
|
|
0
|
my %g = %{$g}; |
|
0
|
|
|
|
|
0
|
|
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
my @N = keys %{$g{$i}}; |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
my $N = $#N + 1; |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
0
|
return 1 if ( $N == 1 ); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
0
|
my $sum = 0; |
70
|
0
|
|
|
|
|
0
|
my $C = constraint($g,$i); |
71
|
0
|
0
|
|
|
|
0
|
if ( $C ) { |
72
|
0
|
|
|
|
|
0
|
foreach my $j ( @N ) { |
73
|
0
|
0
|
|
|
|
0
|
next if ($j eq $i); |
74
|
0
|
|
|
|
|
0
|
my $factor = c($g, $i, $j)/($C/$N); |
75
|
0
|
0
|
|
|
|
0
|
$sum += $factor * log($factor) if ( $factor ); |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
0
|
$sum /= ($N * log($N)); |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
0
|
return $sum; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub p { |
83
|
45
|
|
|
45
|
1
|
45
|
my $g = shift; # graph |
84
|
45
|
|
|
|
|
46
|
my $i = shift; |
85
|
45
|
|
|
|
|
41
|
my $j = shift; |
86
|
45
|
|
|
|
|
45
|
my %g = %{$g}; |
|
45
|
|
|
|
|
136
|
|
87
|
|
|
|
|
|
|
|
88
|
45
|
100
|
|
|
|
215
|
return $p{$i}{$j} if (defined $p{$i}{$j}); |
89
|
|
|
|
|
|
|
|
90
|
18
|
|
|
|
|
20
|
my @v = keys %{$g{$i}}; |
|
18
|
|
|
|
|
121
|
|
91
|
18
|
|
|
|
|
25
|
my $z = 0; |
92
|
|
|
|
|
|
|
|
93
|
18
|
|
|
|
|
18
|
my $total_w = 0; |
94
|
18
|
|
|
|
|
19
|
my $n; |
95
|
18
|
|
|
|
|
18
|
my $w = undef; |
96
|
18
|
|
|
|
|
24
|
foreach $n ( @v ) { |
97
|
42
|
|
50
|
|
|
89
|
$w = $g{$i}{$n} || 1; |
98
|
42
|
|
|
|
|
59
|
$total_w += $w; |
99
|
42
|
100
|
|
|
|
93
|
next unless ( $n eq $j ); |
100
|
12
|
|
|
|
|
21
|
$z += $w; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
18
|
100
|
|
|
|
64
|
return $p{$i}{$j} = 0 unless ($z > 0); |
104
|
|
|
|
|
|
|
|
105
|
12
|
|
|
|
|
24
|
$p{$i}{$j} = ($z/($total_w)); |
106
|
12
|
|
|
|
|
55
|
return $p{$i}{$j}; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub c { |
111
|
9
|
|
|
9
|
1
|
12
|
my $g = shift; # graph |
112
|
9
|
|
|
|
|
11
|
my $i = shift; |
113
|
9
|
|
|
|
|
10
|
my $j = shift; |
114
|
9
|
|
|
|
|
9
|
my %g = %{$g}; |
|
9
|
|
|
|
|
27
|
|
115
|
|
|
|
|
|
|
|
116
|
9
|
|
|
|
|
20
|
my $sum = p($g,$i,$j); |
117
|
|
|
|
|
|
|
|
118
|
9
|
|
|
|
|
11
|
my @v = keys %{$g{$i}}; |
|
9
|
|
|
|
|
26
|
|
119
|
9
|
|
|
|
|
10
|
my $key; |
120
|
9
|
|
|
|
|
15
|
foreach $key ( @v ) { |
121
|
27
|
50
|
|
|
|
60
|
next if $key eq $i; |
122
|
27
|
100
|
|
|
|
51
|
next if $key eq $j; |
123
|
18
|
|
|
|
|
39
|
$sum += p($g, $i, $key) * p($g, $key, $j); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
9
|
|
|
|
|
37
|
return $sum * $sum; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub constraint { |
130
|
4
|
|
|
4
|
1
|
26
|
my $g = shift; # graph |
131
|
4
|
|
|
|
|
6
|
my $i = shift; |
132
|
4
|
|
|
|
|
5
|
my %g = %{$g}; |
|
4
|
|
|
|
|
19
|
|
133
|
|
|
|
|
|
|
|
134
|
4
|
|
|
|
|
9
|
my $sum = 0; |
135
|
|
|
|
|
|
|
|
136
|
4
|
|
|
|
|
4
|
my @v = keys %{$g{$i}}; |
|
4
|
|
|
|
|
14
|
|
137
|
4
|
100
|
|
|
|
17
|
return 1 if ($#v == 0); |
138
|
3
|
|
|
|
|
4
|
my $node; |
139
|
3
|
|
|
|
|
5
|
foreach $node ( @v ) { |
140
|
9
|
50
|
|
|
|
19
|
next if ( $node eq $i ); |
141
|
9
|
|
|
|
|
28
|
$sum += c($g, $i, $node); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
3
|
|
|
|
|
20
|
return $sum; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
__END__ |