From 4a25a5f48dde2edc01881344f56d3be6e57eb831 Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Sat, 10 Jun 2023 15:35:35 -0300 Subject: [PATCH] Clone graphs code --- .gitignore | 2 + Build.ps1 | 3 ++ Build.sh | 3 ++ Source/Graph.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ Source/Graph.h | 44 ++++++++++++++++++++++ Source/Main.cpp | 28 ++++++++++++++ 6 files changed, 178 insertions(+) create mode 100644 .gitignore create mode 100644 Build.ps1 create mode 100644 Build.sh create mode 100644 Source/Graph.cpp create mode 100644 Source/Graph.h create mode 100644 Source/Main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..884d390 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +Binaries/ diff --git a/Build.ps1 b/Build.ps1 new file mode 100644 index 0000000..42971ba --- /dev/null +++ b/Build.ps1 @@ -0,0 +1,3 @@ +#!/usr/bin/pwsh + +g++ Source/Main.cpp -o Binaries/Main.exe Source/Graph.cpp && ./Binaries/Main.exe diff --git a/Build.sh b/Build.sh new file mode 100644 index 0000000..b92f559 --- /dev/null +++ b/Build.sh @@ -0,0 +1,3 @@ +#!/usr/bin/bash + +g++ Source/Main.cpp -o Binaries/Main Source/Graph.cpp && ./Binaries/Main diff --git a/Source/Graph.cpp b/Source/Graph.cpp new file mode 100644 index 0000000..e38af86 --- /dev/null +++ b/Source/Graph.cpp @@ -0,0 +1,98 @@ +#include "Graph.h" + +void Graph::Insert(int u, int v) +{ + InsertD(u, v); + InsertD(v, u); +} + +void Graph::InsertD(int u, int v) +{ + adj[u].push_back(v); +} + +void Graph::Remove(int u, int v) +{ + RemoveD(u, v); + RemoveD(v, u); +} + +void Graph::RemoveD(int u, int v) +{ + auto iv = find(adj[u].begin(), adj[u].end(), v); + *iv = -1; +} + +Graph *Graph::Clone() +{ + Graph *graph = new Graph(length); + + for (int i = 0; i < length; i++) + { + for (auto j = adj[i].begin(); j != adj[i].end(); j++) + { + graph->InsertD(i, *j); + } + } + + return graph; +} + +void Graph::DFS(int u, vector &disc, vector &low, vector &parent, vector> &bridge) +{ + static int time = 0; + disc[u] = low[u] = time; + time += 1; + + for (int v : adj[u]) + { + // vertice não visitado + if (disc[v] == -1) + { + parent[v] = u; + DFS(v, disc, low, parent, bridge); + low[u] = min(low[u], low[v]); + + if (low[v] > disc[u]) + { + bridge.push_back({u, v}); + } + } + else if (v != parent[u]) + { + low[u] = min(low[u], disc[v]); + } + } +} + +int Graph::DFSCount(int v, bool visited[]) +{ + // marcar vertice como visitado + visited[v] = true; + int count = 1; + + for (auto i = adj[v].begin(); i != adj[v].end(); ++i) + { + if (*i != -1 && !visited[*i]) + { + count += DFSCount(*i, visited); + } + } + + return count; +} + +void Graph::Print() +{ + for (int i = 0; i < length; i++) + { + cout << "[" << i << "]: "; + + for (auto j = adj[i].begin(); j != adj[i].end(); j++) + { + cout << *j << " "; + } + + cout << endl; + } +} diff --git a/Source/Graph.h b/Source/Graph.h new file mode 100644 index 0000000..ada85ca --- /dev/null +++ b/Source/Graph.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Graph +{ +private: + int length; + list *adj; + +public: + Graph(int length) + { + this->length = length; + this->adj = new list[length]; + } + + ~Graph() + { + delete[] adj; + } + +public: + void Print(); + void Insert(int u, int v); + void InsertD(int u, int v); + void Remove(int u, int v); + void RemoveD(int u, int v); + Graph *Clone(); + +private: + void DFS(int u, vector &disc, vector &low, vector &parent, vector> &bridge); + int DFSCount(int v, bool visited[]); +}; diff --git a/Source/Main.cpp b/Source/Main.cpp new file mode 100644 index 0000000..08d94a1 --- /dev/null +++ b/Source/Main.cpp @@ -0,0 +1,28 @@ +#include "Graph.h" + +int main() +{ + int length = 9; + Graph *g1 = new Graph(length); + + g1->Insert(0, 1); + g1->Insert(0, 3); + g1->Insert(0, 4); + g1->Insert(1, 2); + g1->Insert(1, 3); + g1->Insert(1, 4); + g1->Insert(2, 3); + g1->Insert(2, 4); + g1->Insert(2, 5); + g1->Insert(3, 5); + g1->Insert(4, 5); + g1->Insert(5, 6); + g1->Insert(6, 7); + g1->Insert(6, 8); + g1->Insert(7, 8); + + cout << "-- GRAFO --\n\n"; + g1->Print(); + + return 0; +}