Skip to content

Graph ​

A graph, as an explicit vertex list and edge list (UndirectedEdge/DirectedEdge) — the data every graph query in this library reads.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageGraph!
Graph(edges)the vertex set is inferred from the edges' endpoints, in first-appearance order.
Graph(vertices, edges)an explicit vertex list, so an isolated vertex (mentioned by no edge) can be included.
Graph(edges, EdgeWeight -> {w1, w2, …})a weighted graph — one weight per edge, in EdgeList order. Switches GraphDistance/FindShortestPath/GraphDistanceMatrix onto Dijkstra automatically, same as Wolfram, and enables WeightedAdjacencyMatrix.
Typed signatures
TypePackageApplies
(any, any*) -> valuecombinatoricsalways

Domain: Collections

Details
  • Inert — Graph(...) holds exactly as written; it never reduces to anything else. Every query head (VertexList, EdgeList, VertexCount, …) reads it directly.
  • Vertex order follows Wolfram's own convention — first appearance: the declared vertex list (if given), then each edge's endpoints, in order.
  • A vertex may be any expression (an integer, a symbol, a string, …); two vertices are the same exactly when their canonical (MathJSON) forms are identical.
  • EdgeWeight must give exactly one weight per edge; a mismatched count leaves the whole Graph call — and every query over it — unevaluated, same as any other malformed call. Every query that is not distance-shaped (VertexDegree, ConnectedComponents, the Is… predicates, IsIsomorphicGraph, …) ignores weight entirely, matching Wolfram's own default. The Property/PropertyValue per-edge weight form is not supported — EdgeWeight is a plain option only.
  • No drawing. Rendering a Graph (vertex placement, an SVG/canvas element) is not implemented — see the graphs.ts module comment for what that would need.

Examples

1 more kept as data, checked against the oracles
Basic #

A triangle, vertices inferred from the edges

live ↗
InGraph([UndirectedEdge(1,2),UndirectedEdge(2,3),UndirectedEdge(3,1)])\mathrm{Graph}(\bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(2, 3), \mathrm{UndirectedEdge}(3, 1)\bigr\rbrack)OutGraph([UndirectedEdge(1,2),UndirectedEdge(2,3),UndirectedEdge(3,1)])\mathrm{Graph}(\bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(2, 3), \mathrm{UndirectedEdge}(3, 1)\bigr\rbrack)

An isolated vertex needs the explicit two-argument form

live ↗
InVertexList(Graph([1,2,3],[UndirectedEdge(1,2)]))\mathrm{VertexList}(\mathrm{Graph}(\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2)\bigr\rbrack))Out[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack

EdgeWeight (Wolfram's own weighting option) is held with the rest of Graph, not consumed on the way in

live ↗
InGraph([UndirectedEdge(1,2),UndirectedEdge(2,3)],KeyValuePair⁡(EdgeWeight,[5,7]))\mathrm{Graph}(\bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(2, 3)\bigr\rbrack, \operatorname{KeyValuePair}(\mathrm{EdgeWeight}, \bigl\lbrack5, 7\bigr\rbrack))OutGraph([UndirectedEdge(1,2),UndirectedEdge(2,3)],Tuple⁡(EdgeWeight,[5,7]))\mathrm{Graph}(\bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(2, 3)\bigr\rbrack, \operatorname{Tuple}(\mathrm{EdgeWeight},\bigl\lbrack5, 7\bigr\rbrack))
About
Examples4
Checked againstWolfram Language 1 agree3 differ
CrosswalkWolfram Language

See also: UndirectedEdge, DirectedEdge, VertexList, EdgeList, WeightedAdjacencyMatrix, GraphDistance