package graph; import OurApplication.OurLogElement; import logging.LogElementList; import java.awt.*; import java.util.*; public abstract class Graph { // ATTRIBUTE private String name; private Vector> vertexes; private Vector> edges; // KONSTRUKTOREN public Graph() { this.edges = new Vector<>(); this.vertexes = new Vector<>(); } public Graph(String s) { this(); this.name = s; } // GET-ER public String getName() { return this.name; } public Vector> getAllEdges() { return this.edges; } public Vector> getAllVertexes() { return this.vertexes; } public abstract visualizationElements.Graph getScreenGraph(); public abstract LogElementList getLogList(); // SET-ER public void setName(String s) { this.name = s; } // Ausgabe public String toString() { String output = ""; for (Vertex i: this.vertexes) { output += i.toString(); output += "\n"; } for (Edge i: this.edges) { output += i.toString(); output += "\n"; } return output; } // HINZUFÜGEN // Kante hinzufügen public void addEdge(MarkedEdge e) { this.edges.add(e); } // Knoten hinzufügen public void addVertex(MarkedVertex n) { this.vertexes.add(n); } // LÖSCHEN // Kante löschen public void removeEdge(MarkedEdge e) { this.edges.remove(e); } public void removeEdge(String s) throws NameDoesNotExistException{ for (MarkedEdge i: this.edges) { if (Objects.equals(i.getName(), s)) { this.removeEdge(i); return; } } throw new NameDoesNotExistException("One of the Edges might not exist"); } // Knoten löschen public void removeVertex(MarkedVertex n) { for (MarkedEdge i: this.edges) { if (i.getSource() == n || i.getDestination() == n) { this.removeEdge(i); } } this.vertexes.remove(n); } public void removeVertex(String s) throws NameDoesNotExistException{ for (MarkedVertex i: this.vertexes) { if (Objects.equals(i.getName(), s)) { this.removeVertex(i); return; } } throw new NameDoesNotExistException("One of the Vertexes might not exist"); } // GRAPH EIGENSCHAFTEN // Kantenmenge public int numberOfEdges() { return this.edges.size(); } // Knotenmenge public int numberOfVertexes() { return this.vertexes.size(); } // Grad des Graphen // https://loeh.app.uni-regensburg.de/teaching/discmath_ws0910/graphentheorie_ueberblick.pdf public int degree() { return 2 * this.edges.size(); } // Prüfung, ob Knoten im Graph public boolean hasVertex(MarkedVertex n) { return this.vertexes.contains(n); } public boolean hasVertex(String s) { for (MarkedVertex i: this.vertexes) { if (Objects.equals(i.getName(), s)) { return true; } } return false; } // Prüfung, ob Kante im Graph public boolean hasEdge(MarkedEdge e) { return this.edges.contains(e); } public boolean hasEdge(String s) { for (MarkedEdge i: this.edges) { if (Objects.equals(i.getName(), s)) { return true; } } return false; } // KNOTEN EIGENSCHAFTEN // Prüfung, ob Kante zwischen zwei Knoten // TODO schauen, ob es Aufgabe entspricht public boolean hasEdge(MarkedVertex v1, MarkedVertex v2) { for (MarkedEdge i: this.edges) { if (i.getSource() == v1 && i.getDestination() == v2) { return true; } else if (i.getSource() == v2 && i.getDestination() == v1) { return true; } } return false; } public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException { MarkedVertex n1 = null; MarkedVertex n2 = null; for (MarkedVertex i: this.getAllVertexes()) { if (Objects.equals(i.getName(), s1)) { n1 = i; } else if (Objects.equals(i.getName(), s2)) { n2 = i; } } if (n1 == null || n2 == null) { throw new NameDoesNotExistException("One of the Vertexes might not exist"); } else { return hasEdge(n1, n2); } } // Prüfung, ob zwei Knoten adjazent sind public boolean areAdjacent(MarkedVertex n1, MarkedVertex n2) { for (MarkedEdge i: this.edges) { if ((i.getSource() == n1 && i.getDestination() == n2) || (i.getSource() == n2 && i.getDestination() == n1)) { return true; } } return false; } public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException { MarkedVertex n1 = null; MarkedVertex n2 = null; for (MarkedVertex i: this.getAllVertexes()) { if (Objects.equals(i.getName(), s1)) { n1 = i; } else if (Objects.equals(i.getName(), s2)) { n2 = i; } } if (n1 == null || n2 == null) { throw new NameDoesNotExistException("One of the Vertexes might not exist"); } else { return areAdjacent(n1, n2); } } // Prüfung, ob Knoten eine Schlinge besitzt public boolean hasLoop(MarkedVertex n) { for (MarkedEdge i: this.edges) { if (i.getSource() == i.getDestination() && i.getSource() == n) { return true; } } return false; } public boolean hasLoop(String s) throws NameDoesNotExistException{ for (MarkedVertex i: this.vertexes) { if (Objects.equals(i.getName(), s)) { return hasLoop(i); } } throw new NameDoesNotExistException("One of the Vertexes might not exist"); } // Methode für das Zurücksetzten der Knotenfarben public void clearScreenGraphColor(){ for(visualizationElements.Vertex screenVertexes : this.getScreenGraph().getVertexes()){ screenVertexes.setColor(Color.BLACK); } } public abstract int getShortestPathDijkstra(MarkedVertex n1, MarkedVertex n2); public abstract int getShortestPathAStar(MarkedVertex n1, MarkedVertex n2); }