-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_graph_from_mtx.cpp
More file actions
67 lines (60 loc) · 2.01 KB
/
generate_graph_from_mtx.cpp
File metadata and controls
67 lines (60 loc) · 2.01 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
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
string filename;
filename = argv[1];
int flag = atoi(argv[2]);
ifstream fin(filename.c_str());
while (fin.peek() == '%') fin.ignore(2048, '\n');
long M, N, L;
long vertices = 0, edges = 0;
set<pair<long, long> > edges_set;
fin >> M >> N >> L;
vector<vector<long> > adjacency_list(M, vector<long>(0));
long max_degree = 0;
cout << "Num vertices: " << M << endl;
for (int i = 0; i < L; i++) {
long a, b;
double d;
fin >> a >> b;
if (flag == 0) fin >> d;
a -= 1;
b -= 1;
if (a == b) continue;
if (edges_set.find(make_pair(a, b)) != edges_set.end() || edges_set.find(make_pair(b, a)) != edges_set.end()) {
continue;
} else {
edges_set.insert(make_pair(a, b));
}
adjacency_list[a].push_back(b);
adjacency_list[b].push_back(a);
if (adjacency_list[a].size() > max_degree) {
max_degree = adjacency_list[a].size();
}
if (adjacency_list[b].size() > max_degree) {
max_degree = adjacency_list[b].size();
}
edges++;
}
vertices = M;
string new_filename = filename.substr(0, filename.length()-4);
new_filename += "_graph";
ofstream fout(new_filename.c_str());
fflush(stdin);
fflush(stdout);
fout << max_degree << "\n";
fout << vertices << "\n";
for (long i = 0; i < M; i++) {
fout << adjacency_list[i].size();
for (long j = 0; j < adjacency_list[i].size(); j++) {
fout << " ";
fout << adjacency_list[i][j];
}
fout << "\n";
}
fout.close();
cout << "Format of the graph generated: " <<endl << "MAX DEGREE "<<endl << "NUM_VERTICES" <<endl ;
cout << "Adjacency List Size for vertex 0<space>Adjacency list for vertex 0" <<endl<<"....."<<endl <<endl;
cout << "Please note: The graph generated is an undirected graph" << endl;
return 0;
}