-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_components.go
40 lines (33 loc) · 913 Bytes
/
find_components.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
package main
func findComponents(adjMatrix [][]uint16) []map[int]void {
components := make([]map[int]void, 0)
visited := make(map[int]bool)
for node := 0; node < len(adjMatrix[0]); node++ {
if visited[node] {
continue
}
component := explore(node, adjMatrix, visited)
components = append(components, component)
//union visited and component
for n := range component {
visited[n] = true
}
}
return components
}
func explore(node int, adjMatrix [][]uint16, visited map[int]bool) map[int]void {
var exists void
component := make(map[int]void, 0)
component[node] = exists
visited[node] = true
for neighbor := 0; neighbor < len(adjMatrix[node]); neighbor++ {
if adjMatrix[node][neighbor] != 0 && !visited[neighbor] {
newNodes := explore(neighbor, adjMatrix, visited)
//union
for newNode := range newNodes {
component[newNode] = exists
}
}
}
return component
}