Skip to content

Commit

Permalink
Add test for undirected graph
Browse files Browse the repository at this point in the history
  • Loading branch information
weilycoder committed Dec 15, 2024
1 parent cc764bc commit faf7b3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 1 addition & 4 deletions cyaron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ def edge_count(self):
"""edge_count(self) -> int
Return the count of the edges in the graph.
"""
cnt = sum(len(node) for node in self.edges)
if not self.directed:
cnt //= 2
return cnt
return len(list(self.iterate_edges()))

def to_matrix(self, **kwargs):
"""to_matrix(self, **kwargs) -> GraphMatrix
Expand Down
15 changes: 15 additions & 0 deletions cyaron/tests/graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,18 @@ def test_forest(self):
if dsu.get_father(i) == i:
count += 1
self.assertEqual(count, part_count)

def test_from_undirected_degree_sequence(self):
get_deg_seq = lambda g: tuple(map(len, g.edges[1:]))
g1 = Graph.from_degree_sequence((2, 2, 1, 1, 1, 1))
self.assertEqual(get_deg_seq(g1), (2, 2, 1, 1, 1, 1))
for _ in range(8):
g0 = Graph.graph(
100, 400, directed=False, self_loop=False, repeated_edges=False
)
dsq = get_deg_seq(g0)
g1 = Graph.from_degree_sequence(dsq)
with self.assertRaises(ValueError):
Graph.from_degree_sequence((6, 6, 6))
with self.assertRaises(ValueError):
Graph.from_degree_sequence((1, 1, 1))

0 comments on commit faf7b3f

Please sign in to comment.