m1.chocobarlite/src/chocobar/bpl/EdgeMatrixModel.java
2009-05-01 08:07:06 +00:00

249 lines
5.4 KiB
Java

package chocobar.bpl;
import java.util.*; // listes
import exception.*;
public class EdgeMatrixModel implements EdgeModel {
private int cardV;
private boolean[][] adjacent;
/**
* Default constructor
**/
public EdgeMatrixModel() {
this(10);
this.cardV=0;
}
/**
* Constructor with card (number of vertices) as a parameter
**/
public EdgeMatrixModel(int card) {
this.cardV=card;
this.adjacent=new boolean[this.cardV][this.cardV];
}
/**
* Fonction qui renvoie la dimension de cette matrice d'adjance
**/
public int getCardV(){
return this.cardV;
}
/** Fonction qui réinitialise à la taille donnée.
*/
public int setSize(int size)
{
cardV=size;
adjacent=new boolean[size][size];
return size;
}
/**
* Fonction qui rajoute un arc partant du sommet srcV
* vers sommet dstV
**/
public void addEdge(int srcV,int dstV)
throws OutOfRangeVerticeException
// plustard qu'on parse un graphe et rajoute des arcs sans
// passer par l'initialisation, il faut detecter le cas qu'on
// rajoute un arc n'importe quoi
{
if ((srcV<0) ||(dstV<0)
||(srcV>=cardV) || (dstV>=cardV)) {
throw new OutOfRangeVerticeException();
}
adjacent[srcV][dstV]=true;
}
/**
* Fonction qui verifie l'existence d'un arc du sommet srcV
* vers sommet dstV
**/
public boolean isEdge(int srcV,int dstV){
return adjacent[srcV][dstV];
}
/**
* Fonction qui verifie si les sommets adjacents a src sont
* uniquement les sommets contenus dans le tableau dst
* Si non alors elle renvoie -1 et si oui elle renvoie le premier
* sommet dst adjacent a src
**/
public int withOnlyEdges(int srcV, Vector dstV){
int j=0;
int adj=-1;
for (int i=0; i<cardV; i++){
if (adjacent[srcV][i]){
if (!dstV.contains(new Integer(i))){
return -1;
}
if (adj==-1)
adj=i;
}
}
return adj;
}
/**
* Fonction qui renvoie la source (sommet racine) de ce graphe
* ou -1 s'il n'existe pas
**/
public int getSourceVertice(){
int i = -1;
if (this.cardV != 0){
i=0;
for (int j=0; j<this.cardV; j++){
if (this.adjacent[j][i]){
i=j;
}
}
for (int j=0; j<this.cardV; j++){
if (!this.adjacent[i][j]){
return -1;
}
}
for (int j=0; j<this.cardV; j++){
if (this.adjacent[j][i]){
return -1;
}
}
}
return i;
}
/**
* Fonction qui renvoie la puis (sommet final) de ce graphe
* ou -1 s'il n'existe pas
**/
public int getFinalVertice(){
int i = -1;
if (this.cardV != 0){
i=0;
for (int j=i+1; j<this.cardV; j++){
if (this.adjacent[i][j]){
i=j;
}
}
for (int j=0; j<this.cardV; j++){
if (this.adjacent[i][j]){
return -1; }
}
for (int j=0; j<this.cardV; j++){
if (!this.adjacent[j][i]){
return -1; }
}
}
return i;
}
/**
* Fonction qui renvoie une liste d'adjacence du sommet srcV
**/
public Collection getAdjacent(int srcV){
LinkedList res=new LinkedList();
for (int i=0; i<cardV; i++){
if (adjacent[srcV][i])
res.add(new Integer(i));
}
return res;
}
/**
* Fonction qui renvoie la liste d'incidence du sommet dstV
**/
public Collection getIncident(int dstV){
Vector incident=new Vector();
for (int i=0; i<cardV; i++){
if (isEdge(i, dstV))
incident.add(new Integer(i));
}
return incident;
}
/**
* Fonction qui efface tous les arcs du graphe
**/
public void clear(){
// we remove edges between every vertice
for (int i=0; i<this.cardV;i++){
for (int j=0;j<this.cardV;j++){
this.adjacent[i][j]=false;
}
}
}
/**
* Fonction qui cree tous les arcs possibles du graphe
**/
public void fill(){
// we add edges between every vertice
for (int i=0; i<this.cardV;i++){
for (int j=0;j<this.cardV;j++){
this.adjacent[i][j]=true;
}
}
// but a vertice cannot have an edge to itself
for (int i=0;i<this.cardV;i++){
this.adjacent[i][i]=false;
}
}
/**
* Fonction qui rajoute un vertice (sommet) a la liste des arcs du graphe
* les arcs sont encore VIDE
**/
public void addVertice(){
// Verify that we have enough free space
// and that 4*cardV < 3*adjlength <=> cardV < 3/4 * adj.length
if ((4*this.cardV) > (3*this.adjacent.length)){
int nuvolen=this.adjacent.length * 2;
boolean[][] copie = new boolean[nuvolen][nuvolen];
for (int i=0; i<this.cardV; i++){
// on copie juste les anciennes valeurs
// les nouvelles cases etant initialisee a 0
for (int j=0; j<this.cardV; j++){
copie[i][j]=adjacent[i][j];
}
}
this.adjacent=copie;
}
this.cardV++;
}
public void removeVerticeAt(int rmIdx)
throws OutOfRangeVerticeException{
if ((rmIdx<0) || (rmIdx>=this.cardV)){
throw new OutOfRangeVerticeException();
}
// create a new array (cardV-1) * (cardV-1)
boolean[][] neoAdj= new boolean[this.cardV-1][this.cardV-1];
// fill the new array zith the values of the previous
// adjacency matrix
int ni=0;
int nj=0;
for (int i=0;i<this.cardV;i++){
for (int j=0;j<this.cardV;j++){
if ((i!=rmIdx) || (j!=rmIdx)){
if (i<rmIdx){ni=i; }
if (i>rmIdx){ni=i-1; }
if (j<rmIdx){nj=j; }
if (j>rmIdx){nj=j-1; }
neoAdj[ni][nj]=this.adjacent[i][j];
}
}
}
// associate the new adjacency matrix to the class
this.cardV=this.cardV-1;
this.adjacent=neoAdj;
}
}