graph

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package graph provides a simple adjacency map graph for network-graph.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Save

func Save(g *Graph) (string, error)

Save serializes the graph to a string.

Types

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph maintains nodes and adjacency edges.

func FromMatrix

func FromMatrix(matrix [][]bool, bidirectional bool) *Graph

FromMatrix creates a new graph from a boolean adjacency matrix.

func Load

func Load(data string) (*Graph, error)

Load deserializes the graph from a string.

func New

func New(isUndirected bool) *Graph

New creates and returns an empty Graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(from, to NodeID) error

AddEdge adds an edge from -> to. If bidirectional is true, adds the reverse edge as well.

func (*Graph) AddNode

func (g *Graph) AddNode(id NodeID) error

AddNode adds a node to the graph.

func (*Graph) EdgeCount

func (g *Graph) EdgeCount() int

EdgeCount returns the number of edges in the graph.

func (*Graph) HasEdge

func (g *Graph) HasEdge(from, to NodeID) bool

HasEdge checks if an edge exists from -> to.

func (*Graph) HasNode

func (g *Graph) HasNode(id NodeID) bool

HasNode reports whether a node with the given id exists.

func (*Graph) Hash

func (g *Graph) Hash() string

Hash returns the SHA-256 hash of the graph.

func (*Graph) IsBidirectional

func (g *Graph) IsBidirectional() bool

[deprecated] This function is deprecated. Use graph.IsUndirected() instead. IsBidirectional returns true if the graph is bidirectional.

func (*Graph) IsUndirected

func (g *Graph) IsUndirected() bool

IsUndirected returns true if the graph is undirected.

func (*Graph) Neighbors

func (g *Graph) Neighbors(id NodeID) []NodeID

Neighbors returns the list of neighbors reachable from the given node id.

func (*Graph) Nodes

func (g *Graph) Nodes() []NodeID

Nodes returns a slice of all node IDs in the graph.

func (*Graph) RemoveEdge

func (g *Graph) RemoveEdge(from, to NodeID) error

RemoveEdge removes the edge from -> to. If bidirectional is true, removes the reverse edge as well.

func (*Graph) RemoveNode

func (g *Graph) RemoveNode(id NodeID) error

RemoveNode removes a node and its incident edges from the graph.

type NodeID

type NodeID string

NodeID uniquely identifies a node in a network-graph.

func (NodeID) String

func (id NodeID) String() string

type Path

type Path struct {
	// contains filtered or unexported fields
}

Path represents an ordered sequence of nodes with a hop distance.

func NewPath

func NewPath(nodes ...NodeID) *Path

New constructs a Path from the given nodes. Distance is hops (edges). If no nodes are provided, the path is considered infinite (unreachable).

func (*Path) Distance

func (p *Path) Distance() int

Distance returns the hop distance of the path.

func (*Path) IsInfinite

func (p *Path) IsInfinite() bool

IsInfinite reports whether the path is infinite (unreachable).

func (*Path) Nodes

func (p *Path) Nodes() []NodeID

Nodes returns the node IDs in the path.

type PathLength

type PathLength map[NodeID]map[NodeID]int

PathLength represents the length of a path between two nodes.

type Paths

type Paths map[NodeID]map[NodeID][]Path

Paths is a mapping of start node IDs to end node IDs and their corresponding paths.

func (Paths) OnlyLength

func (g Paths) OnlyLength() PathLength

OnlyLength returns a slice of PathLength representing the lengths of all paths in the graph.

Directories

Path Synopsis
Package algorithm provides graph algorithms for network analysis.
Package algorithm provides graph algorithms for network analysis.
Package standard_graph provides a standard implementation of a graph data structure.
Package standard_graph provides a standard implementation of a graph data structure.

Jump to

Keyboard shortcuts

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