diff --git a/ASetInterface.java b/ASetInterface.java new file mode 100644 index 0000000..8d4c177 --- /dev/null +++ b/ASetInterface.java @@ -0,0 +1,12 @@ + + +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) ; + + +} diff --git a/Dijkstra.java b/Dijkstra.java new file mode 100644 index 0000000..34ab762 --- /dev/null +++ b/Dijkstra.java @@ -0,0 +1,44 @@ +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 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 ; + + } + +} diff --git a/MainTest.java b/MainTest.java new file mode 100644 index 0000000..5e4b3b5 --- /dev/null +++ b/MainTest.java @@ -0,0 +1,13 @@ + +public class MainTest { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + Box[][] box = new Box[2][2] ; + Maze mazeTest = new Maze(box) ; + mazeTest.initFromTextFile("data/labyrinthe.txt"); + + } + +} diff --git a/Maze.java b/Maze.java index d131178..557f028 100644 --- a/Maze.java +++ b/Maze.java @@ -1,3 +1,5 @@ +import java.io.BufferedReader; +import java.io.FileReader; import java.util.ArrayList; public class Maze @@ -46,6 +48,38 @@ public class Maze return vertexList ; } + public final void initFromTextFile(String filename) { + + FileReader fr = null ; + BufferedReader br = null ; + + try { + fr = new FileReader(filename) ; + br = new BufferedReader(fr) ; + + + int lineLength = br.readLine().length() - 1 ; + int currentLineLength ; + int cInt = br.read() ; + + while (cInt != -1) + { + currentLineLength = br.readLine().length() ; + if (currentLineLength != lineLength) { + throw new MazeReadingException(filename) ; + } + cInt = br.read() ; + } + } catch (MazeReadingException mre) { + mre.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { br.close() ;} catch (Exception e) {} + } + + } + public int getDimensionX() { return dimensionX; } diff --git a/MazeReadingException.java b/MazeReadingException.java new file mode 100644 index 0000000..dd80fec --- /dev/null +++ b/MazeReadingException.java @@ -0,0 +1,18 @@ + +public class MazeReadingException + extends Exception { + + private String filename ; + + public MazeReadingException (String filename) { + + super("Invalid maze format :" + filename) ; + this.filename = filename ; + + } + + public String getFilename() { + return filename ; + } + +} diff --git a/PiInterface.java b/PiInterface.java new file mode 100644 index 0000000..f1a4ac0 --- /dev/null +++ b/PiInterface.java @@ -0,0 +1,10 @@ + +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) ; + +} diff --git a/PreviousInterface.java b/PreviousInterface.java new file mode 100644 index 0000000..de81746 --- /dev/null +++ b/PreviousInterface.java @@ -0,0 +1,10 @@ + +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) ; + +}