From 6f88437b75c398dbca69ea48c03fe42f134f975a Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 25 Feb 2022 14:42:05 +0300 Subject: [PATCH 1/4] .. --- src/main/java/Graph.java | 54 +++++++++++++++++++++++++++++++++++ src/main/java/GraphStack.java | 28 ++++++++++++++++++ src/main/java/Main.java | 21 ++++++++++++++ src/main/java/Vertex.java | 25 ++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 src/main/java/Graph.java create mode 100644 src/main/java/GraphStack.java create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/Vertex.java diff --git a/src/main/java/Graph.java b/src/main/java/Graph.java new file mode 100644 index 0000000..96ae217 --- /dev/null +++ b/src/main/java/Graph.java @@ -0,0 +1,54 @@ +public class Graph { + private int maxVertexCount; + private int[][] connectionMatrix; + Vertex[] vertexList; + private int currentVertexIndex; + private GraphStack graphStack; + + public Graph(int maxVertexCount) { + this.vertexList = new Vertex[maxVertexCount]; + this.maxVertexCount = maxVertexCount; + this.connectionMatrix = new int[maxVertexCount][maxVertexCount]; + this.currentVertexIndex = 0; + this.graphStack = new GraphStack(maxVertexCount); + } + + public void addVertex(String name) { + vertexList[currentVertexIndex++] = new Vertex(name); + } + + public void addArc(int start, int end, int weight, boolean goBack) { + connectionMatrix[start][end] = weight; + connectionMatrix[end][start] = goBack ? weight : -1; + } + + public int check(int vertex) { + for (int i = 0; i < currentVertexIndex; i++) { + if (connectionMatrix[vertex][i] != -1 && vertexList[i].isChecked() == false) { + return i; + } + } + return -1; + } + + public void goDeep(int index) { + System.out.println(vertexList[index].getName()); + vertexList[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[neighbour].getName()); + vertexList[neighbour].setChecked(true); + graphStack.push(neighbour); + } + } + for (int i = 0; i < currentVertexIndex; i++) { + vertexList[i].setChecked(false); + } + } +} 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..d8a2385 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,21 @@ +public class Main { + + public static void main(String[] args){ + Graph graph = new Graph(15); + + graph.addVertex("A"); + graph.addVertex("B"); + graph.addVertex("C"); + graph.addVertex("D"); + graph.addVertex("E"); + graph.addVertex("F"); + + graph.addArc(0,1,3, true); + graph.addArc(1,2,3, true); + graph.addArc(2,3,3, true); + graph.addArc(0,4,3, true); + graph.addArc(4,5,3, true); + + graph.goDeep(2); + } +} 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; + } +} From e25abec63323be3c7f62a7faca0731847c534b9b Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 26 Feb 2022 14:05:01 +0300 Subject: [PATCH 2/4] // --- src/main/java/Graph.java | 51 +++++++++++++++++++++++++++++----------- src/main/java/Main.java | 12 +++++----- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/main/java/Graph.java b/src/main/java/Graph.java index 96ae217..e85903f 100644 --- a/src/main/java/Graph.java +++ b/src/main/java/Graph.java @@ -1,12 +1,17 @@ +import java.util.ArrayList; +import java.util.List; + public class Graph { private int maxVertexCount; private int[][] connectionMatrix; - Vertex[] vertexList; + // Vertex[] vertexList; + List vertexList; private int currentVertexIndex; private GraphStack graphStack; public Graph(int maxVertexCount) { - this.vertexList = new Vertex[maxVertexCount]; +// this.vertexList = new Vertex[maxVertexCount]; + this.vertexList = new ArrayList<>(); this.maxVertexCount = maxVertexCount; this.connectionMatrix = new int[maxVertexCount][maxVertexCount]; this.currentVertexIndex = 0; @@ -14,17 +19,35 @@ public Graph(int maxVertexCount) { } public void addVertex(String name) { - vertexList[currentVertexIndex++] = new Vertex(name); +// vertexList[currentVertexIndex++] = new Vertex(name); + vertexList.add(new Vertex(name)); } - public void addArc(int start, int end, int weight, boolean goBack) { - connectionMatrix[start][end] = weight; - connectionMatrix[end][start] = goBack ? weight : -1; + 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[startIndex][endIndex] = weight; + connectionMatrix[endIndex][startIndex] = goBack ? weight : -1; + } + + public void deleteVertex(String name) { + for (Vertex vertex : vertexList) { + if (vertex.getName().equals(name)) { + vertexList.remove(vertex); + } + } } public int check(int vertex) { - for (int i = 0; i < currentVertexIndex; i++) { - if (connectionMatrix[vertex][i] != -1 && vertexList[i].isChecked() == false) { + for (int i = 0; i < vertexList.size(); i++) { + if (connectionMatrix[vertex][i] != -1 && vertexList.get(i).isChecked() == false) { return i; } } @@ -32,8 +55,8 @@ public int check(int vertex) { } public void goDeep(int index) { - System.out.println(vertexList[index].getName()); - vertexList[index].setChecked(true); + System.out.println(vertexList.get(index).getName()); + vertexList.get(index).setChecked(true); graphStack.push(index); while (!graphStack.isEmpty()) { @@ -42,13 +65,13 @@ public void goDeep(int index) { if (neighbour == -1) { neighbour = graphStack.pop(); } else { - System.out.println(vertexList[neighbour].getName()); - vertexList[neighbour].setChecked(true); + System.out.println(vertexList.get(neighbour).getName()); + vertexList.get(neighbour).setChecked(true); graphStack.push(neighbour); } } - for (int i = 0; i < currentVertexIndex; i++) { - vertexList[i].setChecked(false); + for (int i = 0; i < vertexList.size(); i++) { + vertexList.get(i).setChecked(false); } } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index d8a2385..c382278 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -10,12 +10,12 @@ public static void main(String[] args){ graph.addVertex("E"); graph.addVertex("F"); - graph.addArc(0,1,3, true); - graph.addArc(1,2,3, true); - graph.addArc(2,3,3, true); - graph.addArc(0,4,3, true); - graph.addArc(4,5,3, true); + graph.addArc("A","B",3, true); + graph.addArc("B","C",3, true); + graph.addArc("C","D",3, true); + graph.addArc("A","E",3, true); + graph.addArc("E","F",3, true); - graph.goDeep(2); + graph.goDeep(0); } } From 3d055372063764eb0c4ec3c96d34dc3ef963cf8e Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 5 Mar 2022 12:32:12 +0300 Subject: [PATCH 3/4] add delete option --- src/main/java/Graph.java | 112 ++++++++++++++++++++++++++++++++++----- src/main/java/Main.java | 24 ++++++--- 2 files changed, 116 insertions(+), 20 deletions(-) diff --git a/src/main/java/Graph.java b/src/main/java/Graph.java index e85903f..21c08fd 100644 --- a/src/main/java/Graph.java +++ b/src/main/java/Graph.java @@ -3,9 +3,10 @@ public class Graph { private int maxVertexCount; - private int[][] connectionMatrix; + // private int[][] connectionMatrix; + private List> connectionMatrix; // Vertex[] vertexList; - List vertexList; + private List vertexList; private int currentVertexIndex; private GraphStack graphStack; @@ -13,9 +14,16 @@ 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 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) { @@ -26,28 +34,58 @@ public void addVertex(String 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)){ + for (Vertex vertex : vertexList) { + if (vertex.getName().equals(start)) { startIndex = vertexList.indexOf(vertex); - }else if(vertex.getName().equals(end)){ + } else if (vertex.getName().equals(end)) { endIndex = vertexList.indexOf(vertex); } } - connectionMatrix[startIndex][endIndex] = weight; - connectionMatrix[endIndex][startIndex] = goBack ? weight : -1; + 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) { - for (Vertex vertex : vertexList) { - if (vertex.getName().equals(name)) { - vertexList.remove(vertex); - } + 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) { +// 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; } } @@ -74,4 +112,52 @@ public void goDeep(int index) { 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 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(); + } + + } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c382278..10f9d97 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,6 @@ public class Main { - public static void main(String[] args){ + public static void main(String[] args) { Graph graph = new Graph(15); graph.addVertex("A"); @@ -8,14 +8,24 @@ public static void main(String[] args){ graph.addVertex("C"); graph.addVertex("D"); graph.addVertex("E"); - graph.addVertex("F"); +// graph.addVertex("F"); - graph.addArc("A","B",3, true); - graph.addArc("B","C",3, true); - graph.addArc("C","D",3, true); - graph.addArc("A","E",3, true); - graph.addArc("E","F",3, true); + graph.addArc("A", "B", 1, false); + graph.addArc("A", "C", 2, false); + graph.addArc("A", "D", 3, false); + graph.addArc("B", "E", 4, false); +// graph.addArc("E", "F", 5, false); graph.goDeep(0); + + graph.printConnectionMatrix(); +// for (String arc : graph.listOfInputArcs("E")) { +// System.out.println(arc); +// } +// for (String arc : graph.listOfOutputArcs("E")) { +// System.out.println(arc); +// } + graph.deleteArc("B", "E"); + graph.printConnectionMatrix(); } } From f7b87ba7a143c5042d2682200d6aeada8b92c015 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 5 Mar 2022 19:05:03 +0300 Subject: [PATCH 4/4] done --- src/main/java/Graph.java | 26 +++++ src/main/java/Main.java | 20 +--- src/test/java/TriangleTest.java | 176 +++++++++++++++++++++++++++++++- 3 files changed, 201 insertions(+), 21 deletions(-) diff --git a/src/main/java/Graph.java b/src/main/java/Graph.java index 21c08fd..1025bd2 100644 --- a/src/main/java/Graph.java +++ b/src/main/java/Graph.java @@ -148,6 +148,20 @@ public int getVertexIndexByName(String name) { 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++) { @@ -159,5 +173,17 @@ public void printConnectionMatrix() { 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/Main.java b/src/main/java/Main.java index 10f9d97..eef1dda 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -5,27 +5,9 @@ public static void main(String[] args) { graph.addVertex("A"); graph.addVertex("B"); - graph.addVertex("C"); - graph.addVertex("D"); - graph.addVertex("E"); -// graph.addVertex("F"); graph.addArc("A", "B", 1, false); - graph.addArc("A", "C", 2, false); - graph.addArc("A", "D", 3, false); - graph.addArc("B", "E", 4, false); -// graph.addArc("E", "F", 5, false); - graph.goDeep(0); - - graph.printConnectionMatrix(); -// for (String arc : graph.listOfInputArcs("E")) { -// System.out.println(arc); -// } -// for (String arc : graph.listOfOutputArcs("E")) { -// System.out.println(arc); -// } - graph.deleteArc("B", "E"); - graph.printConnectionMatrix(); + System.out.println(graph.getConnectionMatrix()); } } 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()); + } + }