Documentation
¶
Overview ¶
Package dag provides a simple directed acyclic graph (DAG) implementation. It is a simple graph data structure that uses a map to store the vertices and edges. The package provides methods to add and remove vertices and edges, as well as methods to traverse the graph.
Index ¶
- type Edges
- type Graph
- func (g *Graph) Add(vertices ...Vertex) error
- func (g *Graph) Append(v Vertex, prevVertices []Vertex) error
- func (g *Graph) BFS(start Vertex) (result []Vertex, err error)
- func (g *Graph) Connect(from Vertex, to Vertex) error
- func (g *Graph) DFS(start Vertex) (result []Vertex, err error)
- func (g *Graph) DeepCopy() (*Graph, error)
- func (g *Graph) Deps(vertex string) (result []Vertex, err error)
- func (g *Graph) Disconnect(v Vertex) error
- func (g *Graph) DisconnectEdge(from Vertex, to Vertex) error
- func (g *Graph) Edges() Edges
- func (g *Graph) Exists(vertex Vertex) bool
- func (g *Graph) Leaves() (leaves []Vertex, err error)
- func (g *Graph) Next(vertex Vertex) ([]Vertex, error)
- func (g *Graph) Prev(vertex Vertex) (prev []Vertex, err error)
- func (g *Graph) Remove(v Vertex) (removed []Vertex, err error)
- func (g *Graph) Reverse() (*Graph, error)
- func (g *Graph) ReverseDeps(vertex Vertex) (result []Vertex, err error)
- func (g *Graph) ReverseEdges() (Edges, error)
- func (g *Graph) Roots() ([]Vertex, error)
- func (g *Graph) SubGraph(vertices []Vertex) (graph *Graph, err error)
- func (g *Graph) TopSort() (result []Vertex, err error)
- func (g *Graph) Vertices() []Vertex
- type GraphOptions
- type Vertex
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Edges ¶
keys represent vertex & values are direct edges to vertices Edges represents the edges of the graph
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph represents a directed asyclic graph
Example ¶
package main
import (
"fmt"
"github.com/aacanakin/dag"
)
func main() {
// Create a new directed acyclic graph
// A -> B -> C
// | |
// v v
// D -> E -> F
// Create an empty directed acyclic graph
graph, _ := dag.New()
// Add some vertices
graph.Add("A", "B", "C", "D", "E", "F")
// Add some edges
graph.Connect("A", "B")
graph.Connect("B", "C")
graph.Connect("A", "D")
graph.Connect("D", "E")
graph.Connect("B", "E")
graph.Connect("E", "F")
// Get the topological order
sorted, _ := graph.TopSort()
// Print the topological order
for _, vertex := range sorted {
fmt.Println(vertex)
}
}
Output: A B D C E F
func New ¶
func New(opts ...GraphOptions) (*Graph, error)
New creates an empty graph with no vertices & edges and returns it
func (*Graph) Append ¶
Append adds a new vertex to graph given vertex and previous vertices, returns error if any of the previous vertices is not present in graph
Example ¶
graph := createGraph()
var err error
// Append some vertices
err = graph.Append("G", []dag.Vertex{"F"})
if err != nil {
fmt.Println(err)
return
}
err = graph.Append("H", []dag.Vertex{"G", "F"})
if err != nil {
fmt.Println(err)
return
}
sorted, _ := graph.TopSort()
// Print the topological order
for _, vertex := range sorted {
fmt.Println(vertex)
}
Output: A B D C E F G H
func (*Graph) Connect ¶
Connect connects two vertices in the graph
returns error if;
the edge already exists or if the edge creates a cycle the from vertex is not found in the graph the to vertex is not found in the graph the from vertex is the same as the to vertex
it can be used to lazily initialize vertice connections
func (*Graph) Deps ¶
Deps returns the dependencies of a vertex given vertex, in topological order
Example ¶
package main
import (
"fmt"
"github.com/aacanakin/dag"
)
func main() {
// Create a new directed acyclic graph
// A -> B -> C
// | |
// v v
// D -> E -> F
// Create an empty directed acyclic graph
graph, _ := dag.New()
// Add some vertices
graph.Add("A", "B", "C", "D", "E", "F")
// Add some edges
graph.Connect("A", "B")
graph.Connect("B", "C")
graph.Connect("A", "D")
graph.Connect("D", "E")
graph.Connect("B", "E")
graph.Connect("E", "F")
// Get the dependencies of vertex E
deps, err := graph.Deps("E")
if err != nil {
fmt.Println(err)
return
}
for _, vertex := range deps {
fmt.Println(vertex)
}
}
Output: A B D
func (*Graph) Disconnect ¶
Disconnect disconnects all edges from and to a vertex returns error if the vertex is not found in the graph
Example ¶
package main
import (
"fmt"
"github.com/aacanakin/dag"
)
func main() {
// Create a new directed acyclic graph
// A -> B -> C
// | |
// v v
// D -> E -> F
// Create an empty directed acyclic graph
graph, _ := dag.New()
// Add some vertices
graph.Add("A", "B", "C", "D", "E", "F")
// Add some edges
graph.Connect("A", "B")
graph.Connect("B", "C")
graph.Connect("A", "D")
graph.Connect("D", "E")
graph.Connect("B", "E")
graph.Connect("E", "F")
fmt.Println("disconnecting E")
err := graph.Disconnect("E")
if err != nil {
fmt.Println(err)
return
}
prev, err := graph.Prev("E")
if err != nil {
fmt.Println(err)
return
}
for _, prevVertex := range prev {
fmt.Println(prevVertex)
}
next, err := graph.Next("E")
if err != nil {
fmt.Println(err)
return
}
for _, nextVertex := range next {
fmt.Println(nextVertex)
}
// here, E is disconnected from all vertices, so, it should not have any dependencies or dependants
func (*Graph) DisconnectEdge ¶
DisconnectEdge disconnects two vertices in the graph returns error if the edge does not exist
func (*Graph) Remove ¶
Remove removes node, all next nodes that are connected to that node & clears all edges that are related to node & deps
func (*Graph) ReverseDeps ¶
ReverseDeps returns the reverse dependencies of a vertex given vertex, in topological order
func (*Graph) ReverseEdges ¶
ReverseEdges returns the reverse edges of the graph example: {a: [b, c], b: [c], c: []} -> {a: [], b: [a], c: [a, b]}
func (*Graph) SubGraph ¶
SubGraph returns a subgraph of existing graph that includes input vertices, clips out vertices & non connected edges
type GraphOptions ¶
func WithVertices ¶
func WithVertices(vertices []Vertex) GraphOptions