-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerformance.cs
87 lines (78 loc) · 2.59 KB
/
Performance.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections.Generic;
using System.Linq;
using MNCD.Core;
namespace MNCD.Evaluation.SingleLayer
{
/// <summary>
/// Implement Performance quality function for communities.
/// Based on:
/// Community detection in graphs
/// https://arxiv.org/abs/0906.0612
/// Santo Fortunato.
/// </summary>
public static class Performance
{
/// <summary>
/// Get perfomance for partition of network.
/// </summary>
/// <param name="network">
/// Network that is partitioned.
/// </param>
/// <param name="communities">
/// List of communities for which the performance should be computed.
/// </param>
/// <returns>
/// Performance of a patitioning of network.
/// </returns>
public static double Get(Network network, List<Community> communities)
{
var intra = GetIntraEdges(network, communities);
var inter = GetInterEdges(network, communities);
var n = network.Actors.Count;
var totalPairs = (n * (n - 1)) / 2.0;
if (totalPairs > 0)
{
return (intra + inter) / totalPairs;
}
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 GetInterEdges(Network network, List<Community> communities)
{
var interEdges = 0;
for (var i = 0; i < communities.Count; i++)
{
for (var j = i + 1; j < communities.Count; j++)
{
var c1 = communities[i];
var c2 = communities[j];
var maximalCount = c1.Size * c2.Size;
foreach (var edge in network.FirstLayer.Edges)
{
if ((c1.Contains(edge.From) && c2.Contains(edge.To)) ||
(c2.Contains(edge.From) && c1.Contains(edge.To)))
{
maximalCount--;
}
}
interEdges += maximalCount;
}
}
return interEdges;
}
}
}