-
Notifications
You must be signed in to change notification settings - Fork 1
/
Coverage.cs
69 lines (63 loc) · 1.95 KB
/
Coverage.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using MNCD.Core;
namespace MNCD.Evaluation.SingleLayer
{
/// <summary>
/// Coverage
/// Based on:
/// Community detection in graphs
/// https://arxiv.org/abs/0906.0612
/// Santo Fortunato.
/// </summary>
public static class Coverage
{
/// <summary>
/// Computes coverage for partition, the ratio of the number of intra-community
/// edges by the total number of edges.
/// </summary>
/// <param name="network">
/// Network that is partitioned.
/// </param>
/// <param name="communities">
/// List of communities for which the coverage should be computed.
/// </param>
/// <returns>
/// The ratio of the number of intra-community edges by the total number of edges.
/// </returns>
public static double Get(Network network, List<Community> communities)
{
if (network.LayerCount != 1)
{
throw new ArgumentException("Coverage only works on single layer networks.");
}
var intra = GetIntraEdges(network, communities);
var total = GetTotalEdges(network);
if (total > 0)
{
return intra / (double)total;
}
else
{
return 1.0;
}
}
private static int GetIntraEdges(Network network, List<Community> communities)
{
var count = 0;
foreach (var edge in network.Layers.First().Edges)
{
if (communities.Any(c => c.Actors.Contains(edge.From) && c.Actors.Contains(edge.To)))
{
count++;
}
}
return count;
}
private static int GetTotalEdges(Network network)
{
return network.Layers.First().Edges.Count;
}
}
}