implémentation des fonctions dans la classe path+remove dans BinaryHeap
This commit is contained in:
parent
b059a07cf1
commit
e24134bf5e
4 changed files with 71 additions and 54 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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()){
|
||||||
arcs.add(elu);
|
choisi=arc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (choisi==null){
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
arcs.add(choisi);
|
||||||
|
}
|
||||||
|
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,24 +70,30 @@ 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);
|
||||||
|
}
|
||||||
|
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();
|
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);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
@ -219,33 +233,26 @@ public class Path {
|
||||||
* @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 ){
|
// Cas 2 : nœud unique, pas d'arcs
|
||||||
return true ;
|
if (this.arcs.isEmpty()) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
// Cas 3 : le premier arc doit partir de l'origine
|
||||||
Node origin = this.graph.getNodes().getFirst(); // à revoir ( on prend deux arcs quelconque et compare )
|
if (!this.arcs.get(0).getOrigin().equals(this.origin)) {
|
||||||
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;
|
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).
|
||||||
|
|
|
||||||
|
|
@ -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] }));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue