Permalink
Browse files

Betweenness centrality algorithm no longer has to be passed "ignore" …

…array.
  • Loading branch information...
WebDrake committed Aug 30, 2013
1 parent 87a33a4 commit 8bff8e963265a06132035cf964443ddf13c9f2c9
Showing with 59 additions and 57 deletions.
  1. +1 −3 betweenness.d
  2. +58 −54 dgraph/metric.d
View
@@ -5,9 +5,7 @@ import dgraph.graph, dgraph.metric, dgraph.test.samplegraph50;
void betw(bool directed)(ref Graph!directed g)
{
assert(!directed);
auto ignore = new bool[g.vertexCount];
ignore[] = false;
auto centrality = betweenness(g, ignore);
auto centrality = betweenness(g);
assert(centrality.length == g.vertexCount);
/* writeln("Centrality values:");
foreach (immutable i, immutable c; centrality)
View
@@ -70,7 +70,7 @@ unittest
assert(q.empty);
}
auto betweenness(T = double, bool directed)(ref Graph!directed g, bool[] ignore)
auto betweenness(T = double, bool directed)(ref Graph!directed g, bool[] ignore = null)
if (isFloatingPoint!T)
{
T[] centrality = new T[g.vertexCount];
@@ -88,71 +88,75 @@ auto betweenness(T = double, bool directed)(ref Graph!directed g, bool[] ignore)
foreach (immutable s; 0 .. g.vertexCount)
{
if (!ignore[s])
if (ignore && ignore[s])
{
size_t stackLength = 0;
assert(q.empty);
sigma[s] = to!T(1);
d[s] = 0L;
q.push(s);
continue;
}
while(!q.empty)
size_t stackLength = 0;
assert(q.empty);
sigma[s] = to!T(1);
d[s] = 0L;
q.push(s);
while (!q.empty)
{
size_t v = q.front;
q.pop();
stack[stackLength] = v;
++stackLength;
foreach (immutable w; g.neighboursOut(v))
{
size_t v = q.front;
q.pop();
stack[stackLength] = v;
++stackLength;
foreach (immutable w; g.neighboursOut(v))
if (ignore && ignore[w])
{
if (!ignore[w])
{
if (d[w] < 0L)
{
q.push(w);
d[w] = d[v] + 1L;
assert(sigma[w] == to!T(0));
sigma[w] = sigma[v];
p[w].length = 1;
p[w][0] = v;
}
else if (d[w] > (d[v] + 1L))
{
/* w has already been encountered, but we've
found a shorter path to the source. This
is probably only relevant to the weighted
case, but let's keep it here in order to
be ready for that update. */
d[w] = d[v] + 1L;
sigma[w] = sigma[v];
p[w].length = 1;
p[w][0] = v;
}
else if (d[w] == (d[v] + 1L))
{
sigma[w] += sigma[v];
p[w] ~= v;
}
}
continue;
}
}
while(stackLength > to!size_t(0))
{
--stackLength;
auto w = stack[stackLength];
foreach (immutable v; p[w])
if (d[w] < 0L)
{
q.push(w);
d[w] = d[v] + 1L;
assert(sigma[w] == to!T(0));
sigma[w] = sigma[v];
p[w].length = 1;
p[w][0] = v;
}
else if (d[w] > (d[v] + 1L))
{
delta[v] += ((sigma[v] / sigma[w]) * (to!T(1) + delta[w]));
/* w has already been encountered, but we've
found a shorter path to the source. This
is probably only relevant to the weighted
case, but let's keep it here in order to
be ready for that update. */
d[w] = d[v] + 1L;
sigma[w] = sigma[v];
p[w].length = 1;
p[w][0] = v;
}
if (w != s)
else if (d[w] == (d[v] + 1L))
{
centrality[w] += delta[w];
sigma[w] += sigma[v];
p[w] ~= v;
}
sigma[w] = to!T(0);
delta[w] = to!T(0);
d[w] = -1L;
}
}
while(stackLength > to!size_t(0))
{
--stackLength;
auto w = stack[stackLength];
foreach (immutable v; p[w])
{
delta[v] += ((sigma[v] / sigma[w]) * (to!T(1) + delta[w]));
}
if (w != s)
{
centrality[w] += delta[w];
}
sigma[w] = to!T(0);
delta[w] = to!T(0);
d[w] = -1L;
}
}
static if (!directed)

0 comments on commit 8bff8e9

Please sign in to comment.