-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExclusivity.cs
83 lines (73 loc) · 2.66 KB
/
Exclusivity.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
using System;
using System.Collections.Generic;
using MNCD.Core;
namespace MNCD.Evaluation.MultiLayer
{
/// <summary>
/// Implements exclusivity measure.
/// Based on:
/// Finding Redundant and Complementary Communities in Multidimensional Networks
/// http://www.michelecoscia.com/wp-content/uploads/2012/08/cosciacikm11.pdf
/// Michele Berlingerio, Michele Coscia, Fosca Giannotti.
/// </summary>
public static class Exclusivity
{
/// <summary>
/// Exclusivity can be computed as the ratio between the
/// number of exclusive connections within the community and
/// the total number of connected pairs in c.
/// </summary>
/// <param name="community">Community for which the exclusivity should be computed.</param>
/// <param name="network">Network in which the community resides.</param>
/// <returns>Exclusivity of community in network.</returns>
public static double Compute(Community community, Network network)
{
if (network.LayerCount <= 1)
{
throw new ArgumentException("Exclusivity can be computed only for multi-layered networkx.");
}
if (community.Size == 0)
{
return 0;
}
var pairToLayers = new Dictionary<ValueTuple<Actor, Actor>, HashSet<Layer>>();
foreach (var layer in network.Layers)
{
foreach (var edge in layer.Edges)
{
if (community.Actors.Contains(edge.From) &&
community.Actors.Contains(edge.To))
{
var pair = (edge.From, edge.To);
if (pairToLayers.ContainsKey(pair))
{
pairToLayers[pair].Add(layer);
}
else
{
pairToLayers[pair] = new HashSet<Layer>
{
layer,
};
}
}
}
}
var exclusiveConnections = 0;
var totalConnections = 0;
foreach (var layers in pairToLayers.Values)
{
if (layers.Count == 1)
{
exclusiveConnections++;
}
totalConnections += layers.Count;
}
if (totalConnections == 0)
{
return 0;
}
return exclusiveConnections / (double)totalConnections;
}
}
}