Documentation
¶
Index ¶
- Variables
- func WalkBFS[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
- func WalkDijkstra[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
- func WalkPostOrderDFS[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
- func WalkPreOrderDFS[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
- func WalkShortestPath[T comparable](g Graph[T], source T, dest T, walkFunc WalkFunc[T]) error
- func WalkTopoOrder[T comparable](g Graph[T], walkFunc WalkFunc[T]) error
- func WalkUnreachableVertices[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
- func WriteDot[T comparable](g Graph[T], w io.Writer) error
- type Collector
- type Color
- type Degree
- type DirectedGraph
- type DotAttributes
- type Edge
- type Graph
- type GraphKind
- type UndirectedGraph
- func (g *UndirectedGraph[T]) AddEdge(from, to T) *Edge[T]
- func (g *UndirectedGraph[T]) AddVertex(value T) *Vertex[T]
- func (g *UndirectedGraph[T]) AddWeightedEdge(from, to T, weight float64) *Edge[T]
- func (g *UndirectedGraph[T]) Clone() Graph[T]
- func (g *UndirectedGraph[T]) DeleteEdge(from, to T)
- func (g *UndirectedGraph[T]) DeleteVertex(v T)
- func (g *UndirectedGraph[T]) EdgeExists(from, to T) bool
- func (g *UndirectedGraph[T]) GetDotAttributes() DotAttributes
- func (g *UndirectedGraph[T]) GetEdge(from, to T) *Edge[T]
- func (g *UndirectedGraph[T]) GetEdges() []*Edge[T]
- func (g *UndirectedGraph[T]) GetNeighbourVertices(v T) []*Vertex[T]
- func (g *UndirectedGraph[T]) GetNeighbours(v T) []T
- func (g *UndirectedGraph[T]) GetVertex(value T) *Vertex[T]
- func (g *UndirectedGraph[T]) GetVertexValues() []T
- func (g *UndirectedGraph[T]) GetVertices() []*Vertex[T]
- func (g *UndirectedGraph[T]) Kind() GraphKind
- func (g *UndirectedGraph[T]) NewCollector() *Collector[T]
- func (g *UndirectedGraph[T]) ResetVertexAttributes()
- func (g *UndirectedGraph[T]) VertexExists(value T) bool
- type Vertex
- type WalkFunc
Constants ¶
This section is empty.
Variables ¶
var DotDefaultEdgeAttributes = DotAttributes{
"color": "black",
}
DotDefaultEdgeAttributes represents the map of default attributes to be applied for all edges when representing the graph in Dot format.
var DotDefaultNodeAttributes = DotAttributes{
"color": "lightblue",
"fillcolor": "lightblue",
"fontcolor": "black",
"shape": "record",
"style": "filled, rounded",
}
DefaultNodeAttributes represents the map of default attributes to be applied for al nodes when representing the graph in Dot format.
var ErrCycleDetected = errors.New("cycle detected")
ErrCycleDetected is returned whenever a cycle has been detected in the graph.
var ErrIsNotDirectedGraph = errors.New("graph is not directed")
ErrIsNotDirectedGraph is returned whenever an operation cannot be performed, because the graph is not directed.
var ErrStopWalking = errors.New("walking stopped")
ErrStopWalking is returned by WalkFunc to signal that further walking of the graph should be stopped.
Functions ¶
func WalkBFS ¶
func WalkBFS[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
WalkBFS performs Breadth-first Search (BFS) traversal of the graph, starting from the given source vertex.
func WalkDijkstra ¶
func WalkDijkstra[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
WalkDijkstra implements Dijkstra's algorithm for finding the shortest-path from a given source vertex to all other vertices in the graph.
This method will pass each visited vertex while traversing the shortest path from the source vertex to each other vertex in the graph.
Note, that this method only constructs the shortest-path tree and yields each visited vertex. In order to stop walking the graph callers of this method should return ErrStopWalking error and refer to the shortest-path tree, or use the WalkShortestPath method.
func WalkPostOrderDFS ¶
func WalkPostOrderDFS[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
WalkPostOrderDFS performs post-order Depth-first Search (DFS) traversal of the graph, starting from the given source vertex.
func WalkPreOrderDFS ¶
func WalkPreOrderDFS[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
WalkPreOrderDFS performs pre-order Depth-first Search (DFS) traversal of the graph, starting from the given source vertex.
func WalkShortestPath ¶
func WalkShortestPath[T comparable](g Graph[T], source T, dest T, walkFunc WalkFunc[T]) error
WalkShortestPath yields the vertices which represent the shortest path between SOURCE and DEST.
func WalkTopoOrder ¶
func WalkTopoOrder[T comparable](g Graph[T], walkFunc WalkFunc[T]) error
WalkTopoOrder performs a topological sort and walks over the vertices in topological order.
In case a cycle exists in the graph, WalkTopoOrder will return ErrCycleDetected.
In case ErrCycleDetected is returned, the vertices which remained Gray are forming a cyclic path in the graph.
func WalkUnreachableVertices ¶
func WalkUnreachableVertices[T comparable](g Graph[T], source T, walkFunc WalkFunc[T]) error
WalkUnreachableVertices walks over the vertices which are unreachable from the given source vertex
Types ¶
type Collector ¶
type Collector[T comparable] struct { // contains filtered or unexported fields }
Collector provides an easy way to collect vertices while walking a graph
func NewCollector ¶
func NewCollector[T comparable]() *Collector[T]
NewCollector creates a new collector
type Degree ¶
type Degree struct { // The number of incoming edges In int // The number of outgoing edges Out int }
Degree represents the number of incoming and outgoing edges of a vertex
type DirectedGraph ¶
type DirectedGraph[T comparable] struct { UndirectedGraph[T] }
DirectedGraph represents a directed graph
func (*DirectedGraph[T]) AddEdge ¶
func (g *DirectedGraph[T]) AddEdge(from, to T) *Edge[T]
AddEdge adds an edge between two vertices in the graph
func (*DirectedGraph[T]) DeleteEdge ¶
func (g *DirectedGraph[T]) DeleteEdge(from, to T)
DeleteEdge deletes the edge, which connects the `from` and `to` vertices
func (*DirectedGraph[T]) DeleteVertex ¶ added in v1.0.1
func (g *DirectedGraph[T]) DeleteVertex(v T)
DeleteVertex removes a vertex from the graph
func (*DirectedGraph[T]) EdgeExists ¶
func (g *DirectedGraph[T]) EdgeExists(from, to T) bool
EdgeExists returns a boolean indicating whether an edge between two vertices exists.
func (*DirectedGraph[T]) GetEdge ¶
func (g *DirectedGraph[T]) GetEdge(from, to T) *Edge[T]
GetEdge returns the edge connecting the two vertices
type DotAttributes ¶
DotAttributes contains the map of key/value pairs, which can be associated with vertices and edges.
type Edge ¶
type Edge[T comparable] struct { // From represents the source vertex of the edge From T // To represents the destination vertex of the edge To T // Weight represents the edge weight Weight float64 // DotAttributes represents the list of attributes associated // with the edge. The attributes will be used when // generating the Dot representation of the graph. DotAttributes DotAttributes }
Edge represents an edge connecting two vertices in the graph
func NewEdge ¶
func NewEdge[T comparable](from, to T) *Edge[T]
NewEdge creates an edge, which connects the given vertices
type Graph ¶
type Graph[T comparable] interface { // Kind returns the kind of the graph Kind() GraphKind // AddVertex adds a new vertex to the graph AddVertex(v T) *Vertex[T] // GetVertex returns the vertex associated with the given value GetVertex(v T) *Vertex[T] // DeleteVertex deletes the vertex associated with the given value DeleteVertex(v T) // VertexExists is a predicate for testing whether a vertex // associated with the value exists. VertexExists(v T) bool // GetVertices returns all vertices in the graph GetVertices() []*Vertex[T] // GetVertexValues returns the values associated with all // vertices from the graph GetVertexValues() []T // AddEdge creates a new edge connecting `from` and `to` // vertices AddEdge(from, to T) *Edge[T] // AddWeightedEdge creates a new edge with the given weight AddWeightedEdge(from, to T, weight float64) *Edge[T] // GetEdge returns the edge, which connects `from` and `to` // vertices GetEdge(from, to T) *Edge[T] // DeleteEdge deletes the edge which connects `from` and `to` // vertices DeleteEdge(from, to T) // EdgeExists is a predicate for testing whether an edge // between `from` and `to` exists EdgeExists(from, to T) bool // GetEdges returns all edges from the graph GetEdges() []*Edge[T] // GetNeighbours returns the neighbours of V as values GetNeighbours(v T) []T // GetNeighbourVertices returns the neighbours of V as // vertices GetNeighbourVertices(v T) []*Vertex[T] // ResetVertexAttributes resets the attributes for all // vertices in the graph ResetVertexAttributes() // NewCollector creates and returns a new collector NewCollector() *Collector[T] // Clone creates a new copy of the graph Clone() Graph[T] // GetDotAttribute returns the Dot attributes for the graph GetDotAttributes() DotAttributes }
Graph represents a graph which establishes relationships between various objects.
type UndirectedGraph ¶
type UndirectedGraph[T comparable] struct { // contains filtered or unexported fields }
UndirectedGraph represents an undirected graph
func (*UndirectedGraph[T]) AddEdge ¶
func (g *UndirectedGraph[T]) AddEdge(from, to T) *Edge[T]
AddEdge adds an edge between two vertices in the graph
func (*UndirectedGraph[T]) AddVertex ¶
func (g *UndirectedGraph[T]) AddVertex(value T) *Vertex[T]
AddVertex adds a vertex to the graph
func (*UndirectedGraph[T]) AddWeightedEdge ¶
func (g *UndirectedGraph[T]) AddWeightedEdge(from, to T, weight float64) *Edge[T]
AddWeightedEdge adds an edge between two vertices and sets weight for the edge
func (*UndirectedGraph[T]) Clone ¶
func (g *UndirectedGraph[T]) Clone() Graph[T]
Clone creates a new copy of the graph.
func (*UndirectedGraph[T]) DeleteEdge ¶
func (g *UndirectedGraph[T]) DeleteEdge(from, to T)
DeleteEdge deletes the edge, which connects the `from` and `to` vertices
func (*UndirectedGraph[T]) DeleteVertex ¶
func (g *UndirectedGraph[T]) DeleteVertex(v T)
DeleteVertex removes a vertex from the graph
func (*UndirectedGraph[T]) EdgeExists ¶
func (g *UndirectedGraph[T]) EdgeExists(from, to T) bool
EdgeExists returns a boolean indicating whether an edge between two vertices exists.
func (*UndirectedGraph[T]) GetDotAttributes ¶ added in v1.0.1
func (g *UndirectedGraph[T]) GetDotAttributes() DotAttributes
GetDotAttributes returns the list of attributes for the graph.
func (*UndirectedGraph[T]) GetEdge ¶
func (g *UndirectedGraph[T]) GetEdge(from, to T) *Edge[T]
GetEdge returns the edge connecting the two vertices
func (*UndirectedGraph[T]) GetEdges ¶
func (g *UndirectedGraph[T]) GetEdges() []*Edge[T]
GetEdges returns the set of edges in the graph
func (*UndirectedGraph[T]) GetNeighbourVertices ¶
func (g *UndirectedGraph[T]) GetNeighbourVertices(v T) []*Vertex[T]
GetNeighbourVertices returns the list of neighbour vertices of V
func (*UndirectedGraph[T]) GetNeighbours ¶
func (g *UndirectedGraph[T]) GetNeighbours(v T) []T
GetNeighbours returns the list of direct neighbours of V
func (*UndirectedGraph[T]) GetVertex ¶
func (g *UndirectedGraph[T]) GetVertex(value T) *Vertex[T]
GetVertex returns the vertex associated with the given value
func (*UndirectedGraph[T]) GetVertexValues ¶
func (g *UndirectedGraph[T]) GetVertexValues() []T
GetVertexValues returns the set of vertex values
func (*UndirectedGraph[T]) GetVertices ¶
func (g *UndirectedGraph[T]) GetVertices() []*Vertex[T]
GetVertices returns the set of vertices in the graph
func (*UndirectedGraph[T]) Kind ¶
func (g *UndirectedGraph[T]) Kind() GraphKind
Kind returns the kind of the graph
func (*UndirectedGraph[T]) NewCollector ¶
func (g *UndirectedGraph[T]) NewCollector() *Collector[T]
NewCollector creates a new collector
func (*UndirectedGraph[T]) ResetVertexAttributes ¶
func (g *UndirectedGraph[T]) ResetVertexAttributes()
ResetVertexAttributes resets the attributes for each vertex in the graph
func (*UndirectedGraph[T]) VertexExists ¶
func (g *UndirectedGraph[T]) VertexExists(value T) bool
VertexExists returns a boolean indicating whether a vertex with the given value exists
type Vertex ¶
type Vertex[T comparable] struct { // Value contains the value for the vertex Value T // Color represents the color the vertex is painted with Color Color // DistanceFromSource returns the distance of this vertex from // the source vertex. This field is calculated after // performing a DFS or BFS. DistanceFromSource float64 // Parent represents the parent vertex, which is calculated // during walking of the graph. The resulting relationships // established by this field construct the DFS-tree, BFS-tree // or shortest-path tree, depending on how we walked the // graph. Parent *Vertex[T] // DotAttributes represents the list of attributes associated // with the vertex. The attributes will be used when // generating the Dot representation of the graph. DotAttributes DotAttributes // Degree represents the degree of the vertex Degree Degree }
Vertex represents a vertex in the graph
func NewVertex ¶
func NewVertex[T comparable](value T) *Vertex[T]
NewVertex creates a new vertex with the given value
type WalkFunc ¶
type WalkFunc[T comparable] func(v *Vertex[T]) error
WalkFunc is a function which receives a vertex while traversing the graph