Source file statistics.ml
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
(** Small World analisys *)
open Graph
open ExtLib
open Dose_common
include Util.Logging (struct
let label = "dose_algo.statistics"
end)
module Make (G : Sig.I) = struct
module VS = Set.Make (G.V)
module UndG = Imperative.Graph.Concrete (G.V)
let undirect g =
let g2 = UndG.create () in
G.iter_vertex
(fun v ->
UndG.add_vertex g2 v ;
G.iter_succ (fun v' -> UndG.add_edge g2 v v') g v ;
G.iter_pred (fun v' -> UndG.add_edge g2 v' v) g v)
g ;
g2
let clustering_coefficient graph vertex =
let neighbours = G.succ graph vertex in
let n = List.length neighbours in
if n = 0 then 0.0
else if n = 1 then 1.0
else
let n_edges =
List.fold_left
(fun old_sum v ->
old_sum
+ List.fold_left
(fun old_sum' v' ->
if G.mem_edge graph v v' && v <> v' then old_sum' + 1
else old_sum')
0
neighbours)
0
neighbours
and max_edges = if G.is_directed then n * (n - 1) else n * (n - 1) / 2 in
float_of_int n_edges /. float_of_int max_edges
let average_distance graph vertex =
let rec add_successors distance visited vertices =
let succs = ref [] in
let (n, sum) =
List.fold_left
(fun (old_n, old_sum) v ->
if not (VS.mem v !visited) then (
visited := VS.add v !visited ;
succs := G.succ graph v :: !succs ;
(old_n + 1, old_sum + distance))
else (old_n, old_sum))
(0, 0)
vertices
in
let sf = List.flatten !succs in
if sf <> [] then
let (n', sum') = add_successors (distance + 1) visited sf in
(n + n', sum + sum')
else (n, sum)
in
let visited = ref (VS.singleton vertex) in
let (n, sum) = add_successors 1 visited (G.succ graph vertex) in
if sum = 0 then 0.0 else float_of_int sum /. float_of_int n
module MSin = Map.Make (struct
type t = G.V.t * G.t ref
let compare (v1, _) (v2, g) = G.in_degree !g v1 - G.in_degree !g v2
end)
module MSout = Map.Make (struct
type t = G.V.t * G.t ref
let compare (v1, _) (v2, g) = G.out_degree !g v1 - G.out_degree !g v2
end)
let _avgdegree = ref None
let _outdata = ref None
let _indata = ref None
let _outdatadegree = ref MSout.empty
let _indatadegree = ref MSin.empty
let degree graph =
let nv = G.nb_vertex graph in
let outmax = ref 0 in
let inmax = ref 0 in
let outh = Hashtbl.create 1031 in
let inh = Hashtbl.create 1031 in
let add h d =
if d = 0 then ()
else
try Hashtbl.replace h d (Hashtbl.find h d + 1)
with Not_found -> Hashtbl.add h d 1
in
let total =
G.fold_vertex
(fun v sum ->
let outdeg = G.out_degree graph v in
let indeg = G.in_degree graph v in
add outh outdeg ;
add inh indeg ;
_indatadegree := MSin.add (v, ref graph) indeg !_indatadegree ;
_outdatadegree := MSout.add (v, ref graph) outdeg !_outdatadegree ;
sum + indeg)
graph
0
in
(float_of_int total /. float_of_int nv, !outmax, !inmax, outh, inh)
let computeDegree graph =
if Option.is_some !_avgdegree then ()
else
let (avdeg, _maxout, _maxin, outdata, indata) = degree graph in
_avgdegree := Some avdeg ;
_outdata := Some outdata ;
_indata := Some indata
let maxOutDegree graph =
computeDegree graph ;
snd (MSout.max_binding !_outdatadegree)
let maxInDegree graph =
computeDegree graph ;
snd (MSin.max_binding !_indatadegree)
let averageDegree graph =
computeDegree graph ;
Option.get !_avgdegree
let zdp graph =
G.fold_vertex
(fun v sum ->
if G.in_degree graph v = 0 && G.out_degree graph v = 0 then sum + 1
else sum)
graph
0
let scatteredPlotIn graph =
computeDegree graph ;
Option.get !_indata
let scatteredPlotOut graph =
computeDegree graph ;
Option.get !_outdata
let scatteredPlotBoth graph =
let add h i o =
try Hashtbl.replace h (i, o) (Hashtbl.find h (i, o) + 1)
with Not_found -> Hashtbl.add h (i, o) 1
in
let h = Hashtbl.create 1031 in
G.iter_vertex
(fun v -> add h (G.in_degree graph v) (G.out_degree graph v))
graph ;
h
let centralityDegree graph fd =
let n = float_of_int (G.nb_vertex graph) in
let cd v = float_of_int v /. (n -. 1.0) in
let m =
G.fold_vertex
(fun v max ->
let s = List.length (fd graph v) in
let m = cd s in
if m > max then m else max)
graph
0.0
in
let c =
G.fold_vertex
(fun v sum ->
let s = List.length (fd graph v) in
sum +. (m -. cd s))
graph
0.0
in
c /. (n -. 2.0)
let centralityOutDegree graph = centralityDegree graph G.succ
let centralityInDegree graph = centralityDegree graph G.pred
let clustering graph =
let n = float_of_int (G.nb_vertex graph) in
let c =
G.fold_vertex
(fun v acc -> acc +. clustering_coefficient graph v)
graph
0.0
in
c /. n
let averageShortestPathLength graph =
let n = float_of_int (G.nb_vertex graph) in
let c =
G.fold_vertex (fun v acc -> acc +. average_distance graph v) graph 0.0
in
c /. n
let components graph =
let module C = Components.Make (G) in
C.scc_array graph
let weaklycomponents graph =
let module C = Components.Make (UndG) in
C.scc_array (undirect graph)
let numberComponents c = Array.length c
let averageComponents c =
let sum = Array.fold_left (fun acc i -> acc + List.length i) 0 c in
float_of_int sum /. float_of_int (Array.length c)
let largestComponent c =
Array.sort (fun x y -> compare (List.length x) (List.length y)) c ;
List.length c.(Array.length c - 1)
let density graph =
let n = float_of_int (G.nb_vertex graph) in
let ps_edg = n *. (n -. 1.0) in
float_of_int (G.nb_edges graph) /. ps_edg
let averageTwoStepReach graph =
let module S = Set.Make (struct
type t = G.vertex
let compare = compare
end) in
let n = float_of_int (G.nb_vertex graph) in
let t =
G.fold_vertex
(fun i0 total ->
let s =
G.fold_succ
(fun i1 set1 ->
G.fold_succ (fun i2 set2 -> S.add i2 set2) graph i1 set1)
graph
i0
S.empty
in
total +. float_of_int (S.cardinal s))
graph
0.0
in
t /. n
let removezdp graph =
let g = G.copy graph in
G.iter_vertex
(fun v ->
if G.in_degree graph v = 0 && G.out_degree graph v = 0 then
G.remove_vertex g v)
graph ;
g
end