Add single vertex contains method and replace vertices field with adjMatrix' keys
This commit is contained in:
@@ -27,8 +27,8 @@ private:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
std::map<T, bool> BreadthFirstSearch<T>::initExplore() {
|
std::map<T, bool> BreadthFirstSearch<T>::initExplore() {
|
||||||
std::map<T, bool> graphExplore;
|
std::map<T, bool> graphExplore;
|
||||||
for (const auto& v : G.vertices) {
|
for (const auto& v : G.adjMatrix) {
|
||||||
graphExplore[v] = false;
|
graphExplore[v.first] = false;
|
||||||
}
|
}
|
||||||
return graphExplore;
|
return graphExplore;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ void DecrementalSCC<T>::findSCC() {
|
|||||||
for (auto& C : SCCs) {
|
for (auto& C : SCCs) {
|
||||||
const auto& w = C.representative();
|
const auto& w = C.representative();
|
||||||
|
|
||||||
for (const auto& v : C.vertices)
|
for (const auto& v : C.adjMatrix)
|
||||||
A[v] = w;
|
A[v.first] = w;
|
||||||
|
|
||||||
outTree[w] =
|
outTree[w] =
|
||||||
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
|
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
|
||||||
|
|||||||
@@ -66,9 +66,9 @@ void Tarjan<T>::strongConnect(const T& v) {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<SCC<T>> Tarjan<T>::execute() {
|
std::vector<SCC<T>> Tarjan<T>::execute() {
|
||||||
for (const auto& v : G.vertices) {
|
for (auto& v : G.adjMatrix) {
|
||||||
if (p[v].index == -1) {
|
if (p[v.first].index == -1) {
|
||||||
strongConnect(v);
|
strongConnect(v.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ public:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
Digraph<T>::Digraph(std::map<T, std::set<T>> digraph) {
|
Digraph<T>::Digraph(std::map<T, std::set<T>> digraph) {
|
||||||
Graph<T>::adjMatrix = 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>
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ public:
|
|||||||
// Return true if e(u,v) exists
|
// Return true if e(u,v) exists
|
||||||
virtual bool contains(const T& u, const T& v);
|
virtual bool contains(const T& u, const T& v);
|
||||||
|
|
||||||
|
// Return true if vertex u exists
|
||||||
|
virtual bool contains(const T& u);
|
||||||
|
|
||||||
// Remove edge e(u,v)
|
// Remove edge e(u,v)
|
||||||
virtual void remove(const T& u, const T& v);
|
virtual void remove(const T& u, const T& v);
|
||||||
|
|
||||||
@@ -31,20 +34,16 @@ public:
|
|||||||
virtual void output();
|
virtual void output();
|
||||||
|
|
||||||
// Adjacency matrix representation
|
// Adjacency matrix representation
|
||||||
std::set<T> vertices;
|
|
||||||
std::map<T, std::set<T>> adjMatrix;
|
std::map<T, std::set<T>> adjMatrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Graph<T>::~Graph() {
|
Graph<T>::~Graph() {
|
||||||
vertices.clear();
|
|
||||||
adjMatrix.clear();
|
adjMatrix.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline void Graph<T>::insert(const T& u, const T& v) {
|
inline void Graph<T>::insert(const T& u, const T& v) {
|
||||||
Graph<T>::vertices.insert(u);
|
|
||||||
Graph<T>::vertices.insert(v);
|
|
||||||
Graph<T>::adjMatrix[u].insert(v);
|
Graph<T>::adjMatrix[u].insert(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +52,11 @@ bool Graph<T>::contains(const T& u, const T& v) {
|
|||||||
return adjMatrix[u].contains(v);
|
return adjMatrix[u].contains(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
bool Graph<T>::contains(const T& u) {
|
||||||
|
return adjMatrix.contains(u);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Graph<T>::remove(const T& u, const T& v) {
|
void Graph<T>::remove(const T& u, const T& v) {
|
||||||
adjMatrix[u].erase(v);
|
adjMatrix[u].erase(v);
|
||||||
@@ -60,23 +64,23 @@ void Graph<T>::remove(const T& u, const T& v) {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::uint16_t Graph<T>::V() {
|
std::uint16_t Graph<T>::V() {
|
||||||
return static_cast<std::uint16_t>(vertices.size());
|
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::uint16_t Graph<T>::E() {
|
std::uint16_t Graph<T>::E() {
|
||||||
std::uint16_t edges = 0;
|
std::uint16_t edges = 0;
|
||||||
for (const auto& v : vertices) {
|
for (const auto& v : adjMatrix) {
|
||||||
edges += static_cast<std::uint16_t>(adjMatrix[v].size());
|
edges += static_cast<std::uint16_t>(v.second.size());
|
||||||
}
|
}
|
||||||
return static_cast<std::uint16_t>(edges);
|
return static_cast<std::uint16_t>(edges);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Graph<T>::output() {
|
void Graph<T>::output() {
|
||||||
for (const auto& v : vertices) {
|
for (const auto& v : adjMatrix) {
|
||||||
for (const auto& u : adjMatrix[v]) {
|
for (const auto& u : v.second) {
|
||||||
std::cout << v << "->" << u << '|';
|
std::cout << v.first << "->" << u << '|';
|
||||||
}
|
}
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user