Code Documentation 3.6
Social Network Visualizer
Loading...
Searching...
No Matches
per_source_scratch.h
Go to the documentation of this file.
1
13
14#ifndef SOCNETV_PER_SOURCE_SCRATCH_H
15#define SOCNETV_PER_SOURCE_SCRATCH_H
16
17#include <stack>
18#include <QHash>
19#include <QList>
20#include <QVector>
21#include <QtGlobal>
22#include <cstdlib> // RAND_MAX
23
25{
26 // Brandes traversal stack — vertices ordered by non-increasing distance from s
27 std::stack<int> Stack;
28
29 // Predecessor lists: Ps[vi] = list of vertices that precede vertex vi
30 // on shortest paths from the current source s. Indexed by vertex position.
31 QVector<QList<int>> Ps;
32
33 // Dependency accumulators for Brandes BC back-propagation.
34 // Indexed by vertex position.
35 QVector<qreal> delta;
36
37 // BFS / Dijkstra distances from current source s.
38 // dist[vi] == RAND_MAX means vertex vi has not been reached.
39 // Indexed by vertex position.
40 QVector<qreal> dist;
41
42 // Shortest-path counts (sigma) from current source s.
43 // Indexed by vertex position.
44 QVector<int> sigma;
45
46 // Number of vertices at each distance order from s (used for Power Centrality).
47 // Key = distance, Value = count.
48 QHash<qreal, int> nthOrder;
49
50 // Size of the connected component reachable from s (used for SPC normalisation).
52
53 // Per-source graph-aggregate accumulators.
54 // Phase 2: SSSP functions write here instead of calling graph.addToDistanceSum() /
55 // graph.incGeodesicsCount() / graph.setDiameterCached() directly, which would be
56 // unsafe when multiple threads process different sources concurrently.
57 // The owning thread accumulates these into ThreadLocalState totals after each source,
58 // and the post-loop reduction merges them into the graph-level aggregates.
59 qreal sourceDistanceSum = 0; // replaces graph.addToDistanceSum(dist_w) in BFS
60 int sourceGeodesicsCount = 0; // replaces graph.incGeodesicsCount() in BFS / Dijkstra
61 int sourceDiameter = 0; // replaces graph.setDiameterCached() in BFS / Dijkstra
62
63 // Allocate all containers once for totalVertices positions.
64 // Call this once before the source loop.
65 void allocate(int totalVertices)
66 {
67 Ps.resize(totalVertices);
68 delta.resize(totalVertices);
69 dist.resize(totalVertices);
70 sigma.resize(totalVertices);
71 }
72
73 // Reset per-source state before each new source vertex.
74 // computeCentralities controls whether the Brandes-only structures are reset.
75 void resetPerSource(bool computeCentralities)
76 {
77 dist.fill((qreal)RAND_MAX);
78 sigma.fill(0);
79 // Reset per-source graph-aggregate accumulators so they reflect only this source.
83 if (computeCentralities)
84 {
85 while (!Stack.empty())
86 Stack.pop();
87 for (auto &ps : Ps)
88 ps.clear();
89 nthOrder.clear();
90 }
91 // delta is zeroed in the per-source back-propagation setup loop,
92 // not here, to preserve the existing guard on computeCentralities.
93 }
94
95 // Increment the nth-order neighbourhood count for distance d.
96 void nthOrderIncrement(qreal d)
97 {
98 nthOrder.insert(d, nthOrder.value(d, 0) + 1);
99 }
100};
101
102#endif // SOCNETV_PER_SOURCE_SCRATCH_H
Definition per_source_scratch.h:25
QVector< int > sigma
Definition per_source_scratch.h:44
QHash< qreal, int > nthOrder
Definition per_source_scratch.h:48
int sourceGeodesicsCount
Definition per_source_scratch.h:60
qreal sourceDistanceSum
Definition per_source_scratch.h:59
int componentSize
Definition per_source_scratch.h:51
void allocate(int totalVertices)
Definition per_source_scratch.h:65
QVector< qreal > dist
Definition per_source_scratch.h:40
QVector< qreal > delta
Definition per_source_scratch.h:35
void resetPerSource(bool computeCentralities)
Definition per_source_scratch.h:75
std::stack< int > Stack
Definition per_source_scratch.h:27
void nthOrderIncrement(qreal d)
Definition per_source_scratch.h:96
int sourceDiameter
Definition per_source_scratch.h:61
QVector< QList< int > > Ps
Definition per_source_scratch.h:31