69 lines
1.6 KiB
Java
69 lines
1.6 KiB
Java
package graph;
|
|
|
|
import java.util.Objects;
|
|
import java.util.Vector;
|
|
|
|
public class UndirectedGraph<T, U> extends Graph<T, U> {
|
|
|
|
public UndirectedGraph() {
|
|
super();
|
|
}
|
|
|
|
|
|
public UndirectedGraph(String s) {
|
|
super(s);
|
|
}
|
|
|
|
|
|
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
|
return true;
|
|
}
|
|
|
|
|
|
public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException {
|
|
return true;
|
|
}
|
|
|
|
|
|
public String toString() {
|
|
return "";
|
|
}
|
|
|
|
|
|
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");
|
|
}
|
|
|
|
|
|
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(i.getDestination());
|
|
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
|
|
neighbours.add(i.getSource());
|
|
}
|
|
}
|
|
return neighbours;
|
|
}
|
|
}
|