Refactor: Rename adjMatrix to adjList

This commit is contained in:
stefiosif
2022-10-12 16:57:48 +03:00
parent 8b6d341d18
commit c525aeaa43
14 changed files with 56 additions and 56 deletions

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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);
});
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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);
}

View File

@@ -20,7 +20,7 @@ public:
template<typename T>
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
this->adjMatrix = algo::BreadthFirstSearch<T>(G.adjMatrix).execute(root);
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
}
} // namespace graph

View File

@@ -28,23 +28,23 @@ public:
template<typename T>
Digraph<T>::Digraph(std::map<T, std::set<T>> G) {
this->adjMatrix = G;
this->adjList = G;
}
template<typename T>
bool Digraph<T>::contains(const T& u, const T& v) {
return algo::BreadthFirstSearch<T>(this->adjMatrix).query(u, v);
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
}
template<typename T>
void Digraph<T>::insert(const T& u, const T& v) {
this->adjMatrix[u].insert(v);
this->adjMatrix[v];
this->adjList[u].insert(v);
this->adjList[v];
}
template<typename T>
void Digraph<T>::remove(const T& u, const T& v) {
this->adjMatrix[u].erase(v);
this->adjList[u].erase(v);
}
template<typename T>
@@ -52,7 +52,7 @@ auto Digraph<T>::reverse() {
std::map<T, std::set<T>> revMatrix;
for (const auto& u : this->vertices()) {
for (const auto& v : this->adjMatrix[u]) {
for (const auto& v : this->adjList[u]) {
revMatrix[v].insert(u);
}
}

View File

@@ -36,31 +36,31 @@ public:
std::uint16_t E();
// Adjacency matrix representation
std::map<T, std::set<T>> adjMatrix;
std::map<T, std::set<T>> adjList;
friend std::ostream& operator<<<>(std::ostream& os, Graph<T>& G);
};
template<typename T>
Graph<T>::~Graph() {
adjMatrix.clear();
adjList.clear();
}
template<typename T>
auto Graph<T>::vertices() {
return std::views::keys(adjMatrix);
return std::views::keys(adjList);
}
template<typename T>
std::uint16_t Graph<T>::V() {
return static_cast<std::uint16_t>(adjMatrix.size());
return static_cast<std::uint16_t>(adjList.size());
}
template<typename T>
std::uint16_t Graph<T>::E() {
std::uint16_t edges = 0;
for (const auto& u : vertices()) {
edges += static_cast<std::uint16_t>(adjMatrix[u].size());
edges += static_cast<std::uint16_t>(adjList[u].size());
}
return static_cast<std::uint16_t>(edges);
}
@@ -69,8 +69,8 @@ template<typename T>
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
os << "V: " << G.V() << " E: " << G.E() << '\n';
for (const auto& u : this->vertices()) {
if (!this->adjMatrix[u].empty()) {
for (const auto& v : this->adjMatrix[u]) {
if (!this->adjList[u].empty()) {
for (const auto& v : this->adjList[u]) {
os << u << "->" << v << ' ';
}
os << '\n';

View File

@@ -32,10 +32,10 @@ private:
template<typename T>
void SCC<T>::normalize() {
for (const auto& u : this->vertices()) {
for (const auto& v : this->adjMatrix[u]) {
if (!this->adjMatrix.count(v)) {
this->adjMatrix[u].erase(v);
this->adjMatrix.erase(v);
for (const auto& v : this->adjList[u]) {
if (!this->adjList.count(v)) {
this->adjList[u].erase(v);
this->adjList.erase(v);
}
}
}

View File

@@ -40,9 +40,9 @@ TEST_SUITE("Algorithm") {
G.insert(13, 9);
G.insert(12, 10);
REQUIRE_EQ(G.adjMatrix.size(), 13);
REQUIRE_EQ(G.V(), 13);
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjList).execute();
std::vector<std::vector<std::uint16_t>> expected = {
{9, 10, 11, 12, 13},
@@ -51,7 +51,7 @@ TEST_SUITE("Algorithm") {
};
for (auto i = 0; i < SCCs.size(); i++) {
auto kv = std::views::keys(SCCs[i].adjMatrix);
auto kv = std::views::keys(SCCs[i].adjList);
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(),
expected[i].begin()), true);
}
@@ -67,12 +67,12 @@ TEST_SUITE("Algorithm") {
}
}
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjList).execute();
// Testing whether an scc is a 1-vertex scc which after the normalization
// has no edges, in this case we know tgat there exist no 2-vertex sccs
for (auto& scc : SCCs) {
CHECK_EQ(std::all_of(scc.adjMatrix.begin(), scc.adjMatrix.end(),
CHECK_EQ(std::all_of(scc.adjList.begin(), scc.adjList.end(),
[](const auto& p) {
return p.second.size() == 0;
}), true);
@@ -107,7 +107,7 @@ TEST_SUITE("Algorithm") {
G.insert(12, 10);
auto tree =
algo::BreadthFirstSearch<std::uint16_t>(G.adjMatrix).execute(1);
algo::BreadthFirstSearch<std::uint16_t>(G.adjList).execute(1);
std::map<std::uint16_t, std::set<std::uint16_t>> expected = {
{1, {2}},
@@ -155,7 +155,7 @@ TEST_SUITE("Algorithm") {
G.insert(13, 9);
G.insert(12, 10);
algo::BreadthFirstSearch<std::uint16_t> bfs(G.adjMatrix);
algo::BreadthFirstSearch<std::uint16_t> bfs(G.adjList);
CHECK_EQ(bfs.query(1, 5), true);
CHECK_EQ(bfs.query(1, 10), true);

View File

@@ -33,7 +33,7 @@ TEST_SUITE("Decremental Reachability Test") {
G.insert(8, 9);
G.insert(9, 5);
REQUIRE_EQ(G.adjMatrix.size(), 9);
REQUIRE_EQ(G.V(), 9);
algo::RodittyZwick<std::uint16_t> rodittyZwick(G);
rodittyZwick.init();
@@ -85,7 +85,7 @@ TEST_SUITE("Decremental Reachability Test") {
G.insert(7, 9);
G.insert(8, 9);
REQUIRE_EQ(G.adjMatrix.size(), 9);
REQUIRE_EQ(G.V(), 9);
algo::Italiano<std::uint16_t> italiano(G);
italiano.init();
@@ -142,7 +142,7 @@ TEST_SUITE("Decremental Reachability Test") {
G.insert(8, 9);
G.insert(9, 5);
REQUIRE_EQ(G.adjMatrix.size(), 9);
REQUIRE_EQ(G.V(), 9);
algo::Frigioni<std::uint16_t> frigioni(G);
frigioni.init();

View File

@@ -28,7 +28,7 @@ TEST_SUITE("Dynamic Reachability Test") {
G.insert(7, 9);
G.insert(8, 9);
REQUIRE_EQ(G.adjMatrix.size(), 9);
REQUIRE_EQ(G.V(), 9);
algo::King<std::uint16_t> king(G);
king.init();
@@ -92,7 +92,7 @@ TEST_SUITE("Dynamic Reachability Test") {
G.insert(8, 9);
G.insert(9, 5);
REQUIRE_EQ(G.adjMatrix.size(), 9);
REQUIRE_EQ(G.V(), 9);
algo::HenzingerKing<std::uint16_t> henzingerKing(G);
henzingerKing.init();

View File

@@ -116,7 +116,7 @@ TEST_SUITE("Graph") {
REQUIRE_EQ(G.V(), 9);
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjList).execute();
std::vector<std::vector<std::uint16_t>> expected = {
{5, 6, 7, 8, 9},