Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use CSR instead of AL for preprocessed graphs.

  • Loading branch information...
commit 094df7aea01e735ddc835837272af17e5f419f05 1 parent 32defc6
DavidKurokawa DavidKurokawa authored
9 prpack_adjacency_list.cpp
View
@@ -1,5 +1,6 @@
#include "prpack_adjacency_list.h"
#include <cassert>
+#include <cstdlib>
#include <cstring>
#include <fstream>
using namespace prpack;
@@ -18,15 +19,15 @@ prpack_adjacency_list::prpack_adjacency_list(const string& filename) {
FILE* f = fopen(filename.c_str(), "r");
// read in header
float blah;
- assert(fscanf(f, "%d%f%d", &num_vs, &blah, &num_es) == 3);
+ assert(fscanf(f, "%d %f %d", &num_vs, &blah, &num_es) == 3);
// fill in heads and tails
- num_es = 0;
+ num_self_es = 0;
int* hs = new int[num_es];
int* ts = new int[num_es];
tails = new int[num_vs];
memset(tails, 0, num_vs*sizeof(tails[0]));
for (int i = 0; i < num_es; ++i) {
- assert(fscanf(f, "%d%d%d", &hs[i], &ts[i], &blah) == 3);
+ assert(fscanf(f, "%d %d %f", &hs[i], &ts[i], &blah) == 3);
++tails[ts[i]];
if (hs[i] == ts[i])
++num_self_es;
@@ -39,7 +40,7 @@ prpack_adjacency_list::prpack_adjacency_list(const string& filename) {
heads = new int[num_es];
int* osets = new int[num_vs];
memset(osets, 0, num_vs*sizeof(osets[0]));
- for (int i = 0; i < num_nonself_es; ++i)
+ for (int i = 0; i < num_es; ++i)
heads[tails[ts[i]] + osets[ts[i]]++] = hs[i];
// clean up
free(hs);
50 prpack_preprocessed_scc_graph.cpp
View
@@ -2,14 +2,13 @@
#include <algorithm>
#include <cstdlib>
#include <cstring>
-#include <vector>
using namespace prpack;
using namespace std;
prpack_preprocessed_scc_graph::prpack_preprocessed_scc_graph(prpack_adjacency_list* al) {
// initialize instance variables
num_vs = al->num_vs;
- num_es = al->num_es;
+ num_es = al->num_es - al->num_self_es;
inv_num_outlinks = new double[num_vs];
fill(inv_num_outlinks, inv_num_outlinks + num_vs, 0);
ii = new double[num_vs];
@@ -27,39 +26,39 @@ prpack_preprocessed_scc_graph::prpack_preprocessed_scc_graph(prpack_adjacency_li
int* st = new int[num_vs]; // a stack for the dfs
memset(num, -1, num_vs*sizeof(num[0]));
memset(scc, -1, num_vs*sizeof(scc[0]));
- int* cs1 = new int[num_vs]; // call stack variable for dfs
- vector<int>::iterator* cs2 = new vector<int>::iterator[num_vs]; // call stack variable for dfs
+ int* cs1 = new int[num_vs]; // call stack variable for dfs
+ int* cs2 = new int[num_vs]; // call stack variable for dfs
// run iterative Tarjan's algorithm
for (int root = 0; root < num_vs; ++root) {
if (num[root] != -1)
continue;
int csz = 1;
cs1[0] = root;
- cs2[0] = al->al[root].begin();
+ cs2[0] = al->tails[root];
// dfs
while (csz) {
int p = cs1[csz - 1]; // node we're dfs-ing on
- vector<int>::iterator& it = cs2[csz - 1]; // iteration of the for loop
- if (it == al->al[p].begin()) {
+ int& it = cs2[csz - 1]; // iteration of the for loop
+ if (it == al->tails[p]) {
low[p] = num[p] = mn++;
st[sz++] = p;
} else {
- --it;
- low[p] = min(low[p], low[*it]);
- ++it;
+ low[p] = min(low[p], low[al->heads[it - 1]]);
}
bool done = false;
- for (; it != al->al[p].end(); ++it) {
- if (scc[*it] == -1) {
- if (num[*it] == -1) {
- // dfs(*it, p);
- cs1[csz] = *it;
- cs2[csz++] = al->al[*it].begin();
+ int end_it = (p + 1 != num_vs) ? al->tails[p + 1] : al->num_es;
+ for (; it < end_it; ++it) {
+ int h = al->heads[it];
+ if (scc[h] == -1) {
+ if (num[h] == -1) {
+ // dfs(h, p);
+ cs1[csz] = h;
+ cs2[csz++] = al->tails[h];
++it;
done = true;
break;
}
- low[p] = min(low[p], low[*it]);
+ low[p] = min(low[p], low[h]);
}
}
if (done)
@@ -85,18 +84,21 @@ prpack_preprocessed_scc_graph::prpack_preprocessed_scc_graph(prpack_adjacency_li
for (int tails_i = 0, heads_i = 0; tails_i < num_vs; ++tails_i) {
ii[tails_i] = 0;
tails[tails_i] = heads_i;
- for (vector<int>::iterator curr = al->al[decoding[tails_i]].begin(); curr != al->al[decoding[tails_i]].end(); ++curr) {
- if (tails_i == encoding[*curr]) {
- ii[tails_i] += 1;
- --num_es;
+ int decoded = decoding[tails_i];
+ int start_curr = al->tails[decoded];
+ int end_curr = (decoded + 1 != num_vs) ? al->tails[decoded + 1] : al->num_es;
+ for (int curr = start_curr; curr < end_curr; ++curr) {
+ int h = al->heads[curr];
+ if (tails_i == encoding[h]) {
+ ++ii[tails_i];
} else {
- heads[heads_i++] = encoding[*curr];
- if (scc[*curr] == scc[decoding[tails_i]])
+ heads[heads_i++] = encoding[h];
+ if (scc[h] == scc[decoded])
++num_es_inside;
else
++num_es_outside;
}
- ++inv_num_outlinks[encoding[*curr]];
+ ++inv_num_outlinks[encoding[h]];
}
}
divisions[0] = 0;
37 prpack_preprocessed_schur_graph.cpp
View
@@ -1,34 +1,34 @@
#include "prpack_preprocessed_schur_graph.h"
#include <algorithm>
#include <cstring>
-#include <vector>
using namespace prpack;
using namespace std;
prpack_preprocessed_schur_graph::prpack_preprocessed_schur_graph(prpack_adjacency_list* al) {
// initialize instance variables
num_vs = al->num_vs;
- num_es = al->num_es;
+ num_es = al->num_es - al->num_self_es;
inv_num_outlinks = new double[num_vs];
fill(inv_num_outlinks, inv_num_outlinks + num_vs, 0);
// permute dangling nodes to end
encoding = new int[num_vs];
- memset(encoding, -1, num_vs*sizeof(encoding[0]));
decoding = new int[num_vs];
+ memset(encoding, -1, num_vs*sizeof(encoding[0]));
int seen = 0;
- for (int b = 0; b < num_vs; ++b) {
- for (vector<int>::iterator a = al->al[b].begin(); a != al->al[b].end(); ++a) {
- if (encoding[*a] == -1)
- encoding[*a] = seen++;
- ++inv_num_outlinks[encoding[*a]];
+ for (int t = 0; t < num_vs; ++t) {
+ int start_i = al->tails[t];
+ int end_i = (t + 1 != num_vs) ? al->tails[t + 1] : al->num_es;
+ for (int i = start_i; i < end_i; ++i) {
+ int h = al->heads[i];
+ if (encoding[h] == -1)
+ decoding[encoding[h] = seen++] = h;
+ ++inv_num_outlinks[encoding[h]];
}
}
num_dangling_vs = num_vs - seen;
for (int i = 0; i < num_vs; ++i)
if (encoding[i] == -1)
- encoding[i] = seen++;
- for (int i = 0; i < num_vs; ++i)
- decoding[encoding[i]] = i;
+ decoding[encoding[i] = seen++] = i;
// convert al to head/tail format
ii = new double[num_vs];
tails = new int[num_vs];
@@ -36,13 +36,14 @@ prpack_preprocessed_schur_graph::prpack_preprocessed_schur_graph(prpack_adjacenc
for (int tails_i = 0, heads_i = 0; tails_i < num_vs; ++tails_i) {
ii[tails_i] = 0;
tails[tails_i] = heads_i;
- for (vector<int>::iterator curr = al->al[decoding[tails_i]].begin(); curr != al->al[decoding[tails_i]].end(); ++curr) {
- if (decoding[tails_i] == *curr) {
- ii[tails_i] += 1;
- --num_es;
- } else {
- heads[heads_i++] = encoding[*curr];
- }
+ int decoded = decoding[tails_i];
+ int start_i = al->tails[decoded];
+ int end_i = (decoded + 1 != num_vs) ? al->tails[decoded + 1] : al->num_es;
+ for (int i = start_i; i < end_i; ++i) {
+ if (decoded == al->heads[i])
+ ++ii[tails_i];
+ else
+ heads[heads_i++] = encoding[al->heads[i]];
}
inv_num_outlinks[tails_i] = (inv_num_outlinks[tails_i] == 0) ? -1 : 1/inv_num_outlinks[tails_i];
}
Please sign in to comment.
Something went wrong with that request. Please try again.