61 lines
1.6 KiB
Java
61 lines
1.6 KiB
Java
package graph;
|
|
|
|
import java.util.Objects;
|
|
import java.util.Vector;
|
|
|
|
public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
|
|
|
|
// KONSTRUKTOREN
|
|
|
|
public UndirectedGraph() {
|
|
super();
|
|
}
|
|
|
|
|
|
public UndirectedGraph(String s) {
|
|
super(s);
|
|
}
|
|
|
|
|
|
// KNOTEN EIGENSCHAFTEN
|
|
|
|
// Prüfung des Grades eines Knotens
|
|
public int degree(MarkedVertex<T> n) {
|
|
int degree = 0;
|
|
for (MarkedEdge<U> i: this.getAllEdges()) {
|
|
if (i.getSource() == n) {
|
|
degree += 1;
|
|
}
|
|
if (i.getDestination() == n) {
|
|
degree += 1;
|
|
}
|
|
}
|
|
return degree;
|
|
}
|
|
|
|
|
|
public int degree(String s) throws NameDoesNotExistException{
|
|
for (MarkedVertex<T> i: this.getAllVertexes()) {
|
|
if (Objects.equals(i.getName(), s)) {
|
|
return degree(i);
|
|
}
|
|
}
|
|
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
|
}
|
|
|
|
|
|
// Prüfung, welche Knoten Nachbarn sind
|
|
public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) {
|
|
Vector<MarkedVertex<T>> neighbours = new Vector<>();
|
|
for (MarkedEdge<U> i: this.getAllEdges()) {
|
|
if (i.getSource() == n && !neighbours.contains(i.getDestination())) {
|
|
neighbours.add((MarkedVertex<T>) i.getDestination());
|
|
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
|
|
neighbours.add((MarkedVertex<T>) i.getSource());
|
|
}
|
|
}
|
|
return neighbours;
|
|
}
|
|
|
|
}
|