forked from jegonzal/PowerGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_graph_test.cxx
More file actions
581 lines (516 loc) · 19.7 KB
/
Copy pathlocal_graph_test.cxx
File metadata and controls
581 lines (516 loc) · 19.7 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Copyright (c) 2009 Carnegie Mellon University.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* For more about this software visit:
*
* http://www.graphlab.ml.cmu.edu
*
*/
// standard C++ headers
#include <iostream>
#include <cxxtest/TestSuite.h>
// includes the entire graphlab framework
#include <graphlab/graph/local_graph.hpp>
#include <graphlab/graph/dynamic_local_graph.hpp>
#include <graphlab/util/random.hpp>
#include <graphlab/macros_def.hpp>
/**
* Unit test for graphlab::local_graph.hpp
*/
class local_graph_test : public CxxTest::TestSuite {
public:
struct vertex_data {
size_t value;
vertex_data() : value(0) { }
vertex_data(size_t n) : value(n) { }
};
struct edge_data {
int from;
int to;
edge_data (int f = 0, int t = 0) : from(f), to(t) {}
};
/**
* Test add vertex and add edges
*/
void test_add_vertex() {
graphlab::local_graph<vertex_data, edge_data> g;
test_add_vertex_impl(g, 100);
test_add_vertex_impl(g, 10000);
test_add_vertex_impl(g, 100000);
std::cout << "\n+ Pass test: graph add vertex. :) \n";
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_add_vertex_impl(g2, 100);
test_add_vertex_impl(g2, 10000);
test_add_vertex_impl(g2, 100000);
std::cout << "\n+ Pass test: dynamic graph add vertex. :) \n";
}
void test_add_edge() {
graphlab::local_graph<vertex_data, edge_data> g;
test_add_edge_impl(g, 100);
test_add_edge_impl(g, 10000);
test_add_edge_impl(g, 100000);
std::cout << "\n+ Pass test: graph add edge. :) \n";
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_add_edge_impl(g2, 100);
test_add_edge_impl(g2, 10000);
test_add_edge_impl(g2, 100000);
std::cout << "\n+ Pass test: dynamic graph add edge. :) \n";
}
void test_dynamic_add_edge() {
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_add_edge_impl(g2, 100, true); // add edge dynamically
test_add_edge_impl(g2, 10000, true);
test_add_edge_impl(g2, 100000, true);
std::cout << "\n+ Pass test: graph dynamicly add edge. :) \n";
}
void test_powerlaw_graph() {
graphlab::local_graph<vertex_data, edge_data> g;
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_powerlaw_graph_impl(g, 100); // add edge (powerlaw)
test_powerlaw_graph_impl(g, 10000);
test_powerlaw_graph_impl(g2, 100); // add edge (powerlaw)
test_powerlaw_graph_impl(g2, 10000);
test_powerlaw_graph_impl(g2, 100, true); // add edge (powerlaw) dynamically
test_powerlaw_graph_impl(g2, 10000, true);
std::cout << "\n+ Pass test: powerlaw graph add edge. :) \n";
}
void test_edge_case() {
graphlab::local_graph<vertex_data, edge_data> g;
test_edge_case_impl(g);
std::cout << "\n+ Pass test: edge case test. :) \n";
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_edge_case_impl(g2);
std::cout << "\n+ Pass test: dynamic graph edge case test. :) \n";
}
void test_sparse_graph() {
graphlab::local_graph<vertex_data, edge_data> g;
test_sparse_graph_impl(g);
std::cout << "\n+ Pass test: sparse graph test. :) \n";
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_sparse_graph_impl(g2);
std::cout << "\n+ Pass test: sparse dyanmic graph test. :) \n";
}
void test_grid_graph() {
graphlab::local_graph<vertex_data, edge_data> g;
test_grid_graph_impl(g);
std::cout << "\n+ Pass test: grid graph test. :) \n";
graphlab::dynamic_local_graph<vertex_data, edge_data> g2;
test_grid_graph_impl(g2);
std::cout << "\n+ Pass test: grid dynamic graph test. :) \n";
}
private:
template<typename Graph>
void test_add_vertex_impl(Graph& g, size_t nverts) {
g.clear();
ASSERT_EQ(g.num_vertices(), 0);
for (size_t i = 0; i < nverts; ++i) {
g.add_vertex(i, vertex_data(i));
}
ASSERT_EQ(g.num_vertices(), nverts);
for (size_t i = 0; i < g.num_vertices(); ++i) {
ASSERT_EQ(g.vertex(i).data().value, i);
}
g.finalize();
ASSERT_EQ(g.num_vertices(), nverts);
// graph should still support adding vertices after finalization
// add more vertices and override existing vertex values
for (size_t i = 0; i < 2*nverts; ++i) {
g.add_vertex(i, vertex_data(i*2));
}
ASSERT_EQ(g.num_vertices(), 2*nverts);
for (size_t i = 0; i < g.num_vertices(); ++i) {
ASSERT_EQ(g.vertex(i).data().value, 2*i);
}
}
/**
* Helper function to check the in/out edges of the graph.
*/
template<typename Graph>
void check_adjacency(Graph& g,
boost::unordered_map<typename Graph::vertex_id_type,
std::vector<typename Graph::vertex_id_type> >& in_edges,
boost::unordered_map<typename Graph::vertex_id_type,
std::vector<typename Graph::vertex_id_type> >& out_edges,
size_t nedges) {
typedef typename Graph::edge_list_type edge_list_type;
typedef typename Graph::edge_type edge_type;
typedef typename Graph::vertex_type vertex_type;
typedef typename Graph::vertex_id_type vertex_id_type;
// check size
ASSERT_EQ(g.num_edges(), nedges);
size_t nedges_actual = 0;
// check out edges
typedef typename boost::unordered_map<vertex_id_type, std::vector<vertex_id_type> >::iterator iter_type;
for (iter_type it = out_edges.begin(); it != out_edges.end(); ++it) {
vertex_id_type src = it->first;
std::set<vertex_id_type> dst_expected = std::set<vertex_id_type>(it->second.begin(), it->second.end());
const edge_list_type& ls = g.out_edges(src);
foreach (const edge_type& e, ls) {
ASSERT_EQ(e.source().id(), src);
ASSERT_TRUE(dst_expected.count(e.target().id()) == 1);
dst_expected.erase(e.target().id());
}
nedges_actual += ls.size();
}
ASSERT_EQ(nedges_actual, g.num_edges());
ASSERT_EQ(nedges_actual, nedges);
nedges_actual = 0;
// check in edges
for (iter_type it = in_edges.begin(); it != in_edges.end(); ++it) {
vertex_id_type dst = it->first;
std::set<vertex_id_type> src_expected = std::set<vertex_id_type>(it->second.begin(), it->second.end());
const edge_list_type& ls = g.in_edges(dst);
foreach (const edge_type& e, ls) {
ASSERT_EQ(e.target().id(), dst);
ASSERT_TRUE(src_expected.count(e.source().id()) == 1);
src_expected.erase(e.source().id());
}
nedges_actual += ls.size();
}
ASSERT_EQ(nedges_actual, g.num_edges());
ASSERT_EQ(nedges_actual, nedges);
}
template<typename Graph>
void check_edge_data(Graph& g) {
typedef typename Graph::edge_list_type edge_list_type;
typedef typename Graph::edge_type edge_type;
typedef typename Graph::vertex_type vertex_type;
typedef typename Graph::vertex_id_type vertex_id_type;
for (size_t i = 0; i < g.num_vertices(); ++i) {
const edge_list_type& in_edges = g.in_edges(i);
foreach (const edge_type& e, in_edges) {
ASSERT_EQ(e.data().from, e.source().id());
ASSERT_EQ(e.data().to, e.target().id());
}
const edge_list_type& out_edges = g.out_edges(i);
foreach (const edge_type& e, out_edges) {
ASSERT_EQ(e.data().from, e.source().id());
ASSERT_EQ(e.data().to, e.target().id());
}
}
}
template<typename Graph>
void test_add_edge_impl(Graph& g, size_t nedges, bool use_dynamic=false) {
typedef typename Graph::vertex_id_type vertex_id_type;
srand(0);
g.clear();
ASSERT_EQ(g.num_edges(), 0);
boost::unordered_map<vertex_id_type, std::vector<vertex_id_type> > out_edges;
boost::unordered_map<vertex_id_type, std::vector<vertex_id_type> > in_edges;
boost::unordered_set< std::pair<vertex_id_type,vertex_id_type> > all_edges;
while (all_edges.size() < nedges) {
vertex_id_type src = rand() % (int)(3*sqrt(nedges));
vertex_id_type dst = rand() % (int)(3*sqrt(nedges));
if (src == dst)
continue;
std::pair<vertex_id_type,vertex_id_type> pair(src, dst);
if (!all_edges.count(pair)) {
all_edges.insert(pair);
if (!out_edges.count(src)) {
out_edges[src] = std::vector<vertex_id_type>();
}
if (!in_edges.count(dst)) {
in_edges[dst] = std::vector<vertex_id_type>();
}
in_edges[dst].push_back(src);
out_edges[src].push_back(dst);
}
}
typedef typename boost::unordered_set< std::pair<vertex_id_type,vertex_id_type> >::value_type pair_type;
size_t count = 0;
foreach (const pair_type& p, all_edges) {
g.add_edge(p.first, p.second, edge_data(p.first, p.second));
++count;
if (use_dynamic && (all_edges.size()/5) == 0) {
g.finalize();
}
}
if (!use_dynamic)
ASSERT_EQ(g.num_edges(), 0);
g.finalize();
check_adjacency(g, in_edges, out_edges, all_edges.size());
check_edge_data(g);
}
template<typename Graph>
void test_edge_case_impl(Graph& g) {
// TODO:
// self edges
// duplicate edges
std::cout << "Warning: test not implemented" << std::endl;
}
/**
* Construct a star like sparse graph and test the in/out neighbors.
*/
template<typename Graph>
void test_sparse_graph_impl (Graph& g) {
typedef typename Graph::edge_list_type edge_list_type;
typedef typename Graph::edge_type edge_type;
typedef typename Graph::vertex_type vertex_type;
typedef typename Graph::vertex_id_type vertex_id_type;
size_t num_v = 10;
size_t num_e = 6;
for (size_t i = 0; i < num_v; ++i) {
vertex_data vdata;
g.add_vertex(vertex_id_type(i), vdata);
}
/**
* Create a star graph.
*/
g.add_edge(1,3,edge_data(1,3));
g.add_edge(2,3,edge_data(2,3));
g.add_edge(4,3,edge_data(4,3));
g.add_edge(5,3,edge_data(5,3));
g.add_edge(3,2, edge_data(3,2));
g.add_edge(3,5, edge_data(3,5));
g.finalize();
ASSERT_EQ(g.num_vertices(), num_v);
ASSERT_EQ(g.num_edges(), num_e);
/**
* Test number of in/out edges.
*/
for (vertex_id_type i = 0; i < 6; ++i) {
edge_list_type inedges = g.in_edges(i);
edge_list_type outedges = g.out_edges(i);
size_t arr_insize[] = {0,0,1,4,0,1};
size_t arr_outsize[] = {0,1,1,2,1,1};
if (i != 3) {
ASSERT_EQ(inedges.size(), arr_insize[i]);
ASSERT_EQ(outedges.size(), arr_outsize[i]);
if (outedges.size() > 0)
{
ASSERT_EQ(outedges[0].source().id(), i);
ASSERT_EQ(outedges[0].target().id(), 3);
edge_data data = (outedges[0]).data();
ASSERT_EQ(data.from, i);
ASSERT_EQ(data.to, 3);
}
} else {
std::set<vertex_id_type> out_neighbors;
out_neighbors.insert(5);
out_neighbors.insert(2);
ASSERT_EQ(outedges.size(), out_neighbors.size());
for (size_t j = 0; j < 2; ++j) {
edge_data data = (outedges[j]).data();
ASSERT_EQ(data.from, 3);
ASSERT_TRUE(out_neighbors.count(data.to) == 1);
out_neighbors.erase(data.to);
}
std::set<vertex_id_type> in_neighbors;
in_neighbors.insert(5);
in_neighbors.insert(4);
in_neighbors.insert(2);
in_neighbors.insert(1);
ASSERT_EQ(inedges.size(), in_neighbors.size());
for (size_t j = 0; j < 4; ++j) {
edge_data data = (inedges[j]).data();
ASSERT_EQ(data.to, 3);
ASSERT_TRUE(in_neighbors.count(data.from) == 1);
in_neighbors.erase(data.from);
}
}
}
for (vertex_id_type i = 6; i < num_v; ++i) {
edge_list_type inedges = g.in_edges(i);
edge_list_type outedges = g.out_edges(i);
ASSERT_EQ(0, inedges.size());
ASSERT_EQ(0, outedges.size());
}
}
/**
In this function, we construct the 3 by 3 grid graph.
*/
template<typename Graph>
void test_grid_graph_impl(Graph& g, bool verbose = false) {
typedef typename Graph::edge_list_type edge_list_type;
typedef typename Graph::edge_type edge_type;
typedef typename Graph::vertex_type vertex_type;
typedef typename Graph::vertex_id_type vertex_id_type;
g.clear();
if (verbose)
std::cout << "-----------Begin Grid Test: ID Accessors--------------------" << std::endl;
size_t dim = 3;
size_t num_vertices = 0;
size_t num_edge = 0;
// here we create dim * dim vertices.
for (size_t i = 0; i < dim * dim; ++i) {
// create the vertex data, randomizing the color
vertex_data vdata;
vdata.value = 0;
// create the vertex
g.add_vertex(vertex_id_type(i), vdata);
++num_vertices;
}
// create the edges. The add_edge(i,j,edgedata) function creates
// an edge from i->j. with the edgedata attached. edge_data edata;
for (size_t i = 0;i < dim; ++i) {
for (size_t j = 0;j < dim - 1; ++j) {
// add the horizontal edges in both directions
//
g.add_edge(dim * i + j, dim * i + j + 1, edge_data(dim*i+j, dim*i+j+1));
g.add_edge(dim * i + j + 1, dim * i + j, edge_data(dim*i+j+1, dim*i+j));
// add the vertical edges in both directions
g.add_edge(dim * j + i, dim * (j + 1) + i, edge_data(dim*j+i, dim*(j+1)+i));
g.add_edge(dim * (j + 1) + i, dim * j + i, edge_data(dim*(j+1)+i, dim*j+i));
num_edge += 4;
}
}
// the graph is now constructed
// we need to call finalize.
g.finalize();
if (verbose) printf("Test num_vertices()...\n");
ASSERT_EQ(g.num_vertices(), num_vertices);
if (verbose) printf("+ Pass test: num_vertices :)\n\n");
if (verbose) printf("Test num_edges()...\n");
ASSERT_EQ(g.num_edges(), num_edge);
if (verbose) printf("+ Pass test: num_edges :)\n\n");
// Symmetric graph: #inneighbor == outneighbor
if (verbose) printf("Test num_in_neighbors() == num_out_neighbors() ...\n");
for (size_t i = 0; i < num_vertices; ++i) {
ASSERT_EQ(g.in_edges(i).size(), g.vertex(i).num_in_edges());
ASSERT_EQ(g.out_edges(i).size(), g.vertex(i).num_out_edges());
ASSERT_EQ(g.in_edges(i).size(), g.out_edges(i).size());
}
ASSERT_EQ(g.in_edges(4).size(), 4);
ASSERT_EQ(g.in_edges(0).size(), 2);
if (verbose) printf("+ Pass test: #in = #out...\n\n");
if (verbose)
printf("Test iterate over in/out_edges and get edge data: \n");
for (vertex_id_type i = 0; i < num_vertices; ++i) {
const edge_list_type& out_edges = g.out_edges(i);
const edge_list_type& in_edges = g.in_edges(i);
if (verbose) {
std::cout << "Test v: " << i << "\n"
<< "In edge ids: ";
foreach(edge_type edge, in_edges)
std::cout << "(" << edge.data().from << ","
<< edge.data().to << ") ";
std::cout <<std::endl;
std::cout << "Out edge ids: ";
foreach(edge_type edge, out_edges)
std::cout << "(" << edge.data().from << ","
<< edge.data().to << ") ";
std::cout <<std::endl;
}
foreach(edge_type edge, out_edges) {
edge_data edata = edge.data();
ASSERT_EQ(edge.source().id(), i);
ASSERT_EQ(edata.from, edge.source().id());
ASSERT_EQ(edata.to, edge.target().id());
}
foreach(edge_type edge, in_edges) {
edge_data edata = edge.data();
ASSERT_EQ(edge.target().id(), i);
ASSERT_EQ(edata.from, edge.source().id());
ASSERT_EQ(edata.to, edge.target().id());
}
}
if (verbose)
printf("+ Pass test: iterate edgelist and get data. :) \n");
for (vertex_id_type i = 0; i < num_vertices; ++i) {
vertex_type v = g.vertex(i);
const edge_list_type& out_edges = v.out_edges();
const edge_list_type& in_edges = v.in_edges();
if (verbose) {
std::cout << "Test v: " << i << std::endl;
printf("In edge ids: ");
foreach(edge_type edge, in_edges)
std::cout << "(" << edge.data().from << ","
<< edge.data().to << ") ";
std::cout <<std::endl;
printf("Out edge ids: ");
foreach(edge_type edge, out_edges)
std::cout << "(" << edge.data().from << ","
<< edge.data().to << ") ";
std::cout <<std::endl;
}
foreach(edge_type edge, out_edges) {
edge_data edata = edge.data();
ASSERT_EQ(edge.source().id(), i);
ASSERT_EQ(edata.from, edge.source().id());
ASSERT_EQ(edata.to, edge.target().id());
}
foreach(edge_type edge, in_edges) {
edge_data edata = edge.data();
ASSERT_EQ(edge.target().id(), i);
ASSERT_EQ(edata.from, edge.source().id());
ASSERT_EQ(edata.to, edge.target().id());
}
}
if (verbose) {
printf("+ Pass test: iterate edgelist and get data. :) \n");
std::cout << "-----------End Grid Test--------------------" << std::endl;
}
}
/**
* Test powerlaw graph.
*/
template<typename Graph>
void test_powerlaw_graph_impl(Graph& g, size_t nverts, bool use_dynamic = false, double alpha = 2.1) {
graphlab::random::seed(0);
g.clear();
typedef typename Graph::edge_list_type edge_list_type;
typedef typename Graph::edge_type edge_type;
typedef typename Graph::vertex_type vertex_type;
typedef typename Graph::vertex_id_type vertex_id_type;
boost::unordered_map<vertex_id_type, std::vector<vertex_id_type> > out_edges;
boost::unordered_map<vertex_id_type, std::vector<vertex_id_type> > in_edges;
boost::unordered_set< std::pair<vertex_id_type,vertex_id_type> > all_edges;
// construct powerlaw out degree distribution
std::vector<double> prob(nverts, 0);
for(size_t i = 0; i < prob.size(); ++i)
prob[i] = std::pow(double(i+1), -alpha);
graphlab::random::pdf2cdf(prob);
// A large prime number
const size_t HASH_OFFSET = 2654435761;
// construct powerlaw graph with no dup edges
for(vertex_id_type src = 0; src < nverts; ++src) {
const size_t out_degree = graphlab::random::multinomial_cdf(prob) + 1;
for(size_t i = 0; i < out_degree; ++i) {
vertex_id_type dst = (dst + HASH_OFFSET) % nverts;
while (src == dst) {
dst = (dst + HASH_OFFSET) % nverts;
}
std::pair<vertex_id_type, vertex_id_type> pair(src, dst);
if (!all_edges.count(pair)) {
all_edges.insert(pair);
if (!out_edges.count(src)) {
out_edges[src] = std::vector<vertex_id_type>();
}
if (!in_edges.count(dst)) {
in_edges[dst] = std::vector<vertex_id_type>();
}
in_edges[dst].push_back(src);
out_edges[src].push_back(dst);
}
}
}
typedef typename boost::unordered_set< std::pair<vertex_id_type, vertex_id_type> >::value_type pair_type;
size_t count = 0;
foreach (const pair_type& p, all_edges) {
g.add_edge(p.first, p.second, edge_data(p.first, p.second));
++count;
if (use_dynamic && count % (all_edges.size()/5) == 0) {
g.finalize();
}
}
if (!use_dynamic)
ASSERT_EQ(g.num_edges(), 0);
g.finalize();
check_adjacency(g, in_edges, out_edges, all_edges.size());
check_edge_data(g);
}
};
#include <graphlab/macros_undef.hpp>