Skip to content

Commit 011d6cc

Browse files
committed
add test_GraphMatrix
1 parent c35e622 commit 011d6cc

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

cyaron/graph.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from .utils import *
22
import random
3-
from typing import TypeVar, Callable, List
3+
from typing import TypeVar, Callable
4+
5+
6+
__all__ = ["Edge", "Graph"]
47

58

69
class Edge:
@@ -528,7 +531,11 @@ def _calc_max_edge(point_count, directed, self_loop):
528531

529532

530533
class GraphMatrix:
531-
"""Class GraphMatrix: A class of the graph represented by adjacency matrix"""
534+
"""
535+
Class GraphMatrix: A class of the graph represented by adjacency matrix.
536+
537+
*Deprecation warning: This class may be removed after a generic matrix class is implemented in the project.*
538+
"""
532539

533540
T = TypeVar('T')
534541

cyaron/tests/graph_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,24 @@ def test_DAG_boundary(self):
151151
with self.assertRaises(Exception, msg="the number of edges of connected graph must more than the number of nodes - 1"):
152152
Graph.DAG(8, 6)
153153
Graph.DAG(8, 7)
154+
155+
def test_GraphMatrix(self):
156+
g = Graph(3, True)
157+
edge_set = [(2, 3, 3), (3, 3, 1), (2, 3, 7), (2, 3, 4), (3, 2, 1), (1, 3, 3)]
158+
for u, v, w in edge_set:
159+
g.add_edge(u, v, weight=w)
160+
self.assertEqual(str(g.to_matrix()), "-1 -1 3\n-1 -1 4\n-1 1 1")
161+
self.assertEqual(str(g.to_matrix(default=0)), "0 0 3\n0 0 4\n0 1 1")
162+
# lambda val, edge: edge.weight
163+
gcd = lambda a, b: (gcd(b, a % b) if b else a)
164+
lcm = lambda a, b: a * b // gcd(a, b)
165+
merge1 = lambda v, e: v if v != -1 else e.weight
166+
merge2 = lambda val, edge: max(edge.weight, val)
167+
merge3 = lambda val, edge: min(edge.weight, val)
168+
merge4 = lambda val, edge: gcd(val, edge.weight)
169+
merge5 = lambda val, edge: lcm(val, edge.weight) if val else edge.weight
170+
self.assertEqual(str(g.to_matrix(merge=merge1)), "-1 -1 3\n-1 -1 3\n-1 1 1")
171+
self.assertEqual(str(g.to_matrix(merge=merge2)), "-1 -1 3\n-1 -1 7\n-1 1 1")
172+
self.assertEqual(str(g.to_matrix(default=9, merge=merge3)), "9 9 3\n9 9 3\n9 1 1")
173+
self.assertEqual(str(g.to_matrix(default=0, merge=merge4)), "0 0 3\n0 0 1\n0 1 1")
174+
self.assertEqual(str(g.to_matrix(default=0, merge=merge5)), "0 0 3\n0 0 84\n0 1 1")

0 commit comments

Comments
 (0)