icentral-bbfs 0.1.0

A Rust crate providing Bidirectional Breadth-First Search (BBFS) for efficient computation of betweenness centrality in large graph structures.
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
crate::ix!();

/**
  | If dd=1 will compute both old values
  | and new values to eliminate need for
  | PartialBFS later
  | 
  | IMP: edge.src is assumed to be closer
  | to the source than edge.dst
  |
  */
pub fn bbfs(
    maybe_config:  Option<BBFSConfig>,
    workspace:     &mut ICentralWorkspace,
    component:     &Component,

    // source of the iteration
    source:        NodeId)
-> Result<(),BetweennessCentralityError> 
{
    let config = maybe_config.unwrap_or(default!());

    let mut queue = bbfs_initialize(&config, workspace, source)?;

    debug!(
        "running bbfs from source: {}, config: {:?}, initial_queue: {:#?}", 
        source, 
        config, 
        queue
    );

    while let Some(nodeid) = queue.dequeue() {

        debug!("bbfs processing nodeid {} from queue", nodeid);

        workspace.stack_push(nodeid);

        bbfs_step(
            workspace,
            nodeid, 
            &config, 
            component, 
            &mut queue
        )?;
    }

    Ok(())
}

fn bbfs_initialize(
    config:    &BBFSConfig,
    workspace: &mut ICentralWorkspace,
    source:    NodeId)
-> Result<NodeIdQueue,BetweennessCentralityError>
{
    let mut queue = NodeIdQueue::empty("bbfs_initialize::queue");

    debug!("bbfs_initialize -- initializing sigma and distance values for source node {}", source);

    //  Assumes workspace is initialized
    workspace.set_sigma_value_for_node(source, 1.0);
    workspace.set_distance_for_node(source, 0.0);

    if config.handle_new_sigmas() {

        debug!("also setting `new_sigmas` sigma value for source node {}", source);

        workspace.new_sigmas_set_sigma_value_for_node(source, 1.0);
    }

    debug!("adding source node to queue {}", queue.name());

    queue.enqueue(source);

    Ok(queue)
}

fn bbfs_rbfs_d1_comp_type_graph_handle_nodeid(
    w:                    NodeId,
    delta_bc_of_vertices: &mut BetweennessScores,
    workspace:            &mut ICentralWorkspace,
    source:               NodeId,
    edge:                 Edge) 
-> Result<(),BetweennessCentralityError> 
{
    let parents = workspace.parents_for_node(w);

    for &v in parents.iter() {

        bbfs_update(workspace,v,w);
    }

    bbfs_update_if_match_edge_dst(workspace, w, &edge);

    if w != source {

        workspace.update_delta_bc_of_vertices_for_node(
            w, 
            delta_bc_of_vertices
        );
    }

    Ok(())
}

fn bbfs_rbfs_d1_comp_type_graph(
    delta_bc_of_vertices: &mut BetweennessScores,
    workspace:            &mut ICentralWorkspace,
    source:               NodeId,
    edge:                 Edge) 
-> Result<(),BetweennessCentralityError> 
{
    for i in (0..=workspace.stack_len() - 1).rev() {

        let w: NodeId = workspace.stack_node_at_index(i);

        bbfs_rbfs_d1_comp_type_graph_handle_nodeid(
            w,
            delta_bc_of_vertices,
            workspace,
            source,
            edge
        )?;
    }

    Ok(())
}

fn bbfs_rbfs_d1_comp_type_bcc_step(
    v_n:                  NodeId,
    delta_bc_of_vertices: &mut BetweennessScores,
    workspace:            &mut ICentralWorkspace,
    component:            &Component,
    source:               NodeId,
    edge:                 Edge) 
-> Result<(),BetweennessCentralityError> 
{
    workspace.maybe_update_capital_deltas_for_component(component,source,v_n);
    workspace.update_deltas_for_each_p(component,source,v_n);

    // IMP: this is the only change that happens
    // to workspace.parents, @src should be added as
    // parent for dst
    if v_n == edge.dst {

        let v_p: NodeId = edge.src;

        workspace.update_new_deltas_with_new_delta_ratio(v_p, v_n);

        let new_sp_sn: f64 = workspace.get_new_delta_ratio(v_p,v_n);

        if component.has_articulation_point(source) {

            workspace.update_new_capital_deltas_with_new_delta_ratio(v_p,v_n);

            delta_bc_of_vertices.set_score_for_node(
                v_p,
                {
                    let t0 = delta_bc_of_vertices.score_for_node(v_p);
                    let t1 = workspace.new_capital_deltas_delta_value_for_node(v_n);
                    t0 + t1 * new_sp_sn / 2.0
                }
            );
        }

        if source != v_n {

            delta_bc_of_vertices.decrease_score_for_node(
                v_n, 
                workspace.delta_value_for_node(v_n) / 2.0
            );

            delta_bc_of_vertices.increase_score_for_node(
                v_n, 
                workspace.new_deltas_delta_value_for_node(v_n) / 2.0
            );
        }

        if component.has_articulation_point(source) {

            let vg_s: f64 = component.subgraph_micentraltude_through_articulation_point(source);

            delta_bc_of_vertices.decrease_score_for_node(
                v_n, 
                workspace.delta_value_for_node(v_n) * vg_s
            );

            delta_bc_of_vertices.decrease_score_for_node(
                v_n, 
                workspace.capital_deltas_delta_value_for_node(v_n) / 2.0
            );

            delta_bc_of_vertices.increase_score_for_node(
                v_n, 
                workspace.new_deltas_delta_value_for_node(v_n) * vg_s
            );

            delta_bc_of_vertices.increase_score_for_node(
                v_n, 
                workspace.new_capital_deltas_delta_value_for_node(v_n) / 2.0
            );
        }
    }

    Ok(())
}

