Fixed last dijkstra classes. Algorithm working for squared maze but bugs for non square mazes.
This commit is contained in:
1 parent
5d7c970e1b
commit
d7368f7cda
10 files changed
+64
-54
No files matched your search
@@ -6,10 +6,10 @@ import maze.Box;
|
||||
|
||||
public class ASet implements ASetInterface {
|
||||
|
||||
private HashSet<Box> vertexHashSet = new HashSet<Box>() ;
|
||||
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
||||
|
||||
|
||||
public ASet(HashSet<Box> vertexHashSet) {
|
||||
public ASet(HashSet<VertexInterface> vertexHashSet) {
|
||||
|
||||
this.vertexHashSet = vertexHashSet;
|
||||
|
||||
@@ -23,11 +23,13 @@ public class ASet implements ASetInterface {
|
||||
|
||||
public boolean contains(VertexInterface vertex) {
|
||||
|
||||
for (VertexInterface v: vertexHashSet) {
|
||||
if (v == vertex) return true ;
|
||||
}
|
||||
if (vertexHashSet.contains(vertex)) return true ;
|
||||
return false ;
|
||||
|
||||
}
|
||||
|
||||
public HashSet<VertexInterface> getVertexHashSet() {
|
||||
return vertexHashSet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package dijkstra;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public interface ASetInterface {
|
||||
|
||||
@@ -8,6 +9,8 @@ public interface ASetInterface {
|
||||
|
||||
/** Check if a vertex belongs to ASet */
|
||||
public boolean contains(VertexInterface vertex) ;
|
||||
|
||||
public HashSet<VertexInterface> getVertexHashSet();
|
||||
|
||||
|
||||
}
|
||||
@@ -3,13 +3,11 @@ import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import maze.Box;
|
||||
|
||||
public class Dijkstra {
|
||||
|
||||
private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) {
|
||||
|
||||
a.add(r) ; ;
|
||||
a.add(r) ;
|
||||
VertexInterface pivot = r ;
|
||||
pi.setPi(0, r) ;
|
||||
|
||||
@@ -20,11 +18,12 @@ public class Dijkstra {
|
||||
pi.setPi(Integer.MAX_VALUE, vertex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int n = g.getVertexNumber();
|
||||
|
||||
for (int j = 0; j < n; j++) {
|
||||
for (VertexInterface vertex: vertexList) {
|
||||
if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) {
|
||||
for (VertexInterface vertex: g.generateNeighbours(pivot)) {
|
||||
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) ;
|
||||
previous.setPrevious(pivot, vertex);
|
||||
@@ -48,7 +47,7 @@ public class Dijkstra {
|
||||
|
||||
public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) {
|
||||
|
||||
HashSet<Box> vertexHashSet = new HashSet<Box>() ;
|
||||
HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
||||
ASet aSet = new ASet(vertexHashSet) ;
|
||||
|
||||
Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ;
|
||||
|
||||
@@ -3,13 +3,16 @@ import java.util.ArrayList;
|
||||
|
||||
public interface GraphInterface {
|
||||
|
||||
/** Returns the number of Vertexes */
|
||||
/** Returns the number of Vertexes **/
|
||||
public int getVertexNumber() ;
|
||||
|
||||
/** Return the cost between two vertexes */
|
||||
/** Return the cost between two vertexes **/
|
||||
public int getCost(VertexInterface start, VertexInterface end) ;
|
||||
|
||||
/** Returns an list of Vertexes */
|
||||
/** Returns an list of Vertexes **/
|
||||
public ArrayList<VertexInterface> getVertexes() ;
|
||||
|
||||
/** Returns the neighbour list for a given box */
|
||||
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) ;
|
||||
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package dijkstra;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
|
||||
import maze.Box;
|
||||
|
||||
public class Previous implements PreviousInterface {
|
||||
|
||||
@@ -11,7 +14,7 @@ public class Previous implements PreviousInterface {
|
||||
|
||||
|
||||
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
|
||||
super();
|
||||
|
||||
this.hashPrevious = hashPrevious;
|
||||
this.shortestPath = shortestPath;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ public interface PreviousInterface {
|
||||
public void setPrevious(VertexInterface father, VertexInterface son) ;
|
||||
|
||||
/**Shortest path from root to a vertex */
|
||||
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ;
|
||||
|
||||
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ;
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
package dijkstra;
|
||||
import java.util.List;
|
||||
|
||||
public interface VertexInterface {
|
||||
|
||||
@@ -6,5 +7,4 @@ public interface VertexInterface {
|
||||
public boolean compareTo(VertexInterface vertex) ;
|
||||
|
||||
public String getLabel() ;
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user