Skip to content

Commit b82042d

Browse files
add gif
1 parent 72ae189 commit b82042d

File tree

6 files changed

+97
-11
lines changed

6 files changed

+97
-11
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

animation.gif

709 KB
Loading

main.py

100644100755
Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,75 @@
11
import random
22
from copy import deepcopy
3+
import networkx as nx
4+
import matplotlib.pyplot as plt
5+
import glob
6+
import imageio
7+
import os
8+
import cv2
9+
import numpy as np
310

411

512
class FindMaximumMatching:
613
def __init__(self, edges, vertices):
14+
self.graph = nx.Graph()
715
self.edges = edges
816
self.vertices = set(vertices)
917
self.matching = []
1018
self.saturated_vertices = set()
1119
self.separate = False
20+
self.graph.add_nodes_from(self.vertices)
21+
self.graph.add_edges_from(self.edges)
22+
self.pos = nx.spring_layout(self.graph)
23+
self.x = 0
24+
self.images = []
25+
self.draw_graph()
26+
27+
def draw_graph(self, augmenting_path=None):
28+
29+
nx.draw(self.graph, self.pos, with_labels=True, font_weight='bold', node_size=1000, node_color='#efc20e', width=4,
30+
edge_color='#82807b', alpha=0.8, font_size=16)
31+
temp = []
32+
if augmenting_path:
33+
for edge in augmenting_path:
34+
temp.append(tuple(edge))
35+
nx.draw_networkx_edges(self.graph, self.pos,
36+
edgelist=temp,
37+
width=8, alpha=0.5, edge_color='r')
38+
39+
for edge in self.matching:
40+
temp.append(tuple(edge))
41+
nx.draw_networkx_edges(self.graph, self.pos,
42+
edgelist=temp,
43+
width=8, alpha=0.5, edge_color='b')
44+
nx.draw_networkx_nodes(self.graph, self.pos, nodelist=list(self.saturated_vertices), node_color='#207c36',
45+
node_size=1000, alpha=0.8)
46+
47+
string = 'Matching Number: ' + str(len(self.matching))
48+
plt.axis('off')
49+
plt.text(-1, 1, string)
50+
51+
plt.savefig('img{}.png'.format(self.x), dpi=120, bbox_inches='tight')
52+
self.x = self.x + 1
53+
plt.close()
1254

1355
def find_maximum_matching(self):
1456
print('matching', self.matching)
1557
fake_matching = []
58+
useless_edges = set()
1659
reveiw_augmenting_path = False
1760
i = 0
1861
while True:
1962
print('--------------', i, '--------------')
2063
i = i + 1
2164
unsaturated = self.vertices.difference(self.saturated_vertices)
65+
if reveiw_augmenting_path:
66+
unsaturated = unsaturated.difference(useless_edges)
67+
2268
if len(unsaturated) <= 1:
2369
print('yaaay')
2470
return
2571
if reveiw_augmenting_path:
2672
print('reveiwwwww')
27-
print('fucking unsaturated', unsaturated)
2873
start = unsaturated.pop()
2974
for finish in unsaturated:
3075
path = self.bfs_find_path_between(start, finish)
@@ -42,15 +87,19 @@ def find_maximum_matching(self):
4287
for j in range(0, len(augmenting_vertices) - 1):
4388
augmenting_path.append({augmenting_vertices[j], augmenting_vertices[j + 1]})
4489
print(augmenting_path)
90+
self.draw_graph(augmenting_path)
91+
4592
for edge in augmenting_path:
4693
if edge in self.matching:
4794
self.matching.remove(edge)
4895
else:
4996
self.matching.append(edge)
5097
print('matching', self.matching)
98+
self.draw_graph()
99+
51100
break
52101
else:
53-
self.vertices.remove(start)
102+
useless_edges.add(start)
54103

55104
else:
56105
if not self.separate:
@@ -63,8 +112,10 @@ def find_maximum_matching(self):
63112
for j in range(0, len(augmenting_vertices) - 1):
64113
augmenting_path.append({augmenting_vertices[j], augmenting_vertices[j + 1]})
65114
print(augmenting_path)
115+
self.draw_graph(augmenting_path)
66116
self.matching = [edge for edge in augmenting_path if edge not in self.matching]
67117
print('matching', self.matching)
118+
self.draw_graph()
68119
else:
69120
self.separate = True
70121
fake_matching = []
@@ -83,13 +134,15 @@ def find_maximum_matching(self):
83134
for j in range(0, len(augmenting_vertices) - 1):
84135
augmenting_path.append({augmenting_vertices[j], augmenting_vertices[j + 1]})
85136
print(augmenting_path)
137+
self.draw_graph(augmenting_path)
86138
for edge in augmenting_path:
87139
if edge in self.matching:
88140
self.matching.remove(edge)
89141
else:
90142
self.matching.append(edge)
91143

