Improve readability
This commit is contained in:
@@ -14,7 +14,7 @@ class BreadthFirstSearch {
|
|||||||
public:
|
public:
|
||||||
BreadthFirstSearch() = default;
|
BreadthFirstSearch() = default;
|
||||||
|
|
||||||
BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> adjList)
|
explicit BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> adjList)
|
||||||
: adjList(adjList) {}
|
: adjList(adjList) {}
|
||||||
|
|
||||||
// Traverse whole graph using the BFS search, and save the tree graph
|
// Traverse whole graph using the BFS search, and save the tree graph
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ private:
|
|||||||
std::unordered_map<T, std::unordered_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
|
// For each SCC, store a reachability tree created as a BFS tree
|
||||||
std::unordered_map<T, BreadthFirstTree<T>> RT;
|
std::unordered_map<SCC<T>, BreadthFirstTree<T>, HashSCC<T>> RT;
|
||||||
|
|
||||||
// For each SCC, store collections of incoming, outgoing and internal edges
|
// For each SCC, store collections of incoming, outgoing and internal edges
|
||||||
struct Edges {
|
struct Edges {
|
||||||
@@ -37,37 +37,50 @@ private:
|
|||||||
std::set<std::pair<T, T>> inc;
|
std::set<std::pair<T, T>> inc;
|
||||||
std::set<std::pair<T, T>> out;
|
std::set<std::pair<T, T>> out;
|
||||||
};
|
};
|
||||||
std::unordered_map<T, Edges> E;
|
std::unordered_map<SCC<T>, Edges, HashSCC<T>> E;
|
||||||
|
|
||||||
|
// Structure that keeps candidate components to use as a hook
|
||||||
|
std::unordered_map<SCC<T>, std::stack<T>, HashSCC<T>> H;
|
||||||
|
|
||||||
// Decremental maintenance of strongly connected components
|
// Decremental maintenance of strongly connected components
|
||||||
RodittyZwick<T> rodittyZwick;
|
RodittyZwick<T> rodittyZwick;
|
||||||
|
|
||||||
|
// Delete internal edge e(u, v)
|
||||||
|
void removeInternal(const T& u, const T& v);
|
||||||
|
|
||||||
|
// Delete external edge e(u, v)
|
||||||
|
void removeExternal(const T& u, const T& v);
|
||||||
|
|
||||||
|
// Repair reachability trees
|
||||||
|
void repairTrees();
|
||||||
|
|
||||||
|
//
|
||||||
|
void splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Frigioni<T>::init() {
|
void Frigioni<T>::init() {
|
||||||
auto SCCs = Tarjan<T>(this->G.adjList).execute();
|
|
||||||
rodittyZwick = RodittyZwick<T>(this->G);
|
rodittyZwick = RodittyZwick<T>(this->G);
|
||||||
rodittyZwick.init();
|
rodittyZwick.init();
|
||||||
|
auto C = rodittyZwick.getComponentMap();
|
||||||
|
|
||||||
for (auto& scc : SCCs) {
|
for (const auto& w : std::views::keys(C)) {
|
||||||
RT[scc.id] = BreadthFirstTree<T>(this->G, scc.id);
|
RT[C[w]] = BreadthFirstTree<T>(this->G, w);
|
||||||
for (const auto& u : this->G.vertices()) {
|
for (const auto& u : this->G.vertices()) {
|
||||||
for (const auto& v : this->G.adjList[u]) {
|
for (const auto& v : this->G.adjList[u]) {
|
||||||
if (scc.member(u)) {
|
if (!C[w].contains(u) && C[w].contains(v))
|
||||||
if (scc.member(v))
|
E[C[w]].inc.insert({ u, v });
|
||||||
E[scc.id].in.insert(std::make_pair(u, v));
|
else if (C[w].contains(u) && !C[w].contains(v))
|
||||||
else
|
E[C[w]].out.insert({ u, v });
|
||||||
E[scc.id].out.insert(std::make_pair(u, v));
|
else
|
||||||
} else if (scc.member(v)) {
|
E[C[w]].in.insert({ u, v });
|
||||||
E[scc.id].inc.insert(std::make_pair(u, v));
|
|
||||||
}
|
|
||||||
TC[u][v] = false;
|
TC[u][v] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto& scc : SCCs) {
|
for (const auto& w : std::views::keys(C)) {
|
||||||
for (const auto& u : scc.vertices()) {
|
for (const auto& u : C[w].vertices()) {
|
||||||
for (const auto& v : RT[scc.id].vertices()) {
|
for (const auto& v : RT[C[w]].vertices()) {
|
||||||
TC[u][v] = true;
|
TC[u][v] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,104 +98,132 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
|||||||
std::vector<std::pair<T, T>> Eext;
|
std::vector<std::pair<T, T>> Eext;
|
||||||
|
|
||||||
for (const auto& [u, v] : edges) {
|
for (const auto& [u, v] : edges) {
|
||||||
|
if (!this->G.adjList[u].contains(v)) continue;
|
||||||
|
|
||||||
if (rodittyZwick.query(u, v))
|
if (rodittyZwick.query(u, v))
|
||||||
Eint.push_back({ u, v });
|
Eint.push_back({ u, v });
|
||||||
else
|
else
|
||||||
Eext.push_back({ u, v });
|
Eext.push_back({ u, v });
|
||||||
}
|
}
|
||||||
|
for (const auto& [u, v] : Eint) removeInternal(u, v);
|
||||||
|
for (const auto& [u, v] : Eext) removeExternal(u, v);
|
||||||
|
|
||||||
std::unordered_map<T, std::stack<T>> H;
|
repairTrees();
|
||||||
for (const auto& [u, v] : Eint) {
|
}
|
||||||
this->G.remove(u, v);
|
|
||||||
rodittyZwick.remove(u, v);
|
|
||||||
|
|
||||||
if (!rodittyZwick.query(u, v)) {
|
template<typename T>
|
||||||
TC[u][v] = false;
|
void Frigioni<T>::removeInternal(const T& u, const T& v) {
|
||||||
auto C = rodittyZwick.getSCCs();
|
this->G.remove(u, v);
|
||||||
|
auto L = rodittyZwick.getComponentMap();
|
||||||
|
rodittyZwick.remove(u, v);
|
||||||
|
auto C = rodittyZwick.getComponentMap();
|
||||||
|
|
||||||
E[C[u].id].inc.erase({ u, v });
|
if (rodittyZwick.query(u, v)) {
|
||||||
E[C[u].id].out.erase({ u, v });
|
E[C[u]].in.erase({ u, v });
|
||||||
E[C[u].id].in.erase({ u, v });
|
return;
|
||||||
auto D = E[C[u].id];
|
|
||||||
E.erase(C[u].id);
|
|
||||||
|
|
||||||
for (const auto& id : std::views::keys(C)) {
|
|
||||||
if (RT[id].contains(id, v)) {
|
|
||||||
if (E[C[v].id].inc.size() > 1)
|
|
||||||
H[id].push(C[v].id);
|
|
||||||
else {
|
|
||||||
for (const auto& w : C[id].vertices())
|
|
||||||
TC[w][v] = false;
|
|
||||||
for (const auto& c : E[v].out)
|
|
||||||
H[id].push(C[c.second].id);
|
|
||||||
}
|
|
||||||
RT[id].adjList[u].erase(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& [w, z] : D.inc) {
|
|
||||||
E[C[z].id].inc.insert({ w, z });
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& [w, z] : D.out) {
|
|
||||||
E[C[w].id].out.insert({ w, z });
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& [w, z] : D.in) {
|
|
||||||
if (C[w] == C[z])
|
|
||||||
E[C[w].id].in.insert({ w, z });
|
|
||||||
else {
|
|
||||||
E[C[w].id].out.insert({ w, z });
|
|
||||||
E[C[z].id].in.insert({ w, z });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
E[u].in.erase({ u, v });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& [u, v] : Eext) {
|
if (L[u] == C[u]) {
|
||||||
this->G.remove(u, v);
|
E[L[u]].out.erase({ u, v });
|
||||||
auto C = rodittyZwick.getSCCs();
|
E.erase(L[v]);
|
||||||
|
splitEdges(L, C, v);
|
||||||
for (const auto& id : std::views::keys(C)) {
|
}
|
||||||
if (RT[id].contains(id, v)) {
|
else {
|
||||||
if (E[C[v].id].inc.size() > 1)
|
E[L[v]].inc.erase({ u, v });
|
||||||
H[id].push(C[v].id);
|
E.erase(L[u]);
|
||||||
else {
|
splitEdges(L, C, u);
|
||||||
for (const auto& w : C[id].vertices())
|
|
||||||
TC[w][v] = false;
|
|
||||||
for (const auto& c : E[v].out)
|
|
||||||
H[id].push(C[c.second].id);
|
|
||||||
}
|
|
||||||
RT[id].adjList[u].erase(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto C = rodittyZwick.getSCCs();
|
for (const auto& w : std::views::keys(C)) {
|
||||||
for (const auto& id : std::views::keys(rodittyZwick.getSCCs())) {
|
if (!RT[C[w]].contains(v)) continue;
|
||||||
while (H[id].size() > 0) {
|
|
||||||
const auto& h = H[id].top();
|
RT[C[w]].removeEdgeTo(v);
|
||||||
bool found = false;
|
if (E[C[v]].inc.size() > 0) {
|
||||||
|
H[C[w]].push(v); /*h mipos C[v].id?*/
|
||||||
for (const auto& [u, v] : E[h].inc) {
|
continue;
|
||||||
if (RT[id].contains(id, u)) {
|
}
|
||||||
RT[id].adjList[u].insert(h);
|
for (const auto& x : C[w].vertices()) {
|
||||||
found = true;
|
for (const auto& y : C[v].vertices()) {
|
||||||
|
TC[x][y] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto& [a, b] : E[C[v]].out)
|
||||||
|
H[C[w]].push(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Frigioni<T>::removeExternal(const T& u, const T& v) {
|
||||||
|
this->G.remove(u, v);
|
||||||
|
auto C = rodittyZwick.getComponentMap();
|
||||||
|
E[C[v]].inc.erase({ u, v });
|
||||||
|
E[C[u]].out.erase({ u, v });
|
||||||
|
|
||||||
|
for (const auto& w : std::views::keys(C)) {
|
||||||
|
if (!RT[C[w]].contains(v)) continue;
|
||||||
|
|
||||||
|
RT[C[w]].removeEdgeTo(v);
|
||||||
|
if (E[C[v]].inc.size() > 0) {
|
||||||
|
H[C[w]].push(v);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (const auto& x : C[w].vertices()) {
|
||||||
|
for (const auto& y : C[v].vertices()) {
|
||||||
|
TC[x][y] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto& [a, b] : E[C[v]].out)
|
||||||
|
H[C[w]].push(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Frigioni<T>::repairTrees() {
|
||||||
|
auto C = rodittyZwick.getComponentMap();
|
||||||
|
for (const auto& w : std::views::keys(C)) {
|
||||||
|
while (H[C[w]].size() > 0) {
|
||||||
|
const auto& h = H[C[w]].top();
|
||||||
|
H[C[w]].pop();
|
||||||
|
bool foundHook = false;
|
||||||
|
|
||||||
|
for (const auto& [a, b] : E[C[h]].inc) {
|
||||||
|
if (RT[C[w]].adjList[w].contains(a)) {
|
||||||
|
RT[C[w]].insert(a, h);
|
||||||
|
foundHook = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
H[id].pop();
|
if (foundHook) continue;
|
||||||
if (!found) {
|
|
||||||
auto C = rodittyZwick.getSCCs();
|
|
||||||
for (const auto& w : C[id].vertices())
|
|
||||||
TC[w][h] = false;
|
|
||||||
|
|
||||||
for (const auto& [u, v] : E[h].out) {
|
for (const auto& x : C[w].vertices()) {
|
||||||
H[id].push(v);
|
for (const auto& y : C[h].vertices()) {
|
||||||
|
TC[x][y] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (const auto& [a, b] : E[C[h]].out) {
|
||||||
|
if (RT[C[w]].adjList[h].contains(a)) {
|
||||||
|
H[C[w]].push(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Frigioni<T>::splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w) {
|
||||||
|
for (const auto& [a, b] : E[L[w]].inc)
|
||||||
|
E[C[b]].inc.insert({ a, b });
|
||||||
|
|
||||||
|
for (const auto& [a, b] : E[L[w]].out)
|
||||||
|
E[C[a]].out.insert({ a, b });
|
||||||
|
|
||||||
|
for (const auto& [a, b] : E[L[w]].in) {
|
||||||
|
if (C[a] == C[b]) {
|
||||||
|
E[C[a]].in.insert({ a, b });
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
E[C[a]].out.insert({ a, b });
|
||||||
|
E[C[b]].in.insert({ a, b });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include "algorithm/decremental_reachability.h"
|
#include "algorithm/decremental_reachability.h"
|
||||||
#include "graph/breadth_first_tree.h"
|
#include "graph/breadth_first_tree.h"
|
||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
@@ -25,7 +24,7 @@ public:
|
|||||||
// Delete collection of edges
|
// Delete collection of edges
|
||||||
void remove(const std::vector<std::pair<T, T>>& edges) override;
|
void remove(const std::vector<std::pair<T, T>>& edges) override;
|
||||||
|
|
||||||
// Delete edge e(u, v) and explicitely maintain the transitive closure
|
// Delete edge e(u, v) and explicitly maintain the transitive closure
|
||||||
void remove(const T& u, const T& v);
|
void remove(const T& u, const T& v);
|
||||||
private:
|
private:
|
||||||
// Transitive closure matrix
|
// Transitive closure matrix
|
||||||
@@ -41,8 +40,11 @@ private:
|
|||||||
};
|
};
|
||||||
std::unordered_map<T, Edges> E;
|
std::unordered_map<T, Edges> E;
|
||||||
|
|
||||||
|
//
|
||||||
|
std::unordered_map<T, std::stack<T>> H;
|
||||||
|
|
||||||
// Repair reachability trees after edge deletions
|
// Repair reachability trees after edge deletions
|
||||||
void repairTrees(std::unordered_map<T, std::stack<T>>& H);
|
void repairTrees();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -67,53 +69,54 @@ bool Italiano<T>::query(const T& u, const T& v) {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Italiano<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
void Italiano<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||||
for (const auto& [u, v] : edges)
|
for (const auto& [u, v] : edges) {
|
||||||
|
if (!this->G.adjList[u].contains(v)) continue;
|
||||||
remove(u, v);
|
remove(u, v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Italiano<T>::remove(const T& u, const T& v) {
|
void Italiano<T>::remove(const T& u, const T& v) {
|
||||||
if (!this->G.adjList[u].contains(v)) return;
|
this->G.remove(u, v);
|
||||||
|
|
||||||
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)
|
|
||||||
H[w].push(v);
|
|
||||||
else {
|
|
||||||
TC[w][v] = false;
|
|
||||||
for (const auto& c : E[v].out)
|
|
||||||
H[w].push(c);
|
|
||||||
}
|
|
||||||
RT[w].adjList[u].erase(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
E[u].out.erase(v);
|
E[u].out.erase(v);
|
||||||
E[v].inc.erase(u);
|
E[v].inc.erase(u);
|
||||||
this->G.remove(u, v);
|
|
||||||
|
for (const auto& w : this->G.vertices()) {
|
||||||
|
if (!RT[w].adjList[u].contains(v)) continue;
|
||||||
|
|
||||||
repairTrees(H);
|
RT[w].adjList[u].erase(v);
|
||||||
|
if (E[v].inc.size() > 0) {
|
||||||
|
H[w].push(v);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
TC[w][v] = false;
|
||||||
|
for (const auto& c : E[v].out)
|
||||||
|
H[w].push(c);
|
||||||
|
}
|
||||||
|
repairTrees();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Italiano<T>::repairTrees(std::unordered_map<T, std::stack<T>>& H) {
|
void Italiano<T>::repairTrees() {
|
||||||
for (const auto& z : this->G.vertices()) {
|
for (const auto& w : this->G.vertices()) {
|
||||||
while (H[z].size() > 0) {
|
while (H[w].size() > 0) {
|
||||||
const auto& h = H[z].top();
|
const auto& h = H[w].top();
|
||||||
bool found = false;
|
H[w].pop();
|
||||||
|
bool foundHook = false;
|
||||||
|
|
||||||
for (const auto& i : E[h].inc) {
|
for (const auto& i : E[h].inc) {
|
||||||
if (RT[z].contains(z, i)) {
|
if (RT[w].adjList[w].contains(i)) {
|
||||||
RT[z].adjList[i].insert(h);
|
RT[w].adjList[i].insert(h);
|
||||||
found = true;
|
foundHook = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
H[z].pop();
|
if (foundHook) continue;
|
||||||
if (!found) {
|
|
||||||
TC[z][h] = false;
|
TC[w][h] = false;
|
||||||
for (const auto& o : E[h].out) {
|
for (const auto& o : E[h].out) {
|
||||||
H[z].push(o);
|
if (RT[w].adjList[h].contains(o)) {
|
||||||
|
H[w].push(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public:
|
|||||||
// Remove edge (u,v) and update A accordingly for fast checking query
|
// Remove edge (u,v) and update A accordingly for fast checking query
|
||||||
void remove(const T& u, const T& v);
|
void remove(const T& u, const T& v);
|
||||||
|
|
||||||
std::unordered_map<T, SCC<T>> getSCCs() { return C; }
|
std::unordered_map<T, SCC<T>> getComponentMap() { return C; }
|
||||||
private:
|
private:
|
||||||
// Array used to answer strong connectivity queries in O(1) time
|
// Array used to answer strong connectivity queries in O(1) time
|
||||||
std::unordered_map<T, T> A;
|
std::unordered_map<T, T> A;
|
||||||
@@ -51,16 +51,16 @@ template<typename T>
|
|||||||
void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
||||||
auto SCCs = Tarjan<T>(G.adjList).execute();
|
auto SCCs = Tarjan<T>(G.adjList).execute();
|
||||||
|
|
||||||
for (auto& SCC : SCCs) {
|
for (auto& c : SCCs) {
|
||||||
const auto& w = SCC.id;
|
const auto& w = c.id;
|
||||||
|
|
||||||
for (const auto& v : SCC.vertices())
|
for (const auto& v : c.vertices())
|
||||||
A[v] = w;
|
A[v] = w;
|
||||||
|
|
||||||
Out[w] = BreadthFirstTree<T>(SCC, w);
|
Out[w] = BreadthFirstTree<T>(c, w);
|
||||||
In[w] = BreadthFirstTree<T>(SCC.reverse(), w);
|
In[w] = BreadthFirstTree<T>(c.reverse(), w);
|
||||||
|
|
||||||
C[w] = SCC;
|
C[w] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,19 +82,20 @@ void RodittyZwick<T>::remove(const T& u, const T& v) {
|
|||||||
this->G.remove(u, v);
|
this->G.remove(u, v);
|
||||||
|
|
||||||
// If u and v are not in the same SCC, do nothing
|
// If u and v are not in the same SCC, do nothing
|
||||||
if (A[u] != A[v]) return;
|
if (A[u] != A[v])
|
||||||
|
return;
|
||||||
|
|
||||||
// If edge (u,v) is not contained in both inTree and outTree do nothing
|
// If edge (u,v) is not contained in both inTree and outTree do nothing TODO:remove useless comments
|
||||||
if (!In[w].adjList[u].contains(v) &&
|
// is this not better if i utilize A matrix, since we are going traversing between components.. ? TODO
|
||||||
!Out[w].adjList[u].contains(v))
|
if (!In[w].adjList[u].contains(v) && !Out[w].adjList[u].contains(v))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Update In(w) and Out(w)
|
// Update In(w) and Out(w)
|
||||||
Out[w] = BreadthFirstTree<T>(C[w], w);
|
Out[w] = BreadthFirstTree<T>(C[w], w);
|
||||||
In[w] = BreadthFirstTree<T>(C[w].reverse(), w);
|
In[w] = BreadthFirstTree<T>(C[w].reverse(), w);
|
||||||
|
|
||||||
// If a SCC is broken, compute all SCCs again
|
// If a SCC is broken, compute the new SCCs
|
||||||
if (!In[w].adjList.count(u) || !Out[w].adjList.count(v))
|
if (!In[w].contains(u) || !Out[w].contains(v))
|
||||||
findSCC(C[w]);
|
findSCC(C[w]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public:
|
|||||||
|
|
||||||
//
|
//
|
||||||
auto execute();
|
auto execute();
|
||||||
|
|
||||||
//
|
//
|
||||||
void strongConnect(const T& u);
|
void strongConnect(const T& u);
|
||||||
private:
|
private:
|
||||||
@@ -26,33 +26,33 @@ private:
|
|||||||
std::stack<T> S;
|
std::stack<T> S;
|
||||||
std::int16_t index = 0;
|
std::int16_t index = 0;
|
||||||
std::vector<graph::SCC<T>> SCCs;
|
std::vector<graph::SCC<T>> SCCs;
|
||||||
T cid;
|
T cid{};
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
int index = -1;
|
int index = -1;
|
||||||
int lowlink = -1;
|
int lowlink = -1;
|
||||||
bool onStack = false;
|
bool onStack = false;
|
||||||
};
|
};
|
||||||
std::unordered_map<T, Vertex> vmap;
|
std::unordered_map<T, Vertex> V;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Tarjan<T>::strongConnect(const T& u) {
|
void Tarjan<T>::strongConnect(const T& u) {
|
||||||
vmap[u].index = vmap[u].lowlink = index++;
|
V[u].index = V[u].lowlink = index++;
|
||||||
S.push(u);
|
S.push(u);
|
||||||
vmap[u].onStack = true;
|
V[u].onStack = true;
|
||||||
|
|
||||||
for (const auto& w : adjList[u]) {
|
for (const auto& w : adjList[u]) {
|
||||||
if (vmap[w].index == -1) {
|
if (V[w].index == -1) {
|
||||||
strongConnect(w);
|
strongConnect(w);
|
||||||
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].lowlink);
|
V[u].lowlink = std::min(V[u].lowlink, V[w].lowlink);
|
||||||
} else if (vmap[w].onStack) {
|
} else if (V[w].onStack) {
|
||||||
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].index);
|
V[u].lowlink = std::min(V[u].lowlink, V[w].index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If u is a root node, pop the stack and generate an SCC
|
// If u is a root node, pop the stack and generate an SCC
|
||||||
if (vmap[u].lowlink == vmap[u].index) {
|
if (V[u].lowlink == V[u].index) {
|
||||||
std::unordered_map<T, std::unordered_set<T>> scc;
|
std::unordered_map<T, std::unordered_set<T>> scc;
|
||||||
bool finished = false;
|
bool finished = false;
|
||||||
cid = S.top();
|
cid = S.top();
|
||||||
@@ -60,7 +60,7 @@ void Tarjan<T>::strongConnect(const T& u) {
|
|||||||
do {
|
do {
|
||||||
const auto w = S.top();
|
const auto w = S.top();
|
||||||
S.pop();
|
S.pop();
|
||||||
vmap[w].onStack = false;
|
V[w].onStack = false;
|
||||||
scc[w] = adjList[w];
|
scc[w] = adjList[w];
|
||||||
finished = (w == u);
|
finished = (w == u);
|
||||||
} while (!finished);
|
} while (!finished);
|
||||||
@@ -72,7 +72,7 @@ void Tarjan<T>::strongConnect(const T& u) {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
auto Tarjan<T>::execute() {
|
auto Tarjan<T>::execute() {
|
||||||
for (const auto& u : std::views::keys(adjList)) {
|
for (const auto& u : std::views::keys(adjList)) {
|
||||||
if (vmap[u].index == -1)
|
if (V[u].index == -1)
|
||||||
strongConnect(u);
|
strongConnect(u);
|
||||||
}
|
}
|
||||||
return SCCs;
|
return SCCs;
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ public:
|
|||||||
|
|
||||||
BreadthFirstTree(Digraph<T> G, T root);
|
BreadthFirstTree(Digraph<T> G, T root);
|
||||||
|
|
||||||
T root;
|
void removeEdgeTo(const T& u);
|
||||||
|
|
||||||
|
T root{};
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -23,6 +25,18 @@ BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
|
|||||||
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
|
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void BreadthFirstTree<T>::removeEdgeTo(const T& u) {
|
||||||
|
for (const auto& x : this->vertices()) {
|
||||||
|
for (const auto& y : this->adjList[x]) {
|
||||||
|
if (y == u) {
|
||||||
|
this->remove(x, u);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace graph
|
} // namespace graph
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user