implémentation des fonctions dans la classe path+remove dans BinaryHeap

This commit is contained in:
Benmouffok Helwen 2026-05-12 18:14:08 +02:00
parent b059a07cf1
commit e24134bf5e
4 changed files with 71 additions and 54 deletions

View file

@ -134,7 +134,16 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
@Override @Override
public void remove(E x) throws ElementNotFoundException { public void remove(E x) throws ElementNotFoundException {
// TODO: int index =this.array.indexOf(x);
if (index==-1||index>this.currentSize-1) {
throw new ElementNotFoundException(x);
}
E lastItem=this.array.get(--this.currentSize);
this.arraySet(index, lastItem);
this.percolateUp(index);
this.percolateDown(index);
} }
@Override @Override

View file

@ -29,23 +29,31 @@ public class Path {
*/ */
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes) public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException { throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>(); if (nodes.isEmpty()){
if (nodes.size() <= 2){ return new Path(graph);
throw new IllegalArgumentException(); }
}else{ if (nodes.size()==1){
for (int j = 0; j < nodes.size(); j++){ return new Path(graph,nodes.get(0));
Node premier = nodes.get(j); }
List<Arc> suc = premier.getSuccessors(); List<Arc> arcs = new ArrayList<>();
Arc elu = suc.get(0); for (int i=0;i<nodes.size()-1;i++){
for (int i=0; i < suc.size(); i++){ Node noeud1=nodes.get(i);
Node suivant=nodes.get(i+1);
Arc choisi=null;
if ( suc.get(i).getMinimumTravelTime() <= elu.getMinimumTravelTime()){ for (Arc arc :noeud1.getSuccessors()){
elu = suc.get(i); if (arc.getDestination().equals(suivant)){
if(choisi==null|| arc.getMinimumTravelTime()<choisi.getMinimumTravelTime()){
choisi=arc;
} }
arcs.add(elu);
} }
} }
if (choisi==null){
throw new IllegalArgumentException();
}
arcs.add(choisi);
} }
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -62,25 +70,31 @@ public class Path {
*/ */
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes) public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException { throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>(); if (nodes.isEmpty()){
if (nodes.size() <= 2){ return new Path(graph);
throw new IllegalArgumentException(); }
}else{ if (nodes.size()==1){
for (int j = 0; j < nodes.size(); j++){ return new Path(graph,nodes.get(0));
Node premier = nodes.get(j); }
List<Arc> suc = premier.getSuccessors(); List<Arc> arcs = new ArrayList<>();
Arc elu = suc.get(0); for (int i=0;i<nodes.size()-1;i++){
for (int i=0; i < suc.size(); i++){ Node noeud1=nodes.get(i);
Node suivant=nodes.get(i+1);
Arc choisi=null;
if ( suc.get(i).getLength() <= elu.getLength()){ for (Arc arc :noeud1.getSuccessors()){
elu = suc.get(i); if (arc.getDestination().equals(suivant)){
if(choisi==null|| arc.getLength()<choisi.getLength()){
choisi=arc;
} }
arcs.add(elu);
} }
} }
if (choisi==null){
throw new IllegalArgumentException();
}
arcs.add(choisi);
} }
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -218,34 +232,27 @@ public class Path {
* @return true if the path is valid, false otherwise. * @return true if the path is valid, false otherwise.
* @deprecated Need to be implemented. * @deprecated Need to be implemented.
*/ */
public boolean isValid() { public boolean isValid() {
if(this.arcs.isEmpty()){ // Cas 1 : chemin vide
return true ; if (this.isEmpty()) {
} return true;
if ( this.graph.getNodes().size() <= 1 ){
return true ;
}
Node origin = this.graph.getNodes().getFirst(); // à revoir ( on prend deux arcs quelconque et compare )
List<Arc> firstSucs = origin.getSuccessors();
List<Node> destFirstArc = new ArrayList<>() ;
for( Arc fSuc : firstSucs){
destFirstArc.add(fSuc.getDestination()) ;
}
for (Node secondNode : destFirstArc){
List<Arc> secondSucs = secondNode.getSuccessors();
for (Arc sSuc : secondSucs){
if ( sSuc.getDestination() == origin){
return true ;
}
}
}
return false;
} }
// Cas 2 : nœud unique, pas d'arcs
if (this.arcs.isEmpty()) {
return true;
}
// Cas 3 : le premier arc doit partir de l'origine
if (!this.arcs.get(0).getOrigin().equals(this.origin)) {
return false;
}
// Cas 4 : chaque arc doit enchaîner sur le suivant
for (int i = 0; i < this.arcs.size() - 1; i++) {
if (!this.arcs.get(i).getDestination().equals(this.arcs.get(i + 1).getOrigin())) {
return false;
}
}
return true;
}
/** /**
* Compute the length of this path (in meters). * Compute the length of this path (in meters).

View file

@ -180,6 +180,7 @@ public class PathTest {
assertEquals(expected[i], path.getArcs().get(i)); assertEquals(expected[i], path.getArcs().get(i));
} }
// Not so simple construction // Not so simple construction
path = Path.createFastestPathFromNodes(graph, path = Path.createFastestPathFromNodes(graph,
Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] })); Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));