92144
print('matching', self.matching)
145+
self.draw_graph()
93146
fake_matching = [edge for edge in augmenting_path if edge not in fake_matching]
94147
print('fake matching ', fake_matching)
95148
else:
@@ -157,9 +210,7 @@ def bfs_find_path_between(self, start, finish):
157210
visited.append(node)
158211

159212
def dfs(self, depth, vertices, v, is_matching, fake_matching=None, finish=None):
160-
print("depth ", depth, "vertices", vertices, "v", v, is_matching)
161213
if finish is not None:
162-
print('aaaaa')
163214
if depth is 0 and v is finish:
164215
return [v]
165216
else:
@@ -175,15 +226,13 @@ def dfs(self, depth, vertices, v, is_matching, fake_matching=None, finish=None):
175226

176227
else:
177228
connected_edges = [edge for edge in connected_edges if edge in self.matching]
178-
print(connected_edges)
179229
vertices.remove(v)
180230
if len(connected_edges) is 1:
181231
v1, v2 = connected_edges[0]
182232
if v1 == v:
183233
path = self.dfs(depth - 1, deepcopy(vertices), v2, not is_matching, fake_matching, finish)
184234
else:
185235
path = self.dfs(depth - 1, deepcopy(vertices), v1, not is_matching, fake_matching, finish)
186-
# print('pp', path)
187236
if path:
188237
path.append(v)
189238
return path
@@ -192,15 +241,13 @@ def dfs(self, depth, vertices, v, is_matching, fake_matching=None, finish=None):
192241
connected_edges = [edge for edge in connected_edges if edge not in fake_matching]
193242
else:
194243
connected_edges = [edge for edge in connected_edges if edge not in self.matching]
195-
print(connected_edges)
196244
vertices.remove(v)
197245
for edge in connected_edges:
198246
v1, v2 = edge
199247
if v1 == v:
200248
path = self.dfs(depth - 1, deepcopy(vertices), v2, not is_matching, fake_matching, finish)
201249
else:
202250
path = self.dfs(depth - 1, deepcopy(vertices), v1, not is_matching, fake_matching, finish)
203-
# print('p', path)
204251
if path:
205252
path.append(v)
206253
return path
@@ -219,11 +266,37 @@ def find_connected_edges_to_vertex(self, vertex, vertices):
219266
return result
220267

221268

222-
vertices = [1, 2, 3, 4, 5, 6, 7, 8]
223-
# edges = [{1, 6}, {1, 7}, {1, 9}, {1, 10}, {2, 6}, {2, 7}, {2, 9}, {2, 10}, {3, 8}, {3, 10}, {4, 6}, {4, 10}, {5, 10}]
269+
def make_circuit_video(movie_filename, fps):
270+
# sorting filenames in order
271+
filenames = glob.glob('img*.png')
272+
filenames_sort_indices = np.argsort([int(os.path.basename(filename).split('.')[0][3:]) for filename in filenames])
273+
filenames = [filenames[i] for i in filenames_sort_indices]
274+
275+
# make movie
276+
with imageio.get_writer(movie_filename, mode='I', fps=fps) as writer:
277+
for filename in filenames:
278+
image = imageio.imread(filename)
279+
cv2.imshow('hel', image)
280+
key = cv2.waitKey(1000) # ~ 30 frames per second
281+
282+
os.remove(filename)
283+
writer.append_data(image)
284+
285+
286+
287+
# sample 1
288+
vertices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
289+
edges = [{1, 6}, {1, 7}, {1, 9}, {1, 10}, {2, 6}, {2, 7}, {2, 9}, {2, 10}, {3, 8}, {3, 10}, {4, 6}, {4, 10}, {5, 10}]
290+
291+
## sample 2
292+
# vertices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
224293
# edges = [{1, 6}, {1, 7}, {2, 7}, {2, 9}, {3, 8}, {3, 6}, {4, 7}, {4, 10}, {5, 10}, {5, 8}]
225-
edges = [{1, 3}, {1, 6}, {1, 8}, {2, 4}, {2, 6}, {2, 7}, {2, 8}]
294+
295+
## sample 3
296+
# vertices = [1, 2, 3, 4, 5, 6, 7, 8]
297+
# edges = [{1, 4}, {1, 5}, {1, 6}, {2, 5}, {2, 7}, {2, 8}, {3, 5}, {3, 8}]
226298

227299
f = FindMaximumMatching(edges, vertices)
228300
f.find_maximum_matching()
229301
print(f.matching)
302+
make_circuit_video('animation.gif', fps=1)

requirement.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cycler==0.10.0
2+
decorator==4.4.0
3+
imageio==2.5.0
4+
kiwisolver==1.1.0
5+
matplotlib==3.0.3
6+
networkx==2.3
7+
numpy==1.16.3
8+
opencv-contrib-python==4.1.0.25
9+
Pillow==6.0.0
10+
pyparsing==2.4.0
11+
python-dateutil==2.8.0
12+
python-igraph==0.7.1.post6
13+
six==1.12.0

0 commit comments

Comments
 (0)