-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_algorithm_cpu.cpp
62 lines (50 loc) · 2.11 KB
/
naive_algorithm_cpu.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
//Written by Hakan Uskan
//C++ code that calculates the number of triangles in any graph on the CPU.
//It works by adjacency matrix representation of a graph.
#include <iostream>
#include <vector>
#include <chrono>
// Function to check if three nodes form a triangle
bool is_triangle(const std::vector<std::vector<bool>>& graph, int a, int b, int c) {
return graph[a][b] && graph[b][c] && graph[c][a];
}
// Function to count the number of triangles in the graph
int count_triangles(const std::vector<std::vector<bool>>& graph) {
int count = 0;
int n = graph.size(); // Number of vertices in the graph
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
if (is_triangle(graph, i, j, k)) {
++count;
}
}
}
}
return count;
}
int main() {
auto start = std::chrono::high_resolution_clock::now(); // Start timing
// Example graph represented as an adjacency matrix
std::vector<std::vector<bool>> graph = {
{0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0}
};
std::cout << "Number of triangles: " << count_triangles(graph) << std::endl;
auto end = std::chrono::high_resolution_clock::now(); // End timing
std::cout<<"This calculation took '"<<std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count()<<"' milliseconds."<<std::endl;
return 0;
}