-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclustering.py
30 lines (20 loc) · 828 Bytes
/
clustering.py
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
from disjoint_set import DisjointSet
from graph_data_structures2 import Graph
class DisjointSetWithSize(DisjointSet):
def __len__(self):
return len(list(self.itersets()))
def sort_graph(graph: Graph) -> Graph:
return Graph(*sorted(graph, key=lambda edge: edge.weight))
def clustering(graph: Graph, k: int) -> int:
graph = sort_graph(graph)
ds: DisjointSetWithSize = DisjointSetWithSize()
for vertex in graph.vertices:
ds.find(vertex)
for edge in graph:
if not ds.connected(edge.start, edge.end):
if len(ds) == k:
return edge.weight
component_1 = ds.find(edge.start)
component_2 = ds.find(edge.end)
ds.union(component_1, component_2)
raise Exception("Clustering did not terminate.") # pragma: no cover