dag

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package dag defines a Directed Acyclic Graph.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce

func Reduce(g *Graph)

Reduce performs a transitive reduction on g. A transitive reduction removes as many edges as possible while maintaining the same "reachability" as the original graph: any node N reachable from node S will still be reachable after a reduction.

func StronglyConnectedComponents

func StronglyConnectedComponents(g *Graph) [][]Node

StronglyConnectedComponents returns the list of strongly connected components of the graph using Tarjan algorithm.

func Validate

func Validate(g *Graph) error

Validate checks that the graph doesn't contain cycles

func Walk

func Walk(g *Graph, start []Node, fn WalkFunc) error

Walk performs a depth-first walk of outgoing edges for all nodes in start, invoking the provided fn for each node. Walk returns the error returned by fn.

Nodes unreachable from start will not be passed to fn.

func WalkIncomingNodes

func WalkIncomingNodes(g *Graph, start Node, fn WalkFunc) error

WalkIncomingNodes walks all the nodes that have a direct, incoming edge to start.

func WalkTopological

func WalkTopological(g *Graph, start []Node, fn WalkFunc) error

WalkTopological performs a topological walk of all nodes in start in dependency order: a node will not be visited until its outgoing edges are visited first.

Nodes will not be passed to fn if they are not reachable from start or if not all of their outgoing edges are reachable from start.

Types

type Edge

type Edge struct{ From, To Node }

Edge is a directed connection between two Nodes.

type Graph

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

Graph is a Directed Acyclic Graph. The zero value is ready for use. Graph cannot be modified concurrently.

func (*Graph) Add

func (g *Graph) Add(n Node)

Add adds a new Node into g. Add is a no-op if n already exists in g.

Add will panic if there is another node in g with the same NodeID as n.

func (*Graph) AddEdge

func (g *Graph) AddEdge(e Edge)

AddEdge adds a new Edge into g. AddEdge does not prevent cycles from being introduced; cycles must be detected separately.

AddEdge will panic if either node in the edge doesn't exist in g.

func (*Graph) Clone

func (g *Graph) Clone() *Graph

Clone returns a copy of g.

func (*Graph) Dependants

func (g *Graph) Dependants(n Node) []Node

Dependants returns the list of Nodes that depend on n: all Nodes for which an edge to n is defined.

func (*Graph) Dependencies

func (g *Graph) Dependencies(n Node) []Node

Dependencies returns the list of Nodes that n depends on: all Nodes for which an edge from n is defined.

func (*Graph) Edges

func (g *Graph) Edges() []Edge

Edges returns the set of all edges in g.

func (*Graph) GetByID

func (g *Graph) GetByID(id string) Node

GetByID returns a node from an ID. Returns nil if the ID does not exist in the graph.

func (*Graph) Leaves

func (g *Graph) Leaves() []Node

Leaves returns the set of Nodes in g that have no dependencies. This is useful for walking g in reverse.

func (*Graph) Nodes

func (g *Graph) Nodes() []Node

Nodes returns the set of Nodes in g.

func (*Graph) Remove

func (g *Graph) Remove(n Node)

Remove removes a Node from g. Remove is a no-op if n does not exist in g.

Remove also removes any edge to or from n.

func (*Graph) RemoveEdge

func (g *Graph) RemoveEdge(e Edge)

RemoveEdge removes an edge e from g. RemoveEdge is a no-op if e doesn't exist in g.

func (*Graph) Roots

func (g *Graph) Roots() []Node

Roots returns the set of Nodes in g that have no dependants. This is useful for walking g.

type Node

type Node interface {
	// NodeID returns the display name of the Node.
	NodeID() string
}

Node is an individual Vertex in the DAG.

type WalkFunc

type WalkFunc func(n Node) error

WalkFunc is a function that gets invoked when walking a Graph. Walking will stop if WalkFunc returns a non-nil error.

Jump to

Keyboard shortcuts

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