Refactor: Rename adjMatrix to adjList
This commit is contained in:
@@ -14,8 +14,8 @@ class BreadthFirstSearch {
|
||||
public:
|
||||
BreadthFirstSearch() = default;
|
||||
|
||||
BreadthFirstSearch(std::map<T, std::set<T>> adjMatrix)
|
||||
: adjMatrix(adjMatrix) {}
|
||||
BreadthFirstSearch(std::map<T, std::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)
|
||||
@@ -25,7 +25,7 @@ public:
|
||||
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>> adjMatrix;
|
||||
std::map<T, std::set<T>> adjList;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@@ -40,7 +40,7 @@ std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
for (const auto& u : adjMatrix[v]) {
|
||||
for (const auto& u : adjList[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
tree[v].insert(u);
|
||||
@@ -65,7 +65,7 @@ bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
|
||||
if (v == target) return true;
|
||||
|
||||
for (const auto& u : adjMatrix[v]) {
|
||||
for (const auto& u : adjList[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
Q.push(u);
|
||||
|
||||
@@ -50,14 +50,14 @@ private:
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::init() {
|
||||
auto SCCs = Tarjan<T>(this->G.adjMatrix).execute();
|
||||
auto SCCs = Tarjan<T>(this->G.adjList).execute();
|
||||
rodittyZwick = RodittyZwick<T>(this->G);
|
||||
rodittyZwick.init();
|
||||
|
||||
for (auto& scc : SCCs) {
|
||||
RT[scc.id] = BreadthFirstTree<T>(this->G, scc.id);
|
||||
for (const auto& u : this->G.vertices()) {
|
||||
for (const auto& v : this->G.adjMatrix[u]) {
|
||||
for (const auto& v : this->G.adjList[u]) {
|
||||
if (scc.member(u)) {
|
||||
if (scc.member(v))
|
||||
E[scc.id].in.insert(std::make_pair(u, v));
|
||||
@@ -126,7 +126,7 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
for (const auto& c : E[v].out)
|
||||
H[id].push(C[c.second].id);
|
||||
}
|
||||
RT[id].adjMatrix[u].erase(v);
|
||||
RT[id].adjList[u].erase(v);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
for (const auto& c : E[v].out)
|
||||
H[id].push(C[c.second].id);
|
||||
}
|
||||
RT[id].adjMatrix[u].erase(v);
|
||||
RT[id].adjList[u].erase(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -178,7 +178,7 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
|
||||
for (const auto& [u, v] : E[h].inc) {
|
||||
if (RT[id].contains(id, u)) {
|
||||
RT[id].adjMatrix[u].insert(h);
|
||||
RT[id].adjList[u].insert(h);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -65,8 +65,8 @@ bool HenzingerKing<T>::query(const T& u, const T& v) {
|
||||
|
||||
return std::any_of(S.begin(), S.end(),
|
||||
[&](const T& w) {
|
||||
return In[w].adjMatrix.contains(u) &&
|
||||
Out[w].adjMatrix.contains(v);
|
||||
return In[w].adjList.contains(u) &&
|
||||
Out[w].adjList.contains(v);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ private:
|
||||
template<typename T>
|
||||
void Italiano<T>::init() {
|
||||
for (const auto& u : this->G.vertices()) {
|
||||
for (const auto& v : this->G.adjMatrix[u]) {
|
||||
for (const auto& v : this->G.adjList[u]) {
|
||||
E[v].inc.insert(u);
|
||||
E[u].out.insert(v);
|
||||
TC[u][v] = false;
|
||||
@@ -66,7 +66,7 @@ bool Italiano<T>::query(const T& u, const T& v) {
|
||||
|
||||
template<typename T>
|
||||
void Italiano<T>::remove(const T& u, const T& v) {
|
||||
if (!this->G.adjMatrix[u].contains(v)) return;
|
||||
if (!this->G.adjList[u].contains(v)) return;
|
||||
|
||||
std::map<T, std::stack<T>> H;
|
||||
for (const auto& w : this->G.vertices()) {
|
||||
@@ -78,7 +78,7 @@ void Italiano<T>::remove(const T& u, const T& v) {
|
||||
for (const auto& c : E[v].out)
|
||||
H[w].push(c);
|
||||
}
|
||||
RT[w].adjMatrix[u].erase(v);
|
||||
RT[w].adjList[u].erase(v);
|
||||
}
|
||||
}
|
||||
E[u].out.erase(v);
|
||||
@@ -97,7 +97,7 @@ void Italiano<T>::repairTrees(std::map<T, std::stack<T>>& H) {
|
||||
|
||||
for (const auto& i : E[h].inc) {
|
||||
if (RT[z].contains(z, i)) {
|
||||
RT[z].adjMatrix[i].insert(h);
|
||||
RT[z].adjList[i].insert(h);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ void RodittyZwick<T>::init() {
|
||||
|
||||
template<typename T>
|
||||
void RodittyZwick<T>::findSCC() {
|
||||
auto SCCs = Tarjan<T>(this->G.adjMatrix).execute();
|
||||
auto SCCs = Tarjan<T>(this->G.adjList).execute();
|
||||
|
||||
for (auto& SCC : SCCs) {
|
||||
const auto& w = SCC.id;
|
||||
@@ -78,8 +78,8 @@ void RodittyZwick<T>::remove(const T& u, const T& v) {
|
||||
if (A[u] != A[v]) return;
|
||||
|
||||
// If edge (u,v) is not contained in both inTree and outTree do nothing
|
||||
if (!In[w].adjMatrix[u].contains(v) &&
|
||||
!Out[w].adjMatrix[u].contains(v))
|
||||
if (!In[w].adjList[u].contains(v) &&
|
||||
!Out[w].adjList[u].contains(v))
|
||||
return;
|
||||
|
||||
// Update In(w) and Out(w)
|
||||
@@ -87,7 +87,7 @@ void RodittyZwick<T>::remove(const T& u, const T& v) {
|
||||
In[w] = BreadthFirstTree<T>(C[w].reverse(), w);
|
||||
|
||||
// If a SCC is broken, compute all SCCs again
|
||||
if (!In[w].adjMatrix.count(u) || !Out[w].adjMatrix.count(v))
|
||||
if (!In[w].adjList.count(u) || !Out[w].adjList.count(v))
|
||||
findSCC();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class Tarjan {
|
||||
public:
|
||||
Tarjan() = default;
|
||||
|
||||
Tarjan(std::map<T, std::set<T>> adjMatrix) : adjMatrix(adjMatrix) {}
|
||||
Tarjan(std::map<T, std::set<T>> adjList) : adjList(adjList) {}
|
||||
|
||||
//
|
||||
auto execute();
|
||||
@@ -24,7 +24,7 @@ public:
|
||||
//
|
||||
void strongConnect(const T& u);
|
||||
private:
|
||||
std::map<T, std::set<T>> adjMatrix;
|
||||
std::map<T, std::set<T>> adjList;
|
||||
std::stack<T> S;
|
||||
std::int16_t index = 0;
|
||||
std::vector<SCC<T>> SCCs;
|
||||
@@ -44,7 +44,7 @@ void Tarjan<T>::strongConnect(const T& u) {
|
||||
S.push(u);
|
||||
vmap[u].onStack = true;
|
||||
|
||||
for (const auto& w : adjMatrix[u]) {
|
||||
for (const auto& w : adjList[u]) {
|
||||
if (vmap[w].index == -1) {
|
||||
strongConnect(w);
|
||||
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].lowlink);
|
||||
@@ -63,7 +63,7 @@ void Tarjan<T>::strongConnect(const T& u) {
|
||||
const auto w = S.top();
|
||||
S.pop();
|
||||
vmap[w].onStack = false;
|
||||
scc[w] = adjMatrix[w];
|
||||
scc[w] = adjList[w];
|
||||
finished = (w == u);
|
||||
} while (!finished);
|
||||
|
||||
@@ -73,7 +73,7 @@ void Tarjan<T>::strongConnect(const T& u) {
|
||||
|
||||
template<typename T>
|
||||
auto Tarjan<T>::execute() {
|
||||
for (const auto& u : std::views::keys(adjMatrix)) {
|
||||
for (const auto& u : std::views::keys(adjList)) {
|
||||
if (vmap[u].index == -1)
|
||||
strongConnect(u);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user