graph

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCycle = errors.New("graph has a cycle")

ErrCycle is returned by TopologicalSort when the graph has a cycle.

Functions

This section is empty.

Types

type Graph

type Graph[T any] struct {
	Vertices      map[string]*Vertex[T]   // Map of Vertices in the graph.
	IncomingEdges map[string]int          // Map of incoming edge counts per vertex.
	Edges         map[string][]*Vertex[T] // Map of Edges represented as adjacency lists.
}

Graph represents a generic graph structure with Vertices of any type. It includes maps for Vertices, incoming Edges, and Edges for efficient graph operations.

func NewGraph

func NewGraph[T any]() *Graph[T]

NewGraph creates and returns a new instance of Graph.

func (*Graph[T]) AddEdge

func (g *Graph[T]) AddEdge(fromKey, toKey string)

AddEdge adds a directed edge from one vertex to another. If either vertex does not exist, it logs a warning and does not add the edge.

func (*Graph[T]) AddVertex

func (g *Graph[T]) AddVertex(key string, value T)

AddVertex adds a new vertex with the specified key and value to the graph. If the vertex already exists, it logs a warning and does not overwrite it.

func (*Graph[T]) ExportToGraphviz

func (g *Graph[T]) ExportToGraphviz(filename string) error

func (*Graph[T]) Print

func (g *Graph[T]) Print()

func (*Graph[T]) SortedKeys

func (g *Graph[T]) SortedKeys() []string

SortedKeys returns the keys of all vertices in lexical order, so callers that iterate the graph produce the same output on every run.

func (*Graph[T]) TopologicalSort

func (g *Graph[T]) TopologicalSort() ([]*Vertex[T], []string, error)

TopologicalSort returns the vertices ordered so that every vertex comes after all vertices with an edge into it. Ties are broken by key, so the order is deterministic. When the graph has a cycle, it returns ErrCycle and the keys of the vertices that are part of (or depend on) the cycle.

func (*Graph[T]) VerticesWithNoIncomingEdges

func (g *Graph[T]) VerticesWithNoIncomingEdges() []*Vertex[T]

VerticesWithNoIncomingEdges returns a list of Vertices with no incoming Edges.

type Vertex

type Vertex[T any] struct {
	Key   string // Unique identifier of the vertex.
	Value T      // Value stored in the vertex.
	// contains filtered or unexported fields
}

Vertex represents a node in a graph with a key and a value. It also holds a reference to the graph it belongs to for graph operations.

func (*Vertex[T]) Adjacent

func (v *Vertex[T]) Adjacent() []*Vertex[T]

Adjacent returns a list of adjacent Vertices to this vertex. It returns nil if the vertex is not part of a graph.

func (*Vertex[T]) Incoming

func (v *Vertex[T]) Incoming() []*Vertex[T]

Incoming returns a list of Vertices with Edges incoming to this vertex. It iterates over all Edges in the graph to find incoming connections.

Jump to

Keyboard shortcuts

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