shortest path
This commit is contained in:
parent
96dc076c30
commit
8ca8f98b7e
1 changed files with 16 additions and 4 deletions
|
@ -51,14 +51,23 @@ public class Path {
|
||||||
* @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>();
|
List<Arc> arcs = new ArrayList<Arc>();
|
||||||
Arc candidat = null;
|
List<Node> nodes2 = nodes;
|
||||||
Node deb = nodes.remove(0);
|
Arc candidat = null; //arc candidat à être le plus court entre deux nodes
|
||||||
for(Node fin : nodes){
|
|
||||||
|
if(nodes.size()==0){
|
||||||
|
return new Path(graph);
|
||||||
|
}
|
||||||
|
else if(nodes.size()==1){
|
||||||
|
return new Path(graph,nodes.get(0));
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
Node deb = nodes2.get(0);
|
||||||
|
for(Node fin : nodes2){
|
||||||
|
if(!fin.equals(deb)){
|
||||||
float dist = Float.MAX_VALUE;
|
float dist = Float.MAX_VALUE;
|
||||||
for(Arc a : deb.getSuccessors()){
|
for(Arc a : deb.getSuccessors()){
|
||||||
if(a.getDestination().equals(fin)&&dist>a.getLength()){
|
if(a.getDestination().equals(fin)&&dist>a.getLength()){
|
||||||
|
@ -66,12 +75,15 @@ public class Path {
|
||||||
candidat=a;
|
candidat=a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dist==Float.MAX_VALUE){
|
if(dist==Float.MAX_VALUE){
|
||||||
throw new IllegalArgumentException("Liste de noeuds non valide");
|
throw new IllegalArgumentException("Liste de noeuds non valide");
|
||||||
}
|
}
|
||||||
arcs.add(candidat);
|
arcs.add(candidat);
|
||||||
deb = fin;
|
deb = fin;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue