Add roditty and zwick class
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
#ifndef DMSCC_H_
|
|
||||||
#define DMSCC_H_
|
|
||||||
|
|
||||||
#include "graph/graph.h"
|
|
||||||
using namespace graph;
|
|
||||||
#include "tarjan.h"
|
|
||||||
|
|
||||||
namespace algo {
|
|
||||||
|
|
||||||
// A randomized decremental algorithm for maintaining SCCs
|
|
||||||
template<typename T>
|
|
||||||
class DMSCC {
|
|
||||||
public:
|
|
||||||
DMSCC() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
}; // namespace algo
|
|
||||||
|
|
||||||
#endif
|
|
||||||
55
algorithm/roditty_zwick.h
Normal file
55
algorithm/roditty_zwick.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#ifndef RODITTY_ZWICK_H_
|
||||||
|
#define RODITTY_ZWICK_H_
|
||||||
|
|
||||||
|
#include "graph/digraph.h"
|
||||||
|
#include "graph/scc.h"
|
||||||
|
#include "algorithm/tarjan.h"
|
||||||
|
#include "algorithm/bfs.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace graph;
|
||||||
|
|
||||||
|
namespace algo {
|
||||||
|
|
||||||
|
// A randomized decremental algorithm for maintaining SCCs
|
||||||
|
template<typename T>
|
||||||
|
class RodittyZwick {
|
||||||
|
public:
|
||||||
|
RodittyZwick(Digraph<T> G) : G(G) {}
|
||||||
|
|
||||||
|
//
|
||||||
|
void sccDecrementalMaintenance();
|
||||||
|
|
||||||
|
//
|
||||||
|
void tcDecrementalMaintenance();
|
||||||
|
private:
|
||||||
|
Digraph<T> G;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void RodittyZwick<T>::sccDecrementalMaintenance() {
|
||||||
|
std::map<T, T> A;
|
||||||
|
Tarjan<T> tarjan(G);
|
||||||
|
|
||||||
|
auto sccs = tarjan.run();
|
||||||
|
|
||||||
|
for (auto& scc : sccs) {
|
||||||
|
const auto& root = scc.representative();
|
||||||
|
BFS<T> bfs(scc);
|
||||||
|
auto spt = bfs.run(root);
|
||||||
|
for (const auto& vertex : scc.vertices) {
|
||||||
|
A[vertex] = root;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void RodittyZwick<T>::tcDecrementalMaintenance() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace algo
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
@@ -12,10 +13,19 @@ class Digraph : public Graph<T> {
|
|||||||
public:
|
public:
|
||||||
Digraph() = default;
|
Digraph() = default;
|
||||||
|
|
||||||
|
Digraph(std::map<T, std::set<T>> digraph);
|
||||||
|
|
||||||
// Reverse graph directions
|
// Reverse graph directions
|
||||||
Digraph<T> reverse();
|
Digraph<T> reverse();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Digraph<T>::Digraph(std::map<T, std::set<T>> digraph) {
|
||||||
|
Graph<T>::adjMatrix = digraph;
|
||||||
|
auto kv = std::views::keys(Graph<T>::adjMatrix);
|
||||||
|
Graph<T>::vertices = std::set<T>{ kv.begin(), kv.end() };
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Digraph<T> Digraph<T>::reverse() {
|
Digraph<T> Digraph<T>::reverse() {
|
||||||
return Digraph<T>();
|
return Digraph<T>();
|
||||||
|
|||||||
@@ -12,8 +12,13 @@ class SCC : public Graph<T> {
|
|||||||
public:
|
public:
|
||||||
SCC(std::map<T, std::set<T>> scc);
|
SCC(std::map<T, std::set<T>> scc);
|
||||||
|
|
||||||
|
//
|
||||||
|
T representative() { return proxy; };
|
||||||
private:
|
private:
|
||||||
T root;
|
// Each SCC has a representative vertex that helps
|
||||||
|
// answer strong connectivity queries in O(1) time
|
||||||
|
T proxy;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -21,7 +26,7 @@ SCC<T>::SCC(std::map<T, std::set<T>> scc) {
|
|||||||
Graph<T>::adjMatrix = scc;
|
Graph<T>::adjMatrix = scc;
|
||||||
auto kv = std::views::keys(Graph<T>::adjMatrix);
|
auto kv = std::views::keys(Graph<T>::adjMatrix);
|
||||||
Graph<T>::vertices = std::set<T>{ kv.begin(), kv.end() };
|
Graph<T>::vertices = std::set<T>{ kv.begin(), kv.end() };
|
||||||
root = scc.begin()->first;
|
proxy = scc.begin()->first;
|
||||||
}
|
}
|
||||||
|
|
||||||
}; // namespace graph
|
}; // namespace graph
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "graph/scc.h"
|
#include "graph/scc.h"
|
||||||
#include "algorithm/tarjan.h"
|
#include "algorithm/tarjan.h"
|
||||||
#include "algorithm/bfs.h"
|
#include "algorithm/bfs.h"
|
||||||
|
#include "algorithm/roditty_zwick.h"
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
@@ -91,8 +92,8 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(6, 3);
|
G.insert(6, 3);
|
||||||
G.insert(7, 2);
|
G.insert(7, 2);
|
||||||
|
|
||||||
algo::BFS<std::uint16_t> bfs(G, 1);
|
algo::BFS<std::uint16_t> bfs(G);
|
||||||
auto tree = bfs.run();
|
auto tree = bfs.run(1);
|
||||||
|
|
||||||
/*std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
/*std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
||||||
{1, {2, 4}},
|
{1, {2, 4}},
|
||||||
@@ -111,6 +112,25 @@ TEST_SUITE("Algorithm") {
|
|||||||
{5, {7}}
|
{5, {7}}
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK_EQ(tree, exp);
|
CHECK_EQ(tree.adjMatrix, exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Roditty Zwick A1 T1 ") {
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
algo::RodittyZwick<std::uint16_t> rz(G);
|
||||||
|
rz.sccDecrementalMaintenance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user