ProjektGraph/graph/UndirectedGraph.java

61 lines
1.6 KiB
Java
Raw Normal View History

2024-06-15 14:48:28 +00:00
package graph;
import java.util.Objects;
import java.util.Vector;
public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
// KONSTRUKTOREN
2024-06-15 14:48:28 +00:00
public UndirectedGraph() {
super();
}
public UndirectedGraph(String s) {
super(s);
}
// KNOTEN EIGENSCHAFTEN
// Prüfung des Grades eines Knotens
2024-06-15 14:48:28 +00:00
public int degree(MarkedVertex<T> n) {
int degree = 0;
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.getAllEdges()) {
2024-06-15 14:48:28 +00:00
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
2024-06-15 14:48:28 +00:00
public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> neighbours = new Vector<>();
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.getAllEdges()) {
2024-06-15 14:48:28 +00:00
if (i.getSource() == n && !neighbours.contains(i.getDestination())) {
2024-06-25 15:18:34 +00:00
neighbours.add((MarkedVertex<T>) i.getDestination());
2024-06-15 14:48:28 +00:00
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
2024-06-25 15:18:34 +00:00
neighbours.add((MarkedVertex<T>) i.getSource());
2024-06-15 14:48:28 +00:00
}
}
return neighbours;
}
2024-06-25 15:18:34 +00:00
2024-06-15 14:48:28 +00:00
}