1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/times.h>
#include <iostream>
#include <string.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#define INFINI 9999
using namespace std;
typedef struct arc
{
int source;
int destination;
int poids;
} arc;
/* Fonction qui stocke les resultats d'un algo dans un fichier */
int stocker_resultat(int nbs, int temps, char *algo, char *fichier){
int fd;
char date[40],t[30];
char buffer[64];
time_t now;
struct tm *now_here;
fd=open(fichier,O_CREAT | O_WRONLY | O_APPEND ,S_IRUSR | S_IWUSR);
if(fd<0) {
fprintf(stderr,"Ouverture fd echoue");
close(fd);
return -1;
}
/* On recupere la date */
now = time(NULL);
now_here = localtime(&now);
strftime(t, 30, "%a, %d %b %Y %H:%M", now_here);
/* On ecrit le nom de l'algo */
sprintf(buffer,"Algorithme : %s\r\n",algo);
write(fd,&buffer,strlen(buffer));
/* On ecrit la date */
sprintf(date,"Date : %s \r\n",t);
write(fd,&date,strlen(date));
/* On ecrit le nombre de sommets */
sprintf(buffer,"Nombre de sommets : %d\r\n",nbs);
write(fd,&buffer,strlen(buffer));
/* On ecrit le temps d'executions */
sprintf(buffer,"Temps : %d\r\n --------- \r\n\r\n",temps);
write(fd,&buffer,strlen(buffer));
close(fd);
return 1;
}
int toutParcouru(int tab[]){
int i=0;
int taille=sizeof(tab);
while(i<taille){
if(tab[i]==0) return 0;
i++;
}
return 1;
}
int dijkstra(int source, int nbSommets, int **graphe){
int pred[nbSommets],dist[nbSommets],etat[nbSommets];
int i,j;
int somCh, distmin;
struct tms debutDij, finDij;
int debDij,fDij;
/* Initialisation du tableau dist a la valeur infini et du tableau pred a la valeur NULL*/
debDij= times(&debutDij);
for(i=0;i<nbSommets;i++){
dist[i]=INFINI;
pred[i]=-1;
etat[i]=0; // 0 -> pas marque 1->sommet marque
}
dist[source]=0;
while(!toutParcouru(etat)) {
distmin=INFINI;
/* Choisir le sommet */
for(i=0;i<nbSommets;i++) {
if(etat[i]==0 && dist[i]<distmin){
somCh=i;
distmin=dist[i];
}
}
etat[somCh]=1;
for(j=0;j<nbSommets;j++) {
if(graphe[somCh][j]>0){
if(dist[j]>(dist[somCh]+graphe[somCh][j])){ dist[j]=dist[somCh]+graphe[somCh][j];
pred[j]=somCh;
}
}
}
}
for(i=0;i<nbSommets;i++) {
fprintf(stdout,"Sommet : %d ",i);
fprintf(stdout,"Distance : %d ",dist[i]);
fprintf(stdout,"Pred : %d \n",pred[i]);
}
fDij=times(&finDij);
int tps=(fDij-debDij);
cout<< " Dijkstra execute en "<<tps<<"secondes"<<endl;
stocker_resultat(nbSommets,tps,"Dijkstra","./resultats.txt");
return tps;
}
int BellmanFord(int ** graphe, struct arc tabArcs[], int nbSommets, int nbArcs){
int debBel,fBel;
int i,j ;
int pred[nbSommets];
int dist[nbSommets];
int depart=0;
struct tms debutBel, finBel;
debBel= times(&debutBel);
for (i = 0 ; i<nbSommets; i++){ // Initialisation
pred[i]=0;
dist[i]= INFINI;
}
dist[depart]=0;
int modif=1;
int compteur=1;
cout<<" Avant le while" <<endl;
while ( (compteur < (nbSommets-1) ) & (modif==1) ){
modif=0;
for (i=0;i<nbArcs;i++){
if ( dist[tabArcs[i].destination]> (dist[tabArcs[i].source] + graphe[tabArcs[i].source][tabArcs[i].destination] )){
dist[tabArcs[i].destination]= dist[tabArcs[i].source] + graphe[tabArcs[i].source][tabArcs[i].destination];
pred[tabArcs[i].destination]=tabArcs[i].source ;
modif=1;
}
}
compteur++;
}
/* Bouvle d'affichage des resultats */
for (i=0;i<nbSommets;i++){
printf("Sommets %d : Dist min : %d et predecesseur %d \n", i+1, dist[i],pred[i]);
}
fBel=times(&finBel);
int tps=fBel-debBel;
cout<< " Bellman Ford effectue en "<<tps<< "secondes"<<endl;
return tps ;
}
int main(int argc, char **argv){
int n;
int c_arcs=0;
int i,j;
int rd[2];
int res;
string choix;
cout<<" --- Calcul du plus court chemin dans un graphe pondere positivement ---"<<endl;
cout<<" Entrez le chiffre correspondant �l'action desiree "<<endl;
cout<<"----1/ Generer un Graphe soi meme ----\n";
cout<<"----2/ Graphe genere aleatoirement ----\n";
cin>>choix;
cout<<"Specifier le nombre de sommet :"<<endl;
cin>>n;
int ** graphe2;
graphe2= new int * [2000];
if (graphe2 != NULL) {
printf("creation de 2000 sur 2 dimension\n");
}
n=3000;
/*
int ** graphe;
graphe = new int * [n];
printf("Valeur de n : %d \n Taille tableau : %d",n,sizeof(*graphe));
if (graphe != NULL) {
for (i=0; i<n; ++i) {
printf("Tour de boucle n %d\n",i);
graphe[i]=new int[n];
if (graphe[i]==NULL) {
cout<<"Probleme la creation de la graphe"<<endl;
exit(0);
}
else {
printf("defecation reussite\n");
}
}
}
*/
int ** graphe;
graphe = new int * [n];
printf("Valeur de n : %d \n Taille tableau : %d",n,sizeof(*graphe));
if (graphe != NULL)
{
for (int i=0; i<n; i++)
{
graphe[i]=new int [n];
printf("Tour de boucle n %d\n",i);
if (graphe[i]==NULL)
{
cout<<"Probleme la creation de la matrice"<<endl;
exit(0);
}
}
}
cout << "Ici";
struct arc tabArcs[n*n];
struct arc a;
if (choix=="2")
{
cout<<" Voulez vous que le graphe soit complet ?\n"<<endl;
cout<<" 1/ oui\n";
cout<<" 2/ non \n";
string complet;
cin>>complet;
if ( complet=="1")
{
rd[0]=1;
rd[1]=1;
}
else
{
rd[0]=0;
rd[1]=1;
}
srand(time(NULL));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
res=rd[rand()%2]*rand()%100;
if (res==0){graphe[i][j]=-1;}
else{graphe[i][j]=res;
a.source=i;
a.destination=j;
a.poids=res;
tabArcs[c_arcs]=a;
c_arcs++;
}
}
}
}
else
{
cout<<" Creez les arcs de la maniere suivante :\n";
cout<<"\t - Entrez l'origine puis entr�\n";
cout<<"\t - Entrez le sommet destination puis entr�\n";
cout<<"\t - Entrez le poids de l'arc puis entr�\n";
cout<<"\t - Une invite vous proposera de poursuivre la saisie ou pas\n";
string fin;
int o,d,p;
while (fin!="oui")
{
cout<<"Origine : ";
cin>>o;
a.source=o;
cout<<"Source : " ;
cin>>d;
a.destination=d;
cout<<"Poids : ";
cin>>p;
a.poids=p;
if ( (o<n) | (d<n) )
tabArcs[c_arcs]=a;
graphe[o][d]=p;
c_arcs++;
cout<<" Fin de la saisie ? (oui/non)\n";
cin>>fin;
}
}
int bel=BellmanFord(graphe,tabArcs,n,c_arcs);
int dij=dijkstra(0, n, graphe);
return 1 ;
} |
Partager