Fix infinite path in Dijkstra algorithm

This commit is contained in:
antograb committed 2017-12-27 15:13:40 +01:00
1 parent 11200ebed2
commit c7134e9e91
4 files changed
+14 -7

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ public class Dijkstra {
int n = g.getVertexNumber();
for (int j = 0; j < n; j++) {
for (VertexInterface vertex: g.generateNeighbours(pivot)) {
for (VertexInterface vertex: pivot.generateNeighbours()) {
if (!a.contains(vertex)) {
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
+8 -1
View File
@@ -1,5 +1,7 @@
package dijkstra;
import java.util.ArrayList;
/**
* This describes a vertex as seen in graphs (see INF101 course).
*
@@ -10,5 +12,10 @@ public interface VertexInterface {
* @return The label of the element.
*/
public String getLabel() ;
/**
* Updates the attribute of the element.
* @return The neighbourList of the element.
*/
public ArrayList<VertexInterface> generateNeighbours();
}
+4 -4
View File
@@ -22,7 +22,7 @@ public abstract class Box
private int y ;
private String label = null ;
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
@@ -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) ;
}
public boolean isWalkable() {
@@ -50,7 +50,7 @@ public abstract class Box
return false ;
}
public ArrayList<Box> getNeighbourList() {
public ArrayList<VertexInterface> getNeighbourList() {
return neighbourList;
}
+1 -1
View File
@@ -141,7 +141,7 @@ public class Maze
return neighbourList ;
}
/**Constructs the maze from a text file, according to each single character on each line.
* @param filename
* The path to the file containing the representation of the maze.