Skip to content

Commit 3ca37c8

Browse files
committed
add test_shuffle
1 parent d5f7980 commit 3ca37c8

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

cyaron/graph.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ def __init__(self, point_count, directed=False):
4646
"""
4747
self.directed = directed
4848
self.edges = [[] for i in range(point_count + 1)]
49-
49+
50+
def vertex_count(self):
51+
"""edge_count(self) -> int
52+
Return the vertex of the edges in the graph.
53+
"""
54+
return len(self.edges) - 1
55+
5056
def edge_count(self):
5157
"""edge_count(self) -> int
5258
Return the count of the edges in the graph.

cyaron/tests/graph_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import itertools
2+
import random
13
import unittest
24
from cyaron import Graph
35

@@ -172,3 +174,56 @@ def test_GraphMatrix(self):
172174
self.assertEqual(str(g.to_matrix(default=9, merge=merge3)), "9 9 3\n9 9 3\n9 1 1")
173175
self.assertEqual(str(g.to_matrix(default=0, merge=merge4)), "0 0 3\n0 0 1\n0 1 1")
174176
self.assertEqual(str(g.to_matrix(default=0, merge=merge5)), "0 0 3\n0 0 84\n0 1 1")
177+
178+
def test_shuffle(self):
179+
def read_graph(n, data, directed = False):
180+
g = Graph(n, directed)
181+
for l in data.split('\n'):
182+
u, v, w = map(int, l.split())
183+
g.add_edge(u, v, weight=w)
184+
return g
185+
186+
def isomorphic(graph1, graph2, mapping = None, directed = False):
187+
n = graph1.vertex_count()
188+
if n != graph2.vertex_count():
189+
return False
190+
if graph1.edge_count() != graph2.edge_count():
191+
return False
192+
if mapping is None:
193+
for per in itertools.permutations(range(1, n + 1)):
194+
if isomorphic(graph1, graph2, (0, ) + per):
195+
return True
196+
return False
197+
edges = {}
198+
for e in graph2.iterate_edges():
199+
key, val = (e.start, e.end), e.weight
200+
if key in edges:
201+
edges[key].append(val)
202+
else:
203+
edges[key] = [val]
204+
for e in graph1.iterate_edges():
205+
key, val = (mapping[e.start], mapping[e.end]), e.weight
206+
if not directed and key[0] > key[1]:
207+
key = key[1], key[0]
208+
if key not in edges:
209+
return False
210+
if val not in edges[key]:
211+
return False
212+
edges[key].remove(val)
213+
return True
214+
215+
def unit_test(n, m, shuffle_kwargs = {}, check_kwargs = {}):
216+
g = Graph.graph(n, m)
217+
data = g.to_str(**shuffle_kwargs)
218+
h = read_graph(n, data)
219+
self.assertTrue(isomorphic(g, h, **check_kwargs))
220+
221+
unit_test(8, 20)
222+
unit_test(8, 20, {"shuffle": True})
223+
mapping = [0] + random.sample(range(1, 8), k = 7)
224+
shuffer = lambda seq: list(map(lambda i: mapping[i], seq))
225+
unit_test(7, 10, {"shuffle": True, "node_shuffler": shuffer})
226+
unit_test(7, 14, {"shuffle": True, "node_shuffler": shuffer}, {"mapping": mapping})
227+
shuffer_without_swap = lambda table: random.sample(table, k=len(table))
228+
unit_test(7, 12, {"shuffle": True, "edge_shuffler": shuffer_without_swap}, {"directed": True})
229+

0 commit comments

Comments
 (0)