-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgraph.cpp
42 lines (37 loc) · 1.03 KB
/
graph.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
#include <ctime>
#include "graph.h"
void readGraph(Graph &G, int argc, char **argv) {
int n;
int m;
//If no arguments then read graph from stdin
bool fromStdin = argc <= 2;
if (fromStdin) {
scanf("%d %d", &n, &m);
} else {
srand(12345);
n = atoi(argv[2]);
m = atoi(argv[3]);
}
std::vector<std::vector<int> > adjecancyLists(n);
for (int i = 0; i < m; i++) {
int u, v;
if (fromStdin) {
scanf("%d %d", &u, &v);
adjecancyLists[u].push_back(v);
} else {
u = rand() % n;
v = rand() % n;
adjecancyLists[u].push_back(v);
adjecancyLists[v].push_back(u);
}
}
for (int i = 0; i < n; i++) {
G.edgesOffset.push_back(G.adjacencyList.size());
G.edgesSize.push_back(adjecancyLists[i].size());
for (auto &edge: adjecancyLists[i]) {
G.adjacencyList.push_back(edge);
}
}
G.numVertices = n;
G.numEdges = G.adjacencyList.size();
}