This commit is contained in:
Bensouda Idriss 2023-04-12 10:46:47 +02:00
parent 36249318ce
commit b29967f627
8 changed files with 104 additions and 15 deletions

35
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,35 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "BinaryHeapFormatter",
"request": "launch",
"mainClass": "org.insa.graphs.algorithm.utils.BinaryHeapFormatter",
"projectName": "be-graphes-algos"
},
{
"type": "java",
"name": "MainWindow",
"request": "launch",
"mainClass": "org.insa.graphs.gui.MainWindow",
"projectName": "be-graphes-gui"
},
{
"type": "java",
"name": "Launch",
"request": "launch",
"mainClass": "org.insa.graphs.gui.simple.Launch",
"projectName": "be-graphes-gui"
}
]
}

View file

@ -90,10 +90,11 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
// Reverse the path...
Collections.reverse(arcs);
System.out.println(arcs);
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}
return solution;
}

View file

@ -1,10 +1,13 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import java.util.ArrayList;
import java.util.List;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -20,17 +23,61 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ArrayList<Label> List = new ArrayList<Label>(); //List de labels
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
ArrayList<Arc> arcs = new ArrayList<Arc>();
for (Node x: data.getGraph().getNodes())
{
Label a= new Label(x,false,0,null);
Label a= new Label(x,Double.MAX_VALUE,null);
List.add(a);
}
List.get(0).setCost(0);
Tas.insert(List.get(0));
int i = 1;
Label x;
while (MarqueExiste(List)){
x = Tas.findMin();
x.marque = true;
Tas.deleteMin();
for(Arc suivant : x.sommet.getSuccessors()){
Node y = suivant.getDestination();
for (Label l : List){
if (l.getSommet()==y){
if(!l.marque){
double cout = l.getCost();
System.out.println("c = " + cout);
l.cost = Math.min(l.getCost(), x.getCost()+suivant.getLength());
System.out.println("l = " + l.getCost());
System.out.println("x = " + x.cost);
if(cout != l.getCost()){
if (arcs.size() < i ){
System.out.println("arc");
}else if (cout < arcs.get(i-1).getLength()){
arcs.remove(i-1);
}
Tas.insert(l);
l.parent = suivant;
arcs.add(suivant);
l.marque = true;
}
}
}
}
}
i++;
}
System.out.println(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), arcs));
return solution;
}
public boolean MarqueExiste(ArrayList<Label> list){
boolean existe = false;
for (Label l : list){
if (l.isMarque() == false){
existe = true;
}
}
return existe;
}
}

View file

@ -6,24 +6,31 @@ import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label implements Comparable<Label> {
private Node sommet;
private boolean marque;
private double cost;
private Arc parent;
protected Node sommet;
protected boolean marque;
protected double cost;
protected Arc parent;
public Label(Node sommet, boolean marque, double cost, Arc parent) {
public Label(Node sommet, double cost, Arc parent) {
this.sommet = sommet;
this.marque = marque;
this.marque = false;
this.cost = cost;
this.parent = parent;
}
@Override
public int compareTo(Label arg0) {
// TODO Auto-generated method stub
return 0;
public int compareTo(Label a) {
int result;
if (a.getCost() < cost){
result = 1;
}else if (a.getCost()>cost){
result = -1;
}else {
result = 0;
}
return result;
}
public Node getSommet() {

View file

@ -136,7 +136,6 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
}
@Override
// on est : tout faux utilise paircolletdown
public void remove(E x) throws ElementNotFoundException {
int id = array.indexOf(x);
if (this.isEmpty() || this.size()==0 || id >= currentSize || id == -1){