From 52f777e625a945bc43afeeaaffa4d77cc264c5ab Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Sat, 17 Jun 2023 16:10:47 -0300 Subject: [PATCH] Brute force fail --- Source/Graph.cpp | 39 +++++++++++++++++++++++++++++++++++++++ Source/Graph.h | 3 +++ Source/Main.cpp | 7 +++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Source/Graph.cpp b/Source/Graph.cpp index 47bd6d3..666a436 100644 --- a/Source/Graph.cpp +++ b/Source/Graph.cpp @@ -156,3 +156,42 @@ void Graph::TransitiveClosure() cout << "\n"; } } + +void Graph::PrintPermutations(const vector &array) +{ + for (const auto &permutation : array) + { + cout << permutation << " "; + } + + cout << endl; +} + +void Graph::PermuteArray(vector &array, int start) +{ + if (start == array.size() - 1) + { + PrintPermutations(array); + } + else + { + for (int i = start; i < array.size(); i++) + { + swap(array[start], array[i]); + this->PermuteArray(array, start + 1); + swap(array[start], array[i]); + } + } +} + +void Graph::Permute() +{ + std::vector vertices; + + for (int i = 0; i < length; i++) + { + vertices[i] = i; + } + + this->PermuteArray(vertices, 0); +} diff --git a/Source/Graph.h b/Source/Graph.h index ac495ad..2886b43 100644 --- a/Source/Graph.h +++ b/Source/Graph.h @@ -37,9 +37,12 @@ public: void Remove(int u, int v); void RemoveD(int u, int v); void TransitiveClosure(); + void Permute(); Graph *Clone(); private: void DFS(int u, vector &disc, vector &low, vector &parent, vector> &bridge); int DFSCount(int v, bool visited[]); + void PrintPermutations(const std::vector &array); + void PermuteArray(std::vector &array, int start); }; diff --git a/Source/Main.cpp b/Source/Main.cpp index 9eb2e5a..3e8865e 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -11,8 +11,11 @@ int main() g1->InsertD(2, 0); g1->InsertD(2, 3); - cout << "-- TRANSITIVE CLOSURE --\n\n"; - g1->TransitiveClosure(); + cout << "-- PERMUTATIONS --\n\n"; + g1->Permute(); + + //cout << "-- TRANSITIVE CLOSURE --\n\n"; + //g1->TransitiveClosure(); return 0; }