Added packages, Previous class and ASet class.
This commit is contained in:
1 parent
f252db5e3d
commit
acab098ea1
16 files changed
+99
-5
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package dijkstra;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import maze.Box;
|
||||
|
||||
public class ASet implements ASetInterface {
|
||||
|
||||
private HashSet<Box> vertexHashSet = new HashSet<Box>() ;
|
||||
|
||||
|
||||
public ASet(HashSet<Box> vertexHashSet) {
|
||||
|
||||
this.vertexHashSet = vertexHashSet;
|
||||
|
||||
}
|
||||
|
||||
public void add(VertexInterface vertex) {
|
||||
|
||||
vertexHashSet.add((Box)vertex) ;
|
||||
|
||||
}
|
||||
|
||||
public boolean contains(VertexInterface vertex) {
|
||||
|
||||
for (VertexInterface v: vertexHashSet) {
|
||||
if (v == vertex) return true ;
|
||||
}
|
||||
return false ;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dijkstra;
|
||||
|
||||
|
||||
public interface ASetInterface {
|
||||
|
||||
/** Add a vertex to the ASet */
|
||||
public void add(VertexInterface vertex) ;
|
||||
|
||||
/** Check if a vertex belongs to ASet */
|
||||
public boolean contains(VertexInterface vertex) ;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package dijkstra;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Dijkstra {
|
||||
|
||||
private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) {
|
||||
|
||||
a.add(r) ; ;
|
||||
VertexInterface pivot = r ;
|
||||
pi.setPi(0, r) ;
|
||||
|
||||
ArrayList<VertexInterface> vertexList = g.getVertexes() ;
|
||||
|
||||
for (VertexInterface vertex: vertexList) {
|
||||
if (vertex != r) {
|
||||
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)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int piMax = Integer.MAX_VALUE ;
|
||||
for (VertexInterface vertex: vertexList) {
|
||||
if (! a.contains(vertex) && pi.getPi(vertex) < piMax) {
|
||||
pivot = vertex ;
|
||||
piMax = pi.getPi(vertex) ;
|
||||
}
|
||||
}
|
||||
a.add(pivot);
|
||||
}
|
||||
|
||||
return previous ;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package dijkstra;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public interface GraphInterface {
|
||||
|
||||
/** Returns the number of Vertexes */
|
||||
public int getVertexNumber() ;
|
||||
|
||||
/** Return the cost between two vertexes */
|
||||
public int getCost(VertexInterface start, VertexInterface end) ;
|
||||
|
||||
/** Returns an list of Vertexes */
|
||||
public ArrayList<VertexInterface> getVertexes() ;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dijkstra;
|
||||
|
||||
public interface PiInterface {
|
||||
|
||||
/** Returns the value of pi(v) */
|
||||
public int getPi(VertexInterface vertex) ;
|
||||
|
||||
/** Sets the Pi value of an element */
|
||||
public void setPi(int i, VertexInterface vertex) ;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package dijkstra;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class Previous implements PreviousInterface {
|
||||
|
||||
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
|
||||
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
|
||||
|
||||
public VertexInterface getPrevious(VertexInterface vertex) {
|
||||
|
||||
return hashPrevious.get(vertex) ;
|
||||
|
||||
}
|
||||
|
||||
public void setPrevious(VertexInterface father, VertexInterface son) {
|
||||
|
||||
hashPrevious.put(son, father) ;
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) {
|
||||
|
||||
VertexInterface father = getPrevious(vertex) ;
|
||||
shortestPath.add(father) ;
|
||||
|
||||
if (getPrevious(father) == null) return shortestPath ;
|
||||
else {
|
||||
return getShortestPathTo(father) ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dijkstra;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public interface PreviousInterface {
|
||||
|
||||
/** Returns the father of an element */
|
||||
public VertexInterface getPrevious(VertexInterface vertex) ;
|
||||
|
||||
/** Sets the father of an element to another element */
|
||||
public void setPrevious(VertexInterface father, VertexInterface son) ;
|
||||
|
||||
/**Shortest path from root to a vertex */
|
||||
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dijkstra;
|
||||
|
||||
public interface VertexInterface {
|
||||
|
||||
/** Compare a Vertex to another */
|
||||
public boolean compareTo(VertexInterface vertex) ;
|
||||
|
||||
public String getLabel() ;
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user