Refactor: Replace std::map and std::set with unordered versions
This commit is contained in:
@@ -14,24 +14,24 @@ class BreadthFirstSearch {
|
||||
public:
|
||||
BreadthFirstSearch() = default;
|
||||
|
||||
BreadthFirstSearch(std::map<T, std::set<T>> adjList)
|
||||
BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> 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<T, std::set<T>> execute(const T& root);
|
||||
std::unordered_map<T, std::unordered_set<T>> 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<T, std::set<T>> adjList;
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||
std::map<T, std::set<T>> tree;
|
||||
std::map<T, bool> visited;
|
||||
std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||
std::unordered_map<T, std::unordered_set<T>> tree;
|
||||
std::unordered_map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
Q.push(root);
|
||||
visited[root] = true;
|
||||
@@ -54,7 +54,7 @@ std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||
|
||||
template<typename T>
|
||||
bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
std::map<T, bool> visited;
|
||||
std::unordered_map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
Q.push(root);
|
||||
visited[root] = true;
|
||||
|
||||
@@ -31,10 +31,10 @@ public:
|
||||
void remove(const std::vector<std::pair<T, T>>& edges);
|
||||
private:
|
||||
// Transitive closure matrix, used to answer reachability queries in O(1)
|
||||
std::map<T, std::map<T, bool>> TC;
|
||||
std::unordered_map<T, std::unordered_map<T, bool>> TC;
|
||||
|
||||
// For each SCC, store a reachability tree created as a BFS tree
|
||||
std::map<T, BreadthFirstTree<T>> RT;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> RT;
|
||||
|
||||
// For each SCC, store collections of incoming, outgoing and internal edges
|
||||
struct Edges {
|
||||
@@ -42,7 +42,7 @@ private:
|
||||
std::set<std::pair<T, T>> inc;
|
||||
std::set<std::pair<T, T>> out;
|
||||
};
|
||||
std::map<T, Edges> E;
|
||||
std::unordered_map<T, Edges> E;
|
||||
|
||||
// Decremental maintenance of strongly connected components
|
||||
RodittyZwick<T> rodittyZwick;
|
||||
@@ -101,7 +101,7 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
Eext.push_back({ u, v });
|
||||
}
|
||||
|
||||
std::map<T, std::stack<T>> H;
|
||||
std::unordered_map<T, std::stack<T>> H;
|
||||
for (const auto& [u, v] : Eint) {
|
||||
this->G.remove(u, v);
|
||||
rodittyZwick.remove(u, v);
|
||||
|
||||
@@ -47,8 +47,8 @@ private:
|
||||
std::set<T> S;
|
||||
|
||||
// Maintain in-out bfs trees
|
||||
std::map<T, BreadthFirstTree<T>> In;
|
||||
std::map<T, BreadthFirstTree<T>> Out;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> In;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -28,20 +28,20 @@ public:
|
||||
void remove(const T& u, const T& v) override;
|
||||
private:
|
||||
// Transitive closure matrix
|
||||
std::map<T, std::map<T, bool>> TC;
|
||||
|
||||
std::unordered_map<T, std::unordered_map<T, bool>> TC;
|
||||
|
||||
// For each vertex, store a reachability tree created as a BFS tree
|
||||
std::map<T, BreadthFirstTree<T>> RT;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> RT;
|
||||
|
||||
// For each vertex, store collections of its incoming and outgoing edges
|
||||
struct Edges {
|
||||
std::set<T> inc;
|
||||
std::set<T> out;
|
||||
};
|
||||
std::map<T, Edges> E;
|
||||
std::unordered_map<T, Edges> E;
|
||||
|
||||
// Repair reachability trees after edge deletions
|
||||
void repairTrees(std::map<T, std::stack<T>>& H);
|
||||
void repairTrees(std::unordered_map<T, std::stack<T>>& H);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@@ -68,7 +68,7 @@ template<typename T>
|
||||
void Italiano<T>::remove(const T& u, const T& v) {
|
||||
if (!this->G.adjList[u].contains(v)) return;
|
||||
|
||||
std::map<T, std::stack<T>> H;
|
||||
std::unordered_map<T, std::stack<T>> 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<T>::remove(const T& u, const T& v) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Italiano<T>::repairTrees(std::map<T, std::stack<T>>& H) {
|
||||
void Italiano<T>::repairTrees(std::unordered_map<T, std::stack<T>>& H) {
|
||||
for (const auto& z : this->G.vertices()) {
|
||||
while (H[z].size() > 0) {
|
||||
const auto& h = H[z].top();
|
||||
|
||||
@@ -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<T, Italiano<T>> In;
|
||||
std::map<T, Italiano<T>> Out;
|
||||
std::unordered_map<T, Italiano<T>> In;
|
||||
std::unordered_map<T, Italiano<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -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<T, SCC<T>> getSCCs() { return C; }
|
||||
std::unordered_map<T, SCC<T>> getSCCs() { return C; }
|
||||
private:
|
||||
// Array used to answer strong connectivity queries in O(1) time
|
||||
std::map<T, T> A;
|
||||
std::unordered_map<T, T> A;
|
||||
|
||||
// Connect each representative with its SCC
|
||||
std::map<T, SCC<T>> C;
|
||||
std::unordered_map<T, SCC<T>> C;
|
||||
|
||||
// Maintain in-out bfs trees
|
||||
std::map<T, BreadthFirstTree<T>> In;
|
||||
std::map<T, BreadthFirstTree<T>> Out;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> In;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -16,7 +16,7 @@ class Tarjan {
|
||||
public:
|
||||
Tarjan() = default;
|
||||
|
||||
Tarjan(std::map<T, std::set<T>> adjList) : adjList(adjList) {}
|
||||
Tarjan(std::unordered_map<T, std::unordered_set<T>> adjList) : adjList(adjList) {}
|
||||
|
||||
//
|
||||
auto execute();
|
||||
@@ -24,7 +24,7 @@ public:
|
||||
//
|
||||
void strongConnect(const T& u);
|
||||
private:
|
||||
std::map<T, std::set<T>> adjList;
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||
std::stack<T> S;
|
||||
std::int16_t index = 0;
|
||||
std::vector<SCC<T>> SCCs;
|
||||
@@ -35,7 +35,7 @@ private:
|
||||
int lowlink = -1;
|
||||
bool onStack = false;
|
||||
};
|
||||
std::map<T, Vertex> vmap;
|
||||
std::unordered_map<T, Vertex> vmap;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@@ -55,7 +55,7 @@ void Tarjan<T>::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<T, std::set<T>> scc;
|
||||
std::unordered_map<T, std::unordered_set<T>> scc;
|
||||
bool finished = false;
|
||||
cid = S.top();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user