Skip to content

Commit b8611d9

Browse files
petercorkeclaude
andcommitted
fix: Adjacency() makes self-loops from the diagonal for both graph kinds
DGraph.Adjacency() raised ValueError('loops in graph not supported') on a non-zero diagonal while UGraph.Adjacency() silently dropped it, and every other entry point (connect, add_edge) permits self-loops. The diagonal is now a self-loop, with the element as its cost, for both. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 1df4d4c commit b8611d9

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,8 @@ def Adjacency(
219219
:type names: list(N) of str, optional
220220
:param kwargs: optional arguments passed to the graph constructor, for
221221
example ``vertex_class`` or ``edge_class``
222-
:raises ValueError: ``A`` is not square, the length of ``names`` or
223-
the number of rows of ``coords`` does not match ``A``, or ``A``
224-
has a non-zero diagonal element and the graph is a ``DGraph``
222+
:raises ValueError: ``A`` is not square, or the length of ``names`` or
223+
the number of rows of ``coords`` does not match ``A``
225224
:raises TypeError: ``vertex_class`` or ``edge_class`` is not a
226225
suitable class, see :meth:`_BaseGraph.__init__`
227226
:return: graph
@@ -230,12 +229,14 @@ def Adjacency(
230229
Create a directed or undirected graph where non-zero elements ``A[i,j]``
231230
correspond to edges from vertex ``i`` to vertex ``j``.
232231
232+
A non-zero diagonal element ``A[i,i]`` is a self-loop on vertex ``i``,
233+
for both kinds of graph.
234+
233235
.. warning:: For undirected graph ``A`` should be symmetric but this
234-
is not checked. Only the strictly upper triangular part is used,
235-
so the diagonal is ignored and no self-loops are created. A
236-
directed graph rejects a non-zero diagonal with ``ValueError``.
237-
Edge counts greater than one are not turned into parallel edges;
238-
a non-zero element is a single edge with that element as its cost.
236+
is not checked. Only the upper triangular part, including the
237+
diagonal, is used. Elements are costs, not edge counts: a non-zero
238+
element is a single edge with that element as its cost, so
239+
parallel edges cannot be created this way.
239240
240241
.. runblock:: pycon
241242
@@ -267,18 +268,16 @@ def Adjacency(
267268
g.add_vertex(name=name, coord=coord)
268269

269270
if isinstance(g, UGraph):
270-
# undirected graph
271+
# undirected graph: upper triangle including the diagonal
271272
for i in range(A.shape[0]):
272-
for j in range(i + 1, A.shape[1]):
273+
for j in range(i, A.shape[1]):
273274
if A[i, j] > 0:
274275
g[i].connect(g[j], cost=A[i, j])
275276
else:
276-
# directed graph
277+
# directed graph: whole matrix, a diagonal element is a self-loop
277278
for i in range(A.shape[0]):
278279
for j in range(A.shape[1]):
279280
if A[i, j] > 0:
280-
if i == j:
281-
raise ValueError("loops in graph not supported")
282281
g[i].connect(g[j], cost=A[i, j])
283282

284283
return g

‎tests/test_adjacency_loops.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_diagonal_makes_a_self_loop(self):
3232
g = self.Graph.Adjacency(self.A)
3333

3434
self.assertEqual(g.n, 3)
35-
self.assertEqual(g.ne, 3) # 0--1, 1--2, 1--1
35+
self.assertEqual(g.ne, self.ne_with_loop)
3636
loops = self.loops(g)
3737
self.assertEqual(len(loops), 1)
3838
self.assertIs(loops[0].v1, g[1])
@@ -43,13 +43,12 @@ def test_no_diagonal_no_loop(self):
4343
A[1, 1] = 0
4444
g = self.Graph.Adjacency(A)
4545
self.assertEqual(self.loops(g), [])
46-
self.assertEqual(g.ne, 2)
46+
self.assertEqual(g.ne, self.ne_without_loop)
4747

4848
def test_loop_is_a_real_edge(self):
4949
g = self.Graph.Adjacency(self.A)
5050
self.assertTrue(g.iscyclic())
5151
self.assertEqual(g.distance()[1, 1], 4)
52-
self.assertEqual(g.adjacency()[1, 1], self.loop_adjacency)
5352

5453
def test_other_validation_unchanged(self):
5554
with self.assertRaises(ValueError):
@@ -62,7 +61,9 @@ def test_other_validation_unchanged(self):
6261

6362
class TestUGraphAdjacencyLoops(_AdjacencyLoops, unittest.TestCase):
6463
Graph = UGraph
65-
loop_adjacency = 2 # an undirected loop has two ends
64+
# the symmetric matrix is read once, upper triangle: 0--1, 1--2 and 1--1
65+
ne_with_loop = 3
66+
ne_without_loop = 2
6667

6768
def test_lower_triangle_still_ignored(self):
6869
# documented: only the upper triangle (and now the diagonal) is read
@@ -73,7 +74,9 @@ def test_lower_triangle_still_ignored(self):
7374

7475
class TestDGraphAdjacencyLoops(_AdjacencyLoops, unittest.TestCase):
7576
Graph = DGraph
76-
loop_adjacency = 1
77+
# the whole matrix is read: 0->1, 1->0, 1->2, 2->1 and 1->1
78+
ne_with_loop = 5
79+
ne_without_loop = 4
7780

7881
def test_directed_uses_whole_matrix(self):
7982
A = np.array([[0, 0], [5, 0]])

0 commit comments

Comments
 (0)