Finish Tarjan algorithm and add tests
This commit is contained in:
@@ -4,71 +4,80 @@
|
||||
#include "graph/graph.h"
|
||||
using namespace graph;
|
||||
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <stack>
|
||||
|
||||
namespace algorithm {
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class Tarjan {
|
||||
public:
|
||||
Tarjan(Graph<T> G) : G(G) {}
|
||||
|
||||
void findSCC();
|
||||
//
|
||||
std::vector<std::vector<T>> run();
|
||||
|
||||
void strongConnect(Vertex<T>& v);
|
||||
|
||||
std::uint16_t index = 0;
|
||||
std::stack<Vertex<T>> S;
|
||||
//
|
||||
void strongConnect(const T& v);
|
||||
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;
|
||||
};
|
||||
|
||||
Graph<T> G;
|
||||
std::stack<T> S;
|
||||
std::int16_t index = 0;
|
||||
std::map<T, Payload> vp;
|
||||
std::vector<std::vector<T>> SCCs;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void Tarjan<T>::strongConnect(Vertex<T>& v) {
|
||||
v.index, v.lowlink = index++;
|
||||
v.onStack = true;
|
||||
inline void Tarjan<T>::strongConnect(const T& v) {
|
||||
vp[v].index = vp[v].lowlink = index++;
|
||||
vp[v].onStack = true;
|
||||
S.push(v);
|
||||
|
||||
for (auto& w : v.edges) {
|
||||
if (w.index == -1) {
|
||||
// successor w has not yet been visited, recurse on it
|
||||
for (const auto& w : G.adjMatrix[v]) {
|
||||
if (vp[w].index == -1) {
|
||||
strongConnect(w);
|
||||
v.lowlink = std::min(v.lowlink, w.lowlink);
|
||||
} else if (w.onStack) {
|
||||
// successor w is in stack S and hence in the current SCC
|
||||
// if w is not on stack, then (v, w) is an edge pointing to an SCC
|
||||
// already found and must be ignored
|
||||
v.lowlink = std::min(v.lowlink, w.index);
|
||||
vp[v].lowlink = std::min(vp[v].lowlink, vp[w].lowlink);
|
||||
} else if (vp[w].onStack) {
|
||||
vp[v].lowlink = std::min(vp[v].lowlink, vp[w].index);
|
||||
}
|
||||
}
|
||||
|
||||
// if v is a root node, pop the stack and generate an SCC
|
||||
if (v.lowlink = v.index) {
|
||||
// start a new SCC
|
||||
Vertex<T> w;
|
||||
// If v is a root node, pop the stack and generate an SCC
|
||||
if (vp[v].lowlink == vp[v].index) {
|
||||
std::vector<T> SCC;
|
||||
bool finished = false;
|
||||
do {
|
||||
w = S.top();
|
||||
const auto& w = S.top();
|
||||
S.pop();
|
||||
// add w to current SCC
|
||||
|
||||
} while (w != v);
|
||||
vp[w].onStack = false;
|
||||
SCC.push_back(w);
|
||||
finished = vp[w].index == vp[v].index;
|
||||
} while (!finished);
|
||||
SCCs.push_back(SCC);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Tarjan<T>::findSCC() {
|
||||
inline std::vector<std::vector<T>> Tarjan<T>::run() {
|
||||
|
||||
for (auto& vertex : G.vertices) {
|
||||
if (vertex.index == -1) {
|
||||
strongConnect(vertex);
|
||||
for (const auto& v : G.vertices) {
|
||||
if (vp[v].index == -1) {
|
||||
strongConnect(v);
|
||||
}
|
||||
}
|
||||
|
||||
return SCCs;
|
||||
}
|
||||
|
||||
} // namespace algorithm
|
||||
} // namespace algo
|
||||
|
||||
#endif
|
||||
@@ -1,12 +1,11 @@
|
||||
#ifndef GRAPH_H_
|
||||
#define GRAPH_H_
|
||||
|
||||
#include "vertex.h"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
namespace graph {
|
||||
|
||||
@@ -16,50 +15,44 @@ public:
|
||||
Graph() = default;
|
||||
|
||||
// Add vertex v
|
||||
void insert(Vertex<T>& v);
|
||||
void insert(const T& v);
|
||||
|
||||
// Add edge between v and u
|
||||
void insert(Vertex<T> v, Vertex<T> u);
|
||||
void insert(const T& v, const T& u);
|
||||
|
||||
// Delete vertex v
|
||||
void erase(Vertex<T>& v);
|
||||
void erase(T& v);
|
||||
|
||||
// Delete edge between v and u
|
||||
void erase(Vertex<T>& v, Vertex<T>& u);
|
||||
|
||||
// Return true if v and u are connected
|
||||
bool connected(const Vertex<T>& v, const Vertex<T>& u) const;
|
||||
void erase(T& v, T& u);
|
||||
|
||||
// Adjacency matrix representation
|
||||
std::vector<Vertex<T>> vertices;
|
||||
std::set<T> vertices;
|
||||
std::map<T, std::vector<T>> adjMatrix;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::insert(Vertex<T>& v) {
|
||||
vertices.push_back(v);
|
||||
inline void Graph<T>::insert(const T& v) {
|
||||
vertices.insert(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::insert(Vertex<T> v, Vertex<T> u) {
|
||||
v.edges.push_back(u);
|
||||
vertices.push_back(v);
|
||||
inline void Graph<T>::insert(const T& v, const T& u) {
|
||||
vertices.insert(v);
|
||||
vertices.insert(u);
|
||||
adjMatrix[v].push_back(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::erase(Vertex<T>& v) {
|
||||
inline void Graph<T>::erase(T& v) {
|
||||
//
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::erase(Vertex<T>& v, Vertex<T>& u) {
|
||||
inline void Graph<T>::erase(T& v, T& u) {
|
||||
//
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline bool Graph<T>::connected(const Vertex<T>& v, const Vertex<T>& u) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
#ifndef VERTEX_H_
|
||||
#define VERTEX_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <compare>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
class Vertex {
|
||||
public:
|
||||
Vertex() = default;
|
||||
Vertex(T v) : v(v) {}
|
||||
|
||||
T v;
|
||||
std::uint16_t index = -1;
|
||||
std::uint16_t lowlink = -1;
|
||||
bool onStack = false;
|
||||
std::vector<Vertex<T>> edges;
|
||||
|
||||
auto operator<=>(const Vertex&) const = default;
|
||||
|
||||
template<typename T>
|
||||
friend inline std::ostream& operator<<(std::ostream& os, const Vertex<T>& v);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline std::ostream& operator<<(std::ostream& os, const Vertex<T>& vout) {
|
||||
return os << vout.v;
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
#endif
|
||||
73
test/algorithm_test.cc
Normal file
73
test/algorithm_test.cc
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "graph/graph.h"
|
||||
using namespace graph;
|
||||
#include "algorithm/tarjan.h"
|
||||
|
||||
TEST_SUITE("Algorithms") {
|
||||
|
||||
TEST_CASE("Tarjan T_1") {
|
||||
|
||||
Graph<std::uint16_t> G;
|
||||
G.insert(1, 2);
|
||||
G.insert(2, 4);
|
||||
G.insert(2, 3);
|
||||
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.vertices.size(), 9);
|
||||
|
||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
||||
auto result = tarjan.run();
|
||||
|
||||
std::sort(result.begin(), result.end());
|
||||
std::for_each(result.begin(), result.end(), [&](std::vector<std::uint16_t>& row) {
|
||||
std::sort(row.begin(), row.end());
|
||||
});
|
||||
|
||||
std::vector<std::vector<std::uint16_t>> expected{
|
||||
{1, 2, 4},
|
||||
{3, 5, 7},
|
||||
{6, 8, 9}
|
||||
};
|
||||
|
||||
CHECK_EQ(expected, result);
|
||||
}
|
||||
|
||||
TEST_CASE("Tarjan T_2") {
|
||||
|
||||
Graph<std::uint16_t> G;
|
||||
G.insert(1, 4);
|
||||
G.insert(1, 2);
|
||||
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.vertices.size(), 7);
|
||||
|
||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
||||
auto result = tarjan.run();
|
||||
|
||||
std::sort(result.begin(), result.end());
|
||||
std::for_each(result.begin(), result.end(), [&](std::vector<std::uint16_t>& row) {
|
||||
std::sort(row.begin(), row.end());
|
||||
});
|
||||
|
||||
std::vector<std::vector<std::uint16_t>> expected{
|
||||
{1, 3, 4, 6},
|
||||
{2, 5, 7}
|
||||
};
|
||||
|
||||
CHECK_EQ(expected, result);
|
||||
}
|
||||
}
|
||||
@@ -2,33 +2,22 @@
|
||||
|
||||
#include "graph/graph.h"
|
||||
using namespace graph;
|
||||
#include "algorithm/tarjan.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
TEST_SUITE("Graph test.") {
|
||||
|
||||
TEST_SUITE("Testing Graph.") {
|
||||
|
||||
TEST_CASE("Insert vertices/edges.") {
|
||||
TEST_CASE("Insertion.") {
|
||||
Graph<std::uint16_t> G;
|
||||
//G.insert(1);
|
||||
//G.insert(1, 2);
|
||||
//G.insert(1, 4);
|
||||
//G.insert(1, 6);
|
||||
//G.insert(2, 4);
|
||||
//G.insert(2, 5);
|
||||
//G.insert(3, 4);
|
||||
//G.insert(3, 6);
|
||||
//G.insert(4, 6);
|
||||
//G.insert(5, 6);
|
||||
|
||||
G.insert(1, 2);
|
||||
G.insert(1, 4);
|
||||
G.insert(1, 6);
|
||||
G.insert(2, 4);
|
||||
G.insert(2, 5);
|
||||
G.insert(3, 4);
|
||||
G.insert(3, 6);
|
||||
G.insert(4, 6);
|
||||
G.insert(5, 6);
|
||||
|
||||
Vertex<std::uint16_t> v1(1);
|
||||
Vertex<std::uint16_t> v2(3);
|
||||
Vertex<std::uint16_t> v3(5);
|
||||
algorithm::Tarjan<std::uint16_t> tarjan(G);
|
||||
tarjan.findSCC();
|
||||
|
||||
// CHECK_EQ(G.connected(1, 5), true);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user