-
Notifications
You must be signed in to change notification settings - Fork 2
/
bfs.go
47 lines (42 loc) · 1.17 KB
/
bfs.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
46
47
package graph
import (
"math"
"github.com/sbromberger/bitvec"
"github.com/shawnsmithdev/zermelo/zuint32"
)
const unvisited = math.MaxUint32
// BFS computes a vector of levels from src and returns a vector
// of vertices visited in order along with a vector of distances
// indexed by vertex.
func BFS(g Graph, src uint32) (vertexList, vertLevel []uint32) {
nv := g.NumVertices()
vertLevel = make([]uint32, nv)
for i := uint32(0); i < nv; i++ {
vertLevel[i] = unvisited
}
visited := bitvec.NewBitVec(nv)
curLevel := make([]uint32, 0, nv)
nextLevel := make([]uint32, 0, nv)
nLevel := uint32(1)
vertLevel[src] = 0
visited.TrySet(src)
curLevel = append(curLevel, src)
vertexList = make([]uint32, 0, nv)
vertexList = append(vertexList, src)
for len(curLevel) > 0 {
for _, v := range curLevel {
for _, neighbor := range g.OutNeighbors(v) {
if visited.TrySet(neighbor) {
nextLevel = append(nextLevel, neighbor)
vertLevel[neighbor] = nLevel
vertexList = append(vertexList, neighbor)
}
}
}
nLevel++
curLevel = curLevel[:0]
curLevel, nextLevel = nextLevel, curLevel
zuint32.SortBYOB(curLevel, nextLevel[:nv])
}
return vertexList, vertLevel
}