@@ -67,3 +67,54 @@ def test_self_loop(self):
6767 self .graph .add_edge (1 , 1 , None )
6868 res = sorted (sorted (c ) for c in rustworkx .cycle_basis (self .graph , 0 ))
6969 self .assertEqual ([[0 , 1 , 2 , 3 ], [0 , 1 , 6 , 7 , 8 ], [0 , 3 , 4 , 5 ], [1 ]], res )
70+
71+
72+ class TestCycleBasisEdges (unittest .TestCase ):
73+ def setUp (self ):
74+ self .graph = rustworkx .PyGraph ()
75+ self .graph .add_nodes_from (list (range (10 )))
76+ self .graph .add_edges_from_no_data (
77+ [
78+ (0 , 1 ),
79+ (0 , 2 ),
80+ (1 , 2 ),
81+ (2 , 3 ),
82+ (3 , 4 ),
83+ (3 , 5 ),
84+ (4 , 5 ),
85+ (4 , 6 ),
86+ (6 , 7 ),
87+ (6 , 8 ),
88+ (8 , 9 ),
89+ ]
90+ )
91+
92+ def test_cycle_basis_edges (self ):
93+ graph = self .graph
94+ res = sorted (sorted (c ) for c in rustworkx .cycle_basis_edges (graph , 0 ))
95+ self .assertEqual ([[0 , 1 , 2 ], [4 , 5 , 6 ]], res )
96+
97+ def test_cycle_basis_edges_multiple_roots_same_cycles (self ):
98+ res = sorted (sorted (x ) for x in rustworkx .cycle_basis_edges (self .graph , 0 ))
99+ self .assertEqual ([[0 , 1 , 2 ], [4 , 5 , 6 ]], res )
100+ res = sorted (sorted (x ) for x in rustworkx .cycle_basis_edges (self .graph , 5 ))
101+ self .assertEqual ([[0 , 1 , 2 ], [4 , 5 , 6 ]], res )
102+ res = sorted (sorted (x ) for x in rustworkx .cycle_basis_edges (self .graph , 7 ))
103+ self .assertEqual ([[0 , 1 , 2 ], [4 , 5 , 6 ]], res )
104+
105+ def test_cycle_basis_disconnected_graphs (self ):
106+ self .graph .add_nodes_from (["A" , "B" , "C" ])
107+ self .graph .add_edges_from_no_data ([(10 , 11 ), (10 , 12 ), (11 , 12 )])
108+ cycles = rustworkx .cycle_basis_edges (self .graph , 9 )
109+ res = sorted (sorted (x ) for x in cycles [:- 1 ]) + [sorted (cycles [- 1 ])]
110+ self .assertEqual (res , [[0 , 1 , 2 ], [4 , 5 , 6 ], [11 , 12 , 13 ]])
111+
112+ def test_invalid_types (self ):
113+ digraph = rustworkx .PyDiGraph ()
114+ with self .assertRaises (TypeError ):
115+ rustworkx .cycle_basis_edges (digraph )
116+
117+ def test_self_loop (self ):
118+ self .graph .add_edge (1 , 1 , None )
119+ res = sorted (sorted (c ) for c in rustworkx .cycle_basis_edges (self .graph , 0 ))
120+ self .assertEqual ([[0 , 1 , 2 ], [4 , 5 , 6 ], [11 ]], res )
0 commit comments