graph

package
v0.0.0-...-063ee89 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package graph provides an adjacency-list graph with string-keyed nodes and arbitrary attribute maps, matching the NetworkX dict-of-dicts semantics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EdgeData

type EdgeData struct {
	Source string
	Target string
	Attrs  map[string]any
}

EdgeData represents an edge with its source, target, and attributes.

type Graph

type Graph struct {
	Metadata map[string]any // graph-level metadata (hyperedges, etc.)
	// contains filtered or unexported fields
}

Graph is a string-keyed graph with node and edge attributes.

func FromJSON

func FromJSON(data []byte) (*Graph, error)

FromJSON deserializes a graph from NetworkX node_link_data JSON bytes.

func LoadJSON

func LoadJSON(path string) (*Graph, error)

LoadJSON reads a graph from a NetworkX node_link_data JSON file.

func New

func New(directed bool) *Graph

New creates an empty graph (undirected by default).

func (*Graph) AddEdge

func (g *Graph) AddEdge(source, target string, attrs map[string]any)

AddEdge adds an edge between source and target with attributes. For undirected graphs, the edge is stored in both directions.

func (*Graph) AddNode

func (g *Graph) AddNode(id string, attrs map[string]any)

AddNode adds a node with attributes. If the node already exists, attributes are overwritten (matching NetworkX behavior).

func (*Graph) BFS

func (g *Graph) BFS(startNodes []string, depth int) (visited []string, edges []EdgeData)

BFS performs breadth-first search from start nodes up to the given depth. Returns visited node IDs and edges traversed.

func (*Graph) Degree

func (g *Graph) Degree(id string) int

Degree returns the degree of a node.

func (*Graph) EdgeAttrs

func (g *Graph) EdgeAttrs(source, target string) map[string]any

EdgeAttrs returns the attributes for a specific edge (nil if not found).

func (*Graph) EdgeCount

func (g *Graph) EdgeCount() int

EdgeCount returns the number of edges. For undirected graphs, each edge is counted once.

func (*Graph) Edges

func (g *Graph) Edges() []EdgeData

Edges returns all edges. For undirected graphs, each edge appears once (with source < target lexicographically).

func (*Graph) HasNode

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

HasNode returns true if the node exists.

func (*Graph) IsDirected

func (g *Graph) IsDirected() bool

IsDirected returns whether the graph is directed.

func (*Graph) Neighbors

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

Neighbors returns sorted IDs of all neighbors of a node.

func (*Graph) NodeAttrs

func (g *Graph) NodeAttrs(id string) map[string]any

NodeAttrs returns the attribute map for a node (nil if not found).

func (*Graph) NodeCount

func (g *Graph) NodeCount() int

NodeCount returns the number of nodes.

func (*Graph) Nodes

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

Nodes returns all node IDs in sorted order.

func (*Graph) RemoveNode

func (g *Graph) RemoveNode(id string)

RemoveNode removes a node and all its edges.

func (*Graph) SaveJSON

func (g *Graph) SaveJSON(path string) error

SaveJSON writes the graph to a JSON file in NetworkX node_link_data format.

func (*Graph) ShortestPath

func (g *Graph) ShortestPath(source, target string, maxHops int) []string

ShortestPath returns the shortest path between source and target using BFS. Returns nil if no path exists. maxHops limits the search depth (0 = unlimited).

func (*Graph) String

func (g *Graph) String() string

String returns a short summary of the graph.

func (*Graph) Subgraph

func (g *Graph) Subgraph(nodeIDs []string) *Graph

Subgraph returns a new graph containing only the specified nodes and edges between them.

func (*Graph) ToJSON

func (g *Graph) ToJSON() ([]byte, error)

ToJSON serializes the graph in NetworkX node_link_data format.

func (*Graph) ToUndirected

func (g *Graph) ToUndirected() *Graph

ToUndirected returns an undirected copy of this graph.

Jump to

Keyboard shortcuts

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