Skip to content

Commit c6163f8

Browse files
committed
first directed graph tests
1 parent 10de3b5 commit c6163f8

1 file changed

Lines changed: 63 additions & 57 deletions

File tree

‎test/personalized_pagerank_test.cpp‎

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,72 @@
66
#include <vector>
77
#include <iomanip>
88

9-
int main(int, char*[]) {
10-
using namespace boost;
11-
// deliberately hard (slow-converging) graph
12-
using Graph = adjacency_list<vecS, vecS, directedS>;
13-
std::vector<std::pair<int,int>> edges = {
14-
{0,1},{1,0},{1,2},{2,1},{2,3},{3,2},
15-
{4,5},{5,4},{5,6},{6,5},{6,7},{7,6},{7,8},{8,7},{8,9},{9,8},{9,10},{10,9},
16-
{0,3},{3,0},{1,3},{3,1},{1,4},{4,1},
17-
{4,6},{6,4},{6,9},{9,6},{6,8},{8,6},{7,9},{9,7},{8,10},{10,8},
18-
{11,10},{10,11},{10,12},{12,10}
19-
};
20-
Graph g(edges.begin(), edges.end(), 13);
21-
22-
std::vector<double> ranks(num_vertices(g));
23-
auto rank_map = make_iterator_property_map(ranks.begin(), get(vertex_index, g));
24-
std::vector<double> personalization(num_vertices(g));
25-
auto personalization_map = make_iterator_property_map(personalization.begin(), get(vertex_index, g));
26-
personalization[0] = 1;
27-
personalization[1] = 1;
28-
personalization[2] = 1;
29-
personalization[3] = 1;
9+
using DirectedGraph = typename boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;
10+
using DirectedVertex = typename boost::graph_traits<DirectedGraph>::vertex_descriptor;
11+
using DirectedEdge = typename boost::graph_traits<DirectedGraph>::edge_descriptor;
3012

31-
std::size_t max_iters(100); // Convergence is so bad in this graph that it needs such a high cap.
32-
using Edge = graph_traits<Graph>::edge_descriptor;
33-
auto weight = make_function_property_map<Edge, double>(
34-
[&g](Edge e){ return 1.0 / std::sqrt(double(out_degree(source(e, g), g) * out_degree(target(e, g), g))); });
35-
auto convergence1 = graph::rank_convergence(max_iters, 1.E-9);
36-
convergence1 = graph::personalized_page_rank(g, weight, personalization_map, rank_map, convergence1, 0.9);
13+
struct custom_rank_convergence: public boost::graph::rank_convergence
14+
{
15+
explicit custom_rank_convergence(std::size_t iters, double tol=0) : boost::graph::rank_convergence(iters,tol) {}
16+
std::size_t get_remaining_iters() const { return iters; }
17+
};
3718

38-
std::cout << "ended after "<<max_iters-convergence1.iters << " iterations\n";
39-
std::cout << std::fixed << std::setprecision(4);
40-
for (std::size_t v = 0; v < num_vertices(g); ++v)
19+
void directed_graph_tests(double damping, double renormalize)
20+
{
21+
std::vector<std::pair<std::vector<std::pair<int,int>>, int>> graph_defs = {
22+
// deliberately hard symmetric graph
23+
{{
24+
{0,1},{1,0},{1,2},{2,1},{2,3},{3,2},
25+
{4,5},{5,4},{5,6},{6,5},{6,7},{7,6},{7,8},{8,7},{8,9},{9,8},{9,10},{10,9},
26+
{0,3},{3,0},{1,3},{3,1},{1,4},{4,1},
27+
{4,6},{6,4},{6,9},{9,6},{6,8},{8,6},{7,9},{9,7},{8,10},{10,8},
28+
{11,10},{10,11},{10,12},{12,10}
29+
}, 13},
30+
// undirected circle with 0 and 2 being symmetric, and a non-symmetric directed one-way blocks 2 hops away from 0 ({7,6} blocked) and 2 ({4,5} blocked)
31+
{{ {0,1},{1,0},{1,3},{3,1},{3,2},{2,3},{2,4},{4,2},{5,4},{5,6},{6,5},{6,7},{7,0},{0,7}}, 8},
32+
// fully undirected graph
33+
{{ {0,1}, {2,1}, {0,3}, {2,3}, {3,4}, {4,5}, {1,5}, {5,2}, {5,0}}, 6},
34+
// same as above but missing incoming edges for 0 and 2 (whole graph is a sink, needs correct normalization that guards against zero to not yield nans)
35+
{{ {0,1}, {2,1}, {0,3}, {2,3}, {3,4}, {4,5}, {1,5}}, 6}
36+
};
37+
for(auto& graph_details : graph_defs)
4138
{
42-
std::cout << "vertex " << v << " rank=" << ranks[v] << "\n";
43-
}
44-
BOOST_ASSERT(ranks[0]==ranks[2]); // should be exactly equal due to symmetry, even when considering floating point coarseness
45-
BOOST_ASSERT(ranks[0]<ranks[1]+0.1);
46-
BOOST_ASSERT(ranks[8]==ranks[9]);
47-
BOOST_ASSERT(ranks[11]==ranks[12]);
48-
BOOST_ASSERT(ranks[11]>0);
49-
BOOST_ASSERT(convergence1.iters<max_iters);
39+
DirectedGraph g(graph_details.first.begin(), graph_details.first.end(), graph_details.second);
40+
std::vector<double> ranks(num_vertices(g));
41+
auto rank_map = boost::make_iterator_property_map(ranks.begin(), get(boost::vertex_index, g));
42+
std::vector<double> personalization(num_vertices(g));
43+
auto personalization_map = boost::make_iterator_property_map(personalization.begin(), get(boost::vertex_index, g));
44+
personalization[0] = 1;
45+
personalization[1] = 1;
46+
personalization[2] = 1;
47+
personalization[3] = 1;
5048

51-
// COMPUTING A HIGH-PASS VERSION BY PASSING NEGATIVE DAMPING (resulting values can be negative)
52-
// This is not completely correct yet for computing graph gradients, because it needs to eskew normalization,
53-
// so we need to be able to customize said normalization.
54-
// Damping should generaly be in the range [-1,1].
55-
auto renorm_weight = make_function_property_map<Edge, double>(
56-
[&g](Edge e){ return 1.0 / std::sqrt(double((1.0+out_degree(source(e, g), g)) * (1.0+out_degree(target(e, g), g)))); });
57-
auto convergence2 = graph::rank_convergence(max_iters, 1.E-9);
58-
convergence2 = graph::personalized_page_rank(g, renorm_weight, personalization_map, rank_map, convergence2, -0.8);
59-
std::cout << "ended after "<<max_iters-convergence2.iters << " iterations\n";
60-
std::cout << std::fixed << std::setprecision(4);
61-
for (std::size_t v = 0; v < num_vertices(g); ++v)
62-
{
63-
std::cout << "vertex " << v << " flow=" << ranks[v] << "\n";
49+
std::size_t max_iters(300); // Convergence is just that bad in tested graphs; usually it's much lower.'
50+
auto weight = boost::make_function_property_map<DirectedEdge, double>([&g,renormalize](DirectedEdge e){
51+
auto denom = out_degree(source(e, g), g) * out_degree(target(e, g), g);
52+
if(denom==0) return 0.0;
53+
return 1.0 / std::sqrt(renormalize+double(denom));
54+
});
55+
auto convergence = custom_rank_convergence(max_iters, 1.E-9);
56+
convergence = boost::graph::personalized_page_rank(g, weight, personalization_map, rank_map, convergence, damping);
57+
58+
// the following asserts hold for all tested graphs and personalization: 0,1,2,3 plus some other nodes is a mini-cluster with 0 and 2 being structurally symmetric
59+
assert(convergence.get_remaining_iters()<max_iters); // ran
60+
assert(convergence.get_remaining_iters()>0 || (damping<0.0)); // converged (derivatives may not converge)
61+
assert((ranks[0]<ranks[1]+0.1) || (damping<0.0) || damping>1.0); // holds for all graphs given low-pass damping
62+
assert((ranks[0]!=ranks[1]) == (damping!=0.0)); // but not the same normally, the same 1.0 personalization if damping is zero
63+
assert(ranks[0]==ranks[2] || (damping<=-1.0)); // equal due to symmetry, even under non-convergence and floating coarseness
64+
assert(std::abs(ranks[0]-ranks[2])<1.E-14); // approximately equal in cases where even addition order matters
65+
assert((ranks[0]<0.0)||(ranks[1]<0.0)||(ranks[num_vertices(g)-1]<=0.0)||(damping>0.0)); // ensure that negative flows are possible for negative damping
6466
}
65-
BOOST_ASSERT(ranks[0]==ranks[2]); // should be exactly equal due to symmetry, even when considering floating point coarseness
66-
BOOST_ASSERT(ranks[0]>ranks[1]+0.1);
67-
BOOST_ASSERT(ranks[8]==ranks[9]);
68-
BOOST_ASSERT(ranks[11]==ranks[12]);
69-
BOOST_ASSERT(ranks[11]<0);
70-
BOOST_ASSERT(convergence2.iters<max_iters);
67+
}
68+
69+
int main(int, char*[])
70+
{
71+
directed_graph_tests(0.9, 0); // normal mode
72+
directed_graph_tests(0.9, 1.0); // with renormalization
73+
directed_graph_tests(0.99, 0); // huge damping (asymptotically exponentially slow convergence as we approach 1.0)
74+
directed_graph_tests(0.0, 0); // just yield the personalization again
75+
directed_graph_tests(-0.8, 1.0); // negative flow = some kind of derivate
76+
directed_graph_tests(-1.0, 1.0); // huge negative flow
7177
}

0 commit comments

Comments
 (0)