diff --git a/src/MainTest.java b/src/MainTest.java index e25518d..3de2320 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,3 +1,4 @@ +import dijkstra.Dijkstra; import maze.Maze; public class MainTest { @@ -5,8 +6,8 @@ public class MainTest { public static void main(String[] args) { Maze mazeTest = new Maze("data/labyrinthe.txt") ; - - System.out.println("\n" + mazeTest); + + Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]) ; mazeTest.saveToTextFile("data/labyOutput.txt"); diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java index fb55d18..5dc12b5 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -6,10 +6,10 @@ import maze.Box; public class ASet implements ASetInterface { - private HashSet vertexHashSet = new HashSet() ; + private HashSet vertexHashSet = new HashSet() ; - public ASet(HashSet vertexHashSet) { + public ASet(HashSet 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 getVertexHashSet() { + return vertexHashSet; + } + } diff --git a/src/dijkstra/ASetInterface.java b/src/dijkstra/ASetInterface.java index 27eb66c..c09024c 100644 --- a/src/dijkstra/ASetInterface.java +++ b/src/dijkstra/ASetInterface.java @@ -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 getVertexHashSet(); } diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index e69f0a6..e91af31 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -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 vertexHashSet = new HashSet() ; + HashSet vertexHashSet = new HashSet() ; ASet aSet = new ASet(vertexHashSet) ; Hashtable hashPi = new Hashtable() ; diff --git a/src/dijkstra/GraphInterface.java b/src/dijkstra/GraphInterface.java index 55cfd7e..5c8c8e6 100644 --- a/src/dijkstra/GraphInterface.java +++ b/src/dijkstra/GraphInterface.java @@ -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 getVertexes() ; + + /** Returns the neighbour list for a given box */ + public ArrayList generateNeighbours(VertexInterface vertex) ; } diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index cc1637a..b6acaeb 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -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 hashPrevious, ArrayList shortestPath) { - super(); + this.hashPrevious = hashPrevious; this.shortestPath = shortestPath; } diff --git a/src/dijkstra/PreviousInterface.java b/src/dijkstra/PreviousInterface.java index 9d1f8c1..02c08b9 100644 --- a/src/dijkstra/PreviousInterface.java +++ b/src/dijkstra/PreviousInterface.java @@ -11,7 +11,6 @@ public interface PreviousInterface { public void setPrevious(VertexInterface father, VertexInterface son) ; /**Shortest path from root to a vertex */ - public ArrayList getShortestPathTo(VertexInterface vertex) ; - + public ArrayList getShortestPathTo(VertexInterface vertex) ; } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 84c7b11..e62827c 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -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() ; - } diff --git a/src/maze/Box.java b/src/maze/Box.java index b4a6abf..2326e21 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -9,7 +9,7 @@ public abstract class Box private int x ; private int y ; - private String label ; + private String label = null ; private Maze maze ; private List neighbourList = new ArrayList() ; @@ -22,27 +22,8 @@ public abstract class Box } - public List generateNeighbors() { - - 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 ; + public List generateNeighbors() { + return maze.generateNeighbours(this) ; } @@ -105,6 +86,8 @@ public abstract class Box this.maze = maze; } - + public String toString() { + return label; + } } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 2c2bdbe..93ce736 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -54,6 +54,33 @@ public class Maze return vertexList ; } + public ArrayList generateNeighbours(VertexInterface vertex) { + + Box box = (Box)vertex ; + int x = box.getX() ; + int y = box.getY() ; + ArrayList neighbourList = new ArrayList() ; + + 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) { FileReader fr = null ; @@ -77,9 +104,7 @@ public class Maze String currentLine = null ; currentLine = br.readLine() ; - System.out.println(currentLine); dimensionX = currentLine.length() ; //maze's width - System.out.println("Line length :" + dimensionX); int currentLineLength ; int compteurLigne = 0 ; @@ -90,7 +115,6 @@ public class Maze //System.out.println(currentLine); currentLineLength = currentLine.length() ; - System.out.println("Current line length :" + currentLineLength); for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { @@ -130,13 +154,6 @@ public class Maze 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) {