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_
|
#ifndef BREADTH_FIRST_SEARCH_H_
|
||||||
#define BREADTH_FIRST_SEARCH_H_
|
#define BREADTH_FIRST_SEARCH_H_
|
||||||
|
|
||||||
#include "graph/digraph.h"
|
#include <map>
|
||||||
|
#include <set>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
@@ -14,49 +14,73 @@ class BreadthFirstSearch {
|
|||||||
public:
|
public:
|
||||||
BreadthFirstSearch() = default;
|
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);
|
std::map<T, std::set<T>> execute(const T& root);
|
||||||
|
|
||||||
// Initialize LU table that show which vertices have been traversed
|
// Search if target vertex exists in graph
|
||||||
std::map<T, bool> initExplore();
|
bool query(const T& root, const T& target);
|
||||||
private:
|
|
||||||
Graph<T> G;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
void setGraph(std::map<T, std::set<T>> adjMatrix);
|
||||||
std::map<T, bool> BreadthFirstSearch<T>::initExplore() {
|
private:
|
||||||
std::map<T, bool> graphExplore;
|
// Represents the graph on which the algorithm will be executed
|
||||||
for (const auto& v : G.adjMatrix) {
|
std::map<T, std::set<T>> adjMatrix;
|
||||||
graphExplore[v.first] = false;
|
};
|
||||||
}
|
|
||||||
return graphExplore;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||||
std::map<T, std::set<T>> tree;
|
std::map<T, std::set<T>> tree;
|
||||||
std::map<T, bool> graphExplore = initExplore();
|
std::map<T, bool> visited;
|
||||||
std::queue<T> Q;
|
std::queue<T> Q;
|
||||||
Q.push(root);
|
Q.push(root);
|
||||||
graphExplore[root] = true;
|
visited[root] = true;
|
||||||
|
|
||||||
while (!Q.empty()) {
|
while (!Q.empty()) {
|
||||||
const auto v = Q.front();
|
const auto v = Q.front();
|
||||||
Q.pop();
|
Q.pop();
|
||||||
|
|
||||||
for (const auto& u : G.adjMatrix[v]) {
|
for (const auto& u : adjMatrix[v]) {
|
||||||
if (!graphExplore[u]) {
|
if (!visited[u]) {
|
||||||
graphExplore[u] = true;
|
visited[u] = true;
|
||||||
tree[v].insert(u);
|
tree[v].insert(u);
|
||||||
Q.push(u);
|
Q.push(u);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return tree;
|
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
|
} // namespace algo
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
@@ -13,68 +14,75 @@ namespace algo {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class Tarjan {
|
class Tarjan {
|
||||||
public:
|
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:
|
private:
|
||||||
// Necessary info about vertices when running Tarjan's algorithm
|
std::map<T, std::set<T>> adjMatrix;
|
||||||
struct Payload {
|
|
||||||
std::int16_t index = -1;
|
|
||||||
std::int16_t lowlink = -1;
|
|
||||||
bool onStack = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
Digraph<T> G;
|
|
||||||
std::stack<T> S;
|
std::stack<T> S;
|
||||||
std::int16_t index = 0;
|
std::int16_t index = 0;
|
||||||
std::map<T, Payload> p;
|
|
||||||
std::vector<SCC<T>> SCCs;
|
std::vector<SCC<T>> SCCs;
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
int index = -1;
|
||||||
|
int lowlink = -1;
|
||||||
|
bool onStack = false;
|
||||||
|
};
|
||||||
|
std::map<T, Vertex> vmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Tarjan<T>::strongConnect(const T& v) {
|
void Tarjan<T>::strongConnect(const T& u) {
|
||||||
p[v].index = p[v].lowlink = index++;
|
vmap[u].index = vmap[u].lowlink = index++;
|
||||||
p[v].onStack = true;
|
S.push(u);
|
||||||
S.push(v);
|
vmap[u].onStack = true;
|
||||||
|
|
||||||
for (const auto& w : G.adjMatrix[v]) {
|
for (const auto& w : adjMatrix[u]) {
|
||||||
if (p[w].index == -1) {
|
if (vmap[w].index == -1) {
|
||||||
strongConnect(w);
|
strongConnect(w);
|
||||||
p[v].lowlink = std::min(p[v].lowlink, p[w].lowlink);
|
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].lowlink);
|
||||||
} else if (p[w].onStack) {
|
} else if (vmap[w].onStack) {
|
||||||
p[v].lowlink = std::min(p[v].lowlink, p[w].index);
|
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 u is a root node, pop the stack and generate an SCC
|
||||||
if (p[v].lowlink == p[v].index) {
|
if (vmap[u].lowlink == vmap[u].index) {
|
||||||
//std::vector<T> scc;
|
|
||||||
std::map<T, std::set<T>> scc;
|
std::map<T, std::set<T>> scc;
|
||||||
bool finished = false;
|
bool finished = false;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const auto w = S.top();
|
const auto w = S.top();
|
||||||
S.pop();
|
S.pop();
|
||||||
p[w].onStack = false;
|
vmap[w].onStack = false;
|
||||||
scc[w] = G.adjMatrix[w];
|
scc[w] = adjMatrix[w];
|
||||||
finished = p[w].index == p[v].index;
|
finished = (w == u);
|
||||||
} while (!finished);
|
} while (!finished);
|
||||||
SCCs.push_back(scc);
|
|
||||||
|
SCCs.push_back({ scc, static_cast<T>(vmap[u].lowlink) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<SCC<T>> Tarjan<T>::execute() {
|
auto Tarjan<T>::execute() {
|
||||||
for (auto& v : G.adjMatrix) {
|
for (const auto& u : std::views::keys(adjMatrix)) {
|
||||||
if (p[v.first].index == -1) {
|
if (vmap[u].index == -1)
|
||||||
strongConnect(v.first);
|
strongConnect(u);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return SCCs;
|
return SCCs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Tarjan<T>::setGraph(std::map<T, std::set<T>> adjMatrix) {
|
||||||
|
this->adjMatrix = adjMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace algo
|
} // namespace algo
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -3,79 +3,21 @@
|
|||||||
#include "algorithm/tarjan.h"
|
#include "algorithm/tarjan.h"
|
||||||
#include "algorithm/breadth_first_search.h"
|
#include "algorithm/breadth_first_search.h"
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
#include <functional>
|
||||||
|
constexpr int verticesNum = 10;
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
TEST_SUITE("Algorithm") {
|
TEST_SUITE("Algorithm") {
|
||||||
|
|
||||||
TEST_CASE("Tarjan::execute 1") {
|
TEST_CASE("Tarjan::execute") {
|
||||||
// 1 --> 2 --> 4 --> 1
|
|
||||||
// 2 --> 3 --> 5 --> 7 --> 3
|
|
||||||
// 5 --> 9 --> 6 --> 8 --> 9
|
|
||||||
Digraph<std::uint16_t> G;
|
|
||||||
G.insert(1, 2);
|
|
||||||
G.insert(2, 3);
|
|
||||||
G.insert(2, 4);
|
|
||||||
G.insert(3, 5);
|
|
||||||
G.insert(4, 1);
|
|
||||||
G.insert(5, 7);
|
|
||||||
G.insert(5, 9);
|
|
||||||
G.insert(6, 8);
|
|
||||||
G.insert(7, 3);
|
|
||||||
G.insert(8, 9);
|
|
||||||
G.insert(9, 6);
|
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 9);
|
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
|
||||||
auto r = tarjan.execute();
|
|
||||||
|
|
||||||
std::vector<std::vector<std::uint16_t>> x = {
|
|
||||||
{6, 8, 9},
|
|
||||||
{3, 5, 7},
|
|
||||||
{1, 2, 4}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto i = 0; i < r.size(); i++) {
|
|
||||||
auto kv = std::views::keys(r[i].adjMatrix);
|
|
||||||
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), x[i].begin()), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Tarjan::execute 2") {
|
|
||||||
// 1 --> 2 --> 5 --> 7 --> 2
|
|
||||||
// 1 --> 4 --> 3 --> 1
|
|
||||||
// 4 --> 6 --> 3
|
|
||||||
Digraph<std::uint16_t> G;
|
|
||||||
G.insert(1, 2);
|
|
||||||
G.insert(1, 4);
|
|
||||||
G.insert(2, 5);
|
|
||||||
G.insert(3, 1);
|
|
||||||
G.insert(4, 3);
|
|
||||||
G.insert(4, 6);
|
|
||||||
G.insert(5, 7);
|
|
||||||
G.insert(6, 3);
|
|
||||||
G.insert(7, 2);
|
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 7);
|
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
|
||||||
auto r = tarjan.execute();
|
|
||||||
|
|
||||||
std::vector<std::vector<std::uint16_t>> x = {
|
|
||||||
{2, 5, 7},
|
|
||||||
{1, 3, 4, 6}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto i = 0; i < r.size(); i++) {
|
|
||||||
auto kv = std::views::keys(r[i].adjMatrix);
|
|
||||||
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), x[i].begin()), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Tarjan::execute 3") {
|
|
||||||
// 1 --> 2 --> 3 --> 1
|
// 1 --> 2 --> 3 --> 1
|
||||||
// 3 --> 4 --> 5 --> 3
|
// 3 --> 4 --> 5 --> 3
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
|
// 7 --> 9
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
// 12 --> 10
|
// 12 --> 10
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
@@ -100,53 +42,48 @@ TEST_SUITE("Algorithm") {
|
|||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
|
||||||
auto r = tarjan.execute();
|
|
||||||
|
|
||||||
std::vector<std::vector<std::uint16_t>> x = {
|
std::vector<std::vector<std::uint16_t>> expected = {
|
||||||
{9, 10, 11, 12, 13},
|
{9, 10, 11, 12, 13},
|
||||||
{6, 7, 8},
|
{6, 7, 8},
|
||||||
{1, 2, 3, 4, 5}
|
{1, 2, 3, 4, 5}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto i = 0; i < r.size(); i++) {
|
for (auto i = 0; i < SCCs.size(); i++) {
|
||||||
auto kv = std::views::keys(r[i].adjMatrix);
|
auto kv = std::views::keys(SCCs[i].adjMatrix);
|
||||||
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), x[i].begin()), true);
|
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(),
|
||||||
|
expected[i].begin()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("BreadthFirstSearch::execute 1") {
|
TEST_CASE("Tarjan::execute ~ DAG") {
|
||||||
// 1 --> 2 --> 5 --> 7 --> 2
|
|
||||||
// 1 --> 4 --> 3 --> 1
|
|
||||||
// 4 --> 6 --> 3
|
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
G.insert(1, 2);
|
auto gen = std::bind(std::uniform_real_distribution<>(0, 1), std::default_random_engine());
|
||||||
G.insert(1, 4);
|
|
||||||
G.insert(2, 5);
|
|
||||||
G.insert(3, 1);
|
|
||||||
G.insert(4, 3);
|
|
||||||
G.insert(4, 6);
|
|
||||||
G.insert(5, 7);
|
|
||||||
G.insert(6, 3);
|
|
||||||
G.insert(7, 2);
|
|
||||||
|
|
||||||
algo::BreadthFirstSearch<std::uint16_t> bfs(G);
|
for (int i = 0; i <= verticesNum; ++i) {
|
||||||
auto r = bfs.execute(1);
|
for (int j = i + 1; j <= verticesNum; ++j) {
|
||||||
|
if (gen() < 0.25) G.insert(i, j);
|
||||||
std::map<std::uint16_t, std::set<std::uint16_t>> x = {
|
}
|
||||||
{1, {2, 4}},
|
|
||||||
{2, {5}},
|
|
||||||
{4, {3, 6}},
|
|
||||||
{5, {7}}
|
|
||||||
};
|
|
||||||
|
|
||||||
CHECK_EQ(r, x);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("BreadthFirstSearch::execute 2") {
|
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).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(),
|
||||||
|
[](const auto& p) {
|
||||||
|
return p.second.size() == 0;
|
||||||
|
}), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("BreadthFirstSearch::execute") {
|
||||||
// 1 --> 2 --> 3 --> 1
|
// 1 --> 2 --> 3 --> 1
|
||||||
// 3 --> 4 --> 5 --> 3
|
// 3 --> 4 --> 5 --> 3
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
|
// 7 --> 9
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
// 12 --> 10
|
// 12 --> 10
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
@@ -169,10 +106,10 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(13, 9);
|
G.insert(13, 9);
|
||||||
G.insert(12, 10);
|
G.insert(12, 10);
|
||||||
|
|
||||||
algo::BreadthFirstSearch<std::uint16_t> bfs(G);
|
auto tree =
|
||||||
auto r = bfs.execute(1);
|
algo::BreadthFirstSearch<std::uint16_t>(G.adjMatrix).execute(1);
|
||||||
|
|
||||||
std::map<std::uint16_t, std::set<std::uint16_t>> x = {
|
std::map<std::uint16_t, std::set<std::uint16_t>> expected = {
|
||||||
{1, {2}},
|
{1, {2}},
|
||||||
{2, {3, 6}},
|
{2, {3, 6}},
|
||||||
{3, {4}},
|
{3, {4}},
|
||||||
@@ -185,6 +122,41 @@ TEST_SUITE("Algorithm") {
|
|||||||
{12, {13}}
|
{12, {13}}
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK_EQ(r, x);
|
CHECK_EQ(tree, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("BreadthFirstSearch::query") {
|
||||||
|
// 1 --> 2 --> 3 --> 1
|
||||||
|
// 3 --> 4 --> 5 --> 3
|
||||||
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
|
// 7 --> 9
|
||||||
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
|
// 12 --> 10
|
||||||
|
Digraph<std::uint16_t> G;
|
||||||
|
G.insert(1, 2);
|
||||||
|
G.insert(2, 3);
|
||||||
|
G.insert(3, 1);
|
||||||
|
G.insert(3, 4);
|
||||||
|
G.insert(4, 5);
|
||||||
|
G.insert(5, 3);
|
||||||
|
G.insert(2, 6);
|
||||||
|
G.insert(6, 7);
|
||||||
|
G.insert(7, 8);
|
||||||
|
G.insert(7, 9);
|
||||||
|
G.insert(8, 6);
|
||||||
|
G.insert(6, 9);
|
||||||
|
G.insert(9, 10);
|
||||||
|
G.insert(10, 11);
|
||||||
|
G.insert(11, 12);
|
||||||
|
G.insert(12, 13);
|
||||||
|
G.insert(13, 9);
|
||||||
|
G.insert(12, 10);
|
||||||
|
|
||||||
|
algo::BreadthFirstSearch<std::uint16_t> bfs(G.adjMatrix);
|
||||||
|
|
||||||
|
CHECK_EQ(bfs.query(1, 5), true);
|
||||||
|
CHECK_EQ(bfs.query(1, 10), true);
|
||||||
|
CHECK_EQ(bfs.query(9, 5), false);
|
||||||
|
CHECK_EQ(bfs.query(8, 4), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user