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 ¶
- type EdgeData
- type Graph
- func (g *Graph) AddEdge(source, target string, attrs map[string]any)
- func (g *Graph) AddNode(id string, attrs map[string]any)
- func (g *Graph) BFS(startNodes []string, depth int) (visited []string, edges []EdgeData)
- func (g *Graph) Degree(id string) int
- func (g *Graph) EdgeAttrs(source, target string) map[string]any
- func (g *Graph) EdgeCount() int
- func (g *Graph) Edges() []EdgeData
- func (g *Graph) HasNode(id string) bool
- func (g *Graph) IsDirected() bool
- func (g *Graph) Neighbors(id string) []string
- func (g *Graph) NodeAttrs(id string) map[string]any
- func (g *Graph) NodeCount() int
- func (g *Graph) Nodes() []string
- func (g *Graph) RemoveNode(id string)
- func (g *Graph) SaveJSON(path string) error
- func (g *Graph) ShortestPath(source, target string, maxHops int) []string
- func (g *Graph) String() string
- func (g *Graph) Subgraph(nodeIDs []string) *Graph
- func (g *Graph) ToJSON() ([]byte, error)
- func (g *Graph) ToUndirected() *Graph
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 (*Graph) AddEdge ¶
AddEdge adds an edge between source and target with attributes. For undirected graphs, the edge is stored in both directions.
func (*Graph) AddNode ¶
AddNode adds a node with attributes. If the node already exists, attributes are overwritten (matching NetworkX behavior).
func (*Graph) BFS ¶
BFS performs breadth-first search from start nodes up to the given depth. Returns visited node IDs and edges traversed.
func (*Graph) EdgeCount ¶
EdgeCount returns the number of edges. For undirected graphs, each edge is counted once.
func (*Graph) Edges ¶
Edges returns all edges. For undirected graphs, each edge appears once (with source < target lexicographically).
func (*Graph) IsDirected ¶
IsDirected returns whether the graph is directed.
func (*Graph) RemoveNode ¶
RemoveNode removes a node and all its edges.
func (*Graph) SaveJSON ¶
SaveJSON writes the graph to a JSON file in NetworkX node_link_data format.
func (*Graph) ShortestPath ¶
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) Subgraph ¶
Subgraph returns a new graph containing only the specified nodes and edges between them.
func (*Graph) ToUndirected ¶
ToUndirected returns an undirected copy of this graph.