diff --git a/src/main/java/Graph.java b/src/main/java/Graph.java new file mode 100644 index 0000000..1025bd2 --- /dev/null +++ b/src/main/java/Graph.java @@ -0,0 +1,189 @@ +import java.util.ArrayList; +import java.util.List; + +public class Graph { + private int maxVertexCount; + // private int[][] connectionMatrix; + private List> connectionMatrix; + // Vertex[] vertexList; + private List vertexList; + private int currentVertexIndex; + private GraphStack graphStack; + + public Graph(int maxVertexCount) { +// this.vertexList = new Vertex[maxVertexCount]; + this.vertexList = new ArrayList<>(); + this.maxVertexCount = maxVertexCount; +// this.connectionMatrix = new int[maxVertexCount][maxVertexCount]; + this.connectionMatrix = new ArrayList<>(); + this.currentVertexIndex = 0; + this.graphStack = new GraphStack(maxVertexCount); + for (int t = 0; t < maxVertexCount; t++) { + connectionMatrix.add(new ArrayList<>()); + for (int i = 0; i < maxVertexCount; i++) { + connectionMatrix.get(t).add(0); + } + } + } + + public void addVertex(String name) { +// vertexList[currentVertexIndex++] = new Vertex(name); + vertexList.add(new Vertex(name)); + } + + public void addArc(String start, String end, int weight, boolean goBack) { + Integer startIndex = 0; + Integer endIndex = 0; + for (Vertex vertex : vertexList) { + if (vertex.getName().equals(start)) { + startIndex = vertexList.indexOf(vertex); + } else if (vertex.getName().equals(end)) { + endIndex = vertexList.indexOf(vertex); + } + } + connectionMatrix.get(startIndex).set(endIndex, weight); + if (goBack) { + connectionMatrix.get(endIndex).set(startIndex, weight); + } else { + connectionMatrix.get(endIndex).set(startIndex, -1); + } + +// connectionMatrix[startIndex][endIndex] = weight; +// connectionMatrix[endIndex][startIndex] = goBack ? weight : -1; + } + + public void deleteVertex(String name) { + int vertexIndex = getVertexIndexByName(name); + vertexList.remove(getVertexIndexByName(name)); +// int[][] newArr = new int[maxVertexCount][maxVertexCount]; + for (List list : connectionMatrix) { + list.remove(vertexIndex); + list.add(0); + } + connectionMatrix.remove(vertexIndex); + connectionMatrix.add(new ArrayList<>()); + for (int i = 0; i < maxVertexCount; i++) { + connectionMatrix.get(maxVertexCount - 1).add(0); + } +// for (int t = vertexIndex; t < maxVertexCount - 1; t++) { +// for (int i = vertexIndex; i < maxVertexCount - 1; i++) { +// newArr[t][i] = connectionMatrix[t + 1][i + 1]; +// } +// } + + } + + public void deleteArc(String from, String to) { + int fromN = getVertexIndexByName(from); + int toN = getVertexIndexByName(to); + connectionMatrix.get(fromN).set(toN, 0); + connectionMatrix.get(toN).set(fromN, 0); + } + + public int check(int vertex) { + for (int i = 0; i < vertexList.size(); i++) { +// if (connectionMatrix[vertex][i] != -1 && vertexList.get(i).isChecked() == false) { +// return i; +// } + if (connectionMatrix.get(vertex).get(i) != -1 && vertexList.get(i).isChecked() == false) { + return i; + } + } + return -1; + } + + public void goDeep(int index) { + System.out.println(vertexList.get(index).getName()); + vertexList.get(index).setChecked(true); + graphStack.push(index); + + while (!graphStack.isEmpty()) { + int neighbour = check(graphStack.peek()); + + if (neighbour == -1) { + neighbour = graphStack.pop(); + } else { + System.out.println(vertexList.get(neighbour).getName()); + vertexList.get(neighbour).setChecked(true); + graphStack.push(neighbour); + } + } + for (int i = 0; i < vertexList.size(); i++) { + vertexList.get(i).setChecked(false); + } + } + + public List listOfInputArcs(String vertexName) { + List inputArcs = new ArrayList<>(); + for (int i = 0; i < maxVertexCount; i++) { +// if (connectionMatrix[i][getVertexIndexByName(vertexName)] > 0) { +// inputArcs.add(vertexList.get(i).getName()); +// } + if (connectionMatrix.get(i).get(getVertexIndexByName(vertexName)) > 0) { + inputArcs.add(vertexList.get(i).getName()); + } + } + return inputArcs; + } + + public List listOfOutputArcs(String vertexName) { + List outputArcs = new ArrayList<>(); + for (int i = 0; i < maxVertexCount; i++) { +// if (connectionMatrix[getVertexIndexByName(vertexName)][i] > 0) { +// outputArcs.add(vertexList.get(i).getName()); +// } + if (connectionMatrix.get(getVertexIndexByName(vertexName)).get(i) > 0) { + outputArcs.add(vertexList.get(i).getName()); + } + } + return outputArcs; + } + + public int getVertexIndexByName(String name) { + for (int i = 0; i < maxVertexCount; i++) { + if (vertexList.get(i).getName().equals(name)) { + return i; + } + } + return -1; + } + + public void renameVertex(String oldName, String newName) { + vertexList.get(getVertexIndexByName(oldName)).setName(newName); + } + + public void reWeightArc(String from, String to, int newWeight) { + int fromN = getVertexIndexByName(from); + int toN = getVertexIndexByName(to); + connectionMatrix.get(fromN).set(toN, newWeight); + if (connectionMatrix.get(toN).get(fromN) > 0) { + connectionMatrix.get(toN).set(fromN, newWeight); + } + + } + + public void printConnectionMatrix() { + for (int i = 0; i < maxVertexCount; i++) { + for (int t = 0; t < maxVertexCount; t++) { + System.out.print(connectionMatrix.get(i).get(t) + " "); +// System.out.print(connectionMatrix[i][t] + " "); + } + System.out.println(); + } + System.out.println(); + } + + public ArrayList listAllVertexes() { + ArrayList vertexStringList = new ArrayList<>(); + for (Vertex vertex : vertexList) { + vertexStringList.add(vertex.getName()); + } + return vertexStringList; + } + + public List> getConnectionMatrix() { + return connectionMatrix; + } + + +} diff --git a/src/main/java/GraphStack.java b/src/main/java/GraphStack.java new file mode 100644 index 0000000..287da94 --- /dev/null +++ b/src/main/java/GraphStack.java @@ -0,0 +1,28 @@ +public class GraphStack { + private int size; + private int top; + private int[] body; + + + public GraphStack(int size) { + this.size = size; + this.top = -1; + this.body = new int[size]; + } + + public void push(int vertex) { + body[++top] = vertex; + } + + public int pop() { + return body[top--]; + } + + public int peek() { + return body[top]; + } + + public boolean isEmpty() { + return top == -1; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..eef1dda --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,13 @@ +public class Main { + + public static void main(String[] args) { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + + graph.addArc("A", "B", 1, false); + + System.out.println(graph.getConnectionMatrix()); + } +} diff --git a/src/main/java/Vertex.java b/src/main/java/Vertex.java new file mode 100644 index 0000000..5e044bf --- /dev/null +++ b/src/main/java/Vertex.java @@ -0,0 +1,25 @@ +public class Vertex { + private String name; + private boolean checked; + + public Vertex(String name) { + this.name = name; + this.checked = false; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isChecked() { + return checked; + } + + public void setChecked(boolean checked) { + this.checked = checked; + } +} diff --git a/src/test/java/TriangleTest.java b/src/test/java/TriangleTest.java index b743089..76ce895 100644 --- a/src/test/java/TriangleTest.java +++ b/src/test/java/TriangleTest.java @@ -1,7 +1,10 @@ -import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + public class TriangleTest { @Test @@ -10,4 +13,173 @@ public void testTriangleSquare() { double expectedSquare = 5; assertEquals(expectedSquare, Math.round(triangle.calcSquare())); } + + @Test + public void testListOfInputArcs() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + graph.addVertex("D"); + graph.addVertex("E"); + + graph.addArc("A", "B", 1, false); + graph.addArc("A", "C", 2, false); + graph.addArc("A", "D", 3, false); + graph.addArc("B", "E", 4, false); + + List arcs = new ArrayList<>(); + arcs.add("A"); + assertEquals(arcs, graph.listOfInputArcs("B")); + } + + @Test + public void testListOfOutputArcs() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + graph.addVertex("D"); + graph.addVertex("E"); + + graph.addArc("A", "B", 1, false); + graph.addArc("A", "C", 2, false); + graph.addArc("A", "D", 3, false); + graph.addArc("B", "E", 4, false); + + List arcs = new ArrayList<>(); + arcs.add("B"); + arcs.add("C"); + arcs.add("D"); + assertEquals(arcs, graph.listOfOutputArcs("A")); + } + @Test + public void testAddVertex() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + graph.addVertex("D"); + graph.addVertex("E"); + + graph.addArc("A", "B", 1, false); + graph.addArc("A", "C", 2, false); + graph.addArc("A", "D", 3, false); + graph.addArc("B", "E", 4, false); + + ArrayList vertexes = new ArrayList<>(); + vertexes.add("A"); + vertexes.add("B"); + vertexes.add("C"); + vertexes.add("D"); + vertexes.add("E"); + assertEquals(vertexes, graph.listAllVertexes()); + } + @Test + public void testAddArc() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + + graph.addArc("A", "B", 1, false); + +List> matrix = new ArrayList<>(); + for (int t = 0; t < 15; t++) { + matrix.add(new ArrayList<>()); + for (int i = 0; i < 15; i++) { + matrix.get(t).add(0); + } + } + matrix.get(0).set(1, 1); + matrix.get(1).set(0, -1); + assertEquals(matrix, graph.getConnectionMatrix()); + } + + @Test + public void testDeleteArc() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + + graph.addArc("A", "B", 1, false); + graph.addArc("B", "C", 1, false); + graph.deleteArc("B", "C"); + + List> matrix = new ArrayList<>(); + for (int t = 0; t < 15; t++) { + matrix.add(new ArrayList<>()); + for (int i = 0; i < 15; i++) { + matrix.get(t).add(0); + } + } + matrix.get(0).set(1, 1); + matrix.get(1).set(0, -1); + assertEquals(matrix, graph.getConnectionMatrix()); + } + + @Test + public void testDeleteVertex() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + + graph.addArc("A", "B", 1, false); + graph.addArc("B", "C", 1, false); + graph.deleteVertex("C"); + + ArrayList vertexes = new ArrayList<>(); + vertexes.add("A"); + vertexes.add("B"); + assertEquals(vertexes, graph.listAllVertexes()); + } + @Test + public void testRenameVertex() { + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + + graph.addArc("A", "B", 1, false); + graph.addArc("B", "C", 1, false); + + graph.renameVertex("C", "E"); + + ArrayList vertexes = new ArrayList<>(); + vertexes.add("A"); + vertexes.add("B"); + vertexes.add("E"); + assertEquals(vertexes, graph.listAllVertexes()); + } + + @Test + public void changeArcWeight(){ + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + + graph.addArc("A", "B", 1, false); + graph.reWeightArc("A", "B", 2); + + List> matrix = new ArrayList<>(); + for (int t = 0; t < 15; t++) { + matrix.add(new ArrayList<>()); + for (int i = 0; i < 15; i++) { + matrix.get(t).add(0); + } + } + matrix.get(0).set(1, 2); + matrix.get(1).set(0, -1); + assertEquals(matrix, graph.getConnectionMatrix()); + } + }