Repository navigation
Expand file tree
/
Copy pathgraphgenerators.cpp
More file actions
175 lines (149 loc) · 4.36 KB
/
Copy pathgraphgenerators.cpp
File metadata and controls
175 lines (149 loc) · 4.36 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
//
// graphgenerators.cpp
// graphchi_xcode
//
// Created by Aapo Kyrola on 9/16/13.
//
//
#include <stdlib.h>
#include <cstdio>
#include <sys/time.h>
#include <time.h>
#include <cmath>
#include <vector>
#include <iostream>
int idcounter = 0;
struct Node {
std::vector<Node *> children;
int id;
Node(int id) : id(id) {}
void branch(int mydepth, int maxdepth, int d, int n) {
if (mydepth > maxdepth) return;
while (children.size() < d) {
children.push_back(new Node(idcounter++));
}
for(int i=0; i<children.size(); i++) {
children[i]->branch(mydepth + 1, maxdepth, d, n);
}
}
void output(FILE * f) {
for(int i=0; i<children.size(); i++) {
fprintf(f, "%d %d\n", id, children[i]->id);
children[i]->output(f);
}
}
};
struct Tree {
int d;
Node * root;
Tree(int d, int n) : d(d) {
int depth = (int) (1 + log(n)/log(d));
std::cout << "depth=" << depth<< std::endl;
root = new Node(idcounter++);
root->branch(1, depth, d, n);
}
void output(FILE * f) {
root->output(f);
}
};
int main(int argc, const char ** argv) {
if (argc < 3) {
printf("Usage: generate type n\n");
return 1;
}
std::string type = argv[1];
int n = atoi(argv[2]);
char filename[1024];
sprintf(filename, "%s_%d.edgelist", type.c_str(), n);
FILE * f = fopen(filename, "w");
if (type == "chain") {
for(int x=0; x<n - 1; x++) {
fprintf(f, "%d %d\n", x, x + 1);
}
}
if (type == "grid") {
for(int x=0; x<n; x++) {
for(int y=0; y<n; y++) {
int vid = y * n + x;
if (x < n - 1)
fprintf(f, "%d %d\n", vid, vid + 1);
if (y < n -1)
fprintf(f, "%d %d\n", vid, vid + n);
}
}
}
if (type == "crossgrid") {
for(int x=0; x<n; x++) {
for(int y=0; y<n; y++) {
int vid = y * n + x;
if (x < n - 1)
fprintf(f, "%d %d\n", vid, vid + 1);
if (y < n - 1)
fprintf(f, "%d %d\n", vid, vid + n);
if (x < n - 1 && y < n -1) {
// down and right
fprintf(f, "%d %d\n", vid, vid + n + 1);
}
if (x < n - 1 && y > 0) {
// up and right
fprintf(f, "%d %d\n", vid, vid -n + 1);
}
}
}
}
if (type == "cubegrid") {
for(int x=0; x<n; x++) {
for(int y=0; y<n; y++) {
for(int z=0; z<n; z++) {
int vid = z * n * n + y * n + x;
if (x < n - 1)
fprintf(f, "%d %d\n", vid, vid + 1);
if (y < n -1)
fprintf(f, "%d %d\n", vid, vid + n);
if (z < n - 1)
fprintf(f, "%d %d\n", vid, vid + n * n);
}
}
}
}
if (type == "quadgrid") {
for(int x=0; x<n; x++) {
for(int y=0; y<n; y++) {
for(int z=0; z<n; z++) {
for(int w=0; w<n; w++) {
int vid = w * n * n * n + z * n * n + y * n + x;
if (x < n - 1)
fprintf(f, "%d %d\n", vid, vid + 1);
if (y < n -1)
fprintf(f, "%d %d\n", vid, vid + n);
if (z < n - 1)
fprintf(f, "%d %d\n", vid, vid + n * n);
if (w < n - 1)
fprintf(f, "%d %d\n", vid, vid + n * n * n);
}
}
}
}
}
if (type == "tree2") {
Tree t(2, n);
t.output(f);
}
if (type == "tree3") {
Tree t(3, n);
t.output(f);
}
if (type == "tree4") {
Tree t(4, n);
t.output(f);
}
if (type == "tree5") {
Tree t(5, n);
t.output(f);
}
if (type == "tree50") {
Tree t(50, n);
t.output(f);
}
fclose(f);
}