djikf
This commit is contained in:
parent
36249318ce
commit
b29967f627
8 changed files with 104 additions and 15 deletions
35
.vscode/launch.json
vendored
Normal file
35
.vscode/launch.json
vendored
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -90,10 +90,11 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
|
||||||
// Reverse the path...
|
// Reverse the path...
|
||||||
Collections.reverse(arcs);
|
Collections.reverse(arcs);
|
||||||
|
|
||||||
|
System.out.println(arcs);
|
||||||
|
|
||||||
// Create the final solution.
|
// Create the final solution.
|
||||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
||||||
}
|
}
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
|
import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
import org.insa.graphs.model.Path;
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
|
|
@ -20,17 +23,61 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
ArrayList<Label> List = new ArrayList<Label>(); //List de labels
|
ArrayList<Label> List = new ArrayList<Label>(); //List de labels
|
||||||
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
|
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
|
||||||
|
ArrayList<Arc> arcs = new ArrayList<Arc>();
|
||||||
|
|
||||||
for (Node x: data.getGraph().getNodes())
|
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.add(a);
|
||||||
|
|
||||||
}
|
}
|
||||||
List.get(0).setCost(0);
|
List.get(0).setCost(0);
|
||||||
Tas.insert(List.get(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;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean MarqueExiste(ArrayList<Label> list){
|
||||||
|
boolean existe = false;
|
||||||
|
for (Label l : list){
|
||||||
|
if (l.isMarque() == false){
|
||||||
|
existe = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return existe;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,24 +6,31 @@ import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class Label implements Comparable<Label> {
|
public class Label implements Comparable<Label> {
|
||||||
private Node sommet;
|
protected Node sommet;
|
||||||
private boolean marque;
|
protected boolean marque;
|
||||||
private double cost;
|
protected double cost;
|
||||||
private Arc parent;
|
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.sommet = sommet;
|
||||||
this.marque = marque;
|
this.marque = false;
|
||||||
this.cost = cost;
|
this.cost = cost;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Label arg0) {
|
public int compareTo(Label a) {
|
||||||
// TODO Auto-generated method stub
|
int result;
|
||||||
return 0;
|
if (a.getCost() < cost){
|
||||||
|
result = 1;
|
||||||
|
}else if (a.getCost()>cost){
|
||||||
|
result = -1;
|
||||||
|
}else {
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getSommet() {
|
public Node getSommet() {
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,6 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// on est là : tout faux utilise paircolletdown
|
|
||||||
public void remove(E x) throws ElementNotFoundException {
|
public void remove(E x) throws ElementNotFoundException {
|
||||||
int id = array.indexOf(x);
|
int id = array.indexOf(x);
|
||||||
if (this.isEmpty() || this.size()==0 || id >= currentSize || id == -1){
|
if (this.isEmpty() || this.size()==0 || id >= currentSize || id == -1){
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue