@@ -199,39 +199,109 @@ def test_samecomponent(self):
199199
200200
201201class TestNotRecomputed (unittest .TestCase ):
202- """The traversal runs once, and again only after the graph has changed"""
202+ """The labels are found once, and again only after the graph has changed
203203
204- def traversals (self , g , ask ):
205- calls = []
206- original = type (g [0 ]).adjacent
204+ Recomputing would write every vertex's label, so the labels are replaced by
205+ a marker that nothing else could produce, and a recomputation shows as the
206+ marker being overwritten.
207+ """
207208
208- def spy (vertex , * args , ** kwargs ):
209- calls .append (vertex )
210- return original (vertex , * args , ** kwargs )
209+ MARK = - 7
211210
212- with unittest .mock .patch .object (type (g [0 ]), "adjacent" , spy ):
213- ask ()
214- return len (calls )
211+ def poison (self , g ):
212+ for v in g :
213+ v .__dict__ ["_label" ] = self .MARK
214+
215+ def marks (self , g ):
216+ return [v .__dict__ ["_label" ] for v in g ]
215217
216218 def check (self , cls ):
217219 g = build (cls , 4 , [(0 , 1 ), (2 , 3 )])
218- ask = lambda : (g .nc , g .component (g [0 ]), g .samecomponent (g [0 ], g [1 ]), g [2 ].label )
220+ self .assertEqual (g .nc , 2 ) # the first time: does the work
221+ self .poison (g )
219222
220- self .assertGreater (self .traversals (g , ask ), 0 ) # first time: does the work
221- self .assertEqual (self .traversals (g , ask ), 0 ) # nothing changed: none
222- self .assertEqual (self .traversals (g , ask ), 0 )
223+ # nothing has changed: asked for again, in each way, nothing is redone
224+ self .assertEqual (g .nc , 2 )
225+ g .component (0 )
226+ g .samecomponent (g [0 ], g [1 ])
227+ self .assertEqual (self .marks (g ), [self .MARK ] * 4 )
223228
224229 g .add_edge (g [1 ], g [2 ]) # a change: once more, and the answer is new
225- self .assertGreater (self .traversals (g , ask ), 0 )
226230 self .assertEqual (g .nc , 1 )
227- self .assertEqual (self .traversals (g , ask ), 0 )
231+ self .assertEqual ([v .label for v in g ], [0 , 0 , 0 , 0 ])
232+
233+ self .poison (g )
234+ self .assertEqual (g .nc , 1 )
235+ self .assertEqual (self .marks (g ), [self .MARK ] * 4 )
228236
229237 def test_undirected (self ):
230238 self .check (UGraph )
231239
232240 def test_directed (self ):
233241 self .check (DGraph )
234242
243+ def test_removals_and_additions_are_changes (self ):
244+ for cls in (UGraph , DGraph ):
245+ with self .subTest (cls = cls .__name__ ):
246+ g = build (cls , 4 , [(0 , 1 ), (1 , 2 ), (2 , 3 )])
247+ self .assertEqual (g .nc , 1 )
248+ g .remove_edge (g .edges ()[1 ]) # splits 0-1 from 2-3
249+ self .assertEqual (g .nc , 2 )
250+ self .assertEqual ([v .label for v in g ], [0 , 0 , 1 , 1 ])
251+ g .remove_vertex (g [1 ])
252+ self .assertEqual (g .nc , 2 )
253+ self .assertEqual ([v .label for v in g ], [0 , 1 , 1 ])
254+ g .add_vertex (name = "new" )
255+ self .assertEqual (g .nc , 3 )
256+ self .assertEqual ([v .label for v in g ], [0 , 1 , 1 , 2 ])
257+
258+
259+ class TestLabelNumbering (unittest .TestCase ):
260+ """Components are numbered from 0 in the order of their first vertex"""
261+
262+ def test_numbering_follows_the_first_vertex_not_the_edges (self ):
263+ for cls in (UGraph , DGraph ):
264+ with self .subTest (cls = cls .__name__ ):
265+ # the edges are added last-component-first and end to end the
266+ # wrong way round, but 0 is still the component of vertex 0
267+ g = build (cls , 7 , [(6 , 5 ), (4 , 3 ), (3 , 2 ), (1 , 0 )])
268+ self .assertEqual ([v .label for v in g ], [0 , 0 , 1 , 1 , 1 , 2 , 2 ])
269+ self .assertEqual (g .nc , 3 )
270+
271+ def test_loops_and_parallel_edges (self ):
272+ for cls in (UGraph , DGraph ):
273+ with self .subTest (cls = cls .__name__ ):
274+ g = build (cls , 3 , [(0 , 0 ), (1 , 2 ), (1 , 2 ), (2 , 1 ), (2 , 2 )])
275+ self .assertEqual ([v .label for v in g ], [0 , 1 , 1 ])
276+
277+ def test_agrees_with_a_traversal_written_out_in_full (self ):
278+ rng = np .random .default_rng (11 )
279+ for trial in range (30 ):
280+ n = int (rng .integers (1 , 40 ))
281+ edges = [tuple (int (x ) for x in rng .integers (0 , n , 2 )) for _ in range (int (rng .integers (0 , 2 * n )))]
282+ for cls in (UGraph , DGraph ):
283+ with self .subTest (trial = trial , cls = cls .__name__ ):
284+ g = build (cls , n , edges )
285+ # neighbours of either end, by the edge list, from vertex 0 up
286+ around = {i : set () for i in range (n )}
287+ for a , b in edges :
288+ around [a ].add (b )
289+ around [b ].add (a )
290+ expected , count = [None ] * n , 0
291+ for s in range (n ):
292+ if expected [s ] is not None :
293+ continue
294+ expected [s ], stack = count , [s ]
295+ while stack :
296+ x = stack .pop ()
297+ for y in around [x ]:
298+ if expected [y ] is None :
299+ expected [y ] = count
300+ stack .append (y )
301+ count += 1
302+ self .assertEqual ([v .label for v in g ], expected )
303+ self .assertEqual (g .nc , count )
304+
235305
236306class TestUndirectedComponents (_Components , unittest .TestCase ):
237307 cls = UGraph
0 commit comments