Merge the last Dijkstra classes #7

Merged
antograb merged 18 commits from lastDijsktraClasses into master 2017-12-10 16:51:59 +00:00
10 changed files with 61 additions and 51 deletions
Showing only changes of commit d7368f7cda - Show all commits

No files matched your search

+2 -1
View File
@@ -1,3 +1,4 @@
import dijkstra.Dijkstra;
import maze.Maze; import maze.Maze;
public class MainTest { public class MainTest {
@@ -6,7 +7,7 @@ public class MainTest {
Maze mazeTest = new Maze("data/labyrinthe.txt") ; Maze mazeTest = new Maze("data/labyrinthe.txt") ;
System.out.println("\n" + mazeTest); Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]) ;
mazeTest.saveToTextFile("data/labyOutput.txt"); mazeTest.saveToTextFile("data/labyOutput.txt");
+7 -5
View File
@@ -6,10 +6,10 @@ import maze.Box;
public class ASet implements ASetInterface { 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; this.vertexHashSet = vertexHashSet;
@@ -23,11 +23,13 @@ public class ASet implements ASetInterface {
public boolean contains(VertexInterface vertex) { public boolean contains(VertexInterface vertex) {
for (VertexInterface v: vertexHashSet) { if (vertexHashSet.contains(vertex)) return true ;
if (v == vertex) return true ;
}
return false ; return false ;
} }
public HashSet<VertexInterface> getVertexHashSet() {
return vertexHashSet;
}
} }
+3
View File
@@ -1,5 +1,6 @@
package dijkstra; package dijkstra;
import java.util.HashSet;
public interface ASetInterface { public interface ASetInterface {
@@ -9,5 +10,7 @@ public interface ASetInterface {
/** Check if a vertex belongs to ASet */ /** Check if a vertex belongs to ASet */
public boolean contains(VertexInterface vertex) ; public boolean contains(VertexInterface vertex) ;
public HashSet<VertexInterface> getVertexHashSet();
} }
+5 -6
View File
@@ -3,13 +3,11 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
import maze.Box;
public class Dijkstra { public class Dijkstra {
private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) {
a.add(r) ; ; a.add(r) ;
VertexInterface pivot = r ; VertexInterface pivot = r ;
pi.setPi(0, r) ; pi.setPi(0, r) ;
@@ -22,9 +20,10 @@ 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: vertexList) { for (VertexInterface vertex: g.generateNeighbours(pivot)) {
if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) { 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) ;
previous.setPrevious(pivot, vertex); previous.setPrevious(pivot, vertex);
@@ -48,7 +47,7 @@ public class Dijkstra {
public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) { 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) ; ASet aSet = new ASet(vertexHashSet) ;
Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ; Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ;
+6 -3
View File
@@ -3,13 +3,16 @@ import java.util.ArrayList;
public interface GraphInterface { public interface GraphInterface {
/** Returns the number of Vertexes */ /** Returns the number of Vertexes **/
public int getVertexNumber() ; public int getVertexNumber() ;
/** Return the cost between two vertexes */ /** Return the cost between two vertexes **/
public int getCost(VertexInterface start, VertexInterface end) ; public int getCost(VertexInterface start, VertexInterface end) ;
/** Returns an list of Vertexes */ /** Returns an list of Vertexes **/
public ArrayList<VertexInterface> getVertexes() ; public ArrayList<VertexInterface> getVertexes() ;
/** Returns the neighbour list for a given box */
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) ;
} }
+4 -1
View File
@@ -2,6 +2,9 @@ package dijkstra;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List;
import maze.Box;
public class Previous implements PreviousInterface { public class Previous implements PreviousInterface {
@@ -11,7 +14,7 @@ public class Previous implements PreviousInterface {
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) { public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
super();
this.hashPrevious = hashPrevious; this.hashPrevious = hashPrevious;
this.shortestPath = shortestPath; this.shortestPath = shortestPath;
} }
-1
View File
@@ -13,5 +13,4 @@ public interface PreviousInterface {
/**Shortest path from root to a vertex */ /**Shortest path from root to a vertex */
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ; public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ;
} }
+1 -1
View File
@@ -1,4 +1,5 @@
package dijkstra; package dijkstra;
import java.util.List;
public interface VertexInterface { public interface VertexInterface {
@@ -6,5 +7,4 @@ public interface VertexInterface {
public boolean compareTo(VertexInterface vertex) ; public boolean compareTo(VertexInterface vertex) ;
public String getLabel() ; public String getLabel() ;
} }
+6 -23
View File
@@ -9,7 +9,7 @@ public abstract class Box
private int x ; private int x ;
private int y ; private int y ;
private String label ; private String label = null ;
private Maze maze ; private Maze maze ;
private List<Box> neighbourList = new ArrayList<Box>() ; private List<Box> neighbourList = new ArrayList<Box>() ;
@@ -22,27 +22,8 @@ public abstract class Box
} }
public List<Box> generateNeighbors() { public List<VertexInterface> generateNeighbors() {
return maze.generateNeighbours(this) ;
Box[][] vMaze = maze.getMaze() ;
if (x > 0 && vMaze[x-1][y].isWalkable()) {
neighbourList.add(vMaze[x-1][y]) ;
}
if (x < maze.getDimensionX()-1 && vMaze[x+1][y].isWalkable()) {
neighbourList.add(vMaze[x+1][y]) ;
}
if (y > 0 && vMaze[x][y-1].isWalkable()) {
neighbourList.add(vMaze[x][y-1]) ;
}
if (y < maze.getDimensionY()-1 && vMaze[x][y+1].isWalkable()) {
neighbourList.add(vMaze[x][y+1]) ;
}
return neighbourList ;
} }
@@ -105,6 +86,8 @@ public abstract class Box
this.maze = maze; this.maze = maze;
} }
public String toString() {
return label;
}
} }
+27 -10
View File
@@ -54,6 +54,33 @@ public class Maze
return vertexList ; return vertexList ;
} }
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
Box box = (Box)vertex ;
int x = box.getX() ;
int y = box.getY() ;
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ;
if (x > 0 && maze[x-1][y].isWalkable()) {
neighbourList.add(maze[x-1][y]) ;
}
if (x < dimensionX-1 && maze[x+1][y].isWalkable()) {
neighbourList.add(maze[x+1][y]) ;
}
if (y > 0 && maze[x][y-1].isWalkable()) {
neighbourList.add(maze[x][y-1]) ;
}
if (y < dimensionY-1 && maze[x][y+1].isWalkable()) {
neighbourList.add(maze[x][y+1]) ;
}
return neighbourList ;
}
public final void initFromTextFile(String filename) { public final void initFromTextFile(String filename) {
FileReader fr = null ; FileReader fr = null ;
@@ -77,9 +104,7 @@ public class Maze
String currentLine = null ; String currentLine = null ;
currentLine = br.readLine() ; currentLine = br.readLine() ;
System.out.println(currentLine);
dimensionX = currentLine.length() ; //maze's width dimensionX = currentLine.length() ; //maze's width
System.out.println("Line length :" + dimensionX);
int currentLineLength ; int currentLineLength ;
int compteurLigne = 0 ; int compteurLigne = 0 ;
@@ -90,7 +115,6 @@ public class Maze
//System.out.println(currentLine); //System.out.println(currentLine);
currentLineLength = currentLine.length() ; currentLineLength = currentLine.length() ;
System.out.println("Current line length :" + currentLineLength);
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) {
@@ -130,13 +154,6 @@ public class Maze
try { br.close() ;} catch (Exception e) {} try { br.close() ;} catch (Exception e) {}
} }
//neighbour generation for each box in the maze
for (int i = 0 ; i < dimensionX ; i++) {
for (int j = 0 ; j < dimensionY ; j++) {
maze[i][j].generateNeighbors() ;
}
System.out.println();
}
} }
public final void saveToTextFile(String fileName) { public final void saveToTextFile(String fileName) {