-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.go
More file actions
181 lines (144 loc) · 3.98 KB
/
bfs.go
File metadata and controls
181 lines (144 loc) · 3.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
###############################################################################
# #
# Breadth first search and all algorithms that use bfs #
# This works only on graph which vertex's ID is 0 <= VID < |Vertices| #
# #
# BFS is a truly interesting algorithm since it allows to do things like: #
# - Do a graph traversal #
# - Check for cycles #
# - Check for connectivy between nodes #
# - know the shortest path (in terms of vertex distance) between one vertex #
# and the rest #
###############################################################################
*/
/* Package */
package graphosalgorithms
/* Imports */
import (
"github.com/julinox/go_data_structures/queue"
"github.com/julinox/go_data_structures/graphos"
)
/* Glocals */
/* Types */
type BfsReturn struct {
Path *[]int
Distance *[]int
Order *[]interface{}
}
/* Interface */
/* Functions */
func Bfs(graph graphos.Grapho, s, e int) (*BfsReturn) {
/*
s: Start vertex
e: End vertex (optional)
Path: Track the vertex where you came from
Distance: Track the number of jumps (or layers) needed to reach
the 'current' vertex
qq: Queue of neighbours (bfs traversal)
If 'e' < 0 means not to stop at 'e' but to keep doing the traversal
*/
var forceStop bool
var ret BfsReturn
var vxtLen int
var path []int
var distance []int
var visited []bool
var qq *queue.Queue
if (graph == nil || s < 0) {
return nil
}
// Init
forceStop = false
vxtLen = graph.VertexMax()
qq = queue.InitQueue(uint(vxtLen + 1))
path = make([]int, vxtLen + 1)
distance = make([]int, vxtLen + 1)
visited = make([]bool, vxtLen + 1)
for i := range path {
path[i] = -1
distance[i] = -1
}
// OPS
qq.Enqueue(s)
visited[s] = true
distance[s] = 0
for !qq.IsEmpty() && !forceStop {
current, ta_ok := qq.Dequeue().(int)
if (!ta_ok) { continue }
neighbours := graph.VertexNeighbours(current)
for _, next := range *neighbours {
if !(visited[next]) {
qq.Enqueue(next)
visited[next] = true
path[next] = current
distance[next] = distance[current] + 1
if (next == e) {
forceStop = true
}
}
}
}
ret.Path = &path
ret.Distance = &distance
ret.Order = qq.GetQueue()
return &ret
}
func BfsShortestPathVector(graph graphos.Grapho, s int) (*[]int) {
/*
Get the shortest path vector between 's' and the rest
*/
var bfsRet *BfsReturn
if (graph == nil || s < 0) {
return &[]int{}
}
bfsRet = Bfs(graph, s, -1)
return bfsRet.Distance
}
func BfsShortestPath(graph graphos.Grapho, s, e int) (*[]int) {
/*
Get the shortest path between 's' and 'e'
*/
var bfsRet *BfsReturn
if (graph == nil || s < 0 || e < 0) {
return &[]int{}
}
bfsRet = Bfs(graph, s, e)
return ReconstructPath(bfsRet.Path, s, e)
}
func BfsShortestPathAll(graph graphos.Grapho, s int) (map[int]*[]int) {
/*
Returns the shortest path between 's' and the rest of vertices.
*/
var pr *[]int
var ret map[int]*[]int
if (graph == nil || s < 0) {
return nil
}
ret = make(map[int]*[]int)
for _, v := range *graph.VertexList() {
pr = BfsShortestPath(graph, s, v)
ret[v] = pr
}
return ret
}
func ReconstructPath(rPath *[]int, s, e int) (*[]int) {
var at int
var path []int
var reversed []int
if (rPath == nil || e < 0) {
return &[]int{}
}
at = e
for at >= 0 {
path = append(path, at)
at = (*rPath)[at]
}
for i := len(path) -1; i >= 0; i-- {
reversed = append(reversed, path[i])
}
if (reversed[0] != s) {
return &[]int{}
}
return &reversed
}