Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. Graphe valeursPlusCourtsCheminsFloyd () {  
  2.         // on commence par dupliquer la matrice de départ, on obtient la matrice intitulee matriceM
  3.         Graphe matriceM = this.dupliqueGraphe();
  4.        
  5.         // changer la diagonale : matriceM[i][i] a 0
  6.         for(int i = 0; i<this.getNombreSommets();i++){
  7.             matriceM.graphe[i][i]=0;
  8.         }
  9.        
  10.         // toutes les valeurs < 0 donc sans arcs sont passées à + l'infini
  11.         for(int i = 0; i<this.getNombreSommets();i++){
  12.             for(int j = 0;j<this.getNombreSommets();j++){
  13.                 if(matriceM.graphe[i][j]<0)
  14.                     matriceM.graphe[i][j]=Integer.MAX_VALUE;
  15.             }
  16.         }
  17.                    
  18.         // la triple boucle en "blindant" pour éviter des calculs du type Integer.MAX_VALUE + autre chose qui feraient
  19.         // un depassement de capacite
  20.         for(int k = 0; k<this.getNombreSommets();k++){
  21.             for(int i = 0; i<this.getNombreSommets();i++){
  22.                 for(int j = 0; j<this.getNombreSommets();j++){
  23.                     if(matriceM.graphe[i][k]<Integer.MAX_VALUE && matriceM.graphe[k][j]<Integer.MAX_VALUE){
  24.                         if(matriceM.graphe[i][k]+matriceM.graphe[k][j]<matriceM.graphe[i][j]){
  25.                             matriceM.graphe[i][j]=matriceM.graphe[i][k]+matriceM.graphe[k][j];
  26.                         }
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.        
  32.         return matriceM;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement