Repository navigation
Commit 6a1607a
Fix graph/vertex/edge membership and consistency bugs
A cluster of real, previously-undiscovered bugs found via a deep
review of vertex/edge/graph object lifecycle and matrix-method
correctness, all verified empirically (real pytest assertions on
values, not just shapes/types) rather than by inspection alone:
- add_vertex/vertex_copy: unified into one implementation on
_BaseGraph, parameterized by a new _vertex_cls ClassVar, replacing
three separately-duplicated per-subclass implementations. This was
a real Liskov violation (base add_vertex(vertex, name) vs. subclass
add_vertex(coord, name)) and fixes a copy-paste bug where
UGraph.vertex_copy hardcoded a return of DVertex instead of UVertex.
add_vertex now also rejects a vertex of the wrong subclass instead
of silently mangling or wrongly accepting it.
- Dict(): hardcoded UVertex() construction regardless of graph type,
silently breaking DGraph.Dict(). Now delegates to add_vertex(), which
picks the right subclass via _vertex_cls.
- BaseVertex.connect(): the type check
(dest.__class__.__bases__[0] is self.__class__.__bases__[0]) was
always True for any two direct BaseVertex subclasses and never
actually caught a cross-type or cross-graph connection. Replaced
with an explicit same-graph check, which also rules out
UVertex/DVertex mismatches for free once add_vertex's type-gating
makes them structurally impossible.
- path_BFS/path_UCS/path_Astar: all three validated the goal vertex G
by checking isinstance(S, ...) instead of isinstance(G, ...) --
copy-paste bug that silently defeated goal-vertex validation.
Extracted the shared logic into _resolve_vertex().
- distance()/path_*: silently packed None/nan into results for
costless edges. Extracted _require_cost(), which raises clearly
instead -- None means "not computed", not "infinite"; use
float("inf") for a deliberately-unusable edge.
- incidence(): two bugs -- a shadowed loop variable that scrambled
every row, and (after fixing that) target-only vertices in a DGraph
never being marked, since it iterated each vertex's own outgoing-only
edges() instead of the graph's full edge set.
- Laplacian() docstring corrected: positive-semidefiniteness and the
zero-eigenvalue/component-count relationship hold for undirected
graphs only, not directed ones (verified via a 4-vertex out-tree:
2 zero eigenvalues, not 1).
- _BaseGraph.remove(edge) unconditionally crashed for any DGraph edge,
assuming both endpoints track it -- true for UVertex, false for
DVertex (only the source does). Replaced the single ambiguous
remove() with remove_edge()/remove_vertex(), each checking
membership correctly per endpoint/vertex; remove() is kept as a
deprecated dispatcher.
- DVertex.remove() was a dead one-liner (self._edgelist = None) that
didn't touch the graph, the vertex's own iteration, or incoming
edges at all. Deleted, replaced by a correct BaseVertex.remove() on
the base class that delegates to remove_vertex().
- Edge.__init__, __str__, __repr__: auto-cost computation now
degrades to cost=None instead of crashing when a vertex isn't graph-
connected (standalone Edge() construction is a supported use case,
see docs/source/policy.rst); __str__/__repr__ use the actual class
name and arrow direction instead of a hardcoded "Edge"/"--", and
handle cost=None.
- Edge.remove() revived from dead commented-out code (was missing the
edge's own removal from the graph's edge set); BaseVertex.
heuristic_distance()/distance() now raise clearly instead of
crashing with a bare AttributeError when called on a vertex that
isn't a graph member.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>1 parent c57f685 commit 6a1607a
3 files changed
Lines changed: 769 additions & 270 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
0 commit comments