Documentation
¶
Index ¶
- type Graph
- func (g *Graph[K, T]) AddEdge(from, to K) error
- func (g *Graph[K, T]) AddVertex(id K, item T) error
- func (g *Graph[K, T]) ContainsVertex(id K) bool
- func (g *Graph[K, T]) GetVertex(id K) (T, error)
- func (g *Graph[K, T]) Order() int
- func (g *Graph[K, T]) Size() int
- func (g *Graph[K, T]) Sort() ([]T, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Graph ¶
type Graph[K comparable, T any] struct { // contains filtered or unexported fields }
Graph is a generic directed acyclic graph, generic over 'K' which is a comparable type to be used as the unique ID for each vertex and 'T' which is the data you wish to store in each vertex of the graph.
The ID must be unique within a Graph.
func New ¶
func New[K comparable, T any]() *Graph[K, T]
New creates and returns a new Graph.
It is generic over 'K' which is a comparable type to be used as the unique ID for each vertex, and 'T' which is the data you wish to store in each vertex of the graph.
So for a graph storing integers with a unique ID that is a string, the signature would be:
graph := dag.New[string, int]()
The ID must be unique within a Graph.
func WithCapacity ¶ added in v0.15.0
func WithCapacity[K comparable, T any](capacity int) *Graph[K, T]
WithCapacity creates and returns a new Graph with the given capacity.
This can be a useful performance improvement if the expected maximum number of elements the graph will hold is known ahead of time as it eliminates the need for reallocation.
func (*Graph[K, T]) AddEdge ¶
AddEdge creates a connection from the vertex with id 'from' and one with id 'to'.
For the canonical use of a DAG (dependency graph), a dependency relationship of task "two" depends on task "one" the signature would be:
AddEdge("one", "two")
func (*Graph[K, T]) AddVertex ¶
AddVertex adds an item to the graph as a vertex (or node) in the graph.
If the vertex already exists, an error will be returned.
graph := dag.New[string, int]() graph.AddVertex("one", 1)
The ID must uniquely identify a single vertex in the Graph.
func (*Graph[K, T]) ContainsVertex ¶
ContainsVertex reports whether a vertex with the given id is present in the graph.
func (*Graph[K, T]) GetVertex ¶
GetVertex returns the item stored in a vertex.
If the vertex does not exist, an error will be returned.