graph

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: Apache-2.0, BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NodeInGraph

func NodeInGraph[Node comparable, Edge any](g Graph[Node, Edge], n Node) bool

func NodesFrom

func NodesFrom[Node comparable, Edge any](g Graph[Node, Edge], n Node) iter.Seq[Node]

func ShortestPath

func ShortestPath[Node comparable, Edge any](g Graph[Node, Edge], from, to Node) []Edge

ShortestPath returns the shortest path from -> to in the graph g using Dijkstra's algorithm. The returned slice holds all the edges leading from the source to the destination.

Types

type EnumerableGraph

type EnumerableGraph[Node comparable, Edge any] interface {
	Graph[Node, Edge]
	AllNodes() iter.Seq[Node]
}

type EnumerableSimpleGraph

type EnumerableSimpleGraph[Node comparable, Edge any] interface {
	SimpleGraph[Node, Edge]
	AllNodes() iter.Seq[Node]
}

type Graph

type Graph[Node comparable, Edge any] interface {
	// TODO return iterator?
	// TODO it's useful to be able to tell if a given node
	// is actually part of the graph or not. Could
	// say that returning nil slice signifies non-presence,
	// or (probably better) return ([]Edge, bool).
	EdgesFrom(Node) ([]Edge, bool)
	Nodes(Edge) (from, to Node)
	CmpNode(n0, n1 Node) int
}

type Simple

type Simple[Node cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Simple implements Graph for a concrete set of comparable nodes.

func (*Simple[Node]) AddEdge

func (g *Simple[Node]) AddEdge(from, to Node)

AddEdge adds nodes from and to, and adds an edge from -> to. You don't need to call AddNode first; the nodes will be implicitly added if they don't already exist. The direction means that from depends on to; i.e. to will appear before from in the sorted output. Cycles are allowed.

func (*Simple[Node]) AddNode

func (g *Simple[Node]) AddNode(n Node)

AddNode adds a node. Typically this is only used to add nodes with no incoming or outgoing edges.

func (*Simple[Node]) AllNodes

func (g *Simple[Node]) AllNodes() iter.Seq[Node]

AllNodes implements Graph.AllNodes.

func (*Simple[Node]) CmpNode

func (g *Simple[Node]) CmpNode(n0, n1 Node) int

func (*Simple[Node]) EdgesFrom

func (g *Simple[Node]) EdgesFrom(n Node) ([][2]Node, bool)

AllNodes implements Graph.Edges. Note: the caller should not mutate the returned slice.

func (*Simple[Node]) Nodes

func (g *Simple[Node]) Nodes(e [2]Node) (from, to Node)

AllNodes implements Graph.Nodes.

type SimpleGraph

type SimpleGraph[Node comparable, Edge any] interface {
	Graph[Node, Edge]
	Edge(Node) (Edge, bool)
}

SimpleGraph is a special case of Graph that only allows a single edge between any two nodes. An edge may not connect a node to itself.

type Weighted

type Weighted[Node comparable, Edge any] interface {
	Graph[Node, Edge]

	// EdgeWeight returns the weight of the given edge.
	EdgeWeight(Edge) float64
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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