shortest
This commit is contained in:
parent
c39df6a694
commit
ba0702c092
1 changed files with 104 additions and 11 deletions
|
@ -1,5 +1,7 @@
|
||||||
package org.insa.graphs.model;
|
package org.insa.graphs.model;
|
||||||
|
|
||||||
|
import java.awt.desktop.PrintFilesEvent;
|
||||||
|
import java.time.chrono.ThaiBuddhistChronology;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -25,12 +27,13 @@ public class Path {
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||||
* consecutive nodes in the list are not connected in the graph.
|
* consecutive nodes in the list are not connected in the graph.
|
||||||
* @deprecated Need to be implemented.
|
|
||||||
*/
|
*/
|
||||||
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>();
|
List<Arc> arcs = new ArrayList<Arc>();
|
||||||
// TODO:
|
// TODO:
|
||||||
|
|
||||||
|
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,13 +46,77 @@ public class Path {
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||||
* consecutive nodes in the list are not connected in the graph.
|
* consecutive nodes in the list are not connected in the graph.
|
||||||
* @deprecated Need to be implemented.
|
|
||||||
*/
|
*/
|
||||||
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>();
|
|
||||||
// TODO:
|
if (nodes.size() == 1) {
|
||||||
return new Path(graph, arcs);
|
Node node = nodes.get(0);
|
||||||
|
return new Path(graph, node);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
|
||||||
|
|
||||||
|
List<Arc> arcs = new ArrayList<Arc>();
|
||||||
|
|
||||||
|
// Node leorigin = nodes.get(0);
|
||||||
|
Node ledest = nodes.get(nodes.size() - 1);
|
||||||
|
boolean hsein = true;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (hsein && i < nodes.size()) {
|
||||||
|
float longueurmin = Float.MAX_VALUE;
|
||||||
|
if (!(nodes.get(i).hasSuccessors()) && nodes.get(i) != ledest) {
|
||||||
|
hsein = false;
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The path is invalid: node " + nodes.get(i)
|
||||||
|
+ " has no successors but is not the destination.");
|
||||||
|
|
||||||
|
}
|
||||||
|
boolean bonsuccesseur = false;
|
||||||
|
// parcours de la liste des successeurs verification destinataire
|
||||||
|
if (nodes.get(i) != ledest) {
|
||||||
|
for (int k = 0; k < nodes.get(i).getSuccessors().size(); k++) {
|
||||||
|
if (i < nodes.size()) {
|
||||||
|
if (nodes.get(i).getSuccessors().get(k)
|
||||||
|
.getDestination() == nodes.get(i + 1)) {
|
||||||
|
bonsuccesseur = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// on continue si on a le bonsuccesseur sinon on a une exception
|
||||||
|
if (bonsuccesseur) {
|
||||||
|
Arc arcajouter = null;
|
||||||
|
for (int j = 0; j < nodes.get(i).getSuccessors().size(); j++) {
|
||||||
|
if (nodes.get(i).getSuccessors().get(j)
|
||||||
|
.getDestination() == nodes.get(i + 1)) {
|
||||||
|
if ((float) nodes.get(i).getSuccessors().get(j)
|
||||||
|
.getLength() < longueurmin) {
|
||||||
|
longueurmin =
|
||||||
|
nodes.get(i).getSuccessors().get(j).getLength();
|
||||||
|
arcajouter = nodes.get(i).getSuccessors().get(j);
|
||||||
|
// arcs.add(arcajouter);
|
||||||
|
// System.out.println(arcs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
arcs.add(arcajouter);
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Path(graph, arcs);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -150,7 +217,8 @@ public class Path {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return List of arcs in the path.
|
* @return List of arcs in the// System.out.println("aaaaaaaaaaaaa"); //
|
||||||
|
* System.out.println(arcs); path.
|
||||||
*/
|
*/
|
||||||
public List<Arc> getArcs() {
|
public List<Arc> getArcs() {
|
||||||
return Collections.unmodifiableList(arcs);
|
return Collections.unmodifiableList(arcs);
|
||||||
|
@ -184,13 +252,38 @@ public class Path {
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @return true if the path is valid, false otherwise.
|
* @return true if the path is valid, false otherwise.
|
||||||
* @deprecated Need to be implemented.
|
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
|
||||||
if (this.isEmpty() || !(this.origin.hasSuccessors()) || (this.origin()==this.arcs[0].getOrigin() && (for(i=0;i<this.arcs.getLength(),i++)) )){
|
|
||||||
|
|
||||||
};
|
public boolean tefeha() {
|
||||||
return false;
|
boolean myvar = true;
|
||||||
|
int i = 0;
|
||||||
|
while (myvar && i < this.arcs.size() - 1) {
|
||||||
|
|
||||||
|
if (this.arcs.get(i).getDestination() != this.arcs.get(i + 1).getOrigin()) {
|
||||||
|
myvar = false;
|
||||||
|
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return myvar;// System.out.println("aaaaaaaaaaaaa");
|
||||||
|
// System.out.println(arcs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isValid() {
|
||||||
|
boolean arcssuivi = false;
|
||||||
|
if (this.isEmpty()) {
|
||||||
|
arcssuivi = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (this.size() == 1) {
|
||||||
|
arcssuivi = true;
|
||||||
|
}
|
||||||
|
else if (this.origin == this.arcs.get(0).getOrigin() && tefeha()) {
|
||||||
|
arcssuivi = true;
|
||||||
|
}
|
||||||
|
return arcssuivi;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue