Methode shortestpath

This commit is contained in:
Auriane Lartigue 2020-03-16 16:08:04 +01:00
parent dd4052e4ff
commit 5457c37ba7
2 changed files with 32 additions and 4 deletions

View file

@ -51,13 +51,41 @@ public class Path {
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
* consecutive nodes in the list are not connected in the graph.
*
* @deprecated Need to be implemented.
* Need to be implemented.
*/
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
// TODO:
return new Path(graph, arcs);
if (nodes.size()==1){
return new Path(graph, nodes.get(0));
}
else {
for (int i = 0 ; i < nodes.size()-1; i++) {
double min = Double.POSITIVE_INFINITY;
boolean found = false;
if (nodes.get(i).getSuccessors().size() >= 1) {
Arc min_arc = null;
for(Arc a : nodes.get(i).getSuccessors()) {
if( a.getDestination()==nodes.get(i+1) ) {
if(a.getLength()<min) {
min_arc = a ; // arc dont la distance est la plus petite
min = a.getLength();
}
found = true;
}
}
if (!found) {
throw new IllegalArgumentException("two consecutive nodes in the list are not connected in the graph.");
}
arcs.add( min_arc );
}
}
return new Path(graph, arcs);
}
}
/**

View file

@ -236,7 +236,7 @@ public class PathTest {
@Test(expected = IllegalArgumentException.class)
public void testCreateShortestPathFromNodesException() {
Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
}
}