From fe3bb68c183ab386b98758e6f388a45bf7defa2b Mon Sep 17 00:00:00 2001 From: stefiosif Date: Wed, 20 Jul 2022 23:43:15 +0300 Subject: [PATCH] Add single vertex contains method and replace vertices field with adjMatrix' keys --- algorithm/breadth_first_search.h | 4 ++-- algorithm/decremental_scc.h | 4 ++-- algorithm/tarjan.h | 6 +++--- graph/digraph.h | 2 -- graph/graph.h | 24 ++++++++++++++---------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/algorithm/breadth_first_search.h b/algorithm/breadth_first_search.h index 8bdb4b5..08b905b 100644 --- a/algorithm/breadth_first_search.h +++ b/algorithm/breadth_first_search.h @@ -27,8 +27,8 @@ private: template std::map BreadthFirstSearch::initExplore() { std::map graphExplore; - for (const auto& v : G.vertices) { - graphExplore[v] = false; + for (const auto& v : G.adjMatrix) { + graphExplore[v.first] = false; } return graphExplore; } diff --git a/algorithm/decremental_scc.h b/algorithm/decremental_scc.h index c8674ca..1ab6234 100644 --- a/algorithm/decremental_scc.h +++ b/algorithm/decremental_scc.h @@ -49,8 +49,8 @@ void DecrementalSCC::findSCC() { for (auto& C : SCCs) { const auto& w = C.representative(); - for (const auto& v : C.vertices) - A[v] = w; + for (const auto& v : C.adjMatrix) + A[v.first] = w; outTree[w] = BreadthFirstTree(BreadthFirstSearch(C).execute(w)); diff --git a/algorithm/tarjan.h b/algorithm/tarjan.h index fd18387..1817bba 100644 --- a/algorithm/tarjan.h +++ b/algorithm/tarjan.h @@ -66,9 +66,9 @@ void Tarjan::strongConnect(const T& v) { template std::vector> Tarjan::execute() { - for (const auto& v : G.vertices) { - if (p[v].index == -1) { - strongConnect(v); + for (auto& v : G.adjMatrix) { + if (p[v.first].index == -1) { + strongConnect(v.first); } } diff --git a/graph/digraph.h b/graph/digraph.h index bdf5769..c10f1b6 100644 --- a/graph/digraph.h +++ b/graph/digraph.h @@ -22,8 +22,6 @@ public: template Digraph::Digraph(std::map> digraph) { Graph::adjMatrix = digraph; - auto kv = std::views::keys(Graph::adjMatrix); - Graph::vertices = std::set{ kv.begin(), kv.end() }; } template diff --git a/graph/graph.h b/graph/graph.h index ed81c23..7e2bae6 100644 --- a/graph/graph.h +++ b/graph/graph.h @@ -18,6 +18,9 @@ public: // Return true if e(u,v) exists virtual bool contains(const T& u, const T& v); + // Return true if vertex u exists + virtual bool contains(const T& u); + // Remove edge e(u,v) virtual void remove(const T& u, const T& v); @@ -31,20 +34,16 @@ public: virtual void output(); // Adjacency matrix representation - std::set vertices; std::map> adjMatrix; }; template Graph::~Graph() { - vertices.clear(); adjMatrix.clear(); } template inline void Graph::insert(const T& u, const T& v) { - Graph::vertices.insert(u); - Graph::vertices.insert(v); Graph::adjMatrix[u].insert(v); } @@ -53,6 +52,11 @@ bool Graph::contains(const T& u, const T& v) { return adjMatrix[u].contains(v); } +template +bool Graph::contains(const T& u) { + return adjMatrix.contains(u); +} + template void Graph::remove(const T& u, const T& v) { adjMatrix[u].erase(v); @@ -60,23 +64,23 @@ void Graph::remove(const T& u, const T& v) { template std::uint16_t Graph::V() { - return static_cast(vertices.size()); + return static_cast(adjMatrix.size()); } template std::uint16_t Graph::E() { std::uint16_t edges = 0; - for (const auto& v : vertices) { - edges += static_cast(adjMatrix[v].size()); + for (const auto& v : adjMatrix) { + edges += static_cast(v.second.size()); } return static_cast(edges); } template void Graph::output() { - for (const auto& v : vertices) { - for (const auto& u : adjMatrix[v]) { - std::cout << v << "->" << u << '|'; + for (const auto& v : adjMatrix) { + for (const auto& u : v.second) { + std::cout << v.first << "->" << u << '|'; } std::cout << '\n'; }