-
Notifications
You must be signed in to change notification settings - Fork 0
/
edgeIterator_forward.cpp
65 lines (57 loc) · 1.4 KB
/
edgeIterator_forward.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
#include <bits/stdc++.h>
#include "timer.h"
using namespace std;
#define ll long long
int vertices,edges;
vector<vector<int> > edge;
ll numTri() {
ll common = 0;
for(int i =0; i< vertices ; i++){
// #pragma omp parallel for reduction(+:common)
for(int j = 0;j < edge[i].size();j++) {
int p = 0,q = 0,r = edge[i].size(),s = edge[edge[i][j]].size();
while(p < r and q < s) {
if(edge[i][p] == edge[edge[i][j]][q]) {
p++; q++; common++;
}
else if(edge[i][p] < edge[edge[i][j]][q]) p++;
else q++;
}
}
}
return common;
}
int main(){
double start, end;
scanf("%d %d", &vertices, &edges);
for(int i=0;i<vertices;i++) {
vector<int> temp;
edge.push_back(temp);
}
printf("\nReading Input File\n\n");
GET_TIME(start);
for (int i = 0 ; i < edges ; i++){
int node1,node2;
scanf("%d %d", &node1, &node2 );
if(node1 < node2){
edge[node1].push_back(node2);
}
else{
edge[node2].push_back(node1);
}
}
GET_TIME(end);
printf("Reading Input and adding it to data structure took %e seconds\n\n", end - start);
printf("Sorting\n");
GET_TIME(start);
for(int i = 0; i < vertices ; i++){
sort(edge[i].begin(),edge[i].end());
}
GET_TIME(end);
printf("Sorting took %e seconds\n\n", end - start);
printf("Computing Triangles\n" );
GET_TIME(start);
cout << numTri() << "\n";
GET_TIME(end);
cout << "Time taken for computation = "<< end - start <<" seconds" << endl;
}