graph

package
v0.0.0-...-040724e Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2019 License: BSD-3-Clause, GPL-2.0 Imports: 6 Imported by: 7

Documentation

Overview

Package graph provides common graph algorithms

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTraversalDone is a predefined error for expected early termination of a traversal
	ErrTraversalDone = errors.New("traversal done")
	// ErrNextNode is a predefined error for continuing a traversal
	ErrNextNode = errors.New("next node")
)

Functions

func IsDag

func IsDag(g Graph) error

IsDag returns nil if graph is acyclic. If graph contains a cycle, return error.

func IsTree

func IsTree(g Graph, root Node) error

IsTree returns nil if graph is a rooted tree. If not, it returns error containing a counterexample.

func Print

func Print(opt PrintOpt) string

Print returns a string version of a Graph

func ShortestPath

func ShortestPath(opt ShortestPathOpt) map[Node]int64

ShortestPath implements Dijkstra's algorithm

func Verify

func Verify(graph Graph) error

Verify returns an error in a graph doesn't satisfy some basic consistent properties.

func VisitTree

func VisitTree(opt VisitTreeOpt) error

VisitTree applies a tree visitor.

Types

type DisjointSet

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

A DisjointSet efficiently stores sets of nodes

func NewDisjointSet

func NewDisjointSet() *DisjointSet

NewDisjointSet creates a new disjoint set

func (*DisjointSet) Find

func (a *DisjointSet) Find(n Node) Node

Find returns the representative of a set

func (*DisjointSet) Node

func (a *DisjointSet) Node(i int) Node

Node implements a Graph

func (*DisjointSet) NumNodes

func (a *DisjointSet) NumNodes() int

NumNodes implements a Graph

func (*DisjointSet) NumOuts

func (a *DisjointSet) NumOuts(n Node) int

NumOuts implements a Graph

func (*DisjointSet) Out

func (a *DisjointSet) Out(n Node, i int) Node

Out implements a Graph

func (*DisjointSet) Union

func (a *DisjointSet) Union(x, y Node)

Union merges to sets

type EliminateOpt

type EliminateOpt struct {
	Graph          Graph
	In             func(Node) bool // Should node be included
	KeepMultiEdges bool
}

An EliminateOpt are a set of options to Eliminate

type Graph

type Graph interface {
	NumNodes() int
	Node(int) Node
	NumOuts(Node) int
	Out(Node, int) Node
}

A Graph is a relation between nodes

func Eliminate

func Eliminate(opt EliminateOpt) Graph

Eliminate returns the graph resulting from node elimination. Node elimination removes node n by adding edges (in(n), out(n)) for the product of incoming and outgoing neighbors.

func Reaches

func Reaches(g Graph) Graph

Reaches computes reachability over graph. A reaches B if there is a path from A to B.

func Reverse

func Reverse(graph Graph) Graph

Reverse edges

func Simplify

func Simplify(opt SimplifyOpt) Graph

Simplify graph

func TransitiveReduction

func TransitiveReduction(graph Graph) (Graph, error)

TransitiveReduction computes transitive reduction of a graph. Relatively expensive operation: O(nm).

type IntSet

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

An IntSet is a set of integers

func NewIntSet

func NewIntSet() *IntSet

NewIntSet returns a new IntSet

func (*IntSet) Add

func (a *IntSet) Add(xs ...int) interface{}

Add returns a unique identifier for a set of ints

type Labeler

type Labeler func(interface{}) string

A Labeler is a function that returns a string given an object

type LessThan

type LessThan func(Node, Node) bool

LessThan compares nodes

type MakeQuotientOpt

type MakeQuotientOpt struct {
	Graph         Graph
	Colorer       func(Node) interface{}
	HasColor      func(Node) bool
	Present       func(Node) bool // Should n be included at all
	KeepSelfEdges bool
}

MakeQuotientOpt are options to MakeQuotient

type Node

type Node interface{}

A Node is a node in a graph

func TopoSort

func TopoSort(opt TopoSortOpt) ([]Node, error)

TopoSort returns topological sort of graph. If edge (a, b) is in g, then b < a in the resulting order. Returns an error if graph contains a cycle.

type NodeSet

type NodeSet interface {
	Has(Node) bool
	Values() []Node
	Len() int
}

A NodeSet is a unordered collection of nodes

type PartitionTreeOpt

type PartitionTreeOpt struct {
	Tree       Graph
	Root       Node
	Colors     func(n Node) []int   // Possible colors n may take; all nodes in graph must have at least one color
	EdgeWeight func(a, b int) int64 // Weight between a and b (non-negative); depth(a) < depth(b)
	NodeWeight func(a int) int      // Weight of a
	// contains filtered or unexported fields
}

PartitionTreeOpt is a set of options for PartitionTree

type PrintOpt

type PrintOpt struct {
	Graph        Graph
	NodeLabelers []Labeler
}

PrintOpt are options for Print. Currently only supports dot output.

type QGraph

type QGraph interface {
	Graph
	OrigGraph() Graph    // Original graph that this graph was constructed from
	NumOrigs(Node) int   // Number of nodes in the original graph that this node represents
	Orig(Node, int) Node // The ith node in the original graph that this node represents
}

QGraph is a quotient graph

func MakeQuotient

func MakeQuotient(opt MakeQuotientOpt) QGraph

MakeQuotient returns a quotient graph. Nodes with the same color merged into a single node. A colorless node is treated as having a color distinct from any other node.

type Reachability

type Reachability map[Node]map[Node]bool

func NewReachability

func NewReachability(g Graph) Reachability

NewReachability compute the reachability matrix for the graph

type SDag

type SDag struct {
	Graph Graph
	Roots []Node
	Ins   map[Node]int
}

An SDag is the current state of DAG scheduling

func Schedule

func Schedule(graph Graph) *SDag

Schedule treats directed acyclic graph as a dependency graph and schedules nodes to execute

func (*SDag) Visit

func (a *SDag) Visit(n Node) (r []Node)

Visit marks a node as visited and returns nodes that can be visited next

type ShortestPathOpt

type ShortestPathOpt struct {
	Graph   Graph
	Sources []Node
	Weight  func(x, y Node) int64
}

ShortestPathOpt is a set of options to ShortestPath

type SimplifyOpt

type SimplifyOpt struct {
	Graph            Graph
	RemoveSelfLoops  bool
	RemoveMultiEdges bool
	RemoveNodes      func(Node) bool // Should node be removed
}

A SimplifyOpt are options to Simplify

type StringGraph

type StringGraph struct {
	Nodes []string
	Outs  map[string][]string
}

A StringGraph is a graph where nodes are strings

func (*StringGraph) Node

func (a *StringGraph) Node(i int) Node

Node implements a Graph

func (*StringGraph) NumNodes

func (a *StringGraph) NumNodes() int

NumNodes implements a Graph

func (*StringGraph) NumOuts

func (a *StringGraph) NumOuts(n Node) int

NumOuts implements a Graph

func (*StringGraph) Out

func (a *StringGraph) Out(n Node, i int) Node

Out implements a Graph

type TopoSortOpt

type TopoSortOpt struct {
	Graph     Graph
	NodeOrder LessThan // Optional argument to ensure deterministic output
}

A TopoSortOpt are options to TopoSort

type TreePartition

type TreePartition struct {
	Parts  map[Node]int
	Weight int64
}

A TreePartition is a partition of the nodes of a tree

func PartitionTree

func PartitionTree(opt PartitionTreeOpt) (*TreePartition, error)

PartitionTree partitions a tree. Given a tree and a set of colors that each node may take, select a color for each node that minimizes the weight of the tree.

type TreeVisitor

type TreeVisitor func(n, parent Node, err error) error

A TreeVisitor is a function that can be applied to nodes in a tree

type VisitOpt

type VisitOpt struct {
	Root         Node    // Root of traversal
	Graph        Graph   // Graph to traverse
	Visitor      Visitor // Function to apply when node first encountered
	Seen         Visitor // Function applied on subsequent visits to a node
	BreadthFirst bool    // Visit nodes breadth first
}

VisitOpt is a set of options to Visit

type VisitResult

type VisitResult struct {
	Seen      NodeSet
	Frontiers []NodeSet // If VisitOpt.BreadthFirst, successive frontiers are placed here
}

A VisitResult is the result of Visit

func Visit

func Visit(opt VisitOpt) (res *VisitResult, err error)

Visit applies a visitor to each node reachable from root in some order. Returns nodes visited. If visitor returns an error, stop traversal early and pass returned error.

type VisitTreeOpt

type VisitTreeOpt struct {
	Tree      Graph
	Root      Node
	PreOrder  TreeVisitor // if err != TraversalDone, propagate error
	PostOrder TreeVisitor // if err != TraversalDone, propagate error
}

VisitTreeOpt are a set of options to VisitTree

type Visitor

type Visitor func(Node) error

A Visitor is a function over graph nodes

Jump to

Keyboard shortcuts

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