diff --git a/cpp/graph.hpp b/cpp/graph.hpp index 6585dbd12d79407616158c9bd3d7108ed08eef51..13f997a413f3e565161162a31b8808f43b32d99d 100644 --- a/cpp/graph.hpp +++ b/cpp/graph.hpp @@ -145,7 +145,8 @@ class Graph { // There are two possible edge exchanges // switchType indicates which one is desired // Returns false if the switch is not possible - bool exchangeEdges(unsigned int e1index, unsigned int e2index, bool switchType) { + bool exchangeEdges(unsigned int e1index, unsigned int e2index, + bool switchType, bool trackTriangles = false) { StoredEdge &se1 = edges[e1index]; StoredEdge &se2 = edges[e2index]; const Edge &e1 = se1.e; @@ -165,6 +166,11 @@ class Graph { if (hasEdge({e1.u, e2.u}) || hasEdge({e1.v, e2.v})) return false; // conflicting edges + if (trackTriangles) { + trackedTriangles -= countTriangles(e1); + trackedTriangles -= countTriangles(e2); + } + // Clear old edges badj[e1.u][e1.v] = false; badj[e1.v][e1.u] = false; @@ -183,11 +189,30 @@ class Graph { badj[e1.v][e1.u] = true; badj[e2.u][e2.v] = true; badj[e2.v][e2.u] = true; + + if (trackTriangles) { + trackedTriangles += countTriangles(e1); + trackedTriangles += countTriangles(e2); + } + return true; } - int countTriangles() const { - int triangles = 0; + // Assumes edge exists + // Used for computing triangle-delta's after switch move + unsigned int countTriangles(Edge e) const { + auto triangles = 0u; + if (adj[e.u].size() > adj[e.v].size()) + std::swap(e.u, e.v); + for (auto w : adj[e.u]) { + if (hasEdge({w, e.v})) + ++triangles; + } + return triangles; + } + + unsigned int countTriangles() const { + auto triangles = 0u; for (auto& v : adj) { for (unsigned int i = 0; i < v.size(); ++i) { for (unsigned int j = i + 1; j < v.size(); ++j) { @@ -201,6 +226,8 @@ class Graph { return triangles / 3; } + unsigned int& getTrackedTriangles() { return trackedTriangles; } + // Should return zero int consistencyCheck() const { // Check if info in 'edges' is present @@ -254,5 +281,6 @@ class Graph { std::vector> adj; std::vector> badj; // symmetric binary matrix std::vector edges; + unsigned int trackedTriangles; };