Skip to content

Commit aaf3e2e

Browse files
authored
Merge branch 'main' into fix/dgraph-components-hang
2 parents 433db25 + 6bff60e commit aaf3e2e

2 files changed

Lines changed: 202 additions & 4 deletions

File tree

‎tests/test_graph.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,9 @@ def test_matrices(self):
592592
expected_L = np.diag([2, 1, 1, 0]) - expected_A
593593
nt.assert_array_equal(L, expected_L)
594594

595-
# edge order (columns) is not deterministic -- self.edges() is a
596-
# set -- so check incidence structurally rather than by position:
597-
# each column (edge) touches exactly 2 vertices, and each row
598-
# (vertex)'s total matches its degree
595+
# the structure: each column (edge) touches exactly 2 vertices, and
596+
# each row (vertex)'s total matches its degree. Cell by cell is in
597+
# test_incidence.py
599598
I = g.incidence()
600599
self.assertEqual(I.shape, (g.n, g.ne))
601600
nt.assert_array_equal(I.sum(axis=0), np.full(g.ne, 2))

‎tests/test_incidence.py‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
"""``incidence()`` cell by cell.
2+
3+
``I[i, j]`` is 1 where vertex ``i`` is an end of edge ``j``: rows are vertices
4+
in the order they were created, columns are edges in the order they were added
5+
(the order of ``edges()``). Both ends of an edge are marked whichever way it
6+
points. An undirected self-loop touches its vertex at both ends, so is 2.
7+
8+
0.6.5 returned the wrong matrix for every graph with an edge. A loop variable
9+
overwrote the vertex index, so edges were marked in the wrong rows, and for a
10+
directed graph only each vertex's outgoing edges were visited, so the vertex at
11+
the end of an edge was not marked. The tests that existed checked only column
12+
and row sums, because the edge order was then that of a set, so neither bug was
13+
caught. Each expectation below is written out by hand, not derived from the
14+
code under test.
15+
"""
16+
17+
import unittest
18+
19+
import numpy as np
20+
21+
from pgraph import DGraph, UGraph
22+
23+
24+
def matrix(rows):
25+
return np.array(rows, dtype=float)
26+
27+
28+
class _Incidence:
29+
cls: type
30+
31+
def graph(self, names):
32+
g = self.cls()
33+
return g, [g.add_vertex(name=n) for n in names]
34+
35+
def assertIncidence(self, g, expected):
36+
np.testing.assert_array_equal(g.incidence(), matrix(expected))
37+
38+
# ---------------------------------------------------------------- shape
39+
40+
def test_empty_graph(self):
41+
g = self.cls()
42+
self.assertEqual(g.incidence().shape, (0, 0))
43+
44+
def test_vertices_without_edges(self):
45+
g, _ = self.graph("abc")
46+
self.assertEqual(g.incidence().shape, (3, 0))
47+
48+
# ------------------------------------------------------ the two 0.6.5 bugs
49+
50+
def test_path(self):
51+
# a - b - c: the middle vertex is on both edges
52+
g, (a, b, c) = self.graph("abc")
53+
g.add_edge(a, b)
54+
g.add_edge(b, c)
55+
self.assertIncidence(g, [[1, 0],
56+
[1, 1],
57+
[0, 1]])
58+
59+
def test_star(self):
60+
# a hub with three spokes: the hub row is all ones
61+
g, (h, x, y, z) = self.graph("hxyz")
62+
g.add_edge(h, x)
63+
g.add_edge(h, y)
64+
g.add_edge(h, z)
65+
self.assertIncidence(g, [[1, 1, 1],
66+
[1, 0, 0],
67+
[0, 1, 0],
68+
[0, 0, 1]])
69+
70+
def test_the_end_of_an_edge_is_marked_whichever_way_it_points(self):
71+
# edges that run backwards through the alphabet: the vertex at the
72+
# head of each is marked as well as the one at the tail
73+
g, (a, b, c) = self.graph("abc")
74+
g.add_edge(c, b)
75+
g.add_edge(b, a)
76+
self.assertIncidence(g, [[0, 1],
77+
[1, 1],
78+
[1, 0]])
79+
80+
def test_a_vertex_that_is_only_ever_an_end(self):
81+
# in a directed graph c has no outgoing edge, so a visit to each
82+
# vertex's own edges never finds it
83+
g, (a, b, c) = self.graph("abc")
84+
g.add_edge(a, c)
85+
g.add_edge(b, c)
86+
self.assertIncidence(g, [[1, 0],
87+
[0, 1],
88+
[1, 1]])
89+
90+
# ------------------------------------------------------------- the order
91+
92+
def test_rows_follow_creation_order_not_names(self):
93+
g, (z, a, m) = self.graph("zam")
94+
g.add_edge(z, a)
95+
g.add_edge(a, m)
96+
self.assertIncidence(g, [[1, 0], # z
97+
[1, 1], # a
98+
[0, 1]]) # m
99+
100+
def test_columns_follow_the_order_edges_were_added(self):
101+
g, (a, b, c, d) = self.graph("abcd")
102+
g.add_edge(c, d)
103+
g.add_edge(a, b)
104+
g.add_edge(b, c)
105+
self.assertIncidence(g, [[0, 1, 0],
106+
[0, 1, 1],
107+
[1, 0, 1],
108+
[1, 0, 0]])
109+
110+
def test_columns_are_edges_in_edges_order(self):
111+
g, (a, b, c, d) = self.graph("abcd")
112+
edges = [g.add_edge(a, b), g.add_edge(c, d), g.add_edge(b, c), g.add_edge(a, d)]
113+
self.assertEqual(g.edges(), edges)
114+
I = g.incidence()
115+
index = {v: i for i, v in enumerate(g)}
116+
for j, e in enumerate(edges):
117+
marked = set(np.flatnonzero(I[:, j]))
118+
self.assertEqual(marked, {index[e.v1], index[e.v2]})
119+
120+
def test_connect_and_add_edge_agree(self):
121+
g, (a, b, c) = self.graph("abc")
122+
a.connect(b)
123+
g.add_edge(b, c)
124+
self.assertIncidence(g, [[1, 0],
125+
[1, 1],
126+
[0, 1]])
127+
128+
# ----------------------------------------------- multigraph and removal
129+
130+
def test_parallel_edges_have_a_column_each(self):
131+
g, (a, b, c) = self.graph("abc")
132+
g.add_edge(a, b)
133+
g.add_edge(a, b)
134+
g.add_edge(b, c)
135+
self.assertIncidence(g, [[1, 1, 0],
136+
[1, 1, 1],
137+
[0, 0, 1]])
138+
139+
def test_antiparallel_edges_have_a_column_each(self):
140+
g, (a, b) = self.graph("ab")
141+
g.add_edge(a, b)
142+
g.add_edge(b, a)
143+
self.assertIncidence(g, [[1, 1],
144+
[1, 1]])
145+
146+
def test_columns_close_up_when_an_edge_is_removed(self):
147+
g, (a, b, c) = self.graph("abc")
148+
e1 = g.add_edge(a, b)
149+
g.add_edge(b, c)
150+
g.remove_edge(e1)
151+
self.assertIncidence(g, [[0],
152+
[1],
153+
[1]])
154+
155+
def test_rows_close_up_when_a_vertex_is_removed(self):
156+
# a - b - c - d: removing b takes a-b and b-c with it, leaving c-d
157+
g, (a, b, c, d) = self.graph("abcd")
158+
g.add_edge(a, b)
159+
g.add_edge(b, c)
160+
g.add_edge(c, d)
161+
g.remove_vertex(b)
162+
self.assertIncidence(g, [[0], # a, now isolated
163+
[1], # c
164+
[1]]) # d
165+
166+
def test_an_isolated_vertex_is_a_row_of_zeros(self):
167+
g, (a, b, c) = self.graph("abc")
168+
g.add_edge(a, c)
169+
self.assertIncidence(g, [[1],
170+
[0],
171+
[1]])
172+
173+
174+
class TestUndirectedIncidence(_Incidence, unittest.TestCase):
175+
cls = UGraph
176+
177+
def test_a_loop_touches_its_vertex_at_both_ends(self):
178+
g, (a, b) = self.graph("ab")
179+
g.add_edge(a, a)
180+
g.add_edge(a, b)
181+
self.assertIncidence(g, [[2, 1],
182+
[0, 1]])
183+
184+
185+
class TestDirectedIncidence(_Incidence, unittest.TestCase):
186+
cls = DGraph
187+
188+
def test_a_loop_is_one_in_its_row(self):
189+
# the incidence matrix does not say which end is which, so a directed
190+
# loop is a single 1; the in and out degree of the vertex say more
191+
g, (a, b) = self.graph("ab")
192+
g.add_edge(a, a)
193+
g.add_edge(a, b)
194+
self.assertIncidence(g, [[1, 1],
195+
[0, 1]])
196+
197+
198+
if __name__ == "__main__":
199+
unittest.main()

0 commit comments

Comments
 (0)