@@ -168,6 +168,78 @@ fn bfs(graph: graph, key: node_id) -> bfs_result {
168
168
vec:: from_mut ( marks)
169
169
}
170
170
171
+ #[ doc="Another version of the bfs function.
172
+
173
+ This one uses the same algorithm as the parallel one, just without
174
+ using the parallel vector operators." ]
175
+ fn bfs2 ( graph : graph , key : node_id ) -> bfs_result {
176
+ // This works by doing functional updates of a color vector.
177
+
178
+ enum color {
179
+ white,
180
+ // node_id marks which node turned this gray/black.
181
+ // the node id later becomes the parent.
182
+ gray( node_id ) ,
183
+ black( node_id )
184
+ } ;
185
+
186
+ let mut colors = vec:: from_fn ( graph. len ( ) ) { |i|
187
+ if i as node_id == key {
188
+ gray ( key)
189
+ }
190
+ else {
191
+ white
192
+ }
193
+ } ;
194
+
195
+ fn is_gray ( c : color ) -> bool {
196
+ alt c {
197
+ gray( _) { true }
198
+ _ { false }
199
+ }
200
+ }
201
+
202
+ let mut i = 0 u;
203
+ while vec:: any ( colors, is_gray) {
204
+ // Do the BFS.
205
+ log ( info, #fmt ( "PBFS iteration %?" , i) ) ;
206
+ i += 1 u;
207
+ colors = colors. mapi ( ) { |i, c|
208
+ let c : color = c;
209
+ alt c {
210
+ white {
211
+ let i = i as node_id ;
212
+
213
+ let neighbors = graph[ i] ;
214
+
215
+ let mut color = white;
216
+
217
+ neighbors. each ( ) { |k|
218
+ if is_gray ( colors[ k] ) {
219
+ color = gray ( k) ;
220
+ false
221
+ }
222
+ else { true }
223
+ } ;
224
+
225
+ color
226
+ }
227
+ gray ( parent) { black ( parent) }
228
+ black ( parent) { black ( parent) }
229
+ }
230
+ }
231
+ }
232
+
233
+ // Convert the results.
234
+ vec:: map( colors) { |c|
235
+ alt c {
236
+ white { -1 }
237
+ black( parent) { parent }
238
+ _ { fail "Found remaining gray nodes in BFS " }
239
+ }
240
+ }
241
+ }
242
+
171
243
#[ doc="A parallel version of the bfs function." ]
172
244
fn pbfs ( graph : graph , key : node_id ) -> bfs_result {
173
245
// This works by doing functional updates of a color vector.
@@ -378,6 +450,25 @@ fn main() {
378
450
stop - start) ) ;
379
451
}
380
452
453
+ let start = time:: precise_time_s ( ) ;
454
+ let bfs_tree = bfs2 ( graph, root) ;
455
+ let stop = time:: precise_time_s ( ) ;
456
+
457
+ //total_seq += stop - start;
458
+
459
+ io:: stdout ( ) . write_line (
460
+ #fmt ( "Slow Sequential BFS completed in %? seconds." ,
461
+ stop - start) ) ;
462
+
463
+ if do_validate {
464
+ let start = time:: precise_time_s ( ) ;
465
+ assert ( validate ( edges, root, bfs_tree) ) ;
466
+ let stop = time:: precise_time_s ( ) ;
467
+
468
+ io:: stdout ( ) . write_line ( #fmt ( "Validation completed in %? seconds." ,
469
+ stop - start) ) ;
470
+ }
471
+
381
472
let start = time:: precise_time_s ( ) ;
382
473
let bfs_tree = pbfs ( graph, root) ;
383
474
let stop = time:: precise_time_s ( ) ;
0 commit comments