graph

package
v0.10.0-alpha3 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CyclicError

type CyclicError[T comparable] struct {
	Cycle []T
}

CyclicError is returned when the input graph has a cycle.

func (*CyclicError[T]) Error

func (e *CyclicError[T]) Error() string

type Graph

type Graph[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Graph represents a directed graph.

func NewGraph

func NewGraph[T cmp.Ordered]() *Graph[T]

NewGraph creates a new graph.

func (*Graph[T]) AddEdge

func (g *Graph[T]) AddEdge(source, destination T)

AddEdge adds a directed edge from source to destination. This should be interpreted as "$source depends on $destination," not "$source comes before "$destination." In the topologically sorted output, $destination will come before $source.

func (*Graph[T]) AddNode

func (g *Graph[T]) AddNode(n T)

AddNode adds a node to the graph without adding any edges. [AddEdge] implicitly creates nodes, so this function is only needed in the case where you have a node with no edges to or from it.

This function is always safe to call. You can call it before adding edges to the node or after adding edges. It is idempotent.

func (*Graph[T]) EdgesFrom

func (g *Graph[T]) EdgesFrom(n T) []T

func (*Graph[T]) TopologicalSort

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

TopologicalSort performs a topological sort. For all edges a->b, the output will have b before a.

For the same graph, the same result will be returned, regardless of the order of Add*() calls, and regardless of Go's random map iteration order.

If there is a cycle in the graph, an error message will be returned that names the nodes involved in the cycle.

Jump to

Keyboard shortcuts

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