Implémentation de Dijkstra en cours

This commit is contained in:
Nabzzz 2020-04-13 10:33:09 +02:00
parent 48b3f25718
commit 1b6ff22b79
2 changed files with 46 additions and 15 deletions

View file

@ -8,6 +8,7 @@ import java.util.ArrayList;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
@ -24,32 +25,47 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ShortestPathSolution solution = null;
Graph graph = data.getGraph();
final int nbNodes=graph.size();
BinaryHeap<Node> tas=new BinaryHeap<Node>();
BinaryHeap<Label> tas=new BinaryHeap<Label>();
Label []tabLabel=new Label[nbNodes];
//Initilisation
//Initialization
for(Node n:graph.getNodes())
{
tabLabel[n.getId()]=new Label(n);
}
tabLabel[data.getOrigin().getId()].setCout(0);
tas.insert(data.getOrigin());
int nbMarque=0;
while(nbMarque!=nbNodes)
tas.insert(tabLabel[data.getOrigin().getId()]);
//int nbMarque=0;
while(!tabLabel[data.getDestination().getId()].isMarque())
{
Node X=tas.findMin();
Node X=tas.findMin().getSommetCourant();
tabLabel[X.getId()].setMarque(true);
nbMarque++;
//nbMarque++;
try
{
tas.remove(tabLabel[X.getId()]);
}
catch(ElementNotFoundException e) {System.out.println("The element was not found in the binary heap \n");}
for(Arc a: X.getSuccessors())
{
Node Y=a.getDestination();
if(!tabLabel[Y.getId()].isMarque())
{
//double cout_avant=tabLabel[Y.getId()].getCout();
if((tabLabel[X.getId()].getCout()+a.getLength())<tabLabel[Y.getId()].getCout())
{
tabLabel[Y.getId()].setCout(tabLabel[X.getId()].getCout()+a.getLength());
tas.insert(Y);
tabLabel[Y.getId()].setPere(a);
if(((tabLabel[X.getId()].getCout()+a.getLength())<tabLabel[Y.getId()].getCout()) && data.isAllowed(a))
{ tabLabel[Y.getId()].setCout(tabLabel[X.getId()].getCout()+a.getLength());
try
{
tas.remove(tabLabel[Y.getId()]);
tas.insert(tabLabel[Y.getId()]);
tabLabel[Y.getId()].setPere(a);
}
catch(ElementNotFoundException e) {tas.insert(tabLabel[Y.getId()]);
tabLabel[Y.getId()].setPere(a);}
}
}
@ -57,7 +73,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
// Destination has no predecessor, the solution is infeasible...
if (tabLabel[data.getDestination().getId()].getPere() == null) {
if (tabLabel[data.getDestination().getId()].getPere() == null) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {

View file

@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc;
public class Label {
public class Label implements java.lang.Comparable<Label>{
private Node sommetCourant;
private boolean isMarque;
private double cout;
@ -44,7 +44,22 @@ public class Label {
public double getCout() {
return cout;
}
public int compareTo(Label B)
{
if(this.getCout()<B.getCout())
{
return -1;
}
else if(this.getCout()==B.getCout())
{
return 0;
}
else
{
return 1;
}
}
public void setCout(double cout) {
this.cout = cout;