forked from microsoft/graphrag
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFastLabelPropagationCommunityDetector.cs
More file actions
111 lines (92 loc) · 3.79 KB
/
FastLabelPropagationCommunityDetector.cs
File metadata and controls
111 lines (92 loc) · 3.79 KB
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
using GraphRag.Config;
using GraphRag.Entities;
using GraphRag.Relationships;
namespace GraphRag.Community;
internal static class FastLabelPropagationCommunityDetector
{
public static IReadOnlyDictionary<string, string> AssignLabels(
IReadOnlyList<EntityRecord> entities,
IReadOnlyList<RelationshipRecord> relationships,
ClusterGraphConfig config)
{
ArgumentNullException.ThrowIfNull(entities);
ArgumentNullException.ThrowIfNull(relationships);
ArgumentNullException.ThrowIfNull(config);
var adjacency = BuildAdjacency(entities, relationships);
if (adjacency.Count == 0)
{
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
var random = new Random(config.Seed);
var labels = adjacency.Keys.ToDictionary(node => node, node => node, StringComparer.OrdinalIgnoreCase);
var nodes = adjacency.Keys.ToList();
var maxIterations = Math.Max(1, config.MaxIterations);
for (var iteration = 0; iteration < maxIterations; iteration++)
{
var shuffled = nodes.OrderBy(_ => random.Next()).ToList();
var changed = false;
foreach (var node in shuffled)
{
var neighbors = adjacency[node];
if (neighbors.Count == 0)
{
continue;
}
var labelWeights = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase);
foreach (var (neighbor, weight) in neighbors)
{
if (!labels.TryGetValue(neighbor, out var neighborLabel))
{
continue;
}
labelWeights[neighborLabel] = labelWeights.GetValueOrDefault(neighborLabel) + (weight > 0 ? weight : 1);
}
if (labelWeights.Count == 0)
{
continue;
}
var maxWeight = labelWeights.Values.Max();
var candidates = labelWeights
.Where(pair => Math.Abs(pair.Value - maxWeight) < 1e-6)
.Select(pair => pair.Key)
.ToList();
var chosen = candidates.Count == 1
? candidates[0]
: candidates[random.Next(candidates.Count)];
if (!string.Equals(labels[node], chosen, StringComparison.OrdinalIgnoreCase))
{
labels[node] = chosen;
changed = true;
}
}
if (!changed)
{
break;
}
}
return labels;
}
private static Dictionary<string, List<(string Neighbor, double Weight)>> BuildAdjacency(
IReadOnlyList<EntityRecord> entities,
IReadOnlyList<RelationshipRecord> relationships)
{
var adjacency = entities
.ToDictionary(entity => entity.Title, _ => new List<(string, double)>(), StringComparer.OrdinalIgnoreCase);
foreach (var relationship in relationships)
{
if (!adjacency.TryGetValue(relationship.Source, out var sourceNeighbors))
{
sourceNeighbors = new List<(string, double)>();
adjacency[relationship.Source] = sourceNeighbors;
}
if (!adjacency.TryGetValue(relationship.Target, out var targetNeighbors))
{
targetNeighbors = new List<(string, double)>();
adjacency[relationship.Target] = targetNeighbors;
}
sourceNeighbors.Add((relationship.Target, relationship.Weight));
targetNeighbors.Add((relationship.Source, relationship.Weight));
}
return adjacency;
}
}