Skip to content
Snippets Groups Projects
Commit 0dc38a35 authored by Aymane Amessegher's avatar Aymane Amessegher :headphones:
Browse files

depot fichiers_debase

parent 47d53511
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
all: test_graphe
test_graphe: test_graphe.o graphe.o io_graphe.o
gcc -o test_graphe test_graphe.o graphe.o io_graphe.o
graphe.o: graphe.c graphe.h
gcc -Wall -c graphe.c
io_graphe.o: io_graphe.c graphe.h
gcc -Wall -c io_graphe.c
test_graphe.o: test_graphe.c graphe.h
gcc -Wall -c test_graphe.c
clean:
rm -f *.o test_graphe *~ data/*~
4
0
2
5
8
0 2 3
0 5 8
2 8 3
8 5 2
3
1
2
3
1 1 1
1 2 1
1 3 1
2 1 1
3 2 1
data/gr2 0 → 100644
3
1
2
3
1 1 2
1 2 8
1 3 5
2 1 3
3 2 2
data/gr3 0 → 100644
13
1
2
3
4
5
6
7
8
9
10
11
12
13
1 2 1
1 3 1
1 4 1
1 5 1
2 6 1
2 7 1
5 1 1
7 5 1
7 6 1
8 9 1
10 11 1
10 12 1
10 13 1
11 12 1
data/gr4 0 → 100644
6
1
2
3
4
5
6
1 6 1
2 1 1
2 3 9
2 5 3
3 4 2
3 6 2
4 5 2
5 3 1
6 2 3
data/gr5 0 → 100644
4
1
2
3
4
1 2 1
2 1 1
1 4 1
4 1 1
1 3 1
3 1 1
2 4 1
4 2 1
data/gr6 0 → 100644
4
1
2
3
4
1 2 1
1 3 1
2 2 1
2 3 1
3 4 1
13
1
2
3
4
5
6
7
8
9
10
11
12
13
1 5 1
5 1 1
1 6 1
6 1 1
1 7 1
7 1 1
2 6 1
6 2 1
2 7 1
7 2 1
2 9 1
9 2 1
2 10 1
10 2 1
2 11 1
11 2 1
3 7 1
7 3 1
3 10 1
10 3 1
3 11 1
11 3 1
3 12 1
12 3 1
5 9 1
9 5 1
5 10 1
10 5 1
5 11 1
11 5 1
6 11 1
11 6 1
6 12 1
12 6 1
9 12 1
12 9 1
10 11 1
11 10 1
5
1
2
3
4
5
1 2 1
1 3 1
2 1 1
3 1 1
2 3 1
2 4 1
3 2 1
4 2 1
3 4 1
3 5 1
4 3 1
5 3 1
4 5 1
5 4 1
10
1
2
3
4
5
6
7
8
9
10
1 4 1
1 6 1
1 8 1
1 10 1
2 3 1
2 5 1
2 7 1
2 9 1
3 2 1
3 6 1
3 8 1
3 10 1
4 1 1
4 5 1
4 7 1
4 9 1
5 2 1
5 4 1
5 8 1
5 10 1
6 1 1
6 3 1
6 7 1
6 9 1
7 2 1
7 4 1
7 6 1
7 10 1
8 1 1
8 3 1
8 5 1
8 9 1
9 2 1
9 4 1
9 6 1
9 8 1
10 1 1
10 3 1
10 5 1
10 7 1
10
1
3
5
7
9
2
4
6
8
10
1 4 1
1 6 1
1 8 1
1 10 1
2 3 1
2 5 1
2 7 1
2 9 1
3 2 1
3 6 1
3 8 1
3 10 1
4 1 1
4 5 1
4 7 1
4 9 1
5 2 1
5 4 1
5 8 1
5 10 1
6 1 1
6 3 1
6 7 1
6 9 1
7 2 1
7 4 1
7 6 1
7 10 1
8 1 1
8 3 1
8 5 1
8 9 1
9 2 1
9 4 1
9 6 1
9 8 1
10 1 1
10 3 1
10 5 1
10 7 1
13
1
2
3
4
5
6
7
8
9
10
11
12
13
1 5 1
5 1 1
1 6 1
6 1 1
1 7 1
7 1 1
2 6 1
6 2 1
2 7 1
7 2 1
2 9 1
9 2 1
2 10 1
10 2 1
2 11 1
11 2 1
3 7 1
7 3 1
3 10 1
10 3 1
3 11 1
11 3 1
3 12 1
12 3 1
5 9 1
9 5 1
5 10 1
10 5 1
5 11 1
11 5 1
6 11 1
11 6 1
6 12 1
12 6 1
9 12 1
12 9 1
10 11 1
11 10 1
graphe.c 0 → 100644
/*
Structures de type graphe
Structures de donnees de type liste
(Pas de contrainte sur le nombre de noeuds des graphes)
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "graphe.h"
psommet_t chercher_sommet (pgraphe_t g, int label)
{
psommet_t s ;
s = g ;
while ((s!= NULL) && (s->label != label))
{
s = s->sommet_suivant ;
}
return s ;
}
parc_t existence_arc (parc_t l, psommet_t s)
{
parc_t p = l ;
while (p != NULL)
{
if (p->dest == s)
return p ;
p = p->arc_suivant ;
}
return p ;
}
void ajouter_arc (psommet_t o, psommet_t d, int distance)
{
parc_t parc ;
parc = (parc_t) malloc (sizeof(arc_t)) ;
if (existence_arc (o->liste_arcs, d) != NULL)
{
fprintf(stderr, "ajout d'un arc deja existant\n") ;
exit (-1) ;
}
parc->poids = distance ;
parc->dest = d ;
parc->arc_suivant = o->liste_arcs ;
o->liste_arcs = parc ;
return ;
}
// ===================================================================
int nombre_sommets (pgraphe_t g)
{
psommet_t p = g ;
int nb = 0 ;
while (p != NULL)
{
nb = nb + 1 ;
p = p->sommet_suivant ;
}
return nb ;
}
int nombre_arcs (pgraphe_t g)
{
psommet_t p = g ;
int nb_arcs = 0 ;
while (p != NULL)
{
parc_t l = p->liste_arcs ;
while (l != NULL)
{
nb_arcs = nb_arcs + 1 ;
l = l->arc_suivant ;
}
p = p->sommet_suivant ;
}
return nb_arcs ;
}
void init_couleur_sommet (pgraphe_t g)
{
psommet_t p = g ;
while (p != NULL)
{
p->couleur = 0 ; // couleur indefinie
p = p->sommet_suivant ; // passer au sommet suivant dans le graphe
}
return ;
}
int colorier_graphe (pgraphe_t g)
{
/*
coloriage du graphe g
datasets
graphe data/gr_planning
graphe data/gr_sched1
graphe data/gr_sched2
*/
psommet_t p = g ;
parc_t a ;
int couleur ;
int max_couleur = INT_MIN ; // -INFINI
int change ;
init_couleur_sommet (g) ;
while (p != NULL)
{
couleur = 1 ; // 1 est la premiere couleur
// Pour chaque sommet, on essaie de lui affecter la plus petite couleur
// Choix de la couleur pour le sommet p
do
{
a = p->liste_arcs ;
change = 0 ;
while (a != NULL)
{
if (a->dest->couleur == couleur)
{
couleur = couleur + 1 ;
change = 1 ;
}
a = a->arc_suivant ;
}
} while (change == 1) ;
// couleur du sommet est differente des couleurs de tous les voisins
p->couleur = couleur ;
if (couleur > max_couleur)
max_couleur = couleur ;
p = p->sommet_suivant ;
}
return max_couleur ;
}
void afficher_graphe_largeur (pgraphe_t g, int r)
{
/*
afficher les sommets du graphe avec un parcours en largeur
*/
return ;
}
void afficher_graphe_profondeur (pgraphe_t g, int r)
{
/*
afficher les sommets du graphe avec un parcours en profondeur
*/
return ;
}
void algo_dijkstra (pgraphe_t g, int r)
{
/*
algorithme de dijkstra
des variables ou des chanmps doivent etre ajoutees dans les structures.
*/
return ;
}
// ======================================================================
int degre_sortant_sommet (pgraphe_t g, psommet_t s)
{
/*
Cette fonction retourne le nombre d'arcs sortants
du sommet n dans le graphe g
*/
return 0 ;
}
int degre_entrant_sommet (pgraphe_t g, psommet_t s)
{
/*
Cette fonction retourne le nombre d'arcs entrants
dans le noeud n dans le graphe g
*/
return 0 ;
}
int degre_maximal_graphe (pgraphe_t g)
{
/*
Max des degres des sommets du graphe g
*/
return 0 ;
}
int degre_minimal_graphe (pgraphe_t g)
{
/*
Min des degres des sommets du graphe g
*/
return 0 ;
}
int independant (pgraphe_t g)
{
/* Les aretes du graphe n'ont pas de sommet en commun */
return 0 ;
}
int complet (pgraphe_t g)
{
/* Toutes les paires de sommet du graphe sont jointes par un arc */
return 0 ;
}
int regulier (pgraphe_t g)
{
/*
graphe regulier: tous les sommets ont le meme degre
g est le ponteur vers le premier sommet du graphe
renvoie 1 si le graphe est régulier, 0 sinon
*/
return 0 ;
}
/*
placer les fonctions de l'examen 2017 juste apres
*/
graphe.h 0 → 100644
typedef struct a *parc_t ;
/*
definition des types sommet et pointeur de sommet
un graphe est constitué d'une liste de sommets
*/
typedef struct s
{
int label ; // label du sommet
parc_t liste_arcs ; // arcs sortants du sommet
struct s *sommet_suivant ; // sommet suivant dans le graphe
int couleur ; // couleur du sommet
} sommet_t, *psommet_t ;
/*
definition des types arc et pointeur d'arc
Les arcs sortants d'un sommet sont chainés
Pour chaque arc, il y a un poids qui peut par exemple correspondre a une distance
*/
typedef struct a {
int poids ; // poids de l arc
psommet_t dest ; // pointeur sommet destinataire
struct a * arc_suivant ; // arc suivant
} arc_t, *parc_t ;
/*
pgraphe_t: pointeur vers le premier sommet d'un graphe
*/
typedef psommet_t pgraphe_t ;
psommet_t chercher_sommet (pgraphe_t g, int label) ;
void ajouter_arc (psommet_t o, psommet_t d, int distance) ;
int nombre_arcs (pgraphe_t g) ;
int nombre_sommets (pgraphe_t g) ;
void lire_graphe (char * file_name, pgraphe_t *g) ;
void ecrire_graphe (psommet_t p) ;
void ecrire_graphe_colorie (psommet_t p) ;
int colorier_graphe (pgraphe_t g) ;
void afficher_graphe_profondeur (pgraphe_t g, int r) ;
void afficher_graphe_largeur (pgraphe_t g, int r) ;
void algo_dijkstra (pgraphe_t g, int r) ;
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "graphe.h"
void lire_graphe (char * file_name, pgraphe_t *g)
{
psommet_t p = NULL ; /* premier sommet du graphe */
psommet_t l = NULL ; /* dernier sommet du graphe */
FILE *f ;
int nb_sommets ;
int i ;
psommet_t c ;
int origine, destination, distance ;
printf ("fichier %s\n", file_name) ;
f = fopen (file_name, "r") ;
if (f == NULL)
{
fprintf (stderr,"Le fichier %s n'existe pas\n", file_name) ;
exit (-1) ;
}
fscanf (f, "%d", &nb_sommets) ;
for (i = 0 ; i < nb_sommets; i++)
{
c = (psommet_t) malloc (sizeof (sommet_t)) ;
c->sommet_suivant = NULL ;
c->couleur = 0 ;
if (fscanf (f, "%d", &c->label) == EOF)
{
fprintf (stderr, "erreur dans le contenu du fichier label %d\n", c->label) ;
exit (-1) ;
}
if (chercher_sommet (p, c->label) !=NULL)
{
fprintf (stderr, "erreur label deja dans le graphe %d\n",c->label) ;
exit (-1) ;
}
if (p == NULL)
{
p = c ;
l = c ;
}
else
{
l->sommet_suivant = c ;
l = c ;
}
}
while (fscanf (f, "%d %d %d", &origine, &destination, &distance) != EOF)
{
psommet_t o, d ;
o = chercher_sommet (p, origine) ;
if (o == NULL)
{
fprintf (stderr, "erreur noeud origine %d\n", origine) ;
exit (-1) ;
}
d = chercher_sommet (p, destination) ;
if (d == NULL)
{
fprintf (stderr, "erreur sommet destination %d\n", destination) ;
exit (-1) ;
}
ajouter_arc (o, d, distance) ;
}
*g = p ;
}
void ecrire_graphe (psommet_t g)
{
psommet_t p = g ;
parc_t arc ;
while (p != NULL)
{
printf ("Sommet %d: ", p->label) ;
arc = p->liste_arcs ;
while ( arc != NULL)
{
printf (" (%d %d) ", arc->dest->label, arc->poids ) ;
arc = arc->arc_suivant ;
}
printf ("\n") ;
p = p->sommet_suivant ;
}
return ;
}
void ecrire_graphe_colorie (psommet_t g)
{
psommet_t p = g ;
while (p != NULL)
{
printf ("Sommet %d: couleur %d\n", p->label, p->couleur) ;
p = p->sommet_suivant ;
}
return ;
}
#include <stdio.h>
#include <stdlib.h>
#include "graphe.h"
int main (int argc, char **argv)
{
pgraphe_t g ;
int nc ;
if (argc != 2)
{
fprintf (stderr, "erreur parametre \n") ;
exit (-1) ;
}
/*
la fonction lire_graphe alloue le graphe (matrice,...)
et lit les donnees du fichier passe en parametre
*/
lire_graphe (argv [1], &g) ;
/*
la fonction ecrire_graphe affiche le graphe a l'ecran
*/
printf ("nombre de sommets du graphe %d nombre arcs %d \n", nombre_sommets (g), nombre_arcs (g)) ;
fflush (stdout) ;
ecrire_graphe (g) ;
nc = colorier_graphe (g) ;
printf ("nombre chromatique graphe = %d\n", nc) ;
ecrire_graphe_colorie (g) ;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment