Skip to content

Commit cdbc54a

Browse files
authored
Merge pull request #63 from petercorke/perf/components-union-find
perf: components by a union-find over the edges
2 parents 041033f + eb6974f commit cdbc54a

2 files changed

Lines changed: 126 additions & 39 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,39 +1624,56 @@ def _graphcolor(self) -> None:
16241624
direction of an edge is ignored, so a vertex is in the component of
16251625
every vertex that it has a path to or from.
16261626
1627-
This is a single pass over the vertices and edges, O(V + E), and is
1628-
only done again when the graph has changed since, which every
1627+
This is a single pass over the edges with a union-find, nearly O(E),
1628+
and is only done again when the graph has changed since, which every
16291629
operation that adds or removes a vertex or edge records in the graph's
16301630
``_connectivitychange``.
16311631
1632+
Looking at the two ends of each edge, rather than walking the
1633+
neighbours of each vertex, makes the direction of an edge irrelevant
1634+
and avoids a call per incident edge to find the far end, which is most
1635+
of the time of a traversal. The root of a component is its first
1636+
vertex, so numbering the roots in order of the vertices gives the
1637+
numbering described above.
1638+
16321639
:seealso: :meth:`nc` :meth:`component`
16331640
"""
16341641
if not self._connectivitychange:
16351642
return
16361643

1637-
for vertex in self:
1638-
vertex._label = None
1639-
vertex._connectivitychange = False
1644+
vertices = self._vertexlist
1645+
index = {v: i for i, v in enumerate(vertices)}
1646+
parent = list(range(len(vertices)))
1647+
1648+
for edge in self._edges:
1649+
a = index[edge._v1] # type: ignore[index]
1650+
b = index[edge._v2] # type: ignore[index]
1651+
while parent[a] != a: # find the root, halving the path as we go
1652+
parent[a] = parent[parent[a]]
1653+
a = parent[a]
1654+
while parent[b] != b:
1655+
parent[b] = parent[parent[b]]
1656+
b = parent[b]
1657+
if a < b: # the smaller index is the root, so the first vertex
1658+
parent[b] = a
1659+
elif b < a:
1660+
parent[a] = b
16401661

16411662
count = 0
1642-
for start in self:
1643-
if start._label is not None:
1644-
continue
1645-
start._label = count
1646-
frontier = [start]
1647-
while frontier:
1648-
v = frontier.pop()
1649-
# an edge may be followed against its direction, so the
1650-
# vertices that lead to this one count as well as those it
1651-
# leads to; in an undirected graph they are the same
1652-
around = v.adjacent()
1653-
if self.directed:
1654-
around = around + v.predecessors()
1655-
for w in around:
1656-
if w._label is None:
1657-
w._label = count
1658-
frontier.append(w)
1659-
count += 1
1663+
label = [0] * len(vertices)
1664+
for i, vertex in enumerate(vertices):
1665+
root = i
1666+
while parent[root] != root:
1667+
parent[root] = parent[parent[root]]
1668+
root = parent[root]
1669+
if root == i: # the first vertex of a component
1670+
label[i] = count
1671+
count += 1
1672+
# the graph-level flag is what is consulted; writing the
1673+
# attributes directly avoids the hook of ``Vertex.__setattr__``
1674+
attrs = vertex.__dict__
1675+
attrs["_label"] = label[root]
1676+
attrs["_connectivitychange"] = False
16601677

16611678
self._ncomponents = count
16621679
self._connectivitychange = False

‎tests/test_components.py‎

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,39 +199,109 @@ def test_samecomponent(self):
199199

200200

201201
class 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

236306
class TestUndirectedComponents(_Components, unittest.TestCase):
237307
cls = UGraph

0 commit comments

Comments
 (0)