Skip to content

Refactored Edge, Graph and new Vertex class. Updated public API#3

Merged
nathan-gilbert merged 8 commits intomainfrom
vertex-class
Mar 26, 2026
Merged

Refactored Edge, Graph and new Vertex class. Updated public API#3
nathan-gilbert merged 8 commits intomainfrom
vertex-class

Conversation

@nathan-gilbert
Copy link
Copy Markdown
Owner

@nathan-gilbert nathan-gilbert commented Mar 24, 2026

New Edge, Vertex and Graph classes

Description

Replaces the internal defaultdict[str, list[str]] adjacency storage with a dual-index structure built on first-class Vertex and Edge objects:

  • _vertices: dict[str, Vertex] — O(1) lookup by name
  • _adj: dict[str, dict[str, Edge]] — O(1) edge existence checks via _adj[u][v]

Vertex is a new frozen dataclass (__slots__) with name, optional label, and an immutable attrs mapping. Identity is based on name only. Supports ordering for sorted().

Edge is a frozen dataclass (__slots__) with source/target, optional weight, label, and immutable attrs. Identity is based on the structural triple (source, target, directed).

Graph now stores Vertex and Edge objects directly instead of reconstructing them on every call. The public API has been cleaned up: metadata exposed as properties (label, directed, weighted, order, size), getter prefixes dropped (neighbors(), adjacency_matrix(), vertex(), edge()), and backward-compat methods removed (get_graph(), set_directed(), get_random_vertex()). Construction params input_file/input_graph/input_matrix are now keyword-only, and directed/weighted can be set as constructor kwargs.

Other Changes

Algorithm modules (properties, paths, search, directed, sort), export modules (json_utils, graphviz_utils), numpy_compat, demo.py, conftest.py, and all tests updated to use the new API. No algorithm logic was changed.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactor (no functional changes)
  • CI/build configuration

Checklist

  • Code follows the project style (black, isort, ruff check)
  • Type checking passes (uv run ty check)
  • All new and existing tests pass (uv run pytest)
  • Coverage remains ≥ 90%
  • Docstrings added/updated (reStructuredText/Sphinx format)
  • from __future__ import annotations included in new modules
  • No new dependencies added (or justified if added)
  • Pre-commit hooks pass (pre-commit run --all-files)

@nathan-gilbert nathan-gilbert self-assigned this Mar 24, 2026
@nathan-gilbert nathan-gilbert marked this pull request as draft March 24, 2026 01:07
@nathan-gilbert nathan-gilbert marked this pull request as ready for review March 26, 2026 03:33
@nathan-gilbert nathan-gilbert changed the title Draft: New edge, vertex and graph classes. Updated public API Refactored Edge, Graph and new Vertex class. Updated public API Mar 26, 2026
@nathan-gilbert nathan-gilbert merged commit 7c6ff96 into main Mar 26, 2026
5 checks passed
@nathan-gilbert nathan-gilbert deleted the vertex-class branch March 26, 2026 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant