diff --git a/algorithm/breadth_first_search.h b/algorithm/breadth_first_search.h index de71d0e..6309283 100644 --- a/algorithm/breadth_first_search.h +++ b/algorithm/breadth_first_search.h @@ -14,24 +14,24 @@ class BreadthFirstSearch { public: BreadthFirstSearch() = default; - BreadthFirstSearch(std::map> adjList) + BreadthFirstSearch(std::unordered_map> adjList) : adjList(adjList) {} // Traverse whole graph using the BFS search, and save the tree graph // which is created when visiting new vertices (Breadth First Tree) - std::map> execute(const T& root); + std::unordered_map> execute(const T& root); // Search if target vertex exists in graph bool query(const T& root, const T& target); private: // Represents the graph on which the algorithm will be executed - std::map> adjList; + std::unordered_map> adjList; }; template -std::map> BreadthFirstSearch::execute(const T& root) { - std::map> tree; - std::map visited; +std::unordered_map> BreadthFirstSearch::execute(const T& root) { + std::unordered_map> tree; + std::unordered_map visited; std::queue Q; Q.push(root); visited[root] = true; @@ -54,7 +54,7 @@ std::map> BreadthFirstSearch::execute(const T& root) { template bool BreadthFirstSearch::query(const T& root, const T& target) { - std::map visited; + std::unordered_map visited; std::queue Q; Q.push(root); visited[root] = true; diff --git a/algorithm/frigioni.h b/algorithm/frigioni.h index 86e3bc5..fdd0e44 100644 --- a/algorithm/frigioni.h +++ b/algorithm/frigioni.h @@ -31,10 +31,10 @@ public: void remove(const std::vector>& edges); private: // Transitive closure matrix, used to answer reachability queries in O(1) - std::map> TC; + std::unordered_map> TC; // For each SCC, store a reachability tree created as a BFS tree - std::map> RT; + std::unordered_map> RT; // For each SCC, store collections of incoming, outgoing and internal edges struct Edges { @@ -42,7 +42,7 @@ private: std::set> inc; std::set> out; }; - std::map E; + std::unordered_map E; // Decremental maintenance of strongly connected components RodittyZwick rodittyZwick; @@ -101,7 +101,7 @@ void Frigioni::remove(const std::vector>& edges) { Eext.push_back({ u, v }); } - std::map> H; + std::unordered_map> H; for (const auto& [u, v] : Eint) { this->G.remove(u, v); rodittyZwick.remove(u, v); diff --git a/algorithm/henzinger_king.h b/algorithm/henzinger_king.h index c2c8603..aa9516e 100644 --- a/algorithm/henzinger_king.h +++ b/algorithm/henzinger_king.h @@ -47,8 +47,8 @@ private: std::set S; // Maintain in-out bfs trees - std::map> In; - std::map> Out; + std::unordered_map> In; + std::unordered_map> Out; }; template diff --git a/algorithm/italiano.h b/algorithm/italiano.h index 0eb4917..ab1b40b 100644 --- a/algorithm/italiano.h +++ b/algorithm/italiano.h @@ -28,20 +28,20 @@ public: void remove(const T& u, const T& v) override; private: // Transitive closure matrix - std::map> TC; - + std::unordered_map> TC; + // For each vertex, store a reachability tree created as a BFS tree - std::map> RT; + std::unordered_map> RT; // For each vertex, store collections of its incoming and outgoing edges struct Edges { std::set inc; std::set out; }; - std::map E; + std::unordered_map E; // Repair reachability trees after edge deletions - void repairTrees(std::map>& H); + void repairTrees(std::unordered_map>& H); }; template @@ -68,7 +68,7 @@ template void Italiano::remove(const T& u, const T& v) { if (!this->G.adjList[u].contains(v)) return; - std::map> H; + std::unordered_map> H; for (const auto& w : this->G.vertices()) { if (RT[w].contains(u, v)) { if (E[v].inc.size() > 1) @@ -89,7 +89,7 @@ void Italiano::remove(const T& u, const T& v) { } template -void Italiano::repairTrees(std::map>& H) { +void Italiano::repairTrees(std::unordered_map>& H) { for (const auto& z : this->G.vertices()) { while (H[z].size() > 0) { const auto& h = H[z].top(); diff --git a/algorithm/king.h b/algorithm/king.h index d23991b..a41286a 100644 --- a/algorithm/king.h +++ b/algorithm/king.h @@ -31,8 +31,8 @@ public: void insert(const T& u, const T& v) override; private: // Connect each reachabiliy tree with decremental maintenance data structure - std::map> In; - std::map> Out; + std::unordered_map> In; + std::unordered_map> Out; }; template diff --git a/algorithm/roditty_zwick.h b/algorithm/roditty_zwick.h index 40a8968..aac52b1 100644 --- a/algorithm/roditty_zwick.h +++ b/algorithm/roditty_zwick.h @@ -28,17 +28,17 @@ public: // Remove edge (u,v) and update A accordingly for fast checking query void remove(const T& u, const T& v) override; - std::map> getSCCs() { return C; } + std::unordered_map> getSCCs() { return C; } private: // Array used to answer strong connectivity queries in O(1) time - std::map A; + std::unordered_map A; // Connect each representative with its SCC - std::map> C; + std::unordered_map> C; // Maintain in-out bfs trees - std::map> In; - std::map> Out; + std::unordered_map> In; + std::unordered_map> Out; }; template diff --git a/algorithm/tarjan.h b/algorithm/tarjan.h index 29a479e..018a8c8 100644 --- a/algorithm/tarjan.h +++ b/algorithm/tarjan.h @@ -16,7 +16,7 @@ class Tarjan { public: Tarjan() = default; - Tarjan(std::map> adjList) : adjList(adjList) {} + Tarjan(std::unordered_map> adjList) : adjList(adjList) {} // auto execute(); @@ -24,7 +24,7 @@ public: // void strongConnect(const T& u); private: - std::map> adjList; + std::unordered_map> adjList; std::stack S; std::int16_t index = 0; std::vector> SCCs; @@ -35,7 +35,7 @@ private: int lowlink = -1; bool onStack = false; }; - std::map vmap; + std::unordered_map vmap; }; template @@ -55,7 +55,7 @@ void Tarjan::strongConnect(const T& u) { // If u is a root node, pop the stack and generate an SCC if (vmap[u].lowlink == vmap[u].index) { - std::map> scc; + std::unordered_map> scc; bool finished = false; cid = S.top(); diff --git a/graph/breadth_first_tree.h b/graph/breadth_first_tree.h index 1262400..f77be54 100644 --- a/graph/breadth_first_tree.h +++ b/graph/breadth_first_tree.h @@ -10,7 +10,7 @@ class BreadthFirstTree : public Digraph { public: BreadthFirstTree() = default; - BreadthFirstTree(std::map> G, T root) + BreadthFirstTree(std::unordered_map> G, T root) : BreadthFirstTree(Digraph(G), root) {} BreadthFirstTree(Digraph G, T root); diff --git a/graph/digraph.h b/graph/digraph.h index b2f76b0..4c2689e 100644 --- a/graph/digraph.h +++ b/graph/digraph.h @@ -11,7 +11,7 @@ class Digraph : public Graph { public: Digraph() = default; - Digraph(std::map> G); + Digraph(std::unordered_map> G); // Return true if there is a path from u to v bool contains(const T& u, const T& v); @@ -27,7 +27,7 @@ public: }; template -Digraph::Digraph(std::map> G) { +Digraph::Digraph(std::unordered_map> G) { this->adjList = G; } @@ -49,7 +49,7 @@ void Digraph::remove(const T& u, const T& v) { template auto Digraph::reverse() { - std::map> revMatrix; + std::unordered_map> revMatrix; for (const auto& u : this->vertices()) { for (const auto& v : this->adjList[u]) { diff --git a/graph/graph.h b/graph/graph.h index 29546f8..3968c8d 100644 --- a/graph/graph.h +++ b/graph/graph.h @@ -1,8 +1,8 @@ #ifndef GRAPH_H_ #define GRAPH_H_ -#include -#include +#include +#include #include #include @@ -36,7 +36,7 @@ public: std::uint16_t E(); // Adjacency matrix representation - std::map> adjList; + std::unordered_map> adjList; friend std::ostream& operator<<<>(std::ostream& os, Graph& G); }; diff --git a/graph/scc.h b/graph/scc.h index a228726..8d51af1 100644 --- a/graph/scc.h +++ b/graph/scc.h @@ -13,7 +13,8 @@ class SCC : public Digraph { public: SCC() = default; - SCC(std::map> G, T id) : Digraph(G), id(id) { normalize(); } + SCC(std::unordered_map> G, T id) + : Digraph(G), id(id) { normalize(); } SCC(Digraph G, T id) : id(id) { normalize(); } diff --git a/test/algorithm_test.cc b/test/algorithm_test.cc index aee7fa1..7079bb5 100644 --- a/test/algorithm_test.cc +++ b/test/algorithm_test.cc @@ -109,7 +109,8 @@ TEST_SUITE("Algorithm") { auto tree = algo::BreadthFirstSearch(G.adjList).execute(1); - std::map> expected = { + std::unordered_map> expected = { {1, {2}}, {2, {3, 6}}, {3, {4}}, diff --git a/test/graph_test.cc b/test/graph_test.cc index f512cb7..f0a421a 100644 --- a/test/graph_test.cc +++ b/test/graph_test.cc @@ -39,7 +39,8 @@ TEST_SUITE("Graph") { auto reverse = G.reverse(); - std::map> expected = { + std::unordered_map> expected = { {1, {3}}, {2, {1}}, {3, {2, 5}},