Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Васильев Ф.Р., 'Ориентированный граф [Java]', гр. 3530901/10002 #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions src/main/java/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import java.util.ArrayList;
import java.util.List;

public class Graph {
private int maxVertexCount;
// private int[][] connectionMatrix;
private List<List<Integer>> connectionMatrix;
// Vertex[] vertexList;
private List<Vertex> 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<Integer> 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<String> listOfInputArcs(String vertexName) {
List<String> 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<String> listOfOutputArcs(String vertexName) {
List<String> 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<String> listAllVertexes() {
ArrayList<String> vertexStringList = new ArrayList<>();
for (Vertex vertex : vertexList) {
vertexStringList.add(vertex.getName());
}
return vertexStringList;
}

public List<List<Integer>> getConnectionMatrix() {
return connectionMatrix;
}


}
28 changes: 28 additions & 0 deletions src/main/java/GraphStack.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
25 changes: 25 additions & 0 deletions src/main/java/Vertex.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading