249 lines
5.8 KiB
Java
249 lines
5.8 KiB
Java
package chocobar.combi;
|
|
|
|
import java.util.*; // listes
|
|
|
|
import exception.*;
|
|
|
|
class EdgeListModel implements EdgeModel {
|
|
private HashMap[] adjacent;
|
|
private HashMap[] incident;
|
|
private int cardV;
|
|
|
|
/**
|
|
* Constructeur par defaut
|
|
**/
|
|
public EdgeListModel(){
|
|
this(10);
|
|
this.cardV=0;
|
|
}
|
|
|
|
/**
|
|
* Constructeur d'un modele de graphe avec nbVertices sommets
|
|
**/
|
|
public EdgeListModel(int nbVertices){
|
|
if (nbVertices>=0){
|
|
this.cardV=nbVertices;
|
|
this.adjacent=new HashMap[this.cardV+(this.cardV/3)];
|
|
this.incident=new HashMap[this.cardV+(this.cardV/3)];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fonction qui renvoie le nombre de sommets de cette liste
|
|
* d'adjacence
|
|
**/
|
|
public int getCardV(){
|
|
return this.cardV;
|
|
}
|
|
|
|
/** Fonction qui réinitialise à la taille donnée.
|
|
*/
|
|
public int setSize(int size)
|
|
{
|
|
cardV=size;
|
|
adjacent=new HashMap[this.cardV+(this.cardV/3)];
|
|
incident=new HashMap[this.cardV+(this.cardV/3)];
|
|
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* Fonction qui rajoute un arc de srcV vers dstV
|
|
**/
|
|
public void addEdge(int srcV,int dstV)
|
|
throws OutOfRangeVerticeException
|
|
{
|
|
// on vérifie que les arcs ajoutés joignent des
|
|
// sommets existants dans le graphe
|
|
if ((srcV<0) ||(dstV<0)
|
|
||(srcV>=cardV) || (dstV>=cardV)) {
|
|
throw new OutOfRangeVerticeException();
|
|
} else {
|
|
if (this.adjacent[srcV]==null){
|
|
this.adjacent[srcV]=new HashMap();
|
|
}
|
|
if (this.incident[dstV]==null){
|
|
this.incident[dstV]=new HashMap();
|
|
}
|
|
HashMap srcList=this.adjacent[srcV];
|
|
HashMap dstList=this.incident[dstV];
|
|
Integer d=new Integer(dstV);
|
|
Integer s=new Integer(srcV);
|
|
if (!srcList.containsKey(d)){srcList.put(d,null);}
|
|
if (!dstList.containsKey(s)){dstList.put(s,null);}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fonction qui verifie s'il existe un arc
|
|
* de srcV vers dstV
|
|
**/
|
|
public boolean isEdge(int srcV,int dstV){
|
|
if (this.adjacent[srcV]==null){
|
|
return false;
|
|
}
|
|
//works too with incident...
|
|
HashMap srcAdj=this.adjacent[srcV];
|
|
if (srcAdj.containsKey(new Integer(dstV))){ return true; }
|
|
else { return false; }
|
|
}
|
|
|
|
/**
|
|
* 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 src, Vector dst){
|
|
HashMap srcAdj=this.adjacent[src];
|
|
int taille=srcAdj.size();
|
|
int adj=-1;
|
|
Object[] adjacents=srcAdj.keySet().toArray();
|
|
|
|
if (taille>dst.size()){
|
|
//y a des sommets qui ne sont pas dans le tableau
|
|
return -1;
|
|
} else {
|
|
for (int i=0; i<taille; i++){
|
|
if (!dst.contains(adjacents[i])){
|
|
return -1;
|
|
} else {
|
|
if (adj==-1){
|
|
adj=((Integer)adjacents[i]).intValue();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return adj;
|
|
}
|
|
|
|
/**
|
|
* Fonction qui renvoie la source (sommet racine) de ce graphe
|
|
**/
|
|
public int getSourceVertice(){
|
|
int i = -1;
|
|
if ((this.cardV != 0)&&(this.incident != null)){
|
|
for (int j=0; j<this.cardV; j++){
|
|
if (this.incident[j]==null){
|
|
i=j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
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)&&(this.adjacent != null)){
|
|
for (int j=0; j<this.cardV; j++){
|
|
if (this.adjacent[j]==null){
|
|
i=j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return i;
|
|
}
|
|
|
|
/**
|
|
* Fonction qui renvoie la liste d'adjacence du sommet srcV
|
|
* ou -1 s'il n'existe pas
|
|
**/
|
|
public Collection getAdjacent(int srcV){
|
|
HashMap adj= this.adjacent[srcV];
|
|
if (adj==null){ return null; }
|
|
else { return adj.keySet(); }
|
|
}
|
|
|
|
/**
|
|
* Fonction qui renvoie le premier sommet adjacent a srcV
|
|
**/
|
|
public int getFirstAdjacentVertice(int srcV){
|
|
Iterator i=this.adjacent[srcV].keySet().iterator();
|
|
return ((Integer)i.next()).intValue();
|
|
}
|
|
|
|
/**
|
|
* Fonction qui renvoie la liste de sommets ayant un arc vers
|
|
* sommet dstV
|
|
**/
|
|
public Collection getIncident(int dstV){
|
|
HashMap inc= this.incident[dstV];
|
|
if (inc==null){ return null; }
|
|
else { return inc.keySet(); }
|
|
}
|
|
|
|
/**
|
|
* Fonction qui enleve tous les arcs du graphe
|
|
**/
|
|
public void clear(){
|
|
for (int i=0; i<cardV; i++){
|
|
this.adjacent[i].clear();
|
|
this.incident[i].clear();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fonction qui rajoute tous les arcs possibles du graphe
|
|
**/
|
|
public void fill(){
|
|
for (int src=0; src<cardV; src++){
|
|
for (int dst=0; dst<cardV; dst++){
|
|
if (src==dst) {
|
|
continue;
|
|
} else {
|
|
if (this.adjacent[src]==null){
|
|
this.adjacent[src]=new HashMap();
|
|
}
|
|
if (this.incident[dst]==null){
|
|
this.adjacent[dst]=new HashMap();
|
|
}
|
|
HashMap srcList=this.adjacent[src];
|
|
HashMap dstList=this.incident[dst];
|
|
srcList.put(new Integer(dst),null);
|
|
dstList.put(new Integer(src),null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Fonction qui rajoute un vertice a la fin de table d'adjacence
|
|
* Liste d'adjacence de ce nouveau vertice est 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)){
|
|
System.out.println("ADDVERTICE: duplicating size: "+ this.cardV+"/"+this.adjacent.length+"...");
|
|
int extra=2*this.adjacent.length;
|
|
HashMap[] adjCopy = new HashMap[this.adjacent.length + extra];
|
|
HashMap[] incCopy = new HashMap[this.adjacent.length + extra];
|
|
System.arraycopy(this.adjacent,0,adjCopy,0,this.adjacent.length);
|
|
System.arraycopy(this.incident,0,incCopy,0,this.incident.length);
|
|
this.adjacent=adjCopy;
|
|
this.incident=incCopy;
|
|
System.out.println("ADDVERTICE: duplicating done.");
|
|
}
|
|
this.cardV++;
|
|
}
|
|
|
|
public void removeVerticeAt(int index)
|
|
throws OutOfRangeVerticeException{
|
|
//TODO: écrire!!!
|
|
/*
|
|
if (index >= this.cardV){
|
|
throw (new OutOfRangeVerticeException());
|
|
} else {
|
|
|
|
}*/
|
|
}
|
|
}
|
|
|