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
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

View file

@ -29,23 +29,31 @@ public class Path {
*/
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
if (nodes.size() <= 2){
throw new IllegalArgumentException();
}else{
for (int j = 0; j < nodes.size(); j++){
Node premier = nodes.get(j);
List<Arc> suc = premier.getSuccessors();
Arc elu = suc.get(0);
for (int i=0; i < suc.size(); i++){
if (nodes.isEmpty()){
return new Path(graph);
}
if (nodes.size()==1){
return new Path(graph,nodes.get(0));
}
List<Arc> arcs = new ArrayList<>();
for (int i=0;i<nodes.size()-1;i++){
Node noeud1=nodes.get(i);
Node suivant=nodes.get(i+1);
Arc choisi=null;
if ( suc.get(i).getMinimumTravelTime() <= elu.getMinimumTravelTime()){
elu = suc.get(i);
}
arcs.add(elu);
for (Arc arc :noeud1.getSuccessors()){
if (arc.getDestination().equals(suivant)){
if(choisi==null|| arc.getMinimumTravelTime()<choisi.getMinimumTravelTime()){
choisi=arc;
}
}
}
if (choisi==null){
throw new IllegalArgumentException();
}
arcs.add(choisi);
}
return new Path(graph, arcs);
}
@ -62,24 +70,30 @@ public class Path {
*/
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
if (nodes.size() <= 2){
if (nodes.isEmpty()){
return new Path(graph);
}
if (nodes.size()==1){
return new Path(graph,nodes.get(0));
}
List<Arc> arcs = new ArrayList<>();
for (int i=0;i<nodes.size()-1;i++){
Node noeud1=nodes.get(i);
Node suivant=nodes.get(i+1);
Arc choisi=null;
for (Arc arc :noeud1.getSuccessors()){
if (arc.getDestination().equals(suivant)){
if(choisi==null|| arc.getLength()<choisi.getLength()){
choisi=arc;
}
}
}
if (choisi==null){
throw new IllegalArgumentException();
}else{
for (int j = 0; j < nodes.size(); j++){
Node premier = nodes.get(j);
List<Arc> suc = premier.getSuccessors();
Arc elu = suc.get(0);
for (int i=0; i < suc.size(); i++){
if ( suc.get(i).getLength() <= elu.getLength()){
elu = suc.get(i);
}
arcs.add(elu);
arcs.add(choisi);
}
}
}
return new Path(graph, arcs);
}
@ -219,33 +233,26 @@ public class Path {
* @deprecated Need to be implemented.
*/
public boolean isValid() {
if(this.arcs.isEmpty()){
return true ;
// Cas 1 : chemin vide
if (this.isEmpty()) {
return true;
}
if ( this.graph.getNodes().size() <= 1 ){
return true ;
// Cas 2 : nœud unique, pas d'arcs
if (this.arcs.isEmpty()) {
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 ;
}
}
}
// 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).

View file

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