Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Trabalho_T2/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
Binary file added Trabalho_T2/data/in/binario6.bin
Binary file not shown.
Binary file added Trabalho_T2/src/binario6.bin
Binary file not shown.
23 changes: 22 additions & 1 deletion Trabalho_T2/src/funcionalidades.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,26 @@ void funcionalidade8(){

void funcionalidade9(){}
void funcionalidade10(){}
void funcionalidade11(){}

void funcionalidade11(){
char *dataBIN = malloc(sizeof(char) * 40);
scanf("%s", dataBIN);

FILE *dataFile = fopen(dataBIN, "rb");
checkFile(dataFile);

registroCab rC;
readCabecalho(&rC, dataFile);
verifyStatus(rC);

int numVertices = rC.nroTecnologias;
grafo *grafoFinal = criarGrafo(numVertices);

criarVetElementos(grafoFinal, numVertices, dataFile);
criarListaAdjacencia(grafoFinal, numVertices, dataFile);

algoritmoDeTarjan(grafoFinal, numVertices);

}

void funcionalidade12(){}
96 changes: 86 additions & 10 deletions Trabalho_T2/src/grafo.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
// Ponderado

grafo *criarGrafo(int numVertices){
grafo *novoGrafo = (grafo *) malloc(numVertices*sizeof(grafo));
grafo *novografo = (grafo *) malloc(numVertices*sizeof(grafo));

if (novoGrafo == NULL)
if (novografo == NULL)
exit(0);

for (int i = 0; i < numVertices; i++) {
novoGrafo[i].iVertice = -1;
novoGrafo[i].iAdjacente = NULL;
novoGrafo[i].grauEntrada = 0;
novoGrafo[i].grauGeral = 0;
novoGrafo[i].grauSaida = 0;
novoGrafo[i].iGrupo = -1;
novoGrafo[i].nomeOrigem = NULL;
novografo[i].iVertice = -1;
novografo[i].iAdjacente = NULL;
novografo[i].grauEntrada = 0;
novografo[i].grauGeral = 0;
novografo[i].grauSaida = 0;
novografo[i].iGrupo = -1;
novografo[i].nomeOrigem = NULL;
}

return novoGrafo;
return novografo;
}

lista *criarNo(registro r){
Expand Down Expand Up @@ -205,4 +205,80 @@ void imprimirGrafo(grafo *g, int numVertices){
g[i].iAdjacente = g[i].iAdjacente->prox;
}
}
}

void buscaEmProfundidade(grafo *g, int numVertices, int* ehFortementeConexo, int* numComponentes){
int* cor = (int*)malloc(numVertices * sizeof(int));
int* pre = (int*)malloc(numVertices * sizeof(int));
int* low = (int*)malloc(numVertices * sizeof(int));

for(int i=0; i<numVertices; i++){
cor[i] = BRANCO;
}
for(int i=0; i<numVertices; i++){
if(cor[i] == BRANCO){
visitaVertice(g, i, numVertices,cor, pre, low, ehFortementeConexo, numComponentes);
}
}

free(pre);
free(low);
}

void visitaVertice(grafo* g, int i, int numVertices, int* cor, int* pre, int* low, int* ehFortementeConexo, int* numComponentes) {
int tempo = 0;

cor[i] = CINZA;
pre[i] = low[i] = ++tempo;

lista* adjacente = g[i].iAdjacente;

while (adjacente != NULL) {
char* nomeAdjacente = adjacente->nomeDestino;
int adj = -1; // Inicializa como -1, indicando que não encontrou o vértice

// Procura o vértice no grafo
for (int j = 0; j < numVertices; j++) {
if (strcmp(g[j].nomeOrigem, nomeAdjacente) == 0) {
adj = j;
break;
}
}

if (adj != -1) {
if (cor[adj] == BRANCO) {
visitaVertice(g, adj, numVertices,cor, pre, low, ehFortementeConexo, numComponentes);
low[i] = (low[i] < low[adj]) ? low[i] : low[adj];
} else if (cor[adj] == CINZA) {
low[i] = (low[i] < pre[adj]) ? low[i] : pre[adj];
}
}

adjacente = adjacente->prox;
}

cor[i] = PRETO;

if (pre[i] == low[i]) {
(*numComponentes)++;
}

// Se o número de componentes for igual ao número de vértices, o grafo é fortemente conexo
if (*numComponentes == numVertices) {
*ehFortementeConexo = 1;
}
}

void algoritmoDeTarjan(grafo* g, int numVertices) {
int numComponentes = 0;
int* ehFortementeConexo = (int*)malloc(sizeof(int));
*ehFortementeConexo = 1;

buscaEmProfundidade(g, numVertices, ehFortementeConexo, &numComponentes);

if(*ehFortementeConexo){
printf("O grafo é fortemente conexo e tem %d componentes\n", numComponentes);
}else{
printf("O grafo não é fortemente conexo e tem %d componentes\n", numComponentes);
}
}
9 changes: 9 additions & 0 deletions Trabalho_T2/src/grafo.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef GRAFO_H
#define GRAFO_H

#define BRANCO 0
#define CINZA 1
#define PRETO 2

typedef struct lista{
char *nomeDestino;
int pesoAresta;
Expand Down Expand Up @@ -34,4 +38,9 @@ void quickSort(grafo *g, int baixo, int topo);
void imprimirGrafo(grafo *g, int numVertices);
void calculaGrau(grafo *g, int numVertices);

//void calculaMin(int a, int b);
void buscaEmProfundidade(grafo *g, int numVertices, int *ehFortementeConexo, int* numComponentes);
void visitaVertice(grafo* g, int i, int numVertices,int* cor, int* pre, int* low, int* ehFortementeConexo, int* numComponentes);
void algoritmoDeTarjan(grafo* g, int numVertices);

#endif
Binary file added Trabalho_T2/src/programaTrab
Binary file not shown.