Skip to content

Commit

Permalink
corrigir algoritmo
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermecv committed Dec 10, 2019
1 parent bcc988f commit 06096b3
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 13 deletions.
105 changes: 97 additions & 8 deletions src/grafo.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define FALSE 0
#define TRUE 1

#define DEBUG
// #define DEBUG
#define DEBUG_ON_KRUSKAL

#define INFINITO INT_MAX
Expand Down Expand Up @@ -316,12 +316,12 @@ void libera_grafo (grafo_t *grafo){
/**
* @brief Algoritmo de kruskal
* @param *grafo: grafo onde sera realizada a busca
* @return
* @return
*/
void kruskal_mst(grafo_t *grafo, grafo_t *g_out)
{
int custo_total = 0;
int peso_aresta, menor_peso = INFINITO;
int peso_aresta;
int id_aresta;

no_t *no_vert;
Expand All @@ -347,16 +347,16 @@ void kruskal_mst(grafo_t *grafo, grafo_t *g_out)
vertice = obter_dado(no_vert);
lista_arestas = vertice_get_arestas(vertice);
no_arest = obter_cabeca(lista_arestas);

int menor_peso = INFINITO;
while (no_arest) {

aresta = obter_dado(no_arest); // Obtem a aresta
printf("\nanalisando vertice %d", vertice_get_id(vertice));

//marca como exportada esta aresta

adjacente = aresta_get_adjacente(aresta);
peso_aresta = aresta_get_peso(aresta); // Obtem peso da aresta

#ifdef DEBUG_ON_KRUSKAL
printf("\t%d -- %d [label = %d];\n",
vertice_get_id(vertice),
Expand All @@ -371,11 +371,100 @@ void kruskal_mst(grafo_t *grafo, grafo_t *g_out)
j = vertice_get_id(adjacente);
}




no_arest = obtem_proximo(no_arest);
} // while(no_arest)

no_vert = obtem_proximo(no_vert); // Segue para o proximo vertice
}// while(no_vert)
}

void prim_mst(grafo_t *grafo, int inicial)
{
no_t *no_vert;
no_t *no_arest;
vertice_t *vertice, *vertice_out;
vertice_t *adjacente;
arestas_t *aresta;
arestas_t *contra_aresta;
lista_enc_t *lista_arestas;

int id_aresta, change;
int peso_aresta;

if(grafo == NULL)
{
fprintf(stderr, "prim_mst: ponteiros invalidos");
exit(EXIT_FAILURE);
}
//obtem todos os nos da lista
no_vert = obter_cabeca(grafo->vertices);

while (no_vert){
vertice = obter_dado(no_vert);
lista_arestas = vertice_get_arestas(vertice);
no_arest = obter_cabeca(lista_arestas);

int menor_peso = INFINITO;

while (no_arest) {

aresta = obter_dado(no_arest); // Obtem a aresta
#ifdef DEBUG
printf("\nanalisando vertice %d", vertice_get_id(vertice));
#endif

if(vertice_visitado(vertice))
{
printf("\no vertice ja foi visitado\n");
no_arest = obtem_proximo(no_arest);
continue;
}

adjacente = aresta_get_adjacente(aresta);
contra_aresta = procurar_adjacente(adjacente, vertice);

peso_aresta = aresta_get_peso(aresta); // Obtem peso da aresta
int peso_contra_aresta = aresta_get_peso(contra_aresta);

int menor_peso_temp;

if(peso_aresta < peso_contra_aresta)
menor_peso_temp = peso_aresta;
else
menor_peso_temp = peso_contra_aresta;


if(peso_aresta < menor_peso)
{
change = 1;
menor_peso = peso_aresta;
id_aresta = vertice_get_id(adjacente);
#ifdef DEBUG
printf("\nid_aresta %d, peso %d", id_aresta, menor_peso);
#endif
}

no_arest = obtem_proximo(no_arest);
} // while(no_arest)

if(change)
{
vertice_set_visited(adjacente);
change = 0;
// #ifdef DEBUG
printf("\nVertice %d marcado\n", vertice_get_id(adjacente));
// #endif
printf("\t%d -- %d [label = %d];\n",
vertice_get_id(vertice),
id_aresta,
menor_peso);

}

no_vert = obtem_proximo(no_vert); // Segue para o proximo vertice
}// while(no_vert)
}


}
1 change: 1 addition & 0 deletions src/grafo.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int busca_vertice(lista_enc_t *lista, vertice_t *vertice_procurado);
lista_enc_t* componentes_conexos(grafo_t *grafo);

void kruskal_mst(grafo_t *grafo, grafo_t *g_out);
void prim_mst(grafo_t *grafo, int inicial);


#endif /* GRAFO_GRAFO_H_ */
5 changes: 3 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ int main(void) {
}
}

// exportar_grafo_dot("graph.dot", grafo);
exportar_grafo_dot("graph.dot", grafo);

kruskal_mst(grafo, grafo_out);
// kruskal_mst(grafo, grafo_out);
prim_mst(grafo, 1);

libera_fila(fila);
libera_grafo(grafo);
Expand Down
18 changes: 15 additions & 3 deletions src/vertice.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ struct vertices {
/* Informações para componentes conexos */
int id_grupo;
vertice_t* pai;

int visitado;
};

struct arestas {
int peso; /*!< Identificação numérica da aresta */
vertice_t *fonte; /*!< Vértice fonte. Geralmente vértice pertencente a lista de arestas do vértice */
vertice_t *dest; /*!< Vértice destino */
int visitada; // Marca se a aresta ja foi visitada

status_aresta_t status; /*!< Estado de exportação. Utilizado na função de exportação para dot. */
};

Expand All @@ -54,6 +54,8 @@ vertice_t *cria_vertice(int id)
p->id_grupo = -1;
p->pai = NULL;

p->visitado = 0;

return p;
}

Expand Down Expand Up @@ -289,7 +291,7 @@ void vertice_set_distance(vertice_t *vertice, int distance)
exit(EXIT_FAILURE);
}


}

void aresta_set_visited(arestas_t *aresta)
Expand All @@ -312,3 +314,13 @@ int aresta_visitada(arestas_t *aresta)

return (aresta->visitada);
}

void vertice_set_visited(vertice_t *vertice)
{
vertice->visitado = 0;
}

int vertice_visitado(vertice_t *vertice)
{
return(vertice->visitado);
}
2 changes: 2 additions & 0 deletions src/vertice.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ void vertice_set_distance(vertice_t *vertice, int distance);
void aresta_set_visited(arestas_t *aresta);
int aresta_visitada(arestas_t *aresta);

void vertice_set_visited(vertice_t *vertice);
int vertice_visitado(vertice_t *vertice);


#endif /* GRAFO_VERTICE_H_ */

0 comments on commit 06096b3

Please sign in to comment.