/// BiconnectedComponents or MinimumUnionCycle
/// case: involved case, with external pairs
/// contribution
///
fn bbfs_rbfs_d1_comp_type_bcc(
    delta_bc_of_vertices:  &mut BetweennessScores,
    workspace:             &mut ICentralWorkspace,
    component:             &Component,
    source:                NodeId,
    edge:                  Edge) 
-> Result<(),BetweennessCentralityError> 
{
    for i in (0..=workspace.stack_len() - 1).rev() {

        let v_n: NodeId = workspace.stack_node_at_index(i);

        bbfs_rbfs_d1_comp_type_bcc_step(
            v_n,
            delta_bc_of_vertices,
            workspace,
            component,
            source,
            edge
        )?;
    }

    Ok(())
}

fn bbfs_rbfs_d1(
    delta_bc_of_vertices:  &mut BetweennessScores,
    workspace:             &mut ICentralWorkspace,
    component:             &Component,
    source:                NodeId,
    edge:                  Edge) 
-> Result<(),BetweennessCentralityError> 
{
    bbfs(
        Some(BBFSConfig::new_rbfs_d1(edge)),
        workspace,
        component,
        source
    )?;

    workspace.refill_deltass(component.num_nodes());

    match component.ty() {

        CompType::Graph => bbfs_rbfs_d1_comp_type_graph(
            delta_bc_of_vertices,
            workspace,
            source,
            edge
        )?,

        CompType::BiconnectedComponent => bbfs_rbfs_d1_comp_type_bcc(
            delta_bc_of_vertices,
            workspace,
            &component,
            source,
            edge
        )?,

        _ => { unimplemented!() }
    }

    Ok(())
}

fn bbfs_step(
    workspace: &mut ICentralWorkspace,
    nodeid:    NodeId,
    config:    &BBFSConfig, 
    component: &Component, 
    queue:     &mut NodeIdQueue

) -> Result<(),BetweennessCentralityError> {

    if config.handle_new_sigmas() {

        let edge = config.edge();

        if nodeid == edge.dst {

            let val = workspace.sigma_value_for_node(edge.src);

            debug!("updating sigmas for node={} with val={}", nodeid, val);

            workspace.new_sigmas_increment_sigma_value_for_node(nodeid, val);
        }
    }

    debug!(
        "component.subgraph {:#?}", 
        component.subgraph()
    );

    let nbrs = component.neighbors(nodeid);

    debug!("nbrs: {:#?}", nbrs);

    for &v_n in nbrs.iter() {

        debug!("bbfs from node={}, scanning neighbor={}", nodeid, v_n);

        if workspace.distance_is_infinite(v_n) {

            debug!(
                "found that neighbor={} is infinite distance away from node={}! enqueuing and setting as one step away", 
                v_n, 
                nodeid
            );

            queue.enqueue(v_n);

            workspace.set_distance_one_step_away(v_n,nodeid);

        } else {

            let dist = workspace.distance(v_n);

            debug!("found neighbor={}! {} steps away", v_n, dist);
        }

        if workspace.distance(v_n) == workspace.distance(nodeid) + 1.0 {

            debug!(
                "distance of neighbor={} is one step away from node={}, will increment sigma value", 
                v_n, 
                nodeid
            );

            workspace.increment_sigma_value_for_node(
                v_n, 
                workspace.sigma_value_for_node(nodeid)
            );

            if config.handle_new_sigmas() {

                workspace.new_sigmas_increment_sigma_value_for_node(
                    v_n, 
                    workspace.new_sigmas_sigma_value_for_node(nodeid)
                );
            }

            debug!("parenting {} to its neighbor {}", nodeid, v_n);

            workspace.add_parent(v_n, nodeid);
        }
    }

    debug!("bbfs_step, finished processing {} neighbors for node={}", nbrs.len(), nodeid);

    Ok(())
}

fn bbfs_update_deltas(
    workspace: &mut ICentralWorkspace, 
    v:         NodeId, 
    w:         NodeId

) {
    let step = workspace.calculate_deltas_step(v,w);

    workspace.increment_delta_value_for_node(v, step);
}

fn bbfs_update_new_deltas(
    workspace: &mut ICentralWorkspace, 
    v: NodeId, 
    w: NodeId) 
{
    let sigma_ratio = workspace.new_sigmas_sigma_ratio(v,w);

    let new_delta_w = workspace.new_deltas_delta_value_for_node(w);

    let update      = sigma_ratio * (1.0 + new_delta_w);

    workspace.new_deltas_increment_delta_value_for_node(
        v, 
        update
    );
}

fn bbfs_update(
    workspace: &mut ICentralWorkspace, 
    v: NodeId, 
    w: NodeId
) {
    bbfs_update_deltas(workspace,v,w);
    bbfs_update_new_deltas(workspace,v,w);
}

fn bbfs_update_if_match_edge_dst(
    workspace: &mut ICentralWorkspace, 
    w: NodeId, 
    e: &Edge) 
{
    if w == e.dst {

        let v_p: NodeId = e.src;

        let new_sp_sn: f64 = {
            let t0 = workspace.new_deltas_delta_value_for_node(v_p);
            let t1 = workspace.new_deltas_delta_value_for_node(w);
            t0 as f64 / t1 as f64
        };

        workspace.new_deltas_set_delta_value_for_node(
            v_p, 
            {
                let t0 = workspace.new_deltas_delta_value_for_node(v_p);
                let t1 = 1.0 + workspace.new_deltas_delta_value_for_node(w);
                t0 + new_sp_sn * t1
            }
        );
    }
}