Documentation
¶
Overview ¶
Package gonx is a performance-oriented graph library for Go, in the spirit of Python's networkx but built around dense integer node IDs and a compact, cache-friendly representation.
The library separates mutation from reading. A Builder accumulates nodes and edges, and Builder.Build freezes it into an immutable Graph stored in Compressed Sparse Row (CSR) form. The CSR layout gives zero-copy, O(1) neighbor iteration, which is the dominant access pattern for the simulations and graph metrics this library targets.
Node IDs are dense integers in the range [0, N). Graphs are undirected and unweighted. All randomized operations take an explicit *math/rand/v2.Rand so results are fully reproducible; the package never touches a global RNG.
Index ¶
- Variables
- func NewRand(seed uint64) *rand.Rand
- type Builder
- func (b *Builder) AddEdge(u, v int) bool
- func (b *Builder) AddEdgeUnchecked(u, v int) bool
- func (b *Builder) AddNode() int
- func (b *Builder) Build() *Graph
- func (b *Builder) Degree(u int) int
- func (b *Builder) HasEdge(u, v int) bool
- func (b *Builder) NumEdges() int
- func (b *Builder) NumNodes() int
- func (b *Builder) RemoveEdge(u, v int) bool
- type Graph
- func (g *Graph) Degree(u int) int
- func (g *Graph) Edges() iter.Seq2[int, int]
- func (g *Graph) HasEdge(u, v int) bool
- func (g *Graph) Neighbors(u int) []int32
- func (g *Graph) NeighborsSeq(u int) iter.Seq[int]
- func (g *Graph) Nodes() iter.Seq[int]
- func (g *Graph) NumEdges() int
- func (g *Graph) NumNodes() int
- func (g *Graph) RandomNeighbor(u int, r *rand.Rand) (v int, ok bool)
- func (g *Graph) ToBuilder() *Builder
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidParam indicates a generator or transform was given parameters // that cannot produce a valid graph (e.g. odd degree for Watts-Strogatz). ErrInvalidParam = errors.New("gonx: invalid parameter") // ErrNotPermutation indicates a relabeling slice is not a permutation of [0, N). ErrNotPermutation = errors.New("gonx: not a permutation of node ids") )
Sentinel errors returned by constructors and algorithms. Wrap-friendly: callers may test with errors.Is.
Functions ¶
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a mutable undirected graph used to assemble a topology before freezing it into an immutable Graph. It is not safe for concurrent use.
Edge methods (AddEdge, RemoveEdge, HasEdge) treat out-of-range endpoints as absent edges and report false; Degree panics on an out-of-range node.
func NewBuilder ¶
NewBuilder returns a Builder with n isolated nodes (IDs 0..n-1). It panics if n exceeds 2^31-1, the maximum node count supported by the int32 CSR layout.
func (*Builder) AddEdge ¶
AddEdge inserts the undirected edge {u, v}. It returns false (and does nothing) for self-loops, out-of-range endpoints, or edges that already exist, so the resulting graph is always simple.
func (*Builder) AddEdgeUnchecked ¶
AddEdgeUnchecked inserts the undirected edge {u, v} without checking whether it already exists. Endpoints are still validated and self-loops rejected (returning false), but inserting an edge that is already present corrupts the Builder: the graph silently becomes a multigraph with a double-counted NumEdges. Use it only when each pair is known to be produced at most once — e.g. generators that enumerate pairs with u < v — where skipping the duplicate scan turns dense O(n*m) builds into O(m).
func (*Builder) AddNode ¶
AddNode appends a new isolated node and returns its ID. It panics if the node count would exceed 2^31-1.
func (*Builder) Build ¶
Build freezes the Builder into an immutable CSR Graph. Neighbor lists are sorted so the resulting Graph supports binary-search edge tests and has a canonical, deterministic layout. The Builder may be reused afterwards.
Build panics if the total adjacency size (2 * edges) exceeds 2^31-1, the capacity of the int32 CSR offsets.
func (*Builder) Degree ¶
Degree returns the number of neighbors of u. It panics if u is out of range.
func (*Builder) RemoveEdge ¶
RemoveEdge deletes the undirected edge {u, v}, returning whether it existed.
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph is an immutable, undirected, unweighted graph stored in Compressed Sparse Row form. The neighbors of node u occupy data[offsets[u]:offsets[u+1]] and are sorted ascending. A Graph is safe for concurrent reads.
Accessors that take a node ID (Degree, Neighbors, NeighborsSeq, RandomNeighbor) panic with a descriptive message when the ID is outside [0, N); HasEdge is the exception and reports false for out-of-range endpoints.
func (*Graph) HasEdge ¶
HasEdge reports whether the undirected edge {u, v} exists. It uses binary search over the smaller-degree endpoint, so it runs in O(log deg).
func (*Graph) Neighbors ¶
Neighbors returns u's neighbor IDs as a sorted, zero-copy slice into the graph's backing storage. Callers MUST NOT modify the returned slice. It panics if u is out of range.
func (*Graph) NeighborsSeq ¶
NeighborsSeq iterates over u's neighbors in ascending order as ints. It is a convenience wrapper over Graph.Neighbors for callers who want int node IDs end-to-end; hot paths should prefer Neighbors, which exposes the backing slice with no per-element call overhead. It panics if u is out of range.
func (*Graph) RandomNeighbor ¶
RandomNeighbor returns a uniformly random neighbor of u. ok is false when u is isolated. It panics if u is out of range.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
communities
command
Renders a planted-partition graph for the README gallery: four dense communities joined by sparse bridges, built directly with gonx.Builder and colored by community.
|
Renders a planted-partition graph for the README gallery: four dense communities joined by sparse bridges, built directly with gonx.Builder and colored by community. |
|
internal/render
Package render lays out gonx graphs and writes them as self-contained SVG images.
|
Package render lays out gonx graphs and writes them as self-contained SVG images. |
|
karate
command
Renders Zachary's Karate Club — the classic 34-node social network from Zachary (1977) — for the README gallery.
|
Renders Zachary's Karate Club — the classic 34-node social network from Zachary (1977) — for the README gallery. |
|
scalefree
command
Renders a Barabási–Albert scale-free network for the README gallery.
|
Renders a Barabási–Albert scale-free network for the README gallery. |
|
smallworld
command
Renders a Watts–Strogatz small-world network for the README gallery.
|
Renders a Watts–Strogatz small-world network for the README gallery. |
|
Package generators builds graphs from classic random-graph models.
|
Package generators builds graphs from classic random-graph models. |
|
internal
|
|
|
pool
Package pool provides a minimal static work-partitioning helper used by the parallel graph metrics.
|
Package pool provides a minimal static work-partitioning helper used by the parallel graph metrics. |
|
Package metrics computes structural graph properties: clustering, connectivity, and shortest-path-based measures.
|
Package metrics computes structural graph properties: clustering, connectivity, and shortest-path-based measures. |
|
Package transform contains structure-preserving and structure-randomizing graph transformations: copying, relabeling, and degree-preserving edge swaps.
|
Package transform contains structure-preserving and structure-randomizing graph transformations: copying, relabeling, and degree-preserving edge swaps. |