Documentation
¶
Overview ¶
Package generators builds graphs from classic random-graph models. Every randomized generator takes an explicit *math/rand/v2.Rand so output is reproducible: the same seed and parameters always produce a byte-identical graph.
Index ¶
- func BarabasiAlbert(n, m int, r *rand.Rand) (*gonx.Graph, error)
- func Complete(n int) (*gonx.Graph, error)
- func ErdosRenyi(n int, p float64, r *rand.Rand) (*gonx.Graph, error)
- func RandomAvgDegree(n int, avgDegree float64, r *rand.Rand) (*gonx.Graph, error)
- func WattsStrogatz(n, k int, p float64, r *rand.Rand) (*gonx.Graph, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BarabasiAlbert ¶
BarabasiAlbert builds a scale-free graph by preferential attachment: starting from m seed nodes, each new node attaches to m existing nodes chosen with probability proportional to their current degree.
Note: m is the number of edges added per new node, NOT the average degree (the resulting average degree is approximately 2m). This matches networkx.barabasi_albert_graph, whose second argument is likewise m.
Example ¶
package main
import (
"fmt"
"github.com/LuisLSousa/gonx"
"github.com/LuisLSousa/gonx/generators"
)
func main() {
r := gonx.NewRand(7)
g, _ := generators.BarabasiAlbert(150, 2, r)
// Each of the n-m arrivals adds exactly m edges.
fmt.Println(g.NumNodes(), g.NumEdges())
}
Output: 150 296
func Complete ¶
Complete returns the complete graph K_n, in which every pair of distinct nodes is connected. It has n*(n-1)/2 edges.
func ErdosRenyi ¶
ErdosRenyi builds a G(n, p) random graph: each of the n*(n-1)/2 possible edges is included independently with probability p.
func RandomAvgDegree ¶
RandomAvgDegree adds uniformly random edges until the average degree reaches at least avgDegree. Unlike ErdosRenyi it targets a mean degree directly rather than an edge probability, which is convenient when generating graphs of equal density across different sizes.
func WattsStrogatz ¶
WattsStrogatz builds a Watts-Strogatz small-world graph: a ring lattice in which every node is joined to its k nearest neighbors (k must be even), with each edge then rewired to a random endpoint with probability p. The edge count, n*k/2, is preserved by rewiring. With p == 0 the result is the pure ring lattice; with p == 1 it is essentially a random graph of the same degree.
This mirrors networkx.watts_strogatz_graph (the non-connected variant).
Example ¶
package main
import (
"fmt"
"github.com/LuisLSousa/gonx"
"github.com/LuisLSousa/gonx/generators"
)
func main() {
r := gonx.NewRand(42)
g, _ := generators.WattsStrogatz(1000, 8, 0.1, r)
// Rewiring preserves the edge count: n*k/2.
fmt.Println(g.NumNodes(), g.NumEdges())
}
Output: 1000 4000
Types ¶
This section is empty.