ProjektGraph/graph/UndirectedGraph.java

129 lines
3.3 KiB
Java
Raw Normal View History

2024-06-15 14:48:28 +00:00
package graph;
2024-07-03 17:41:46 +00:00
import OurApplication.OurLogElement;
import logging.LogElementList;
2024-07-02 22:46:21 +00:00
import visualizationElements.Edge;
import visualizationElements.EdgeStyle;
import visualizationElements.Vertex;
2024-06-15 14:48:28 +00:00
import java.util.Objects;
import java.util.Vector;
public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
2024-07-02 22:46:21 +00:00
// ATTRIBUTE
private visualizationElements.Graph screenGraph;
2024-07-03 17:41:46 +00:00
private LogElementList<OurLogElement> logList;
2024-07-02 22:46:21 +00:00
// KONSTRUKTOREN
2024-06-15 14:48:28 +00:00
public UndirectedGraph() {
super();
2024-07-02 22:46:21 +00:00
this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct);
2024-07-03 17:41:46 +00:00
this.logList = new LogElementList<>();
2024-06-15 14:48:28 +00:00
}
public UndirectedGraph(String s) {
super(s);
2024-07-02 22:46:21 +00:00
this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct);
2024-07-03 17:41:46 +00:00
this.logList = new LogElementList<>();
2024-07-02 22:46:21 +00:00
}
// GET-ER
public visualizationElements.Graph getScreenGraph() {
return this.screenGraph;
}
2024-07-03 17:41:46 +00:00
public LogElementList<OurLogElement> getLogList() {
return this.logList;
}
2024-07-02 22:46:21 +00:00
// HINZUFÜGEN
// Kante hinzufügen
public void addEdge(MarkedEdge<U> e) {
super.addEdge(e);
this.screenGraph.getEdges().add(e.getScreenEdge());
}
// Knoten hinzufügen
public void addVertex(MarkedVertex<T> n) {
super.addVertex(n);
this.screenGraph.getVertexes().add(n.getScreenVertex());
}
// LÖSCHEN
// Kante löschen
public void removeEdge(MarkedEdge<U> e) {
super.removeEdge(e);
this.screenGraph.getEdges().remove(e.getScreenEdge());
}
// Knoten löschen
public void removeVertex(MarkedVertex<T> n) {
super.removeVertex(n);
this.screenGraph.getVertexes().remove(n.getScreenVertex());
2024-06-15 14:48:28 +00:00
}
// 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-07-03 17:41:46 +00:00
public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return 1;
}
2024-07-03 19:49:51 +00:00
public int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return 2;
}
2024-07-03 17:41:46 +00:00
2024-06-15 14:48:28 +00:00
}