dag

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dag implements a simple Directed Acyclical Graph (DAG) for deterministic topological sorts

It should not be externally exposed, and is intended to be a very simple dag implementation utilizing adjacency lists to store edges.

This package is intended to be used for small scales, where performance of the algorithms is not critical. (e.g. sub 10k entries) Thus none of the algorithms in here are benchmarked, and just have correctness checks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DAG

type DAG struct {
	// contains filtered or unexported fields
}

DAG struct maintains a directed acyclic graph, using adjacency lists to track edges.

func NewDAG

func NewDAG(nodes []string) DAG

func (*DAG) AddEdge

func (dag *DAG) AddEdge(u, v string) error

AddEdge checks if either edge between u and v exists and adds a directed edge from u -> v

func (*DAG) AddFirstElements

func (dag *DAG) AddFirstElements(nodes ...string) error

AddFirstElements sets the provided elements to be first in all orderings. So if were making an ordering over {A, B, C, D, E}, and elems provided is {D, B, A} then we are guaranteed that the total ordering will begin with {D, B, A}

func (*DAG) AddLastElements

func (dag *DAG) AddLastElements(nodes ...string) error

AddLastElements sets the provided elements to be last in all orderings. So if were making an ordering over {A, B, C, D, E}, and elems provided is {D, B, A} then we are guaranteed that the total ordering will end with {D, B, A}

func (DAG) Copy

func (dag DAG) Copy() DAG

Copy returns a new dag struct that is a copy of the original dag. Edges can be mutated in the copy, without altering the original.

func (*DAG) ReplaceEdge

func (dag *DAG) ReplaceEdge(u, v string) error

ReplaceEdge adds a directed edge from u -> v. it removes any edge that may already exist between the two.

func (DAG) TopologicalSort

func (dag DAG) TopologicalSort() []string

Returns a Topological Sort of the DAG, using Kahn's algorithm. https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm

Jump to

Keyboard shortcuts

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