Add default param constructors, create bool query for bfs and tests
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#ifndef BREADTH_FIRST_SEARCH_H_
|
||||
#define BREADTH_FIRST_SEARCH_H_
|
||||
|
||||
#include "graph/digraph.h"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
|
||||
using namespace graph;
|
||||
@@ -14,49 +14,73 @@ class BreadthFirstSearch {
|
||||
public:
|
||||
BreadthFirstSearch() = default;
|
||||
|
||||
BreadthFirstSearch(Digraph<T> G) : G(G) {}
|
||||
BreadthFirstSearch(std::map<T, std::set<T>> adjMatrix)
|
||||
: adjMatrix(adjMatrix) {}
|
||||
|
||||
// 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);
|
||||
|
||||
// Initialize LU table that show which vertices have been traversed
|
||||
std::map<T, bool> initExplore();
|
||||
private:
|
||||
Graph<T> G;
|
||||
};
|
||||
// Search if target vertex exists in graph
|
||||
bool query(const T& root, const T& target);
|
||||
|
||||
template<typename T>
|
||||
std::map<T, bool> BreadthFirstSearch<T>::initExplore() {
|
||||
std::map<T, bool> graphExplore;
|
||||
for (const auto& v : G.adjMatrix) {
|
||||
graphExplore[v.first] = false;
|
||||
}
|
||||
return graphExplore;
|
||||
}
|
||||
void setGraph(std::map<T, std::set<T>> adjMatrix);
|
||||
private:
|
||||
// Represents the graph on which the algorithm will be executed
|
||||
std::map<T, std::set<T>> adjMatrix;
|
||||
};
|
||||
|
||||
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> graphExplore = initExplore();
|
||||
std::map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
Q.push(root);
|
||||
graphExplore[root] = true;
|
||||
visited[root] = true;
|
||||
|
||||
while (!Q.empty()) {
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
for (const auto& u : G.adjMatrix[v]) {
|
||||
if (!graphExplore[u]) {
|
||||
graphExplore[u] = true;
|
||||
for (const auto& u : adjMatrix[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
tree[v].insert(u);
|
||||
Q.push(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
std::map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
Q.push(root);
|
||||
visited[root] = true;
|
||||
|
||||
while (!Q.empty()) {
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
if (v == target) return true;
|
||||
|
||||
for (const auto& u : adjMatrix[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
Q.push(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void BreadthFirstSearch<T>::setGraph(std::map<T, std::set<T>> adjMatrix) {
|
||||
this->adjMatrix = adjMatrix;
|
||||
}
|
||||
|
||||
} // namespace algo
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <ranges>
|
||||
|
||||
using namespace graph;
|
||||
|
||||
@@ -13,68 +14,75 @@ namespace algo {
|
||||
template<typename T>
|
||||
class Tarjan {
|
||||
public:
|
||||
Tarjan(Digraph<T> G) : G(G) {}
|
||||
Tarjan() = default;
|
||||
|
||||
std::vector<SCC<T>> execute();
|
||||
Tarjan(std::map<T, std::set<T>> adjMatrix) : adjMatrix(adjMatrix) {}
|
||||
|
||||
void strongConnect(const T& v);
|
||||
auto execute();
|
||||
|
||||
void strongConnect(const T& u);
|
||||
|
||||
void setGraph(std::map<T, std::set<T>> adjMatrix);
|
||||
private:
|
||||
// Necessary info about vertices when running Tarjan's algorithm
|
||||
struct Payload {
|
||||
std::int16_t index = -1;
|
||||
std::int16_t lowlink = -1;
|
||||
bool onStack = false;
|
||||
};
|
||||
|
||||
Digraph<T> G;
|
||||
std::map<T, std::set<T>> adjMatrix;
|
||||
std::stack<T> S;
|
||||
std::int16_t index = 0;
|
||||
std::map<T, Payload> p;
|
||||
std::vector<SCC<T>> SCCs;
|
||||
|
||||
struct Vertex {
|
||||
int index = -1;
|
||||
int lowlink = -1;
|
||||
bool onStack = false;
|
||||
};
|
||||
std::map<T, Vertex> vmap;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void Tarjan<T>::strongConnect(const T& v) {
|
||||
p[v].index = p[v].lowlink = index++;
|
||||
p[v].onStack = true;
|
||||
S.push(v);
|
||||
|
||||
for (const auto& w : G.adjMatrix[v]) {
|
||||
if (p[w].index == -1) {
|
||||
void Tarjan<T>::strongConnect(const T& u) {
|
||||
vmap[u].index = vmap[u].lowlink = index++;
|
||||
S.push(u);
|
||||
vmap[u].onStack = true;
|
||||
|
||||
for (const auto& w : adjMatrix[u]) {
|
||||
if (vmap[w].index == -1) {
|
||||
strongConnect(w);
|
||||
p[v].lowlink = std::min(p[v].lowlink, p[w].lowlink);
|
||||
} else if (p[w].onStack) {
|
||||
p[v].lowlink = std::min(p[v].lowlink, p[w].index);
|
||||
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].lowlink);
|
||||
} else if (vmap[w].onStack) {
|
||||
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].index);
|
||||
}
|
||||
}
|
||||
|
||||
// If v is a root node, pop the stack and generate an SCC
|
||||
if (p[v].lowlink == p[v].index) {
|
||||
//std::vector<T> scc;
|
||||
// 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;
|
||||
bool finished = false;
|
||||
|
||||
do {
|
||||
const auto w = S.top();
|
||||
S.pop();
|
||||
p[w].onStack = false;
|
||||
scc[w] = G.adjMatrix[w];
|
||||
finished = p[w].index == p[v].index;
|
||||
vmap[w].onStack = false;
|
||||
scc[w] = adjMatrix[w];
|
||||
finished = (w == u);
|
||||
} while (!finished);
|
||||
SCCs.push_back(scc);
|
||||
|
||||
SCCs.push_back({ scc, static_cast<T>(vmap[u].lowlink) });
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::vector<SCC<T>> Tarjan<T>::execute() {
|
||||
for (auto& v : G.adjMatrix) {
|
||||
if (p[v.first].index == -1) {
|
||||
strongConnect(v.first);
|
||||
}
|
||||
auto Tarjan<T>::execute() {
|
||||
for (const auto& u : std::views::keys(adjMatrix)) {
|
||||
if (vmap[u].index == -1)
|
||||
strongConnect(u);
|
||||
}
|
||||
|
||||
return SCCs;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Tarjan<T>::setGraph(std::map<T, std::set<T>> adjMatrix) {
|
||||
this->adjMatrix = adjMatrix;
|
||||
}
|
||||
|
||||
} // namespace algo
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user