Documentation ¶
Index ¶
- Variables
- func EdgesToMap[T any](e map[int64][]Edge[T]) map[int64][]int64
- func EnsureGraphEdges[T any](t *testing.T, expected map[int64][]int64, graphEdges map[int64][]Edge[T])
- func EnsureGraphNodes[T any](t *testing.T, expected []int64, nodes []Node[T])
- func NodeIDs[T any](nodes []Node[T]) []int64
- type Edge
- type Graph
- func (g *Graph[T]) AddEdge(from, to int64) error
- func (g *Graph[T]) AddNode(id int64, v T) error
- func (g *Graph[T]) Adj(id int64) []*Node[T]
- func (g *Graph[T]) BreadthFirstSearch(id int64, visitFunc VisitFunc[T]) error
- func (g *Graph[T]) DepthFirstSearch(start int64, visitFunc VisitFunc[T]) error
- func (g *Graph[T]) Node(id int64) (*Node[T], error)
- func (g *Graph[T]) NodeList(id ...int64) ([]*Node[T], error)
- type Node
- type VisitFunc
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func EnsureGraphEdges ¶
func EnsureGraphEdges[T any](t *testing.T, expected map[int64][]int64, graphEdges map[int64][]Edge[T])
EnsureGraphEdges is a test helper function that is used inside the dag tests and outside in other implementations to easily ensure that we are using the dag properly.
func EnsureGraphNodes ¶
EnsureGraphNodes is a test helper function that is used inside the dag tests and outside in other implementations to easily ensure that we are using the dag properly.
Types ¶
type Edge ¶
Edge is a connection from one node to another. Because this is a Directed graph, the edge has a direction. A connection from 'node A' to 'node B' is not the same as a connection from 'node B' to 'node A'.
type Graph ¶
type Graph[T any] struct { Nodes []Node[T] Edges map[int64][]Edge[T] // contains filtered or unexported fields }
Graph is a data structure that stores a list of Nodes (data) and Edges that connect nodes. Because it is a Directed graph, the edges connect from a node to another node, and the connection is not equal if reversed. Because it is an Acyclic graph, the nodes can not be connected in a loop or a cycle. If the nodes/edges look like (0 -> 1 -> 2 -> 0), then that is a cycle and is not allowed.
func (*Graph[T]) AddEdge ¶
AddEdge adds a new node from node with the ID 'from' to the node with the ID 'to'.
func (*Graph[T]) Adj ¶
Adj returns nodes with edges that start at the provided node (n) (Where 'From' is this node). This function does not return nodes with edges that end at the provided node (where 'To' is this node).
func (*Graph[T]) BreadthFirstSearch ¶
func (*Graph[T]) DepthFirstSearch ¶
DepthFirstSearch performs a depth-first search and calls the provided visitFunc callback for every node. 'visitFunc' is not called more than once per node. If 'visitFunc' returns an error, then so will this function. If 'visitFunc' returns ErrorBreak, then this function will return nil and will stop visiting nodes.