Graphes/TP/ClassesDeDepart/Graphe.java

126 lines
2.5 KiB
Java
Raw Normal View History

2024-02-28 12:00:20 +01:00
/**
* Classe définissant des graphes au sens mathématiques
* @author Luc Dartois
* @version 1.0
*/
public class Graphe{
/**
* Le nombre de sommets du graphe, numérotés de 0 à ordre-1
*/
private int ordre;
/**
* Ses arêtes données sous forme de matrice carré d'adjacence (voir classe MatriceCarre)
*/
private MatriceCarre adj;
/**
* Indique si le graphe est orienté ou non
*/
private boolean oriente;
/**
* Construit un graphe vide
*@param ord indique l'ordre du graphe
*@param o indique s'il est orienté
*/
public Graphe(int ord,boolean o){
this.ordre=ord;
this.adj=new MatriceCarre(ord);
this.oriente=o;
}
/**
* Getter pour l'ordre
*@return L'ordre du graphe
*/
public int getOrdre(){
return this.ordre;
}
/**
* Renvoit vrai s'il y a une arête de i à j
*@param i,j Deux sommets du graphe
*@return Vrai si le graphe possède une arête de i à j
*/
public boolean getArete(int i,int j){
return this.adj.getCoeff(i,j)==1;
}
/**
* Ajoute une arête de i à j
*@param i,j Deux sommets du graphe
*/
public void ajoutArete(int i,int j){
if(i>=this.ordre||j>=this.ordre||i<0||j<0){
throw new IllegalArgumentException("Erreur : Sommet inexistant.");
}
this.adj.setCoeff(i,j,1);
if(!this.oriente){
this.adj.setCoeff(j,i,1);
}
}
/**
* Affiche la liste des voisins de i
*@param i Un sommet du graphe
*/
public void voisinage(int i){
if(!this.oriente){
System.out.print("Voisins de "+i+" : ");
for(int j=0;j<this.ordre;j++){
if(this.adj.getCoeff(i,j)==1){
System.out.print(j+", ");
}
}
System.out.println();
}
else{
System.out.print("Voisins sortants de "+i+" : ");
for(int j=0;j<this.ordre;j++){
if(this.adj.getCoeff(i,j)==1){
System.out.print(j+", ");
}
}
System.out.println();
System.out.print("Voisins entrants de "+i+" : ");
for(int j=0;j<this.ordre;j++){
if(this.adj.getCoeff(j,i)==1){
System.out.print(j+", ");
}
}
System.out.println();
}
}
/**
* Calcule la somme du nombre de voisins de tous les sommets
*@return Un entier représentant la somme de tous les voisins.
*/
public int sommeVoisins(){
int s=0;
for(int i=0;i<this.ordre;i++){
for(int j=0;j<this.ordre;j++){
s+=this.adj.getCoeff(i,j);
}
}
return s;
}
public String toString(){
String s="Graphe ";
if(!this.oriente){
s+="non ";
}
s+="oriente de taille : "+this.ordre+".\n";
return s;
}
public boolean equals(Graphe g){
if(this.ordre!=g.ordre)
return false;
if(this.oriente!=g.oriente)
return false;
if(!this.adj.equals(g.adj))
return false;
return true;
}
}