A Directed Acyclic Graph (DAG) is a graph that is directed and contains no cycles. This means that it is impossible to start at any node and follow a sequence of edges that eventually loops back to the starting node. DAGs are used in various applications such as scheduling, data processing, and representing structures with dependencies.
To implement a DAG in Go (Golang), you can use a map to represent the graph, where the keys are nodes, and the values are slices of nodes representing the edges pointing from the key node to other nodes. To check for cycles, you can perform a Depth-First Search (DFS) with an additional set to keep track of the visited nodes.
Here's an example implementation of a DAG in Go, including a cycle detection function:
package main
import (
"fmt"
)
// Node represents a node in the graph
type Node struct {
Value string
}
// DAG represents a Directed Acyclic Graph
type DAG struct {
// Edges represent the adjacency list of the graph where each node points to a list of nodes
Edges map[*Node][]*Node
}
// NewDAG creates a new DAG instance
func NewDAG() *DAG {
return &DAG{
Edges: make(map[*Node][]*Node),
}
}
// AddEdge adds an edge from the parent node to the child node
func (g *DAG) AddEdge(parent, child *Node) {
g.Edges[parent] = append(g.Edges[parent], child)
}
// hasCycleUtil is a utility function for cycle detection
func (g *DAG) hasCycleUtil(current *Node, visited map[*Node]bool, recStack map[*Node]bool) bool {
visited[current] = true
recStack[current] = true
for _, child := range g.Edges[current] {
if !visited[child] {
if g.hasCycleUtil(child, visited, recStack) {
return true
}
} else if recStack[child] {
// Cycle detected
return true
}
}
recStack[current] = false
return false
}
// HasCycle checks if the graph contains a cycle
func (g *DAG) HasCycle() bool {
visited := make(map[*Node]bool)
recStack := make(map[*Node]bool)
for node := range g.Edges {
if !visited[node] {
if g.hasCycleUtil(node, visited, recStack) {
return true
}
}
}
return false
}
func main() {
// Create a new DAG
graph := NewDAG()
// Create nodes
nodeA := &Node{"A"}
nodeB := &Node{"B"}
nodeC := &Node{"C"}
// Add edges
graph.AddEdge(nodeA, nodeB)
graph.AddEdge(nodeB, nodeC)
// Uncomment the following line to create a cycle and test cycle detection
// graph.AddEdge(nodeC, nodeA)
// Check if the graph has a cycle
if graph.HasCycle() {
fmt.Println("The graph has a cycle")
} else {
fmt.Println("The graph does not have a cycle")
}
}
In the above code:
- We define a
Node struct to represent a node in the graph.
- The
DAG struct contains a map called Edges that represents the graph's adjacency list.
- The
AddEdge method adds an edge from a parent node to a child node.
- The
HasCycle method checks for cycles in the graph by calling the hasCycleUtil utility function on each unvisited node. This function uses a depth-first search approach with two maps, visited and recStack, to keep track of visited nodes and nodes in the current recursive stack, respectively.
- In the
main function, we create a simple DAG and add some edges. By uncommenting the line graph.AddEdge(nodeC, nodeA), a cycle is introduced, and the cycle detection can be tested.
Please note that this is a simple implementation for illustrative purposes. In a production environment, you might need to implement additional functionality such as error handling, concurrency safety, and more sophisticated graph algorithms.