Add graph theory subsystem (src/graph/)#13
Draft
msollami wants to merge 1 commit into
Draft
Conversation
Introduce a Mathematica-compatible graph subsystem modelled on the
Wolfram Language, mirroring the src/linalg/ layout (one builtin per
translation unit, registered via graph_init()).
Graphs are ordinary Expr trees -- Graph[List[verts], List[edges]] with
each edge a DirectedEdge[u, v] or UndirectedEdge[u, v] -- so the generic
tools (Part, Map, ReplaceAll, pattern matching) work on them unchanged.
Rule/-> and TwoWayRule/<-> are accepted as parse-time sugar and
normalised on construction; <-> is added to the parser at Rule
precedence. The constructor validates structure (2-arg edges, endpoints
present, no self-loops, no parallel edges) and leaves malformed input
unevaluated.
Builtins span construction/predicates, queries, matrix views
(Adjacency/Incidence/Kirchhoff/Distance), structural operations,
generators, algorithms (shortest path, components, spanning tree,
connectivity, Euler/Hamilton, cliques, independent sets, covers,
matching, colouring, chromatic number/polynomial), centrality
(PageRank, Katz, betweenness, closeness, degree, clustering) and drawing
(GraphPlot -> Graphics, Graph3D/GraphPlot3D -> Graphics3D, HighlightGraph).
PageRank and KatzCentrality are computed exactly: a rational
transition/resolvent matrix is assembled and solved with the existing
LinearSolve, yielding exact rationals rather than floating-point
approximations.
Integration:
- src/core.c registers graph_init() after graphics_init().
- src/sym_names.{c,h} intern the Graph/edge/option symbols.
- src/parse.c adds the <-> (TwoWayRule) operator.
- src/print.c prints edges infix and a bare Graph as a terse
Graph[<n vertices, m edges>] summary (InputForm/FullForm still
round-trip through the literal constructor).
- src/graphics/graphics_json.c gains graph-diagram serialisation
(graphics3d_to_plotly_json, diagram mode for Disk/Point/Text); a bare
valid Graph/Graph3D result auto-renders in the REPL and notebook
sidecar, the same way Graphics does.
- makefile compiles src/graph/*.c and adds -I./src/graph.
- tests/CMakeLists.txt builds the graph_tests target.
Every builtin is Protected, carries a docstring, is documented in
docs/spec/builtins/graphs.md, and is covered by tests/test_graph.c.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a Mathematica-compatible graph theory subsystem (
src/graph/) toMathilda, modelled on the Wolfram Language and mirroring the existing
src/linalg/layout (one builtin per translation unit, registered viagraph_init()). Graphs are ordinaryExprtrees —Graph[List[verts], List[edges]]with each edge aDirectedEdge[u, v]orUndirectedEdge[u, v]— so the generic tools (Part,Map,ReplaceAll,pattern matching) work on them unchanged.
Rule/->andTwoWayRule/<->are accepted as parse-time sugar and normalised on construction.
Builds cleanly under the existing toolchain (
-std=c99 -Wall -Wextra,USE_GRAPHICS/USE_LAPACK/USE_MPFR/USE_ECM) with no changes to anyexisting subsystem's behaviour.
PageRankandKatzCentralityare computedexactly by assembling a rational matrix and calling the existing
LinearSolve.Changes
src/graph/(new, 103 files): construction/predicates (Graph,GraphQ,DirectedGraphQ,TreeGraphQ,BipartiteQ,ConnectedGraphQ, …); queries(
VertexList,EdgeList,VertexCount,VertexDegree,DegreeSequence,AdjacencyList,GraphDensity, …); matrix views (AdjacencyMatrix,IncidenceMatrix,KirchhoffMatrix,DistanceMatrix); structural ops(
GraphComplement,GraphUnion,GraphProduct,LineGraph,TransitiveClosureGraph, vertex/edge add/delete/contract, …); generators(
CompleteGraph,CycleGraph,CirculantGraph,GeneralizedPetersenGraph,PrismGraph,RandomGraph, …); algorithms (FindShortestPath,ConnectedComponents,FindSpanningTree,VertexConnectivity,FindEulerianCycle,FindHamiltonianCycle,FindClique,FindVertexCover,FindGraphMatching,FindVertexColoring,ChromaticNumber,ChromaticPolynomial, …); centrality (PageRank,KatzCentrality,BetweennessCentrality,ClosenessCentrality,clustering coefficients); and drawing (
GraphPlot→Graphics,Graph3D/GraphPlot3D→Graphics3D,HighlightGraph).src/core.c: registergraph_init()aftergraphics_init().src/sym_names.{c,h}: internGraph/Graph3D/DirectedEdge/UndirectedEdge/TwoWayRuleand graph-drawing option symbols.src/parse.c: add the<->(TwoWayRule) operator atRuleprecedence.src/print.c: print edges infix (u -> v/u <-> v) and a bareGraphas a terse
Graph[<n vertices, m edges>]summary;InputForm/FullFormround-trip through the literal constructor.
src/graphics/graphics_json.c: graph-diagram serialisation(
graphics3d_to_plotly_json, diagram mode forDisk/Point/Text); a barevalid
Graph/Graph3Dresult auto-renders likeGraphics.makefile: compilesrc/graph/*.c, add-I./src/graph.tests/CMakeLists.txt: build thegraph_teststarget.docs/spec/builtins/graphs.md; index row inMathilda_spec.md;layout/init notes in
SPEC.md; changelog entry underdocs/spec/changelog/2026-06-29.md.Testing
make -j— clean build,./Mathildalinks with graphics/LAPACK/MPFR/ECM,zero warnings from
src/graph/.cd tests && cmake -B build && cmake --build build --target graph_teststhen./build/graph_tests→ all graph tests pass (1000+assert_eval_eqassertions across ~90 test functions, exercising parse → eval → print,
including
<->/->edge sugar).JIRA Ticket
N/A