-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiduccia_mattheyses.py
68 lines (56 loc) · 2.29 KB
/
fiduccia_mattheyses.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
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
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
def initial_partition(graph, k):
subsets = [[] for _ in range(k)]
vertices = list(graph.nodes())
random.shuffle(vertices)
for i, vertex in enumerate(vertices):
subsets[i % k].append(vertex)
return subsets
def compute_gain(graph, vertex, current_subset, other_subset):
gain = 0
for neighbor in graph.neighbors(vertex):
if neighbor in current_subset: gain -= 1
if neighbor in other_subset: gain += 1
return gain
def fiduccia_mattheyses(graph, k, max_iterations=100):
current_partition = initial_partition(graph, k)
for _ in range(max_iterations):
moved = False
for subset_index, current_subset in enumerate(current_partition):
for vertex in current_subset:
gains = [compute_gain(graph, vertex, current_subset, other_subset)
for other_subset in current_partition if other_subset != current_subset]
max_gain_subset = max(range(len(gains)), key=lambda i: gains[i])
if gains[max_gain_subset] > 0:
current_partition[subset_index].remove(vertex)
current_partition[max_gain_subset].append(vertex)
moved = True
yield current_partition
if not moved: break
G = nx.complete_graph(10)
k = 3
pos = nx.spring_layout(G)
fig, ax = plt.subplots()
ax.set_title('Fiduccia-Mattheyses Algorithm Animation')
def update(partition):
ax.clear()
ax.set_title('Fiduccia-Mattheyses Algorithm Animation')
colors = ['blue', 'green', 'yellow']
for i, subset in enumerate(partition):
nx.draw_networkx_nodes(G, pos, nodelist=subset, node_color=colors[i])
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)
ani = animation.FuncAnimation(fig,
update,
frames=fiduccia_mattheyses(G, k),
blit=False,
interval=1000,
repeat=False,
save_count=1000)
file_format = 'gif'
filename = 'fiduccia_mattheyses_animation.' + file_format
ani.save(filename, writer='imagemagick', fps=1)
plt.show()