Select Git revision
graphe.c 9.08 KiB
#include "graphe.h"
graph create_graph(int num, int fourmiliere, int nourriture)
{
graph gr = malloc(sizeof(g));
if (gr == NULL)
{
return NULL;
}
gr->num = num;
// init edges (links) matrix
matrix *mat = malloc(sizeof(matrix));
matrix_init(mat, num, num, 0);
gr->edges = mat;
// init pheromon matrix
matrix *mat2 = malloc(sizeof(matrix));
matrix_init(mat2, num, num, 1);
gr->pheromone = mat2;
//initialisation des variables de la matrice
gr->foumiliere = fourmiliere;
gr->nourriture = nourriture;
return gr;
}
int has_edge(graph g, int from_node, int to_node) //permet de contrôle s'il y a une arête entre deux sommets
{
assert(g != NULL);
assert(from_node < g->num);
assert(to_node < g->num);
return g->edges->data[from_node][to_node];
}
void add_edge(graph g, int from_node, int to_node, double distance) //permet d'ajouter une arête entre deux sommets
{
assert(g != NULL);
assert(from_node < g->num);
assert(to_node < g->num);
if (has_edge(g, from_node, to_node))
{
return;
}
g->edges->data[from_node][to_node] = distance;
}
graph main_create_graph(char *filename) //crée la matrice à partir d'un fichier csv
{
graph g;
FILE *fp;
char buffer[100];
fp = fopen(filename, "r"); //ouverture fichier
//contrôle ouverture fichier
if (fp == NULL)
{
printf("non ouvert\n");
}
int line_counter = 0;
while (fgets(buffer, 100, fp) != NULL)
{
int a, b;
int c_int;
double c;
//récupérer les données de la première ligne qui indique les informations de la matrice
if (line_counter == 0)
{
sscanf(buffer, "%d %d %d", &a, &b, &c_int);