Fix infinite path in Dijkstra algorithm
This commit is contained in:
1 parent
11200ebed2
commit
c7134e9e91
3 files changed
+12
-5
No files matched your search
@@ -22,7 +22,7 @@ public class Dijkstra {
|
|||||||
int n = g.getVertexNumber();
|
int n = g.getVertexNumber();
|
||||||
|
|
||||||
for (int j = 0; j < n; j++) {
|
for (int j = 0; j < n; j++) {
|
||||||
for (VertexInterface vertex: g.generateNeighbours(pivot)) {
|
for (VertexInterface vertex: pivot.generateNeighbours()) {
|
||||||
if (!a.contains(vertex)) {
|
if (!a.contains(vertex)) {
|
||||||
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
|
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
|
||||||
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
|
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package dijkstra;
|
package dijkstra;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This describes a vertex as seen in graphs (see INF101 course).
|
* This describes a vertex as seen in graphs (see INF101 course).
|
||||||
*
|
*
|
||||||
@@ -11,4 +13,9 @@ public interface VertexInterface {
|
|||||||
*/
|
*/
|
||||||
public String getLabel() ;
|
public String getLabel() ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the attribute of the element.
|
||||||
|
* @return The neighbourList of the element.
|
||||||
|
*/
|
||||||
|
public ArrayList<VertexInterface> generateNeighbours();
|
||||||
}
|
}
|
||||||
+4
-4
@@ -22,7 +22,7 @@ public abstract class Box
|
|||||||
private int y ;
|
private int y ;
|
||||||
private String label = null ;
|
private String label = null ;
|
||||||
private Maze maze ;
|
private Maze maze ;
|
||||||
private ArrayList<Box> neighbourList = new ArrayList<Box>() ;
|
private ArrayList<VertexInterface> neighbourList;
|
||||||
|
|
||||||
public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation
|
public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation
|
||||||
|
|
||||||
@@ -33,9 +33,9 @@ public abstract class Box
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<VertexInterface> generateNeighbors() {
|
public ArrayList<VertexInterface> generateNeighbours() {
|
||||||
|
this.neighbourList = maze.generateNeighbours(this);
|
||||||
return maze.generateNeighbours(this) ;
|
return maze.generateNeighbours(this) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isWalkable() {
|
public boolean isWalkable() {
|
||||||
@@ -50,7 +50,7 @@ public abstract class Box
|
|||||||
return false ;
|
return false ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Box> getNeighbourList() {
|
public ArrayList<VertexInterface> getNeighbourList() {
|
||||||
return neighbourList;
|
return neighbourList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user