diff --git a/cpp/graph.hpp b/cpp/graph.hpp index a6c5ea22dffe491607b800b87a7d540be013d5ea..f17e7a43d51d97356827405ba26fbfd48d1ebf2b 100644 --- a/cpp/graph.hpp +++ b/cpp/graph.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -35,7 +36,6 @@ class Graph { unsigned int edgeCount() const { return edges.size(); } - Edge &getEdge(unsigned int i) { return edges[i]; } const Edge &getEdge(unsigned int i) const { return edges[i]; } bool createFromDegreeSequence(const DegreeSequence &d) { @@ -178,6 +178,21 @@ class Graph { return true; } + int countTriangles() const { + int triangles = 0; + for (auto& v : adj) { + for (unsigned int i = 0; i < v.size(); ++i) { + for (unsigned int j = i + 1; j < v.size(); ++j) { + if (hasEdge({v[i], v[j]})) { + ++triangles; + } + } + } + } + assert(triangles % 3 == 0); + return triangles / 3; + } + private: // Graph is saved in two formats for speed // The two should be kept consistent at all times