Clone graphs code

This commit is contained in:
Guilherme Werner
2023-06-10 15:35:35 -03:00
parent 3e4ce42710
commit 4a25a5f48d
6 changed files with 178 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode/
Binaries/

3
Build.ps1 Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/pwsh
g++ Source/Main.cpp -o Binaries/Main.exe Source/Graph.cpp && ./Binaries/Main.exe

3
Build.sh Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/bash
g++ Source/Main.cpp -o Binaries/Main Source/Graph.cpp && ./Binaries/Main

98
Source/Graph.cpp Normal file
View File

@ -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<int> &disc, vector<int> &low, vector<int> &parent, vector<pair<int, int>> &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;
}
}

44
Source/Graph.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <list>
#include <sstream>
#include <string.h>
#include <thread>
using namespace std;
class Graph
{
private:
int length;
list<int> *adj;
public:
Graph(int length)
{
this->length = length;
this->adj = new list<int>[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<int> &disc, vector<int> &low, vector<int> &parent, vector<pair<int, int>> &bridge);
int DFSCount(int v, bool visited[]);
};

28
Source/Main.cpp Normal file
View File

@ -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;
}