2024-06-11 21:48:59 +00:00
|
|
|
package graph;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public abstract class Graph<T, U> {
|
2024-06-11 21:48:59 +00:00
|
|
|
|
|
|
|
private String name;
|
|
|
|
private Vector<MarkedVertex<T>> vertexes;
|
|
|
|
private Vector<MarkedEdge<U>> edges;
|
|
|
|
|
|
|
|
public Graph() {
|
|
|
|
this.edges = new Vector<>();
|
|
|
|
this.vertexes = new Vector<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Graph(String s) {
|
|
|
|
this();
|
|
|
|
this.name = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setName(String s) {
|
|
|
|
this.name = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void addEdge(MarkedEdge<U> e) {
|
|
|
|
this.edges.add(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void addVertex(MarkedVertex<T> n) {
|
|
|
|
this.vertexes.add(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public void removeEdge(MarkedEdge<U> e) {
|
|
|
|
this.edges.remove(e);
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public void removeEdge(String s) throws NameDoesNotExistException{
|
2024-06-11 21:48:59 +00:00
|
|
|
for (MarkedEdge<U> i: this.edges) {
|
|
|
|
if (Objects.equals(i.getName(), s)) {
|
2024-06-15 14:35:42 +00:00
|
|
|
removeEdge(i);
|
|
|
|
return;
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-15 14:35:42 +00:00
|
|
|
throw new NameDoesNotExistException("One of the Edges might not exist");
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public void removeVertex(MarkedVertex<T> n) {
|
|
|
|
this.vertexes.remove(n);
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public void removeVertex(String s) throws NameDoesNotExistException{
|
2024-06-11 21:48:59 +00:00
|
|
|
for (MarkedVertex<T> i: this.vertexes) {
|
|
|
|
if (Objects.equals(i.getName(), s)) {
|
2024-06-15 14:35:42 +00:00
|
|
|
removeVertex(i);
|
|
|
|
return;
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-15 14:35:42 +00:00
|
|
|
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Vector<MarkedEdge<U>> getAllEdges() {
|
|
|
|
return this.edges;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Vector<MarkedVertex<T>> getAllVertexes() {
|
|
|
|
return this.vertexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int numberOfEdges() {
|
|
|
|
return this.edges.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int numberOfVertexes() {
|
|
|
|
return this.vertexes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean hasVertex(String s) {
|
|
|
|
for (MarkedVertex<T> i: this.vertexes) {
|
|
|
|
if (Objects.equals(i.getName(), s)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean hasVertex(MarkedVertex<T> n) {
|
|
|
|
return this.vertexes.contains(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean hasEdge(String s) {
|
|
|
|
for (MarkedEdge<U> i: this.edges) {
|
|
|
|
if (Objects.equals(i.getName(), s)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public boolean hasEdge(MarkedEdge<U> e) {
|
|
|
|
return this.edges.contains(e);
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException {
|
|
|
|
MarkedVertex<T> n1 = null;
|
|
|
|
MarkedVertex<T> n2 = null;
|
|
|
|
for (MarkedVertex<T> i: this.getAllVertexes()) {
|
|
|
|
if (Objects.equals(i.getName(), s1)) {
|
|
|
|
n1 = i;
|
|
|
|
} else if (Objects.equals(i.getName(), s2)) {
|
|
|
|
n2 = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n1 == null || n2 == null) {
|
|
|
|
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
|
|
|
} else {
|
|
|
|
return hasEdge(n1, n2);
|
|
|
|
}
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO schauen, ob es Aufgabe entspricht
|
|
|
|
public boolean hasEdge(MarkedVertex<T> v1, MarkedVertex<T> v2) {
|
|
|
|
for (MarkedEdge<U> i: this.edges) {
|
|
|
|
if (i.getSource() == v1 && i.getDestination() == v2) {
|
|
|
|
return true;
|
|
|
|
} else if (i.getSource() == v2 && i.getDestination() == v1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int degree() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public abstract boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2);
|
2024-06-11 21:48:59 +00:00
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public abstract boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException;
|
2024-06-11 21:48:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
public boolean hasLoop(MarkedVertex<T> n) {
|
|
|
|
for (MarkedEdge<U> i: this.edges) {
|
|
|
|
if (i.getSource() == i.getDestination() && i.getSource() == n) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-15 14:35:42 +00:00
|
|
|
public boolean hasLoop(String s) throws NameDoesNotExistException{
|
2024-06-11 21:48:59 +00:00
|
|
|
for (MarkedVertex<T> i: this.vertexes) {
|
|
|
|
if (Objects.equals(i.getName(), s)) {
|
|
|
|
return hasLoop(i);
|
|
|
|
}
|
|
|
|
}
|
2024-06-15 14:35:42 +00:00
|
|
|
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
2024-06-11 21:48:59 +00:00
|
|
|
}
|
|
|
|
}
|