-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_valid_tree.go
45 lines (43 loc) · 1.05 KB
/
graph_valid_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package graph
import "container/list"
func GraphValidTree(n int, edges [][]int) bool {
// Base case
if len(edges) == 0 {
return n == 1
}
// Adjacency list
adjacencyList := make([][]int, n)
for _, edge := range edges {
adjacencyList[edge[0]] = append(adjacencyList[edge[0]], edge[1])
adjacencyList[edge[1]] = append(adjacencyList[edge[1]], edge[0])
}
// Array to keep track of visited nodes
visited := make([]bool, n)
// Queue for BFS traversal
nodes := list.New()
nodes.PushBack(edges[0][0])
for nodes.Len() > 0 {
node := nodes.Remove(nodes.Front()).(int)
if visited[node] {
return false
}
visited[node] = true
for _, neighbor := range adjacencyList[node] {
nodes.PushBack(neighbor)
index := 0
for i := 0; i < len(adjacencyList[neighbor]); i++ {
if adjacencyList[neighbor][i] == node {
index = i
break
}
}
adjacencyList[neighbor] = append(adjacencyList[neighbor][:index], adjacencyList[neighbor][:index]...)
}
}
for _, element := range visited {
if !element {
return false
}
}
return true
}