transform

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package transform contains structure-preserving and structure-randomizing graph transformations: copying, relabeling, and degree-preserving edge swaps.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(g *gonx.Graph) *gonx.Graph

Copy returns an independent deep copy of g.

func DoubleEdgeSwap

func DoubleEdgeSwap(g *gonx.Graph, nswap, maxTries int, r *rand.Rand) (*gonx.Graph, int, error)

DoubleEdgeSwap randomizes a graph while exactly preserving every node's degree. It repeatedly picks two edges {a,b} and {c,d} and rewires them to {a,d} and {c,b}, rejecting any swap that would create a self-loop or a duplicate edge. It performs up to nswap successful swaps, giving up after maxTries attempts. The returned int is the number of swaps actually performed.

The operation works on a copy; g is left unchanged. This mirrors networkx.double_edge_swap and is the standard way to build degree-preserving null models: graphs with the same degree sequence as the original but otherwise randomized wiring.

Example
package main

import (
	"fmt"

	"github.com/LuisLSousa/gonx"
	"github.com/LuisLSousa/gonx/generators"
	"github.com/LuisLSousa/gonx/transform"
)

func main() {
	g, _ := generators.WattsStrogatz(100, 6, 0, gonx.NewRand(1))
	swapped, n, _ := transform.DoubleEdgeSwap(g, 50, 10000, gonx.NewRand(2))
	// Swaps rewire edges but every node keeps its exact degree.
	fmt.Println(n, swapped.NumEdges() == g.NumEdges(), swapped.Degree(0) == g.Degree(0))
}
Output:
50 true true

func RelabelNodes

func RelabelNodes(g *gonx.Graph, perm []int) (*gonx.Graph, error)

RelabelNodes returns a new graph in which node i becomes node perm[i]. perm must be a permutation of [0, N). The transformation is a graph isomorphism: it preserves the degree sequence and all structural metrics, changing only the identities attached to each position.

func Shuffle

func Shuffle(g *gonx.Graph, r *rand.Rand) *gonx.Graph

Shuffle returns g with its node labels randomly permuted. The result is isomorphic to g (same structure, relabeled nodes).

func ShuffleWithPerm

func ShuffleWithPerm(g *gonx.Graph, r *rand.Rand) (*gonx.Graph, []int)

ShuffleWithPerm is like Shuffle but also returns the permutation applied, where perm[old] = new.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL