Documentation ¶
Overview ¶
gogl provides a framework for representing and working with graphs.
gogl is divided chiefly into two parts: datastructures and algorithms. These components are loosely coupled; the algorithms rely strictly on gogl's various exported Graph interfaces to do their work.
Index ¶
- Constants
- func CopyGraph(from Graph, to interface{}) interface{}
- type AdjacencyEnumerator
- type DataEdge
- type DataEdgeList
- type DataEdgeSetMutator
- type DataGraph
- type DegreeChecker
- type Digraph
- type DirectedDegreeChecker
- type Edge
- func CollectArcsFrom(v Vertex, g IncidentArcEnumerator) (edges []Edge)
- func CollectArcsTo(v Vertex, g IncidentArcEnumerator) (edges []Edge)
- func CollectEdges(g EdgeEnumerator) (edges []Edge)
- func CollectEdgesIncidentTo(v Vertex, g IncidentEdgeEnumerator) (edges []Edge)
- func NewEdge(u, v Vertex) Edge
- type EdgeEnumerator
- type EdgeLambda
- type EdgeList
- type EdgeMembershipChecker
- type EdgeSetMutator
- type Graph
- type GraphProperties
- type GraphSource
- type GraphSpec
- func (b GraphSpec) Basic() GraphSpec
- func (b GraphSpec) Create(f func(GraphSpec) Graph) Graph
- func (b GraphSpec) DataEdges() GraphSpec
- func (b GraphSpec) Directed() GraphSpec
- func (b GraphSpec) Immutable() GraphSpec
- func (b GraphSpec) Labeled() GraphSpec
- func (b GraphSpec) Loop() GraphSpec
- func (b GraphSpec) MultiGraph() GraphSpec
- func (b GraphSpec) Mutable() GraphSpec
- func (b GraphSpec) Parallel() GraphSpec
- func (b GraphSpec) PseudoGraph() GraphSpec
- func (b GraphSpec) SimpleGraph() GraphSpec
- func (b GraphSpec) Undirected() GraphSpec
- func (b GraphSpec) Using(g GraphSource) GraphSpec
- func (b GraphSpec) Weighted() GraphSpec
- type IncidentArcEnumerator
- type IncidentEdgeEnumerator
- type LabeledEdge
- type LabeledEdgeList
- type LabeledEdgeSetMutator
- type LabeledGraph
- type MutableDataGraph
- type MutableGraph
- type MutableLabeledGraph
- type MutableWeightedGraph
- type SimpleGraph
- type Transposer
- type Vertex
- type VertexEnumerator
- type VertexLambda
- type VertexMembershipChecker
- type VertexSetMutator
- type WeightedEdge
- type WeightedEdgeList
- type WeightedEdgeSetMutator
- type WeightedGraph
Constants ¶
const ( // Edge directedness. Flags are provided for both, though gogl does not really support // a hybrid graph containing both directed and undirected edges. Algorithms would have // undefined results. G_UNDIRECTED = 1 << iota G_DIRECTED // Edge type. Basic (untyped edges, represented solely by the Edge interface) is the implied zero-value. G_BASIC G_LABELED G_WEIGHTED G_DATA // Multiplicity. Simple (no loops or multiple edges) is the implied zero-value. G_SIMPLE G_LOOPS G_PARALLEL // Mutability. Immutable is the implied zero-value. G_IMMUTABLE G_MUTABLE G_PERSISTENT = 1<<iota | G_MUTABLE // Persistent graphs are, kinda weirdly, both. )
const NullGraph = nullGraph(false)
The null graph is a graph without any edges or vertices. It implements all possible (non-mutable) graph interfaces.
In effect, it is the zero-value of all possible graph types.
Variables ¶
This section is empty.
Functions ¶
func CopyGraph ¶ added in v0.4.0
func CopyGraph(from Graph, to interface{}) interface{}
Copies the first graph's edges and vertices into the second, and returns the second.
The second argument must be some flavor of MutableGraph, otherwise this function will panic.
Generally speaking, this is a very naive copy operation; it should not be relied on for complex cases.
Types ¶
type AdjacencyEnumerator ¶ added in v0.4.0
type AdjacencyEnumerator interface {
EachAdjacentTo(start Vertex, adjacentVertexLambda VertexLambda)
}
An AdjacencyEnumerator iteratively enumerates a given vertex's adjacent vertices.
type DataEdge ¶ added in v0.3.0
type DataEdge interface { Edge Data() interface{} }
A DataEdge is an Edge that also holds arbitrary data.
func NewDataEdge ¶ added in v0.4.0
Create a new "data" edge - an edge with arbitrary embedded data.
type DataEdgeList ¶ added in v0.4.0
type DataEdgeList []DataEdge
A DataEdgeList is a naive GraphSource implementation that is backed only by an edge slice.
This variant is for labeled edges.
func (DataEdgeList) EachEdge ¶ added in v0.4.0
func (el DataEdgeList) EachEdge(fn EdgeLambda)
func (DataEdgeList) EachVertex ¶ added in v0.4.0
func (el DataEdgeList) EachVertex(fn VertexLambda)
func (DataEdgeList) Order ¶ added in v0.4.0
func (el DataEdgeList) Order() int
type DataEdgeSetMutator ¶ added in v0.4.0
A DataEdgeSetMutator allows the addition and removal of data edges from a set.
type DataGraph ¶ added in v0.3.0
A data graph is a graph subtype where the edges carry arbitrary Go data; as described by the DataEdge interface, this identifier is an interface{}.
DataGraphs have both the HasEdge() and HasDataEdge() methods. Correct implementations should treat the difference as a matter of strictness:
HasEdge() should return true as long as an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), regardless of its label.
HasDataEdge() should return true iff an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), AND if the edge data is the same. Simple comparison will typically be used to establish data equality, which means that using noncomparables (a slice, map, or non-pointer struct containing a slice or a map) for the data will cause a panic.
type DegreeChecker ¶ added in v0.4.0
type DegreeChecker interface {
DegreeOf(Vertex) (degree int, exists bool) // Number of incident edges; if vertex is present
}
A DegreeChecker reports the number of edges incident to a given vertex.
type Digraph ¶ added in v0.4.0
type Digraph interface { Graph IncidentArcEnumerator // Enumerates a vertex's incident in- and out-arcs to an injected lambda DirectedDegreeChecker // Reports in- and out-degree of vertices Transposer // Digraphs can produce a transpose of themselves }
Digraph (directed graph) describes a Graph where all the edges are directed.
gogl treats edge directionality as a property of the graph, not the edge itself. Thus, implementing this interface is gogl's only signal that a graph's edges are directed.
type DirectedDegreeChecker ¶ added in v0.4.0
type DirectedDegreeChecker interface { InDegreeOf(Vertex) (degree int, exists bool) // Number of in-edges; if vertex is present OutDegreeOf(Vertex) (degree int, exists bool) // Number of out-edges; if vertex is present }
A DirectedDegreeChecker reports the number of in or out-edges incident to given vertex.
type Edge ¶
The Edge interface describes a connection between two vertices.
Edge does not have an intrinsic opinion about directionality; gogl treats that as a property of the overall Graph object in which the Edge appears rather than a property of any individual Edge.
func CollectArcsFrom ¶ added in v0.4.0
func CollectArcsFrom(v Vertex, g IncidentArcEnumerator) (edges []Edge)
Collects all of a given vertex's out-edges into an edge slice, for easy range-ing.
This is a convenience function. Avoid it on very large graphs or in performance critical sections.
func CollectArcsTo ¶ added in v0.4.0
func CollectArcsTo(v Vertex, g IncidentArcEnumerator) (edges []Edge)
Collects all of a given vertex's in-edges into an edge slice, for easy range-ing.
This is a convenience function. Avoid it on very large graphs or in performance critical sections.
func CollectEdges ¶ added in v0.4.0
func CollectEdges(g EdgeEnumerator) (edges []Edge)
Collects all of a graph's edges into an edge slice, for easy range-ing.
This is a convenience function. Avoid it on very large graphs or in performance critical sections.
func CollectEdgesIncidentTo ¶ added in v0.4.0
func CollectEdgesIncidentTo(v Vertex, g IncidentEdgeEnumerator) (edges []Edge)
Collects all of a given vertex's incident edges into an edge slice, for easy range-ing.
This is a convenience function. Avoid it on very large graphs or in performance critical sections.
type EdgeEnumerator ¶ added in v0.4.0
type EdgeEnumerator interface {
EachEdge(EdgeLambda)
}
An EdgeEnumerator iteratively enumerates edges, and can indicate the number of edges present.
type EdgeLambda ¶ added in v0.4.0
EdgeLambdas are used as arguments to various enumerators. They are called once for each edge produced by the enumerator.
If the lambda returns true, the calling enumerator is expected to end enumeration and return control to its caller.
type EdgeList ¶
type EdgeList []Edge
An EdgeList is a naive GraphSource implementation that is backed only by an edge slice.
EdgeLists are primarily intended for use as fixtures.
It is inherently impossible for an EdgeList to represent a vertex isolate (degree 0) fully correctly, as vertices are only described in the context of their edges. One can be a little hacky, though, and represent one with a loop. As gogl expects graph implementations to simply discard loops if they are disallowed by the graph's constraints (i.e., in simple and multigraphs), they *should* be interpreted as vertex isolates.
func (EdgeList) EachEdge ¶ added in v0.4.0
func (el EdgeList) EachEdge(fn EdgeLambda)
func (EdgeList) EachVertex ¶ added in v0.4.0
func (el EdgeList) EachVertex(fn VertexLambda)
type EdgeMembershipChecker ¶ added in v0.4.0
An EdgeMembershipChecker can indicate the presence of an edge.
type EdgeSetMutator ¶ added in v0.4.0
An EdgeSetMutator allows the addition and removal of edges from a set.
type Graph ¶
type Graph interface { VertexEnumerator // Enumerates vertices to an injected lambda EdgeEnumerator // Enumerates edges to an injected lambda AdjacencyEnumerator // Enumerates a vertex's adjacent vertices to an injected lambda IncidentEdgeEnumerator // Enumerates a vertex's incident edges to an injected lambda VertexMembershipChecker // Allows inspection of contained vertices EdgeMembershipChecker // Allows inspection of contained edges DegreeChecker // Reports degree of vertices Order() int // Reports total number of vertices in the graph Size() int // Reports total number of edges in the graph }
Graph is gogl's most basic interface: it contains only the methods that *every* type of graph implements.
Graph is intentionally underspecified: both directed and undirected graphs implement it; simple graphs, multigraphs, weighted, labeled, or any combination thereof.
The semantics of some of these methods vary slightly from one graph type to another, but in general, the basic Graph methods are supplemented, not superceded, by the methods in more specific interfaces.
Graph is a purely read oriented interface; the various Mutable*Graph interfaces contain the methods for writing.
func AdjacencyList ¶ added in v0.4.0
Create a graph implementation in the adjacency list style from the provided GraphSpec.
If the GraphSpec contains a GraphSource, it will be imported into the provided graph. If the GraphSpec indicates a graph type that is not currently implemented, this function will panic.
type GraphProperties ¶ added in v0.4.0
type GraphProperties uint16
Describes the properties of a graph as a bitfield.
type GraphSource ¶ added in v0.4.0
type GraphSource interface { VertexEnumerator EdgeEnumerator Order() int }
GraphSource is a subinterface of Graph, describing the minimal set of methods necessary to accomplish a naive full graph traversal and copy.
type GraphSpec ¶ added in v0.4.0
type GraphSpec struct { Props GraphProperties Source GraphSource }
func G ¶ added in v0.4.0
func G() GraphSpec
Create a graph spec, which allows specification and creation of a graph through a fluent builder-style interface.
func (GraphSpec) Basic ¶ added in v0.4.0
Specify that the edges should be "basic" - no weights, labels, or data.
func (GraphSpec) Create ¶ added in v0.4.0
Creates a graph from the spec, using the provided creator function.
This is just a convenience method; the creator function can always be called directly.
func (GraphSpec) DataEdges ¶ added in v0.4.0
Specify that the edges should contain arbitrary data. See DataEdge
func (GraphSpec) Directed ¶ added in v0.4.0
Specify that the graph should have directed edges (be a digraph).
func (GraphSpec) Labeled ¶ added in v0.4.0
Specify that the edges should be labeled. See LabeledEdge
func (GraphSpec) Loop ¶ added in v0.4.0
Specify that the graph allows loops - edges connecting a vertex to itself.
func (GraphSpec) MultiGraph ¶ added in v0.4.0
Specify that the graph is a multigraph - allows parallel edges, but no loops.
func (GraphSpec) PseudoGraph ¶ added in v0.4.0
Specify that the graph is a pseudograph - allows both loops and parallel edges.
func (GraphSpec) SimpleGraph ¶ added in v0.4.0
Specify that the graph should be simple - have no loops or multiple edges.
func (GraphSpec) Undirected ¶ added in v0.4.0
Specify that the graph should have undirected edges.
func (GraphSpec) Using ¶ added in v0.4.0
func (b GraphSpec) Using(g GraphSource) GraphSpec
Specify that the graph should be populated from the provided source "graph".
The GraphSource interface is used here because this is an ideal place at which to load in, for example, graph data exported into a flat file; a GraphSource can represent that data and only implement the minimal interface.
type IncidentArcEnumerator ¶ added in v0.4.0
type IncidentArcEnumerator interface { EachArcFrom(v Vertex, outEdgeLambda EdgeLambda) EachArcTo(v Vertex, inEdgeLambda EdgeLambda) }
An IncidentArcEnumerator iteratively enumerates a given vertex's incident arcs (directed edges). One enumerator provides inbound edges, the other outbound edges.
type IncidentEdgeEnumerator ¶ added in v0.4.0
type IncidentEdgeEnumerator interface {
EachEdgeIncidentTo(v Vertex, incidentEdgeLambda EdgeLambda)
}
An IncidentEdgeEnumerator iteratively enumerates a given vertex's incident edges.
type LabeledEdge ¶ added in v0.2.0
A LabeledEdge is an Edge that also has a string label.
func NewLabeledEdge ¶ added in v0.4.0
func NewLabeledEdge(u, v Vertex, label string) LabeledEdge
Create a new labeled edge.
type LabeledEdgeList ¶ added in v0.4.0
type LabeledEdgeList []LabeledEdge
A LabeledEdgeList is a naive GraphSource implementation that is backed only by an edge slice.
This variant is for labeled edges.
func (LabeledEdgeList) EachEdge ¶ added in v0.4.0
func (el LabeledEdgeList) EachEdge(fn EdgeLambda)
func (LabeledEdgeList) EachVertex ¶ added in v0.4.0
func (el LabeledEdgeList) EachVertex(fn VertexLambda)
func (LabeledEdgeList) Order ¶ added in v0.4.0
func (el LabeledEdgeList) Order() int
type LabeledEdgeSetMutator ¶ added in v0.4.0
type LabeledEdgeSetMutator interface { AddEdges(edges ...LabeledEdge) RemoveEdges(edges ...LabeledEdge) }
A LabeledEdgeSetMutator allows the addition and removal of labeled edges from a set.
type LabeledGraph ¶ added in v0.2.0
type LabeledGraph interface { Graph HasLabeledEdge(e LabeledEdge) bool }
A labeled graph is a graph subtype where the edges have an identifier; as described by the LabeledEdge interface, this identifier is a string.
LabeledGraphs have both the HasEdge() and HasLabeledEdge() methods. Correct implementations should treat the difference as a matter of strictness:
HasEdge() should return true as long as an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), regardless of its label.
HasLabeledEdge() should return true iff an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), AND if the edge labels are the same.
type MutableDataGraph ¶ added in v0.3.0
type MutableDataGraph interface { DataGraph VertexSetMutator DataEdgeSetMutator }
MutableDataGraph is the mutable version of a propety graph. Its AddEdges() method is incompatible with MutableGraph, guaranteeing only property edges can be present in the graph.
type MutableGraph ¶ added in v0.1.0
type MutableGraph interface { Graph VertexSetMutator EdgeSetMutator }
MutableGraph describes a graph with basic edges (no weighting, labeling, etc.) that can be modified freely by adding or removing vertices or edges.
func NewUndirected ¶ added in v0.1.0
func NewUndirected() MutableGraph
type MutableLabeledGraph ¶ added in v0.2.0
type MutableLabeledGraph interface { LabeledGraph VertexSetMutator LabeledEdgeSetMutator }
LabeledWeightedGraph is the mutable version of a labeled graph. Its AddEdges() method is incompatible with MutableGraph, guaranteeing only labeled edges can be present in the graph.
type MutableWeightedGraph ¶ added in v0.1.0
type MutableWeightedGraph interface { WeightedGraph VertexSetMutator WeightedEdgeSetMutator }
MutableWeightedGraph is the mutable version of a weighted graph. Its AddEdges() method is incompatible with MutableGraph, guaranteeing only weighted edges can be present in the graph.
type SimpleGraph ¶
A simple graph is in opposition to a multigraph or pseudograph: it disallows loops and parallel edges.
type Transposer ¶ added in v0.4.0
type Transposer interface {
Transpose() Digraph
}
A Transposer produces a transposed version of a Digraph.
type Vertex ¶
type Vertex interface{}
A Vertex in gogl is a value of empty interface type.
This is largely comensurate with graph theory, as vertices are, in the general case, known to be unique within the graph context, but the particular property that makes them unique has no bearing on the graph's behavior. In Go-speak, that translates pretty nicely to interface{}.
func CollectVertices ¶ added in v0.4.0
func CollectVertices(g VertexEnumerator) (vertices []Vertex)
Collects all of a graph's vertices into a vertex slice, for easy range-ing.
This is a convenience function. Avoid it on very large graphs or in performance critical sections.
func CollectVerticesAdjacentTo ¶ added in v0.4.0
func CollectVerticesAdjacentTo(v Vertex, g AdjacencyEnumerator) (vertices []Vertex)
Collects all of a given vertex's adjacent vertices into a vertex slice, for easy range-ing.
This is a convenience function. Avoid it on very large graphs or in performance critical sections.
type VertexEnumerator ¶ added in v0.4.0
type VertexEnumerator interface {
EachVertex(VertexLambda)
}
A VertexEnumerator iteratively enumerates vertices, and can indicate the number of vertices present.
type VertexLambda ¶ added in v0.4.0
VertexLambdas are used as arguments to various enumerators. They are called once for each vertex produced by the enumerator.
If the lambda returns true, the calling enumerator is expected to end enumeration and return control to its caller.
type VertexMembershipChecker ¶ added in v0.4.0
type VertexMembershipChecker interface {
HasVertex(Vertex) bool // Whether or not the vertex is present in the set
}
A VertexMembershipChecker can indicate the presence of a vertex.
type VertexSetMutator ¶ added in v0.4.0
A VertexSetMutator allows the addition and removal of vertices from a set.
type WeightedEdge ¶ added in v0.1.0
A WeightedEdge is an Edge that also has a numerical weight.
func NewWeightedEdge ¶ added in v0.4.0
func NewWeightedEdge(u, v Vertex, weight float64) WeightedEdge
Create a new weighted edge.
type WeightedEdgeList ¶ added in v0.4.0
type WeightedEdgeList []WeightedEdge
A WeightedEdgeList is a naive GraphSource implementation that is backed only by an edge slice.
This variant is for weighted edges.
func (WeightedEdgeList) EachEdge ¶ added in v0.4.0
func (el WeightedEdgeList) EachEdge(fn EdgeLambda)
func (WeightedEdgeList) EachVertex ¶ added in v0.4.0
func (el WeightedEdgeList) EachVertex(fn VertexLambda)
func (WeightedEdgeList) Order ¶ added in v0.4.0
func (el WeightedEdgeList) Order() int
type WeightedEdgeSetMutator ¶ added in v0.4.0
type WeightedEdgeSetMutator interface { AddEdges(edges ...WeightedEdge) RemoveEdges(edges ...WeightedEdge) }
A WeightedEdgeSetMutator allows the addition and removal of weighted edges from a set.
type WeightedGraph ¶ added in v0.1.0
type WeightedGraph interface { Graph HasWeightedEdge(e WeightedEdge) bool }
A weighted graph is a graph subtype where the edges have a numeric weight; as described by the WeightedEdge interface, this weight is a signed int.
WeightedGraphs have both the HasEdge() and HasWeightedEdge() methods. Correct implementations should treat the difference as a matter of strictness:
HasEdge() should return true as long as an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), regardless of its weight.
HasWeightedEdge() should return true iff an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), AND if the edge weights are the same.