graph

package
v0.0.0-...-be83087 Latest Latest
Warning

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

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

Documentation

Overview

Package graph contains utilities for (sparse|dense) (un|)directed (un|)weighted graphs.

One note about the convention: whenever there's a directed edge (u, v), its tail will be called u and its head will be called v.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteDOT

func WriteDOT(g Any, w io.Writer, name string, directed bool, nodeAttr func(v int) map[string]string, edgeAttr func(u, v int) map[string]string) (err error)

WriteDOT writes the graph out in GraphViz format. The `nodeAttr` and `edgeAttr` callback functions are optional, and can be used to add extra attributes to the node. If the callback returns a "label" attribute, it takes precedence over the usual node name / edge weight.

Types

type Any

type Any interface {
	Len() int
	V(string) (int, bool)
	Label(v int) string
	W(u, v int) int
	ForSucc(u int, cb func(v int) bool) bool
	// contains filtered or unexported methods
}

AnyGraph represents the common subset of all graph types.

It's useful for writing generic functions on both sparse and dense graphs. Note that there's generally some performance impact, so it's best left for non-sensitive utility functions.

type Builder

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

A Builder is used to incrementally grow a graph.

Note that there is no checking for duplicate edges by default. The caller should pick one of:

  • Ensure that the input only lists each edge once.
  • Accept that the resulting graph will in fact be a multigraph.
  • Use one of the dense graphs, which use an adjacency matrix (inherently robust for this).
  • TODO: add methods for deduplicating

func NewBuilder

func NewBuilder() *Builder

NewBuilder returns a new, empty Builder.

func (*Builder) AddEdge

func (b *Builder) AddEdge(from, to int)

AddEdge records a new edge between two vertices in the graph.

func (*Builder) AddEdgeL

func (b *Builder) AddEdgeL(from, to string)

AddEdgeL records a new edge between two vertices (denoted by labels, created if necessary).

func (*Builder) AddEdgeW

func (b *Builder) AddEdgeW(from, to, w int)

AddEdgeW records a new weighted edge between two vertices in the graph.

func (*Builder) AddEdgeWL

func (b *Builder) AddEdgeWL(from, to string, w int)

AddEdgeWL records a new weighted edge between two vertices (denoted by labels, created if necessary).

func (*Builder) DenseDigraph

func (b *Builder) DenseDigraph() (g *Dense)

DenseDigraph returns the contents of the builder as an unweighted dense digraph.

func (*Builder) DenseDigraphW

func (b *Builder) DenseDigraphW() (g *DenseW)

DenseDigraphW returns the contents of the builder as a weighted dense digraph.

If multiple edges have been recorded between two vertices, their values sum up.

func (*Builder) DenseGraph

func (b *Builder) DenseGraph() (g *Dense)

DenseGraph returns the contents of the builder as an unweighted dense undirected graph.

func (*Builder) DenseGraphW

func (b *Builder) DenseGraphW() (g *DenseW)

DenseGraphW returns the contents of the builder as a weighted dense undirected graph.

If multiple edges have been recorded between two vertices (in any order), their values sum up.

func (*Builder) Len

func (b *Builder) Len() int

Len returns the number of vertices added to the builder so far.

func (*Builder) SparseDigraph

func (b *Builder) SparseDigraph() (g *Sparse)

SparseDigraph returns the contents of the builder as an unweighted sparse digraph.

func (*Builder) SparseDigraphW

func (b *Builder) SparseDigraphW() (g *SparseW)

SparseDigraphW returns the contents of the builder as an unweighted sparse digraph.

func (*Builder) V

func (b *Builder) V(label string) int

V returns the vertex number corresponding to the given label, creating it if necessary.

type Dense

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

func (*Dense) DelEdge

func (g *Dense) DelEdge(u, v int)

DelEdge removes the edge (u, v) from the graph. If the edge did not exist, the call does nothing.

func (*Dense) E

func (g *Dense) E(u, v int) bool

E returns true if the edge (u, v) exists in the graph.

func (*Dense) ForSucc

func (g *Dense) ForSucc(u int, cb func(v int) bool) bool

ForSucc iterates over the successors of u, returning true if the end was reached. Return false from the callback to stop short; that is then returned to the caller.

func (*Dense) ForSuccW

func (g *Dense) ForSuccW(u int, cb func(v, w int) bool) bool

ForSuccW is like ForSucc, but the weight (always 1) will also be passed to the callback.

func (Dense) Label

func (l Dense) Label(v int) string

Label returns the label corresponding to a vertex index.

func (Dense) Len

func (l Dense) Len() int

Len returns the number of vertices in the graph.

func (*Dense) Next

func (g *Dense) Next(it DenseIt) DenseIt

Next moves an edge iterator (from Succ or Pred) one step further.

func (*Dense) NumPred

func (g *Dense) NumPred(v int) (n int)

NumPred returns the total number of successors of u. This is an O(|V|) operation.

func (*Dense) NumSucc

func (g *Dense) NumSucc(u int) (n int)

NumSucc returns the total number of successors of u. This is an O(|V|) operation.

func (*Dense) Pred

func (g *Dense) Pred(v int) DenseIt

Pred returns an iterator for the predecessors of vertex v.

func (*Dense) Succ

func (g *Dense) Succ(u int) DenseIt

Succ returns an iterator for the successors of vertex u.

func (*Dense) TopoSort

func (g *Dense) TopoSort(keepEdges bool) []int

TopoSort returns a topological sort order for the graph.

func (Dense) V

func (l Dense) V(name string) (idx int, ok bool)

V returns the vertex index of the vertex with the given name.

func (*Dense) W

func (g *Dense) W(u, v int) int

W returns 1 if an edge exists between the two vertices, or 0 otherwise.

type DenseIt

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

DenseIt is an iterator over the edges of a dense graph.

func (DenseIt) At

func (it DenseIt) At() (u, v int)

func (DenseIt) Head

func (it DenseIt) Head() int

func (DenseIt) Tail

func (it DenseIt) Tail() int

func (DenseIt) Valid

func (it DenseIt) Valid() bool

type DenseW

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

func (*DenseW) E

func (g *DenseW) E(u, v int) bool

E returns true if the edge (u, v) exists and has a non-zero weight in the graph.

func (*DenseW) ForSucc

func (g *DenseW) ForSucc(u int, cb func(v int) bool) bool

ForSucc iterates over the successors of u, returning true if the end was reached. Return false from the callback to stop short; that is then returned to the caller.

func (*DenseW) ForSuccW

func (g *DenseW) ForSuccW(u int, cb func(v, w int) bool) bool

ForSuccW is like ForSucc, but the weight will also be passed to the callback.

func (DenseW) Label

func (l DenseW) Label(v int) string

Label returns the label corresponding to a vertex index.

func (DenseW) Len

func (l DenseW) Len() int

Len returns the number of vertices in the graph.

func (*DenseW) Next

func (g *DenseW) Next(it DenseIt) DenseIt

Next moves an edge iterator (from Succ or Pred) one step further.

func (*DenseW) Pred

func (g *DenseW) Pred(v int) DenseIt

Pred returns an iterator for the predecessors of vertex v.

func (*DenseW) Succ

func (g *DenseW) Succ(u int) DenseIt

Succ returns an iterator for the successors of vertex u.

func (DenseW) V

func (l DenseW) V(name string) (idx int, ok bool)

V returns the vertex index of the vertex with the given name.

func (*DenseW) W

func (g *DenseW) W(u, v int) int

W returns the weight of an edge between two vertices.

type Sparse

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

func (*Sparse) E

func (g *Sparse) E(u, v int) bool

E returns true if the edge (u, v) exists in the graph.

func (*Sparse) ForSucc

func (g *Sparse) ForSucc(u int, cb func(v int) bool) bool

ForSucc iterates over the successors of u, returning true if the end was reached. Return false from the callback to stop short; that is then returned to the caller.

func (*Sparse) ForSuccW

func (g *Sparse) ForSuccW(u int, cb func(v, w int) bool) bool

ForSuccW is like ForSucc, but the weight (always 1) will also be passed to the callback.

func (Sparse) Label

func (l Sparse) Label(v int) string

Label returns the label corresponding to a vertex index.

func (Sparse) Len

func (l Sparse) Len() int

Len returns the number of vertices in the graph.

func (*Sparse) Next

func (g *Sparse) Next(it SparseIt) SparseIt

Next moves an edge iterator (from Succ or Pred) one step further.

func (*Sparse) Succ

func (g *Sparse) Succ(u int) SparseIt

Succ returns an iterator for the successors of vertex u.

func (Sparse) V

func (l Sparse) V(name string) (idx int, ok bool)

V returns the vertex index of the vertex with the given name.

func (*Sparse) W

func (g *Sparse) W(u, v int) int

W returns 1 if an edge exists between the two vertices, or 0 otherwise.

type SparseIt

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

SparseIt is an iterator over the edges of a sparse unweighted graph.

func (SparseIt) At

func (it SparseIt) At() (u, v int)

func (SparseIt) Head

func (it SparseIt) Head() int

func (SparseIt) Tail

func (it SparseIt) Tail() int

func (SparseIt) Valid

func (it SparseIt) Valid() bool

type SparseItW

type SparseItW struct {
	SparseIt
	// contains filtered or unexported fields
}

SparseWIt is an iterator over the edges of a sparse weighted graph.

func (SparseItW) W

func (it SparseItW) W() int

type SparseW

type SparseW struct {
	Sparse
	// contains filtered or unexported fields
}

func (*SparseW) E

func (g *SparseW) E(u, v int) bool

E returns true if the edge (u, v) exists and has a non-zero weight in the graph.

func (*SparseW) ForSuccW

func (g *SparseW) ForSuccW(u int, cb func(v, w int) bool) bool

ForSuccW is like ForSucc, but the weight will also be passed to the callback.

func (SparseW) Label

func (l SparseW) Label(v int) string

Label returns the label corresponding to a vertex index.

func (SparseW) Len

func (l SparseW) Len() int

Len returns the number of vertices in the graph.

func (*SparseW) Next

func (g *SparseW) Next(it SparseItW) SparseItW

Next moves an edge iterator (from Succ or Pred) one step further.

func (*SparseW) NumSucc

func (g *SparseW) NumSucc(u int) int

NumSucc returns the number of successors of u. This is an O(1) operation.

func (*SparseW) PredI

func (g *SparseW) PredI(v, i int) int

PredI returns the i'th predecessor of u (ordered by vertex index). Note that this is an O(|V|+|E|) operation.

func (*SparseW) Succ

func (g *SparseW) Succ(u int) SparseItW

Succ returns an iterator for the successors of vertex u.

func (*SparseW) SuccI

func (g *SparseW) SuccI(u, i int) int

SuccI returns the i'th successor of u. This is an O(1) operation.

func (*SparseW) TopoSort

func (g *SparseW) TopoSort(keepEdges bool) []int

TopoSort returns a topological sort order for the graph.

func (SparseW) V

func (l SparseW) V(name string) (idx int, ok bool)

V returns the vertex index of the vertex with the given name.

func (*SparseW) W

func (g *SparseW) W(u, v int) int

W returns the weight of an edge between two vertices. Note that this involves a linear scan for the edge in the edge list; if iterating, get the value from the iterator instead.

Jump to

Keyboard shortcuts

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