-
Notifications
You must be signed in to change notification settings - Fork 4
/
random_graph_generator.cpp
80 lines (68 loc) · 1.54 KB
/
random_graph_generator.cpp
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
/**
* Generates randomized graph of specified size
*
* @author Ashwin Joisa
* @author Praveen Gupta
*
* Compile with [g++ random_graph_generator.cpp -std=c++11]
**/
//=============================================================================================//
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <graph_file_name>\n";
return 0;
}
int n, m;
int x, y;
cout << "Enter number of nodes: ";
cin >> n;
cout << "Enter number of edges: ";
cin >> m;
freopen(argv[1], "w", stdout);
cout << n << " " << m << endl;
vector<int> *v = new vector<int>[n+1];
// srand(time(0));
// By defaut srand(0), so the generated graphs are same everytime
for (int i = 0; i < m; i++) {
do {
x = rand() % n;
y = rand() % n;
} while (x == y);
// Edge between x and y
v[x].push_back(y);
v[y].push_back(x);
}
// R
cout << "0 ";
int cur = 0;
for(int i=0; i<n; i++) {
cur += v[i].size();
cout << cur << " ";
}
cout << endl;
// C
for(int i=0; i<n; i++)
for(int it : v[i])
cout << it << " ";
cout << endl;
delete[] v;
}
/*
-------------------------------
Example CSR format for
nodes = 5
edges = 5
-------------------------------
nodeCount edgeCount
row offset
column indices
-------------------------------
5 5
0 2 5 7 9 10
2 3 3 2 4 0 1 1 0 1
-------------------------------
*/