modified: createShortestPath()

This commit is contained in:
Nabzzz 2020-03-16 16:08:40 +01:00
parent 3dcba7891b
commit 37bb2f5f1c
2 changed files with 49 additions and 3 deletions

View file

@ -56,7 +56,38 @@ 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>(); List<Arc> arcs = new ArrayList<Arc>();
// TODO: double min_length;
Arc min_a=null;
Arc a;
boolean found;
if (nodes.size()==1)
{
return new Path(graph,nodes.get(0));
}
for(int i=0;i<nodes.size()-1;i++)
{ min_length=Double.POSITIVE_INFINITY;
found=false;
for(int j=0;j<nodes.get(i).getSuccessors().size();j++)
{
a=nodes.get(i).getSuccessors().get(j);
if(a.getDestination()==nodes.get(i+1))
{
if(a.getLength()<min_length)
{
min_length=a.getLength();
min_a=a;
found=true;
}
}
}
if(!found)
{
throw new IllegalArgumentException("Cannot generate arc from these two nodes");
}
arcs.add(min_a);
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -198,10 +229,25 @@ 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.
*/ */
public boolean isValid() { public boolean isValid() {
// TODO: if (this.isEmpty() || this.size()==1)
{
return true;
}
else if(this.getOrigin()==arcs.get(0).getOrigin())
{
for(int i=0;i<this.arcs.size()-1;i++)
{
if(this.arcs.get(i).getDestination()!=this.arcs.get(i+1).getOrigin())
{
return false;
}
}
return true;
}
return false; return false;
} }