graphalg

package
v0.0.0-...-f10218a Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package graphalg implements common graph algorithms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DomFrontier

func DomFrontier(g graph.BiGraph, root int, idom []int) [][]int

DomFrontier returns the dominance frontier of each node in g. idom must be IDom(g, root). idom may be nil, in which case this computes IDom.

func IDom

func IDom(g graph.BiGraph, root int) []int

IDom returns the immediate dominator of each node of g. Nodes that don't have an immediate dominator (including root) are assigned -1.

func PostOrder

func PostOrder(g graph.Graph, root int) []int

PostOrder returns the nodes of g visited in post-order.

func PreOrder

func PreOrder(g graph.Graph, root int) []int

PreOrder returns the nodes of g visited in pre-order.

func Reverse

func Reverse(xs []int) []int

Reverse reverses xs in place and returns the slice. This is useful in conjunction with PreOrder and PostOrder to compute reverse post-order and reverse pre-order.

func SimplifyMulti

func SimplifyMulti(g graph.Graph) graph.Weighted

SimplifyMulti simplifies a multigraph to a weighted simple graph.

If g is a weighted graph, each edge in the result receives the sum of the weights of the combined edges in g. If g is not weighted, each edge in g is assumed to have a weight of 1.

Types

type DomTree

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

DomTree is a dominator tree.

It also satisfies the BiGraph interface, which edges pointing toward children.

func Dom

func Dom(idom []int) *DomTree

Dom computes the dominator tree from the immediate dominators (as computed by IDom). The nodes of the resulting DomTree have the same numbering as the nodes in the original graph.

func (*DomTree) IDom

func (t *DomTree) IDom(n int) int

func (*DomTree) In

func (t *DomTree) In(n int) []int

func (*DomTree) NumNodes

func (t *DomTree) NumNodes() int

func (*DomTree) Out

func (t *DomTree) Out(n int) []int

type Euler

type Euler struct {
	// Enter is called when a node a first visited. It may be nil.
	Enter func(n int)

	// Exit is called when all of the children of n have been
	// visited. It may be nil.
	//
	// Calls to Enter and Exit are always paired in nested order.
	Exit func(n int)
}

Euler visits a graph using an Euler tour.

For a tree, the Euler tour is well-defined and unique (given an ordering of the children of a node). For a general graph, this uses the tree formed by the pre-order traversal of the graph.

func (Euler) Visit

func (e Euler) Visit(g graph.Graph, root int)

Visit performs a Euler tour over g starting at root and invokes the callbacks on e.

type NodeMarks

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

NodeMarks is a structure for marking nodes in a graph.

func NewNodeMarks

func NewNodeMarks() *NodeMarks

NewNodeMarks returns a node mark set with no marks set.

func (*NodeMarks) Mark

func (m *NodeMarks) Mark(i int)

Mark marks node i.

func (NodeMarks) Next

func (m NodeMarks) Next(i int) int

Next returns the index of the next set mark after mark i, or -1 if there are no set marks after i.

This is typically used to loop over set marks like:

for i := m.Next(-1); i >= 0; i = m.Next(i) { ... }

func (NodeMarks) Test

func (m NodeMarks) Test(i int) bool

Test returns whether node i is marked.

func (*NodeMarks) Unmark

func (m *NodeMarks) Unmark(i int)

Unmark clears the mark on node i.

type SCCFlags

type SCCFlags int

SCCFlags is a set of optional analyses to perform when constructing strongly-connected components.

const (
	// SCCSubnodeComponent instructs SCC to record a mapping from
	// subnode to component ID containing that subnode.
	SCCSubnodeComponent SCCFlags = 1 << iota

	// SCCEdges instructs SCC to record edges between components.
	// Otherwise, the resulting SCC graph will have a node for
	// each strongly-connected component, but no edges.
	SCCEdges
)

type SCCGraph

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

SCCGraph is a set of strongly-connected components of another graph.

Each strongly-connected component is a node in this graph. The components are numbered in reverse topological sort order.

If the graph was constructed with flag SCCEdges, then it also has edges between the components that follow the edges in the underlying graph.

func SCC

func SCC(g graph.Graph, flags SCCFlags) *SCCGraph

SCC computes the strongly-connected components of graph g.

This implements Tarjan's strongly connected components algorithm [1]. It runs in O(V + E) time and O(V) space.

[1] Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms", SIAM Journal on Computing, 1 (2): 146–160.

func (*SCCGraph) NumNodes

func (g *SCCGraph) NumNodes() int

NumNodes returns the number of strongly-connected components in g.

func (*SCCGraph) Out

func (g *SCCGraph) Out(cid int) []int

Out returns the IDs of the components for which there are any edges in the underlying graph from component cid.

Graph g must have been constructed with flag SCCEdges. Otherwise this returns nil.

func (*SCCGraph) SubnodeComponent

func (g *SCCGraph) SubnodeComponent(subID int) (componentID int)

SubnodeComponent returns the component ID (a node ID in g) of sub-graph node subID (a node ID in the underlying graph).

Graph g must have been constructed with flag SCCSubnodeComponent.

func (*SCCGraph) Subnodes

func (g *SCCGraph) Subnodes(cid int) []int

Subnodes returns the IDs of the nodes in the underlying graph that comprise component cid.

Jump to

Keyboard shortcuts

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