Documentation
¶
Overview ¶
Package dag provides generic DAG algorithms that work with any data structure.
Index ¶
- Variables
- func ComputeAncestorsFn[ID comparable](allIDs []ID, startID ID, getDeps func(ID) []ID) []ID
- func ComputeDescendantsFn[ID comparable](allIDs []ID, startID ID, getDeps func(ID) []ID) []ID
- func DetectCycle[ID comparable, T CycleDetectable[ID]](items []T) []ID
- func DetectCycleFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) []ID
- func FindLeaves[ID comparable, T CycleDetectable[ID]](items []T) []ID
- func FindLeavesFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) []ID
- func FindRoots[ID comparable, T CycleDetectable[ID]](items []T) []ID
- func FindRootsFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) []ID
- func HasCycle[ID comparable, T CycleDetectable[ID]](items []T) bool
- func HasCycleFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) bool
- func TopologicalSort[ID comparable, T CycleDetectable[ID]](items []T) ([]ID, error)
- func TopologicalSortFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) ([]ID, error)
- type CycleDetectable
Constants ¶
This section is empty.
Variables ¶
var ErrCycleDetected = errors.New("cycle detected in graph")
ErrCycleDetected is returned when a cycle is detected during topological sort.
Functions ¶
func ComputeAncestorsFn ¶
func ComputeAncestorsFn[ID comparable](allIDs []ID, startID ID, getDeps func(ID) []ID) []ID
ComputeAncestorsFn computes all ancestors of a node.
func ComputeDescendantsFn ¶
func ComputeDescendantsFn[ID comparable](allIDs []ID, startID ID, getDeps func(ID) []ID) []ID
ComputeDescendantsFn computes all descendants of a node. Useful for impact analysis.
func DetectCycle ¶
func DetectCycle[ID comparable, T CycleDetectable[ID]](items []T) []ID
DetectCycle detects a cycle in a collection of CycleDetectable items.
func DetectCycleFn ¶
func DetectCycleFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) []ID
DetectCycleFn detects a cycle in a graph using a higher-order function. Returns the cycle path if found, nil otherwise.
Parameters:
- allIDs: All node identifiers in the graph
- getDeps: Function that returns dependencies (children) for a given node
Example:
getDeps := func(id string) []string {
switch id {
case "A": return []string{"B", "C"}
case "B": return []string{"C"}
default: return nil
}
}
cycle := DetectCycleFn([]string{"A", "B", "C"}, getDeps)
func FindLeaves ¶
func FindLeaves[ID comparable, T CycleDetectable[ID]](items []T) []ID
FindLeaves finds all leaf nodes from a collection of items.
func FindLeavesFn ¶
func FindLeavesFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) []ID
FindLeavesFn finds all leaf nodes (nodes with no outgoing edges).
Parameters:
- allIDs: All node identifiers in the graph
- getDeps: Function that returns dependencies (children) for a given node
Returns: Slice of leaf node IDs
func FindRoots ¶
func FindRoots[ID comparable, T CycleDetectable[ID]](items []T) []ID
FindRoots finds all root nodes from a collection of items.
func FindRootsFn ¶
func FindRootsFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) []ID
FindRootsFn finds all root nodes (nodes with no incoming edges).
Parameters:
- allIDs: All node identifiers in the graph
- getDeps: Function that returns dependencies (children) for a given node
Returns: Slice of root node IDs
func HasCycle ¶
func HasCycle[ID comparable, T CycleDetectable[ID]](items []T) bool
HasCycle checks if a cycle exists in a collection of CycleDetectable items.
func HasCycleFn ¶
func HasCycleFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) bool
HasCycleFn checks if a cycle exists in the graph. This is faster than DetectCycleFn when you don't need the cycle path.
func TopologicalSort ¶
func TopologicalSort[ID comparable, T CycleDetectable[ID]](items []T) ([]ID, error)
TopologicalSort performs a topological sort on a collection of CycleDetectable items.
func TopologicalSortFn ¶
func TopologicalSortFn[ID comparable](allIDs []ID, getDeps func(ID) []ID) ([]ID, error)
TopologicalSortFn performs a topological sort on a graph. Returns the nodes in topological order (dependencies before dependents).
Parameters:
- allIDs: All node identifiers in the graph
- getDeps: Function that returns dependencies (children) for a given node
Returns: Sorted slice of node IDs, or error if cycle detected
Types ¶
type CycleDetectable ¶
type CycleDetectable[ID comparable] interface { ID() ID Dependencies() []ID }
CycleDetectable is an interface for types that can be checked for cycles.