Accueil | Aide en ligne | Annuaire | Sécurité | Articles | Astuces | Téléchargements | Association 


Mon compte SOS Ordi

Vous n'êtes pas identifié
[-> S'inscrire à SOS Ordi
[-> Mot de passe perdu ?
S'identifier


Rechercher sur SOS Ordi

Ok

Dans





 Catégories de l'aide en ligne

 Aide en ligne




 Partenaires

  SOS Ordi > Aide en ligne > Logiciel > Développement > Langages > Java > Programmation d'un graphe

Programmation d'un graphe

2 réponse(s)
dont 1 ont aidé boz22 à résoudre son problème

  Problème résolu posté le 15/01/2006 à 17:08
Profil Configuration PC
boz22


Aucun
État : Absent
Bonjour,

Je souhaite faire un programme sur les graphes. J'ai trois classes : Graphe, Sommet et Global. Mon graphe possède une liste de sommet et Sommet une liste de string (pour les voisins). Mon problème est que la liste string des voisins dans sommet n'est pas conservée.
Voici le code ci-dessous : d'où provient l'erreur?
package Set;
public class Graphe {
Double MatriceAdjacence[][];
int taille;
Sommet Liste[];

Graphe(int nbSommet, Sommet L[]){
MatriceAdjacence = new Double[nbSommet][nbSommet];
this.taille = nbSommet;
for(int i=0; i<nbSommet; i++)
for(int j=0; j<nbSommet; j++)
MatriceAdjacence[i][j] = new Double(-1);
this.Liste = L;
}
void saisiePoids(String Ligne, int ind){
int index1 = 0; int s = 0;
Double un = new Double(-1);
int index2 = Ligne.indexOf(" ");
for(int l=0; l<taille-1; l++){
if(index2 != -1){
MatriceAdjacence[ind][l] = Double.valueOf(Ligne.substring(index1, index2));
index1 = index2+1;
index2 = Ligne.indexOf(" ", index1+1);
}}
MatriceAdjacence[ind][taille-1] = Double.valueOf(Ligne.substring(index1));
for(int l=0; l<taille; l++){
if(MatriceAdjacence[ind][l].compareTo(un) != 0){
Liste[ind].Voisins = new String[s+1];
Liste[ind].Voisins[s] = Liste[l].getNom();
s++;
}}
}
int getTaille(){
return this.taille;
}

void setTaille(int TailleG){
this.taille = TailleG;
}

void affichageMatrice(){
String Ligne = "";
for(int i=0; i<taille; i++){
for(int j=0; j<taille; j++){
String Temp = (MatriceAdjacence[i][j]).toString();
Ligne = Ligne.concat(Temp);
Ligne = Ligne.concat(" ");
}
System.out.println(Ligne);
Ligne = "";
}
}

void affichageListeSommet(){
String nomsSommet = "";
for(int i=0; i<this.taille; i++){
nomsSommet = nomsSommet + this.Liste[i].getNom();
nomsSommet = nomsSommet + "/";
}
System.out.println(nomsSommet);
}
}
/*******************************************************************/
public class Sommet {
String nom;
int nbVoisins;
String Voisins[];

Sommet(String n){
this.nom = n;
this.Voisins = null;
}

void setNom(String n){
this.nom = n;
}

String getNom(){
return this.nom;
}
int nbVoisins(){
return this.nbVoisins;
}

String voisinsSommet(){
String v = "";
for(int i=0; i<this.nbVoisins(); i++){
v = v + this.Voisins[i];
v = v + " ";
}
return v;
}
}
/**********************************************************************/
MAIN

package Set;
import java.lang.String;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.*;

public class Global {
static int NbSommets;
static DataInputStream DIS = new DataInputStream(System.in);

static Graphe lectureFichier(String fichier) throws IOException{
String ligne = ""; int index = 0;
BufferedReader in = new BufferedReader(new FileReader(fichier));
ligne = in.readLine();
int nbG = ligne.length();
nbG = (nbG/2) + 1;
Graphe g = new Graphe(nbG, init(ligne, nbG));
while((ligne = in.readLine()) != null){
g.saisiePoids(ligne, index);
index ++;
}
return g;
}
static Sommet[] init(String firstLigne, int longueur){
int index = 0;
String temp = null;
Sommet listeSommet[] = new Sommet[longueur];
for(int i = 0; i < longueur; i++){
temp = firstLigne.substring(index, index+1);
listeSommet[i] = new Sommet(temp);
index = index + 2;
}
return listeSommet;
}
public static void main(String[] args) {
try{
Graphe g = lectureFichier("test.txt");
g.affichageListeSommet();
g.affichageMatrice();
}
catch(IOException ioe){
System.out.println(ioe.toString());
System.exit(1);
}
}

}

   

Bas de page 


Information

Pour participer vous devez d'abord être membre ou identifié :
[-> M'identifier
[-> Je ne suis pas membre SOS Ordi, je souhaite m'inscrire
[-> J'ai perdu mon mot de passe


  Réponse postée le 20/03/2006 à 13:22
Profil du membre Configuration PC
spottydj



État : Absent
Bonjour,

Je n'ai pas trouvé l'endroit ou tu instancie la Liste tongue.gif

C'est difficile sans l'indentation et sans la coloration D.gif

Réplique du développeur : Moi on m'a demandé un programme qui fonctionne, pas une oeuvre d'art !

  Réponse postée le 10/05/2006 à 11:49
Profil du membre Configuration PC
Robot SOSORDI



État : Absent
Réponse automatique :

Cette question est résolue



Haut de page 

 

Version 5.6 | Infos / Contacts | Partenariat | Publicité