Skip to content

Commit 928544e

Browse files
weilycoderMr-Python-in-China
authored andcommitted
Add test for undirected graph
1 parent 7a5ae57 commit 928544e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

cyaron/graph.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,7 @@ def edge_count(self):
240240
"""edge_count(self) -> int
241241
Return the count of the edges in the graph.
242242
"""
243-
cnt = sum(len(node) for node in self.edges)
244-
if not self.directed:
245-
cnt //= 2
246-
return cnt
243+
return len(list(self.iterate_edges()))
247244

248245
def to_matrix(self, **kwargs):
249246
"""to_matrix(self, **kwargs) -> GraphMatrix

cyaron/tests/graph_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,18 @@ def test_forest(self):
225225
if dsu.get_father(i) == i:
226226
count += 1
227227
self.assertEqual(count, part_count)
228+
229+
def test_from_undirected_degree_sequence(self):
230+
get_deg_seq = lambda g: tuple(map(len, g.edges[1:]))
231+
g1 = Graph.from_degree_sequence((2, 2, 1, 1, 1, 1))
232+
self.assertEqual(get_deg_seq(g1), (2, 2, 1, 1, 1, 1))
233+
for _ in range(8):
234+
g0 = Graph.graph(
235+
100, 400, directed=False, self_loop=False, repeated_edges=False
236+
)
237+
dsq = get_deg_seq(g0)
238+
g1 = Graph.from_degree_sequence(dsq)
239+
with self.assertRaises(ValueError):
240+
Graph.from_degree_sequence((6, 6, 6))
241+
with self.assertRaises(ValueError):
242+
Graph.from_degree_sequence((1, 1, 1))

0 commit comments

Comments
 (0)