Split graph folder into graph and tree
This commit is contained in:
@@ -12,10 +12,12 @@ namespace algo {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class BFS {
|
class BFS {
|
||||||
public:
|
public:
|
||||||
BFS(Graph<T> G) : G(G) {}
|
BFS() = default;
|
||||||
|
|
||||||
|
BFS(Digraph<T> G) : G(G) {}
|
||||||
|
|
||||||
//
|
//
|
||||||
Digraph<T> run(const T& root);
|
std::map<T, std::set<T>> run(const T& root);
|
||||||
|
|
||||||
// Initialize the lookup table that is used to
|
// Initialize the lookup table that is used to
|
||||||
// show which vertices have been traversed
|
// show which vertices have been traversed
|
||||||
@@ -34,7 +36,7 @@ std::map<T, bool> BFS<T>::initExplore() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Digraph<T> BFS<T>::run(const T& root) {
|
std::map<T, std::set<T>> BFS<T>::run(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> graphExplore = initExplore();
|
||||||
std::queue<T> Q;
|
std::queue<T> Q;
|
||||||
@@ -54,7 +56,7 @@ Digraph<T> BFS<T>::run(const T& root) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Digraph<T>(tree);
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace algo
|
} // namespace algo
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ void DecrementalSCC<T>::findSCC() {
|
|||||||
const auto& w = C.representative();
|
const auto& w = C.representative();
|
||||||
|
|
||||||
// Create shortest-paths out-tree/in-tree
|
// Create shortest-paths out-tree/in-tree
|
||||||
auto outTree = BFS<T>(C).run(w);
|
auto outTree = Digraph<T>(BFS<T>(C).run(w));
|
||||||
SPT[w] = std::make_pair(outTree, outTree.reverse());
|
SPT[w] = std::make_pair(outTree, outTree.reverse());
|
||||||
|
|
||||||
// Update A with current SCCs vertices
|
// Update A with current SCCs vertices
|
||||||
@@ -89,13 +89,13 @@ void DecrementalSCC<T>::remove(const T& u, const T& v) {
|
|||||||
// Update In(w) and Out(w)
|
// Update In(w) and Out(w)
|
||||||
connection[w].adjMatrix[u].erase(v);
|
connection[w].adjMatrix[u].erase(v);
|
||||||
G.adjMatrix[u].erase(v);
|
G.adjMatrix[u].erase(v);
|
||||||
auto inTree = BFS<T>(connection[w]).run(w);
|
auto inTree = Digraph<T>(BFS<T>(connection[w]).run(w));
|
||||||
SPT[w] = std::make_pair(inTree, inTree.reverse());
|
SPT[w] = std::make_pair(inTree, inTree.reverse());
|
||||||
|
|
||||||
// If a SCC is broken, compute all SCCs again
|
// If a SCC is broken, compute all SCCs again
|
||||||
if (!SPT[w].second.vertices.contains(u) ||
|
if (!SPT[w].second.vertices.contains(u) ||
|
||||||
!SPT[w].first.vertices.contains(v)) {
|
!SPT[w].first.vertices.contains(v)) {
|
||||||
auto SCCs = Tarjan<T>(G).findSCC();
|
findSCC();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
17
graph/dag.h
17
graph/dag.h
@@ -1,17 +0,0 @@
|
|||||||
#ifndef DAG_H_
|
|
||||||
#define DAG_H_
|
|
||||||
|
|
||||||
#include "digraph.h"
|
|
||||||
|
|
||||||
namespace graph {
|
|
||||||
|
|
||||||
// Directed Acyclic Graph
|
|
||||||
template<typename T>
|
|
||||||
class DAG {
|
|
||||||
public:
|
|
||||||
DAG() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace graph
|
|
||||||
|
|
||||||
#endif
|
|
||||||
16
graph/directed_acyclic_graph.h
Normal file
16
graph/directed_acyclic_graph.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef DIRECTED_ACYCLIC_GRAPH_H_
|
||||||
|
#define DIRECTED_ACYCLIC_GRAPH_H_
|
||||||
|
|
||||||
|
#include "digraph.h"
|
||||||
|
|
||||||
|
namespace graph {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class DirectedAcyclicGraph {
|
||||||
|
public:
|
||||||
|
DirectedAcyclicGraph() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace graph
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -112,7 +112,7 @@ TEST_SUITE("Algorithm") {
|
|||||||
{5, {7}}
|
{5, {7}}
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK_EQ(tree.adjMatrix, exp);
|
CHECK_EQ(tree, exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Roditty Zwick A1 T1 ") {
|
TEST_CASE("Roditty Zwick A1 T1 ") {
|
||||||
|
|||||||
41
test/tree_test.cc
Normal file
41
test/tree_test.cc
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#include "graph/scc.h"
|
||||||
|
#include "graph/digraph.h"
|
||||||
|
#include "algorithm/tarjan.h"
|
||||||
|
#include "tree/breadth_first_tree.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace graph;
|
||||||
|
using namespace tree;
|
||||||
|
|
||||||
|
TEST_SUITE("Tree") {
|
||||||
|
TEST_CASE("Breadth First Tree") {
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
BreadthFirstTree<std::uint16_t> tree(G);
|
||||||
|
|
||||||
|
std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
||||||
|
{1, {2, 4}},
|
||||||
|
{2, {5}},
|
||||||
|
{4, {3, 6}},
|
||||||
|
{5, {7}}
|
||||||
|
};
|
||||||
|
|
||||||
|
CHECK_EQ(tree.adjMatrix, exp);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
tree/breadth_first_tree.h
Normal file
30
tree/breadth_first_tree.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef BREADTH_FIRST_TREE_H_
|
||||||
|
#define BREADTH_FIRST_TREE_H_
|
||||||
|
|
||||||
|
#include "algorithm/bfs.h"
|
||||||
|
#include "out_tree.h"
|
||||||
|
using namespace graph;
|
||||||
|
|
||||||
|
namespace tree {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class BreadthFirstTree : public OutTree<T> {
|
||||||
|
public:
|
||||||
|
BreadthFirstTree() = default;
|
||||||
|
|
||||||
|
BreadthFirstTree(Digraph<T> G);
|
||||||
|
|
||||||
|
BreadthFirstTree(std::map<T, std::set<T>> G)
|
||||||
|
: OutTree<T>::OutTree(G) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G) {
|
||||||
|
auto bfs = algo::BFS<T>(G).run(1);
|
||||||
|
|
||||||
|
Graph<T>::adjMatrix = bfs;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace tree
|
||||||
|
|
||||||
|
#endif
|
||||||
20
tree/directed_rooted_tree.h
Normal file
20
tree/directed_rooted_tree.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef DIRECTED_ROOTED_TREE_H_
|
||||||
|
#define DIRECTED_ROOTED_TREE_H_
|
||||||
|
|
||||||
|
#include "tree.h"
|
||||||
|
using namespace graph;
|
||||||
|
|
||||||
|
namespace tree {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class DirectedRootedTree : public Tree<T> {
|
||||||
|
public:
|
||||||
|
DirectedRootedTree() = default;
|
||||||
|
|
||||||
|
DirectedRootedTree(std::map<T, std::set<T>> G)
|
||||||
|
: Tree<T>::Tree(G) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace tree
|
||||||
|
|
||||||
|
#endif
|
||||||
20
tree/in_tree.h
Normal file
20
tree/in_tree.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef IN_TREE_H_
|
||||||
|
#define IN_TREE_H_
|
||||||
|
|
||||||
|
#include "directed_rooted_tree.h"
|
||||||
|
using namespace graph;
|
||||||
|
|
||||||
|
namespace tree {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class InTree : public DirectedRootedTree<T> {
|
||||||
|
public:
|
||||||
|
InTree() = default;
|
||||||
|
|
||||||
|
InTree(std::map<T, std::set<T>> G)
|
||||||
|
: DirectedRootedTree<T>::DirectedRootedTree(G) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace tree
|
||||||
|
|
||||||
|
#endif
|
||||||
20
tree/out_tree.h
Normal file
20
tree/out_tree.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef OUT_TREE_H_
|
||||||
|
#define OUT_TREE_H_
|
||||||
|
|
||||||
|
#include "directed_rooted_tree.h"
|
||||||
|
using namespace graph;
|
||||||
|
|
||||||
|
namespace tree {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class OutTree : public DirectedRootedTree<T> {
|
||||||
|
public:
|
||||||
|
OutTree() = default;
|
||||||
|
|
||||||
|
OutTree(std::map<T, std::set<T>> G)
|
||||||
|
: DirectedRootedTree<T>::DirectedRootedTree(G) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace tree
|
||||||
|
|
||||||
|
#endif
|
||||||
19
tree/tree.h
Normal file
19
tree/tree.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef TREE_H_
|
||||||
|
#define TREE_H_
|
||||||
|
|
||||||
|
#include "graph/digraph.h"
|
||||||
|
using namespace graph;
|
||||||
|
|
||||||
|
namespace tree {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Tree : public Digraph<T> {
|
||||||
|
public:
|
||||||
|
Tree() = default;
|
||||||
|
|
||||||
|
Tree(std::map<T, std::set<T>> G) : Digraph<T>::Digraph(G) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace tree
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user