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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
import java.io.*;
import java.util.*;
public class Matrice{
public int nbSommet;
public int [][] matriceAdjacence;
public int [][] matriceAdjacence2;
public int [][] matriceIncidence;
public int [][] matriceIncJM;
public int [][] matriceIncJP;
public BufferedReader br;
public int ind;
public boolean orientation;
public int distCompteur;
public int compteurDeZero;
public BufferedReader lecture;
public String STRINGSommetDeDepard;
public String STRINGSommetDeFin;
public int SommetDeDepard;
public int SommetDeFin;
public ArrayList<Integer> listeDistances;
public ArrayList<Integer> listeChemins;
public Iterator<Integer> it;
public boolean testFin;
public Matrice(int nbSom) {
/**** DEBUT Initialisations ****/
this.testFin = true;
this.nbSommet = nbSom;
this.ind = 0;
this.orientation = false;
this.distCompteur = 0;
this.compteurDeZero = 0;
this.matriceAdjacence = new int[nbSom][nbSom];
this.matriceAdjacence2 = new int[nbSom][nbSom];
this.matriceIncidence = new int[nbSom][nbSom-1];
this.matriceIncJP = new int[nbSom][nbSom*nbSom+1];
this.matriceIncJM = new int[nbSom][nbSom*nbSom+1];
this.lecture= new BufferedReader(new InputStreamReader(System.in));
this.STRINGSommetDeDepard = "";
this.STRINGSommetDeFin = "";
this.SommetDeDepard = 0;
this.SommetDeFin = 0;
this.listeDistances = new ArrayList<Integer>();
this.listeChemins = new ArrayList<Integer>();
this.it = listeDistances.iterator();
/**** FIN Init ****/
/**** DEBUT Traitements, operations effectuees en fonction du contenu du fichier d'entree ****/
lireFichier("mat1.txt");
afficher();
this.orientation = oriente();
if (orientation == true) {
System.out.println("\n--> Graphe orienté :");
matriceIncOrienteJP();
matriceIncOrienteJM();
listeAdjacences();
}else{
System.out.println("\n--> Graphe non orienté :");
matriceIncNonOriente();
}
listeAretes(this.ind);
densite(this.ind,this.nbSommet);
if (orientation == true) {
System.out.println("\n- Degrés extérieurs des sommets :\n");
degreSommetExt();
degreSommetInt();
}else{
System.out.println("\n- Degrés des sommets :\n");
degreSommetExt();
}
distanceFinal();
/**** FIN Traitements ****/
}
/**** DEBUT Definition des differentes fonctions ****/
public void lireFichier(String mat) {
int a; // caractere courant lu
int i = 0;
int j = 0;
try{
br = new BufferedReader(new FileReader(mat));
br.read(); // on lit le premier caractere : nombre de sommets
br.read(); // on lit le deuxieme caractere : retour a la ligne
a = br.read(); // enfin, nous lisons les caractères de notre format d'entree
while (a != -1) { // tant qu'on est pas a la fin du fichier
matriceAdjacence[i][j] = a-48; // on sauvegarde les caracteres lus
a = br.read(); // on lit l'élément suivant
j++; // on passe à la colonne suivante de notre tableau
if ((a < 47 || a > 57) && a != 10) { // si l'élément suivant lu est un espace
a = br.read(); // on passe à l'élément suivant
}
if (a == 10) { // si l'élément suivant lu est un caractère de fin de ligne
i++; // on passe a la ligne suivant de notre tableau
j=0; // on revient en début de colonnes
a = br.read(); // on passe à l'élément suivant
}
}
}catch(Exception e){}
}
public void afficher() {
int i = 0;
int j = 0;
System.out.println("- Matrice d'adjacence :\n");
for (i = 0; i < this.nbSommet; i++) {
for (j = 0; j < this.nbSommet; j++) {
// notre tableau matriceAdjacence, contient uniquement les caractere de la matrice, et sous forme de matrice
// nous pouvons donc afficher notre matrice en parcourant notre tableau entièrement simplement
this.matriceAdjacence2[i][j] = this.matriceAdjacence[i][j];
System.out.print(this.matriceAdjacence[i][j]+" ");
if (j == this.nbSommet-1) { // on passe a la ligne suivante a chaque fin de ligne
System.out.print("\n");
}
}
}
}
public boolean oriente() {
int i = 0;
int j = 1;
int deb = 0;
// divise la matrice d'adjacence en deux partie à partir de sa diagonale
// Si la matrice est non orienté, les deux partie devraient être égales
for (i = 0; i < this.nbSommet - 1; i++) {
deb++;
for (j = deb; j < this.nbSommet ; j++) {
// on parcours les deux parties simultanéments, si il n'y pas égalité entre deux deux caractères symétriques
// nous sommes alors dans une graphe orienté, retourne true
if (this.matriceAdjacence[i][j] != this.matriceAdjacence[j][i]) { // compare les case symétrique par rapport à la diago. de la matrice
return true;
}
}
}
return false;
}
public void matriceIncNonOriente() {
int deb = 0;
int i = 0;
int j = 0;
ind = 0;
// dans le cas d'une matrice non orienté, nous avons seulement une seul matrice d'incidence correspondante
for (i = 0; i < this.nbSommet - 1; i++) {
deb++; // nous lisons uniquement la moitié de la matrice vu qu'elle est symétrique
for (j = deb; j < this.nbSommet ; j++) {
if (this.matriceAdjacence[i][j] == 1) {
this.matriceIncidence[i][ind] = 1;
this.matriceIncidence[j][ind] = 1;
ind++; // permet d'affecter une colonne par arête
}
}
}
System.out.println("\n- Matrice d'incidence :\n");
for (i = 0; i < this.nbSommet ; i++) {
for (j = 0; j < this.nbSommet-1 ; j++) {
if (this.matriceIncidence[i][j] != 1) {
this.matriceIncidence[i][j] = 0;
System.out.print(this.matriceIncidence[i][j]+" ");
}else{
System.out.print(this.matriceIncidence[i][j]+" ");
}
if (j == this.nbSommet-2) {
System.out.print("\n");
}
}
}
}
public void matriceIncOrienteJP() {
int i = 0;
int j = 0;
ind = 0;
for (i = 0; i < this.nbSommet; i++) {
for (j = 0; j < this.nbSommet ; j++) {
if (this.matriceAdjacence[i][j] == 1) {
this.matriceIncJP[i][ind] = 1;
ind++;
}
}
}
System.out.println("\n- Matrice d'incidence J+ :\n");
for (i = 0; i < this.nbSommet ; i++) {
for (j = 0; j < ind ; j++) {
if (this.matriceIncJP[i][j] != 1) {
this.matriceIncJP[i][j] = 0;
System.out.print(this.matriceIncJP[i][j]+" ");
}else{
System.out.print(this.matriceIncJP[i][j]+" ");
}
if (j == ind-1) {
System.out.print("\n");
}
}
}
}
public void matriceIncOrienteJM() {
int i = 0;
int j = 0;
ind = 0;
for (i = 0; i < this.nbSommet; i++) {
for (j = 0; j < this.nbSommet ; j++) {
if (this.matriceAdjacence[i][j] == 1) {
this.matriceIncJM[j][ind] = 1;
ind++;
}
}
}
System.out.println("\n- Matrice d'incidence J- :\n");
for (i = 0; i < this.nbSommet ; i++) {
for (j = 0; j < ind; j++) {
if (this.matriceIncJM[i][j] != 1) {
this.matriceIncJM[i][j] = 0;
System.out.print(this.matriceIncJM[i][j]+" ");
}else{
System.out.print(this.matriceIncJM[i][j]+" ");
}
if (j == ind-1) {
System.out.print("\n");
}
}
}
}
public void listeAdjacences() {
int i = 0;
int j = 0;
int compteur = 0;
System.out.println("\n- Listes d'adjacences :\n");
for (i = 0; i < this.nbSommet; i++) {
System.out.print((i+1)+" : ");
for (j = 0; j < this.nbSommet; j++) {
if (this.matriceAdjacence[i][j] == 1 && compteur > 0) {
System.out.print(", "+(j+1));
}else if (this.matriceAdjacence[i][j] == 1) {
System.out.print((j+1));
compteur++;
}
if (j == this.nbSommet-1 && compteur == 0) {
System.out.print("aucun");
}
if (j == this.nbSommet-1) {
System.out.print("\n");
compteur = 0;
}
}
}
}
public void listeAretes(int nbAr) {
int i = 0;
int j = 0;
System.out.println("\n- Listes d'arêtes ou d'arcs :\n");
System.out.println("Il y a "+nbAr+" Arêtes dans le graphe.\n");
for (i = 0; i < this.nbSommet; i++) {
for (j = 0; j < this.nbSommet; j++) {
if (this.matriceAdjacence[i][j] == 1) {
System.out.println("Noeud "+(i+1)+" ---> "+"Noeud "+(j+1));
}
}
}
}
public void densite(int nbAr, int nbSom) {
double densitePourcentage = ((double)nbAr)*100/(double)(nbSom*nbSom);
System.out.println("\n- La densité du graphe est de "+densitePourcentage+"%");
}
public void degreSommetExt() {
int i = 0;
int j = 0;
int compteur = 0;
for (i = 0; i < this.nbSommet; i++) {
for (j = 0; j < this.nbSommet; j++) {
if (this.matriceAdjacence[i][j] == 1) {
compteur++;
}
}
System.out.println("Le sommet "+(i+1)+" a un degré extérieur de "+compteur);
compteur = 0;
}
}
public void degreSommetInt() {
int i = 0;
int j = 0;
int compteur = 0;
System.out.println("\n- Degrés intérieurs des sommets :\n");
for (i = 0; i < this.nbSommet; i++) {
for (j = 0; j < this.nbSommet; j++) {
if (this.matriceAdjacence[j][i] == 1) {
compteur++;
}
}
System.out.println("Le sommet "+(i+1)+" a un degré intérieur de "+compteur);
compteur = 0;
}
}
public int distanceCalculRecursion(int SomDeb, int SomFin) {
// La complexité réside dans l'obtention du chemin le plus court LORSQU'IL y a plusieurs chemins possibles
int i = 0;
int j = 0;
int beurk = 0;
this.compteurDeZero = 0;
for (i = 0; i <this.nbSommet; i++) {
if (this.matriceAdjacence2[SomDeb-1][i] == 1) {
for (j = 0; j < this.nbSommet; j++) {
if (this.matriceAdjacence2[SomDeb-1][j] == 1) {
beurk++;
System.out.println("1");
}
}
if (beurk > 1) {
this.matriceAdjacence2[SomDeb-1][i] = 0;
this.testFin = false;
System.out.println("2");
beurk = 0;
}
this.distCompteur++;
SomDeb = i+1;
System.out.println("3");
if (SomDeb == SomFin) {
System.out.println("4");
this.listeDistances.add(this.distCompteur);
int val = this.distCompteur;
this.distCompteur = 0;
System.out.println("5");
return val;
}
break;
}else{
this.compteurDeZero++; // Si il est égal au nombre de sommets, alors on il n'y a que des 0 sur la ligne
// on ne peut donc pas atteindre notre sommet
}
}
if (this.compteurDeZero == this.nbSommet) {
System.out.println("\nLes sommets ne peuvent respectivement pas être atteints");
return -1;
}
System.out.println("6");
return distanceCalculRecursion(SomDeb,SomFin); // Récursion, nous recommençons l'étape précédente avec les nouvelles valeurs
}
/*public int distanceCalculRecursion(int SomDeb, int SomFin) {
// La complexité réside dans l'obtention du chemin le plus court LORSQU'IL y a plusieurs chemins possibles
int i = 0;
this.compteurDeZero = 0;
for (i = 0; i <this.nbSommet; i++) {
if (this.matriceAdjacence[SomDeb-1][i] == 1) {
this.distCompteur++;
SomDeb = i+1;
if (SomDeb == SomFin) {
//this.listeDistances.add(this.distCompteur);
return this.distCompteur;
}
break;
}else{
this.compteurDeZero++; // Si il est égal au nombre de sommets, alors on il n'y a que des 0 sur la ligne
// on ne peut donc pas atteindre notre sommet
}
}
if (this.compteurDeZero == this.nbSommet) {
return -1;
}
return distanceCalculRecursion(SomDeb,SomFin); // Récursion, nous recommençons l'étape précédente avec les nouvelles valeurs
}*/
public int renvoieCompteurDistance() {
this.distCompteur = this.listeDistances.get(0);
//System.out.println(this.listeDistances.size());
while (it.hasNext()) {
int tmp = it.next();
if (this.distCompteur > tmp) {
this.distCompteur = tmp;
}
}
/*for (int i = 0; i <= this.listeDistances.size();i++) {
if (this.distCompteur > this.listeDistances.get(i)) {
this.distCompteur = this.listeDistances.get(i);
}
}*/
return this.distCompteur;
}
public void distanceFinal() {
int distCourante = 0;
try{
System.out.print("\nVeuillez entrer le sommet de dépard : ");
this.STRINGSommetDeDepard = this.lecture.readLine();
System.out.print("\nVeuillez entrer le sommet d'arrivé : ");
this.STRINGSommetDeFin = this.lecture.readLine();
}catch (Exception e) {
System.err.println("Erreur lors de la saisie");
System.out.println(e);
}
this.SommetDeDepard = Integer.parseInt(STRINGSommetDeDepard);
this.SommetDeFin = Integer.parseInt(STRINGSommetDeFin);
distanceCalculRecursion(SommetDeDepard,SommetDeFin);
System.out.println(this.testFin);
while (this.testFin == false) {
this.testFin = true;
distCourante = distanceCalculRecursion(SommetDeDepard,SommetDeFin);
}
System.out.print("ici1");
this.distCompteur = renvoieCompteurDistance();
//this.distCompteur = distanceCalculRecursion(SommetDeDepard,SommetDeFin);
System.out.print("ici2");
if (distCourante == -1) {
System.out.println("\nOn ne peut pas atteindre le sommet "+this.SommetDeFin+" à partir du sommet "+this.SommetDeDepard);
}else{
System.out.println("\nLa distance entre le sommet "+this.SommetDeDepard+" et le sommet "+this.SommetDeFin+" vaut : "+this.distCompteur);
}
}
/**** FIN Definition des differentes fonctions ****/
} |
Partager