Documentation
¶
Overview ¶
Package graph implements graph data structures and algorithms.
There are four different type of graphs implementd:
- Undirected Graph
- Directed Graph
- Weighted Undirected Graph
- Weighted Directed Graph
Index ¶
- type ConnectedComponents
- type Directed
- func (g *Directed) AddEdge(v, w int)
- func (g *Directed) Adj(v int) []int
- func (g *Directed) DOT() string
- func (g *Directed) DirectedCycle() *DirectedCycle
- func (g *Directed) E() int
- func (g *Directed) InDegree(v int) int
- func (g *Directed) Orders(strategy TraversalStrategy) *Orders
- func (g *Directed) OutDegree(v int) int
- func (g *Directed) Paths(s int, strategy TraversalStrategy) *Paths
- func (g *Directed) Reverse() *Directed
- func (g *Directed) StronglyConnectedComponents() *StronglyConnectedComponents
- func (g *Directed) Topological() *Topological
- func (g *Directed) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
- func (g *Directed) V() int
- type DirectedCycle
- type DirectedEdge
- type FlowEdge
- type FlowNetwork
- type MinimumSpanningTree
- type OptimizationStrategy
- type Orders
- type Paths
- type ShortestPathTree
- type StronglyConnectedComponents
- type Topological
- type TraversalStrategy
- type Undirected
- func (g *Undirected) AddEdge(v, w int)
- func (g *Undirected) Adj(v int) []int
- func (g *Undirected) ConnectedComponents() *ConnectedComponents
- func (g *Undirected) DOT() string
- func (g *Undirected) Degree(v int) int
- func (g *Undirected) E() int
- func (g *Undirected) Orders(strategy TraversalStrategy) *Orders
- func (g *Undirected) Paths(s int, strategy TraversalStrategy) *Paths
- func (g *Undirected) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
- func (g *Undirected) V() int
- type UndirectedEdge
- type Visitors
- type WeightedDirected
- func (g *WeightedDirected) AddEdge(e DirectedEdge)
- func (g *WeightedDirected) Adj(v int) []DirectedEdge
- func (g *WeightedDirected) DOT() string
- func (g *WeightedDirected) E() int
- func (g *WeightedDirected) Edges() []DirectedEdge
- func (g *WeightedDirected) InDegree(v int) int
- func (g *WeightedDirected) Orders(strategy TraversalStrategy) *Orders
- func (g *WeightedDirected) OutDegree(v int) int
- func (g *WeightedDirected) Paths(s int, strategy TraversalStrategy) *Paths
- func (g *WeightedDirected) Reverse() *WeightedDirected
- func (g *WeightedDirected) ShortestPathTree(s int) *ShortestPathTree
- func (g *WeightedDirected) StronglyConnectedComponents() *StronglyConnectedComponents
- func (g *WeightedDirected) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
- func (g *WeightedDirected) V() int
- type WeightedUndirected
- func (g *WeightedUndirected) AddEdge(e UndirectedEdge)
- func (g *WeightedUndirected) Adj(v int) []UndirectedEdge
- func (g *WeightedUndirected) ConnectedComponents() *ConnectedComponents
- func (g *WeightedUndirected) DOT() string
- func (g *WeightedUndirected) Degree(v int) int
- func (g *WeightedUndirected) E() int
- func (g *WeightedUndirected) Edges() []UndirectedEdge
- func (g *WeightedUndirected) MinimumSpanningTree() *MinimumSpanningTree
- func (g *WeightedUndirected) Orders(strategy TraversalStrategy) *Orders
- func (g *WeightedUndirected) Paths(s int, strategy TraversalStrategy) *Paths
- func (g *WeightedUndirected) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
- func (g *WeightedUndirected) V() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnectedComponents ¶
type ConnectedComponents struct {
// contains filtered or unexported fields
}
ConnectedComponents is used for determining all the connected components in a an undirected graph. A connected component is a maximal set of connected vertices (every two vertices are connected with a path between them).
func (*ConnectedComponents) Components ¶
func (c *ConnectedComponents) Components() [][]int
Components returns the vertices partitioned in the connected components.
func (*ConnectedComponents) ID ¶
func (c *ConnectedComponents) ID(v int) int
ID returns the component id of a vertex.
func (*ConnectedComponents) IsConnected ¶
func (c *ConnectedComponents) IsConnected(v, w int) bool
IsConnected determines if two vertices v and w are connected.
type Directed ¶
type Directed struct {
// contains filtered or unexported fields
}
Directed represents a directed graph data type.
func NewDirected ¶
NewDirected creates a new directed graph.
func (*Directed) DirectedCycle ¶
func (g *Directed) DirectedCycle() *DirectedCycle
DirectedCycle determines if the graph has a cyclic path.
func (*Directed) Orders ¶
func (g *Directed) Orders(strategy TraversalStrategy) *Orders
Orders determines ordering of vertices in the graph.
func (*Directed) Paths ¶
func (g *Directed) Paths(s int, strategy TraversalStrategy) *Paths
Paths finds all paths from a source vertex to every other vertex.
func (*Directed) StronglyConnectedComponents ¶
func (g *Directed) StronglyConnectedComponents() *StronglyConnectedComponents
StronglyConnectedComponents determines all the strongly-connected components in the graph.
func (*Directed) Topological ¶
func (g *Directed) Topological() *Topological
Topological determines the topological sort of the graph. If the graph has a directed cycle (not DAG), it does not have a topological order.
type DirectedCycle ¶
type DirectedCycle struct {
// contains filtered or unexported fields
}
DirectedCycle is used for determining if a directed graph has a cycle. A cycle is a path whose first and last vertices are the same.
func (*DirectedCycle) Cycle ¶
func (c *DirectedCycle) Cycle() ([]int, bool)
Cycle returns a cyclic path. If no cycle exists, the second return value will be false.
type DirectedEdge ¶
type DirectedEdge struct {
// contains filtered or unexported fields
}
DirectedEdge represents a weighted directed edge data type.
func (DirectedEdge) From ¶
func (e DirectedEdge) From() int
From returns the vertex this edge points from.
func (DirectedEdge) Weight ¶
func (e DirectedEdge) Weight() float64
Weight returns the weight of this edge.
type FlowEdge ¶
type FlowEdge struct {
// contains filtered or unexported fields
}
FlowEdge represents a capacitated edge data type.
func (*FlowEdge) AddResidualFlowTo ¶
AddResidualFlowTo changes the flow on the edge in the direction to the given vertex. If vertex is the tail vertex (backward edge), this decreases the flow on the edge by delta. If vertex is the head vertex (forward edge), this increases the flow on the edge by delta. If delta is valid, edge will be modified and true will be returned. If delta is not valid, edge will not be modified and false will be returned.
func (*FlowEdge) ResidualCapacityTo ¶
ResidualCapacityTo returns the residual capacity of the edge in the direction to the given vertex.
type FlowNetwork ¶
type FlowNetwork struct {
// contains filtered or unexported fields
}
FlowNetwork represents a capacitated network graph data type. Each directed edge has a real number capacity and flow.
func NewFlowNetwork ¶
func NewFlowNetwork(V int, edges ...FlowEdge) *FlowNetwork
NewFlowNetwork creates a new capacitated network graph.
func (*FlowNetwork) AddEdge ¶
func (g *FlowNetwork) AddEdge(e FlowEdge)
AddEdge adds a new edge to the network.
func (*FlowNetwork) Adj ¶
func (g *FlowNetwork) Adj(v int) []FlowEdge
Adj returns the edges both pointing to and from a vertex.
func (*FlowNetwork) DOT ¶ added in v0.10.0
func (g *FlowNetwork) DOT() string
DOT generates a DOT representation of the graph.
func (*FlowNetwork) Edges ¶
func (g *FlowNetwork) Edges() []FlowEdge
Edges returns all directed edges in the graph.
type MinimumSpanningTree ¶
type MinimumSpanningTree struct {
// contains filtered or unexported fields
}
MinimumSpanningTree is used for calculating the minimum spanning trees (forest) of a weighted undirected graph. Given an edge-weighted undirected graph G with positive edge weights, an MST of G is a sub-graph T that is:
Tree: connected and acyclic Spanning: includes all of the vertices Minimum: sum of the edge wights are minimum
func (*MinimumSpanningTree) Edges ¶
func (mst *MinimumSpanningTree) Edges() []UndirectedEdge
Edges returns the edges in a minimum spanning tree (or forest).
func (*MinimumSpanningTree) Weight ¶
func (mst *MinimumSpanningTree) Weight() float64
Weight returns the sum of the edge weights in a minimum spanning tree (or forest).
type OptimizationStrategy ¶
type OptimizationStrategy int
OptimizationStrategy is the strategy for optimizing weighted graphs.
const ( // None ignores edge weights. None OptimizationStrategy = iota // Minimize picks the edges with minimum weight. Minimize // Maximize picks the edges with maximum weight. Maximize )
type Orders ¶
type Orders struct {
// contains filtered or unexported fields
}
Orders is used for determining ordering of vertices in a graph.
func (*Orders) ReversePostOrder ¶
ReversePostOrder returns the reverse post ordering of vertices.
type Paths ¶
type Paths struct {
// contains filtered or unexported fields
}
Paths is used for finding all paths from a source vertex to every other vertex in a graph. A path is a sequence of vertices connected by edges.
type ShortestPathTree ¶
type ShortestPathTree struct {
// contains filtered or unexported fields
}
ShortestPathTree is used for calculating the shortest path tree of a weighted directed graph. A shortest path from vertex s to vertex t in a weighted directed graph is a directed path from s to t such that no other path has a lower weight.
func (*ShortestPathTree) PathTo ¶
func (spt *ShortestPathTree) PathTo(v int) ([]DirectedEdge, float64, bool)
PathTo returns shortest path from the source vertex (s) to vertex (v). The second return value is distance from the source vertex (s) to vertex (v). If no such path exists, the last return value will be false.
type StronglyConnectedComponents ¶
type StronglyConnectedComponents struct {
// contains filtered or unexported fields
}
StronglyConnectedComponents is used for determining all the strongly connected components in a directed graph. A strongly connected component is a maximal set of strongly connected vertices (every two vertices are strongly connected with paths in both directions between them).
func (*StronglyConnectedComponents) Components ¶
func (c *StronglyConnectedComponents) Components() [][]int
Components returns the vertices partitioned in the connected components.
func (*StronglyConnectedComponents) ID ¶
func (c *StronglyConnectedComponents) ID(v int) int
ID returns the component id of a vertex.
func (*StronglyConnectedComponents) IsStronglyConnected ¶
func (c *StronglyConnectedComponents) IsStronglyConnected(v, w int) bool
IsStronglyConnected determines if two vertices v and w are strongly connected.
type Topological ¶
type Topological struct {
// contains filtered or unexported fields
}
Topological is used for determining the topological order of a directed acyclic graph (DAG). A directed graph has a topological order if and only if it is a DAG (no directed cycle exists).
func (*Topological) Order ¶
func (t *Topological) Order() ([]int, bool)
Order returns a topological order of the directed graph. If the directed graph does not have a topologial order (has a cycle), the second return value will be false.
type TraversalStrategy ¶
type TraversalStrategy int
TraversalStrategy is the strategy for traversing vertices in a graph.
const ( // DFS is recursive depth-first search traversal. DFS TraversalStrategy = iota // DFSi is iterative depth-first search traversal. DFSi // BFS is breadth-first search traversal. BFS )
type Undirected ¶
type Undirected struct {
// contains filtered or unexported fields
}
Undirected represents an undirected graph data type.
func NewUndirected ¶
func NewUndirected(V int, edges ...[2]int) *Undirected
NewUndirected creates a new undirected graph.
func (*Undirected) AddEdge ¶
func (g *Undirected) AddEdge(v, w int)
AddEdge adds a new edge to the graph.
func (*Undirected) Adj ¶
func (g *Undirected) Adj(v int) []int
Adj returns the vertices adjacent from vertex.
func (*Undirected) ConnectedComponents ¶
func (g *Undirected) ConnectedComponents() *ConnectedComponents
ConnectedComponents determines all the connected components in the graph.
func (*Undirected) DOT ¶ added in v0.10.0
func (g *Undirected) DOT() string
DOT generates a DOT representation of the graph.
func (*Undirected) Degree ¶
func (g *Undirected) Degree(v int) int
Degree returns the degree of a vertex.
func (*Undirected) Orders ¶
func (g *Undirected) Orders(strategy TraversalStrategy) *Orders
Orders determines ordering of vertices in the graph.
func (*Undirected) Paths ¶
func (g *Undirected) Paths(s int, strategy TraversalStrategy) *Paths
Paths finds all paths from a source vertex to every other vertex.
func (*Undirected) Traverse ¶
func (g *Undirected) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
Traverse is used for visiting all vertices and edges in graph.
type UndirectedEdge ¶
type UndirectedEdge struct {
// contains filtered or unexported fields
}
UndirectedEdge represents a weighted undirected edge data type.
func (UndirectedEdge) Either ¶
func (e UndirectedEdge) Either() int
Either returns either of this edge's vertices.
func (UndirectedEdge) Other ¶
func (e UndirectedEdge) Other(v int) int
Other returns the other vertex of this edge.
func (UndirectedEdge) Weight ¶
func (e UndirectedEdge) Weight() float64
Weight returns the weight of this edge.
type Visitors ¶
type Visitors struct {
VertexPreOrder func(int) bool
VertexPostOrder func(int) bool
EdgePreOrder func(int, int, float64) bool
}
Visitors provides a method for visiting vertices and edges when traversing a graph. VertexPreOrder is called when visiting a vertex in a graph. VertexPostOrder is called when visiting a vertex in a graph. EdgePreOrder is called when visiting an edge in a graph. The graph traversal will immediately stop if the return value from any of these functions is false.
type WeightedDirected ¶
type WeightedDirected struct {
// contains filtered or unexported fields
}
WeightedDirected represents a weighted directed graph data type.
func NewWeightedDirected ¶
func NewWeightedDirected(V int, edges ...DirectedEdge) *WeightedDirected
NewWeightedDirected creates a new weighted directed graph.
func (*WeightedDirected) AddEdge ¶
func (g *WeightedDirected) AddEdge(e DirectedEdge)
AddEdge adds a new edge to the graph.
func (*WeightedDirected) Adj ¶
func (g *WeightedDirected) Adj(v int) []DirectedEdge
Adj returns the vertices adjacent from vertex.
func (*WeightedDirected) DOT ¶ added in v0.10.0
func (g *WeightedDirected) DOT() string
DOT generates a DOT representation of the graph.
func (*WeightedDirected) Edges ¶
func (g *WeightedDirected) Edges() []DirectedEdge
Edges returns all directed edges in the graph.
func (*WeightedDirected) InDegree ¶
func (g *WeightedDirected) InDegree(v int) int
InDegree returns the number of directed edges incident to a vertex.
func (*WeightedDirected) Orders ¶
func (g *WeightedDirected) Orders(strategy TraversalStrategy) *Orders
Orders determines ordering of vertices in the graph.
func (*WeightedDirected) OutDegree ¶
func (g *WeightedDirected) OutDegree(v int) int
OutDegree returns the number of directed edges incident from a vertex.
func (*WeightedDirected) Paths ¶
func (g *WeightedDirected) Paths(s int, strategy TraversalStrategy) *Paths
Paths finds all paths from a source vertex to every other vertex.
func (*WeightedDirected) Reverse ¶
func (g *WeightedDirected) Reverse() *WeightedDirected
Reverse returns the reverse of the directed graph.
func (*WeightedDirected) ShortestPathTree ¶
func (g *WeightedDirected) ShortestPathTree(s int) *ShortestPathTree
ShortestPathTree calculates the shortest path tree of the graph.
func (*WeightedDirected) StronglyConnectedComponents ¶
func (g *WeightedDirected) StronglyConnectedComponents() *StronglyConnectedComponents
StronglyConnectedComponents determines all the connected components in the graph.
func (*WeightedDirected) Traverse ¶
func (g *WeightedDirected) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
Traverse is used for visiting all vertices and edges in graph.
type WeightedUndirected ¶
type WeightedUndirected struct {
// contains filtered or unexported fields
}
WeightedUndirected represents a weighted undirected graph data type.
func NewWeightedUndirected ¶
func NewWeightedUndirected(V int, edges ...UndirectedEdge) *WeightedUndirected
NewWeightedUndirected creates a new weighted undirected graph.
func (*WeightedUndirected) AddEdge ¶
func (g *WeightedUndirected) AddEdge(e UndirectedEdge)
AddEdge adds a new edge to the graph.
func (*WeightedUndirected) Adj ¶
func (g *WeightedUndirected) Adj(v int) []UndirectedEdge
Adj returns the vertices adjacent from vertex.
func (*WeightedUndirected) ConnectedComponents ¶
func (g *WeightedUndirected) ConnectedComponents() *ConnectedComponents
ConnectedComponents determines all the connected components in the graph.
func (*WeightedUndirected) DOT ¶ added in v0.10.0
func (g *WeightedUndirected) DOT() string
DOT generates a DOT representation of the graph.
func (*WeightedUndirected) Degree ¶
func (g *WeightedUndirected) Degree(v int) int
Degree returns the degree of a vertex.
func (*WeightedUndirected) Edges ¶
func (g *WeightedUndirected) Edges() []UndirectedEdge
Edges returns all edges in the graph.
func (*WeightedUndirected) MinimumSpanningTree ¶
func (g *WeightedUndirected) MinimumSpanningTree() *MinimumSpanningTree
MinimumSpanningTree calculates the minimum spanning tree (or forest) of the graph.
func (*WeightedUndirected) Orders ¶
func (g *WeightedUndirected) Orders(strategy TraversalStrategy) *Orders
Orders determines ordering of vertices in the graph.
func (*WeightedUndirected) Paths ¶
func (g *WeightedUndirected) Paths(s int, strategy TraversalStrategy) *Paths
Paths finds all paths from a source vertex to every other vertex.
func (*WeightedUndirected) Traverse ¶
func (g *WeightedUndirected) Traverse(s int, strategy TraversalStrategy, visitors *Visitors)
Traverse is used for visiting all vertices and edges in graph.
func (*WeightedUndirected) V ¶
func (g *WeightedUndirected) V() int
V returns the number of vertices.




