A Directed Acyclic Graph (DAG) is a finite directed graph with no directed cycles. DAGs are used in various applications such as schedule dependency, topological sorting, data flow analysis, etc. A DAG has the following properties:
- Directed: Each edge in a DAG has a direction, which means that the edge has a starting vertex (source) and an ending vertex (destination).
- Acyclic: There are no cycles in a DAG. A cycle is a path in a graph where we can start from a vertex and visit the same vertex again by following the edges’ direction.
To implement a DAG in Go, we can use a slice of slices to represent the graph's adjacency list. Each inner slice represents the neighbors of a vertex. Here's an example:
type Graph struct {
vertices int
edges [][]int
}
func NewGraph(vertices int) *Graph {
return &Graph{
vertices: vertices,
edges: make([][]int, vertices),
}
}
func (g *Graph) AddEdge(u, v int) {
g.edges[u] = append(g.edges[u], v)
}
In this example, we define a Graph struct with two fields, vertices and edges. The vertices field represents the number of vertices in the graph, and the edges field represents the adjacency list. The NewGraph function initializes a new Graph object with the specified number of vertices. The AddEdge function adds a directed edge between vertices u and v.
To check for cycles in a DAG, we can perform a topological sort. A topological sort is an algorithm that arranges the vertices of a directed acyclic graph in a linear order such that for every directed edge u -> v, vertex u comes before vertex v in the ordering. If a topological sort is possible, the graph has no cycles.
Here's an example implementation of a topological sort and a cycle detection algorithm in Go:
func (g *Graph) TopologicalSort() ([]int, bool) {
indegrees := make([]int, g.vertices)
queue := make([]int, 0, g.vertices)
// Calculate the in-degree of each vertex
for _, edges := range g.edges {
for _, v := range edges {
indegrees[v]++
}
}
// Enqueue vertices with in-degree 0
for i, deg := range indegrees {
if deg == 0 {
queue = append(queue, i)
}
}
// Perform the topological sort
result := make([]int, 0, g.vertices)
for len(queue) > 0 {
u := queue[0]
queue = queue[1:]
result = append(result, u)
// Decrease the in-degree of the neighbors of u
for _, v := range g.edges[u] {
indegrees[v]--
if indegrees[v] == 0 {
queue = append(queue, v)
}
}
}
// Check for cycles
if len(result) != g.vertices {
return nil, false
}
return result, true
}
In this implementation, we first calculate the in-degree of each vertex by iterating over the graph's edges. A vertex's in-degree is the number of edges pointing to it. We then enqueue all vertices with an in-degree of 0 into a queue. We then iterate over the queue and perform the topological sort. For each vertex u, we decrease the in-degree of its neighbors and enqueue them if their in-degree becomes 0.
Finally, we check if the length of the topologically sorted vertices is equal to the number of vertices in the graph. If it is, the graph is acyclic, and we return the topologically sorted vertices. Otherwise, the graph contains a cycle, and we return nil and false.
Here's an example usage of the Graph struct and the TopologicalSort function:
g := NewGraph(6)
g.AddEdge(0, 1)
g.AddEdge(0