pyvefr¶
pyvefr binds vefr for two graphs with vertices 0..n-1 (Python ints) and numbered edges. Colors and weights
are lists: a color per vertex index, a weight per edge number; a list too short raises ValueError. Results are
lists by vertex index, None where there is no vertex or no distance, inf where no path leads.
import pyvefr
g = pyvefr.Graph([(0, 1), (1, 2), (2, 0)])
predecessors, distances = g.breadth_first_search([0])
form = g.canonical_form(colors=[0, 0, 1])
python/examples holds runnable examples of both graphs.
Graphs¶
- class pyvefr.Graph(edges=None)¶
A graph that grows and shrinks: urd’s adjacency lists. Edges are numbered as added; a number is never reused. In-edges are kept.
- add_vertex() int¶
- add_edge(source: int, target: int) int¶
- remove_edge(edge: int) None¶
- remove_vertex(vertex: int) None¶
- clear() None¶
- class pyvefr.StaticGraph(graph: Graph | list[tuple[int, int]])¶
A graph built once from a
Graph: urd’s compressed sparse rows, edges keeping their numbers, in-edges kept.
Both have num_vertices, num_edges, vertices(), neighbours(vertex), in_neighbours(vertex),
out_degree(vertex), in_degree(vertex), adjacent(source, target), edges(), out_edges(vertex),
in_edges(vertex), edge(source, target), source(edge) and target(edge); edges are their numbers.
vertex_value(vertex) and edge_value(edge) give a graph’s data, None where it carries none.
- class pyvefr.Edge¶
An edge as the algorithms hand it out (cycles, visitor events):
source,targetandid.
Certificates and canonical forms¶
The algorithms are methods of each graph, Graph, StaticGraph and any a library binds (below).
colors is a color per vertex index; without it, a graph colors its vertices by what they hold, as its library
binds it (pyvefr’s own graphs hold nothing: one color).
- Graph.color_refinement(colors=None) tuple[Certificate1, list[int]]¶
- Graph.weisfeiler_leman(k, colors=None) tuple[Certificate2 | Certificate3, list[int]]¶
kis 2 or 3 for pyvefr’s graphs, the k its library binds for another graph. The colors are the stable color of each vertex by position, or of each k-tuple.
- class pyvefr.Certificate1¶
Compares and hashes;
num_rounds.Certificate<k>is the certificate over k-tuples:Certificate1color refinement’s,Certificate2andCertificate3Weisfeiler-Leman’s.
- class pyvefr.Certificate2¶
- class pyvefr.Certificate3¶
- Graph.canonical_form(colors=None) CanonicalForm¶
- class pyvefr.CanonicalForm¶
Compares and hashes: equal exactly for isomorphic graphs.
labelingis each vertex’s canonical position; of two equal forms, a vertex maps to the other graph’s vertex of the same canonical position.
Algorithms¶
- Graph.is_loopless() bool¶
- Graph.is_multigraph() bool¶
- Graph.is_symmetric() bool¶
- Graph.is_simple_undirected() bool¶
- Graph.breadth_first_search(sources, visitor=None) tuple[list[int | None], list[int | None]]¶
- Graph.depth_first_search(sources, visitor=None) list[int | None]¶
- Graph.breadth_first_visit(sources, visitor) None¶
- Graph.depth_first_visit(sources, visitor) None¶
- Graph.strong_components() tuple[int, list[int]]¶
- Graph.topological_sort() list[int]¶
- Graph.dijkstra_shortest_paths(sources, weights, visitor=None, targets=None) tuple[list[int | None], list[float]]¶
With
targets, it stops once their distances are final. Only a graph that numbers its edges has it (weights are a list by edge number).
- Graph.floyd_warshall_all_pairs_shortest_paths(weights) list[list[float]]¶
Only a graph that numbers its edges has it.
- class pyvefr.Visitor¶
Subclass it and override the events wanted:
start_vertex,discover_vertex,examine_vertex,finish_vertextake a vertex;examine_edge,tree_edge,non_tree_edge,gray_target,black_target,back_edge,forward_or_cross_edge,finish_edge,edge_relaxedandedge_not_relaxedanEdge. An event returnsNoneto go on, orControlFlow.STOPto stop the search.
- class pyvefr.ControlFlow¶
PROCEEDorSTOP.class Order(pyvefr.Visitor): def __init__(self) -> None: super().__init__() self.order: list[int] = [] def discover_vertex(self, vertex: int) -> None: self.order.append(vertex)
Binding a graph of one’s own¶
A library built on vefr binds its graph class (an urd graph) with two calls: urd binds the graph’s members
(urd::python::bind_graph, urd/python/graphs.hpp), vefr its algorithms and the class’s DOT text
(vefr::python::bind_algorithms, vefr/python/bindings.hpp) as methods of the class, so its generated stub lists
them. bind_algorithms binds every algorithm the graph takes; one binder each (bind_find_cycle,
bind_breadth_first_search, …) binds a choice of them. Its vertex coloring, a function of the graph and a vertex
whose result is an integer, an enum or an urd index (urd::vertex_data where the vertex data is one), colors the
refinements and canonical forms where Python gives no colors: by what a vertex holds, never by its number.
bind_weisfeiler_leman<2, 3, 4>(cls, coloring) binds the k a graph needs; a k beyond 3 needs its certificate,
bind_certificate<4>(m), in one module. The module imports pyvefr first, which registers the types
every graph shares (Edge, Visitor, the certificates).
nb::module_::import_("pyvefr");
auto cls = nb::class_<MyGraph>(m, "MyGraph"); // MyGraph: an urd graph, its labels as its data
urd::python::bind_graph(cls);
vefr::python::bind_algorithms(cls, vefr::python::one_color, urd::vertex_data, urd::edge_data);