Uebungsaufgaben/graph/DirectedGraph.java
2024-06-15 16:35:42 +02:00

125 lines
3.3 KiB
Java

package graph;
import java.util.Objects;
import java.util.Vector;
public class DirectedGraph<T, U> extends Graph<T, U> {
public DirectedGraph() {
super();
}
public DirectedGraph(String s) {
super(s);
}
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return true;
}
public boolean areAdjacent(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 areAdjacent(n1, n2);
}
}
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
boolean n1ton2 = false;
boolean n2ton1 = false;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n1 && i.getDestination() == n2) {
n1ton2 = true;
} else if (i.getSource() == n2 && i.getDestination() == n1) {
n2ton1 = true;
}
}
return (n1ton2 && n2ton1);
}
public boolean areStrongAdjacent(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 areStrongAdjacent(n1, n2);
}
}
public int inDegree(MarkedVertex<T> n) {
int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getDestination() == n) {
degree += 1;
}
}
return degree;
}
public int inDegree(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) {
return inDegree(i);
}
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
}
public int outDegree(MarkedVertex<T> n) {
int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
}
return degree;
}
public int outDegree(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) {
return outDegree(i);
}
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
}
public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) {
return null;
}
public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) {
return null;
}
}