Programmieren_Projekt/graph/UndirectedGraph.java

84 lines
2.6 KiB
Java

package graph;
import logging.LogElement;
import logging.LogElementList;
import java.util.Vector;
class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
public UndirectedGraph() {}
public UndirectedGraph(String s) {
super(s);
}
LogElementList<LogElement> logList = new LogElementList<>(); // Erzeugen einer LogElementList für die Protokollierung
private visualizationElements.Graph visualizedGraph;
public int degree(MarkedVertex<T> n) {
int degree = 0;
for (MarkedEdge<U> edge : getAllEdges()) {
if (edge.getSource().equals(n) || edge.getDestination().equals(n)) {
degree++;
}
}
return degree;
}
public int degree(String s) {
for (MarkedVertex<T> vertex : getAllVertexes()) {
if (vertex.getName().equals(s)) {
return degree(vertex);
}
}
return 0;
}
public visualizationElements.Graph getVisualizedGraph() {
return this.visualizedGraph;
}
public LogElementList<LogElement> getLogList(){
return this.logList;
}
public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> neighbours = new Vector<>();
for (MarkedEdge<U> edge : getAllEdges()) {
if (edge.getSource().equals(n)) {
neighbours.add((MarkedVertex<T>) edge.getDestination());
} else if (edge.getDestination().equals(n)) {
neighbours.add((MarkedVertex<T>) edge.getSource());
}
}
return neighbours;
}
@Override
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
for (MarkedEdge<U> edge : getAllEdges()) {
if ((edge.getSource().equals(n1) && edge.getDestination().equals(n2)) ||
(edge.getSource().equals(n2) && edge.getDestination().equals(n1))) {
return true;
}
}
return false;
}
@Override
public boolean areAdjacent(String s1, String s2) {
for (MarkedEdge<U> edge : getAllEdges()) {
if ((edge.getSource().getName().equals(s1) && edge.getDestination().getName().equals(s2)) ||
(edge.getSource().getName().equals(s2) && edge.getDestination().getName().equals(s1))) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "UndirectedGraph{} " + super.toString();
}
}