ProjektGraph/graph/DirectedGraph.java

123 lines
3.4 KiB
Java

package graph;
import java.util.Objects;
import java.util.Vector;
public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
// KONSTRUKTOREN
public DirectedGraph() {
super();
}
public DirectedGraph(String s) {
super(s);
}
// KNOTEN EIGENSCHAFTEN
// Prüfung, ob zwei Knoten stark adjazent sind
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
boolean n1ton2 = false;
boolean n2ton1 = false;
for (MarkedEdge<U, T> 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);
}
}
// Prüfung des Eingangsgrades eines Knotens
public int inDegree(MarkedVertex<T> n) {
int degree = 0;
for (MarkedEdge<U, T> 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");
}
// Prüfung des Ausgangsgrades eines Knotens
public int outDegree(MarkedVertex<T> n) {
int degree = 0;
for (MarkedEdge<U, T> 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");
}
// Prüfung, welche Knoten Vorgänger sind
public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> predecessors = new Vector<>();
for (MarkedEdge<U, T> i: this.getAllEdges()) {
if (i.getDestination() == n) {
predecessors.add(i.getSource());
}
}
return predecessors;
}
// Prüfung, welche Knoten Nachfolger sind
public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> successors = new Vector<>();
for (MarkedEdge<U, T> i: this.getAllEdges()) {
if (i.getSource() == n) {
successors.add(i.getDestination());
}
}
return successors;
}
}