|
| 1 | +// TopologicalSort.go |
| 2 | +// |
| 3 | +// Topological Sort (Kahn’s Algorithm) |
| 4 | +// |
| 5 | +// Description: |
| 6 | +// This program implements Topological Sorting using Kahn’s Algorithm. |
| 7 | +// It finds a valid linear ordering of vertices in a Directed Acyclic Graph (DAG) |
| 8 | +// such that for every directed edge u → v, vertex u comes before v in the ordering. |
| 9 | +// |
| 10 | +// Purpose / Use cases: |
| 11 | +// - Used for dependency resolution (e.g., build systems, course scheduling). |
| 12 | +// - Works only on Directed Acyclic Graphs (DAG). |
| 13 | +// |
| 14 | +// Approach / Methodology: |
| 15 | +// - Compute indegree for every vertex. |
| 16 | +// - Use a queue to repeatedly remove nodes with indegree 0 and |
| 17 | +// reduce indegree of their neighbors. |
| 18 | +// - If all nodes are processed, the graph is acyclic. |
| 19 | +// - If not, it contains a cycle. |
| 20 | +// |
| 21 | +// Complexity Analysis: |
| 22 | +// - Time: O(V + E) |
| 23 | +// - Space: O(V + E) |
| 24 | +// |
| 25 | +// File contents: |
| 26 | +// - Graph struct and methods. |
| 27 | +// - TopologicalSort() function implementing Kahn’s Algorithm. |
| 28 | +// - Example test in main() for validation. |
| 29 | + |
| 30 | +package main |
| 31 | + |
| 32 | +import ( |
| 33 | + "fmt" |
| 34 | +) |
| 35 | + |
| 36 | +// Graph represents a directed graph using adjacency list. |
| 37 | +type Graph struct { |
| 38 | + adj map[int][]int |
| 39 | +} |
| 40 | + |
| 41 | +// NewGraph creates an empty directed graph. |
| 42 | +func NewGraph() *Graph { |
| 43 | + return &Graph{ |
| 44 | + adj: make(map[int][]int), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// AddEdge adds a directed edge from u → v. |
| 49 | +func (g *Graph) AddEdge(u, v int) { |
| 50 | + g.adj[u] = append(g.adj[u], v) |
| 51 | + if _, exists := g.adj[v]; !exists { |
| 52 | + g.adj[v] = []int{} |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// TopologicalSort performs Kahn’s Algorithm to return a valid ordering. |
| 57 | +// If the graph contains a cycle, returns an empty slice. |
| 58 | +func (g *Graph) TopologicalSort() []int { |
| 59 | + indegree := make(map[int]int) |
| 60 | + for u := range g.adj { |
| 61 | + indegree[u] = 0 |
| 62 | + } |
| 63 | + for _, neighbors := range g.adj { |
| 64 | + for _, v := range neighbors { |
| 65 | + indegree[v]++ |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + queue := []int{} |
| 70 | + for node, deg := range indegree { |
| 71 | + if deg == 0 { |
| 72 | + queue = append(queue, node) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + order := []int{} |
| 77 | + for len(queue) > 0 { |
| 78 | + u := queue[0] |
| 79 | + queue = queue[1:] |
| 80 | + order = append(order, u) |
| 81 | + for _, v := range g.adj[u] { |
| 82 | + indegree[v]-- |
| 83 | + if indegree[v] == 0 { |
| 84 | + queue = append(queue, v) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if len(order) != len(g.adj) { |
| 90 | + fmt.Println("Cycle detected — topological sort not possible.") |
| 91 | + return []int{} |
| 92 | + } |
| 93 | + return order |
| 94 | +} |
| 95 | + |
| 96 | +// main runs an example DAG to demonstrate Topological Sort. |
| 97 | +func main() { |
| 98 | + fmt.Println("Topological Sort (Kahn’s Algorithm) Demo") |
| 99 | + |
| 100 | + g := NewGraph() |
| 101 | + g.AddEdge(5, 0) |
| 102 | + g.AddEdge(5, 2) |
| 103 | + g.AddEdge(4, 0) |
| 104 | + g.AddEdge(4, 1) |
| 105 | + g.AddEdge(2, 3) |
| 106 | + g.AddEdge(3, 1) |
| 107 | + |
| 108 | + order := g.TopologicalSort() |
| 109 | + fmt.Printf("Topological Order: %v\n", order) |
| 110 | +} |
0 commit comments