ProjektGraph/graph/Graph.java

418 lines
12 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-07 14:29:45 +00:00
import java.awt.*;
2024-06-25 15:18:34 +00:00
import java.util.*;
2024-06-15 14:48:28 +00:00
2024-07-07 21:07:56 +00:00
/**
* Abstract class representing a generic graph.
* @param <T> Type parameter for vertex markings, extends VertexMarking.
* @param <U> Type parameter for edge markings, extends EdgeMarking.
*/
public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// ATTRIBUTE
2024-06-15 14:48:28 +00:00
2024-07-07 21:07:56 +00:00
/** The name of the graph. */
2024-06-15 14:48:28 +00:00
private String name;
2024-07-07 21:07:56 +00:00
/** Vector containing all marked vertices in the graph. */
2024-06-15 14:48:28 +00:00
private Vector<MarkedVertex<T>> vertexes;
2024-07-07 21:07:56 +00:00
/** Vector containing all marked edges in the graph. */
2024-06-25 15:18:34 +00:00
private Vector<MarkedEdge<U>> edges;
// KONSTRUKTOREN
2024-06-15 14:48:28 +00:00
2024-07-07 21:07:56 +00:00
/**
* Default constructor initializes an empty graph.
*/
2024-06-15 14:48:28 +00:00
public Graph() {
this.edges = new Vector<>();
this.vertexes = new Vector<>();
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Constructor that initializes the graph with a given name.
* @param s The name of the graph.
*/
2024-06-15 14:48:28 +00:00
public Graph(String s) {
this();
this.name = s;
}
// GET-ER
2024-07-07 21:07:56 +00:00
/**
* Returns the name of the graph.
* @return The name of the graph.
*/
public String getName() {
return this.name;
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Returns all edges in the graph.
* @return Vector containing all edges in the graph.
*/
2024-06-25 15:18:34 +00:00
public Vector<MarkedEdge<U>> getAllEdges() {
return this.edges;
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Returns all vertices in the graph.
* @return Vector containing all vertices in the graph.
*/
public Vector<MarkedVertex<T>> getAllVertexes() {
return this.vertexes;
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Abstract method to get the visualization representation of the graph.
* @return Visualization representation of the graph.
*/
2024-07-03 17:41:46 +00:00
public abstract visualizationElements.Graph getScreenGraph();
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Abstract method to get the log list associated with the graph.
* @return LogElementList containing log elements associated with the graph.
*/
2024-07-03 17:41:46 +00:00
public abstract LogElementList<OurLogElement> getLogList();
// SET-ER
2024-07-07 21:07:56 +00:00
/**
* Sets the name of the graph.
* @param s The name to set for the graph.
*/
2024-06-15 14:48:28 +00:00
public void setName(String s) {
this.name = s;
}
2024-07-07 21:07:56 +00:00
/**
* Returns a string representation of the graph.
* @return String representation of the graph.
*/
2024-06-15 14:48:28 +00:00
public String toString() {
2024-06-26 00:20:36 +00:00
String output = "";
for (Vertex i: this.vertexes) {
output += i.toString();
output += "\n";
}
for (Edge i: this.edges) {
output += i.toString();
output += "\n";
}
return output;
2024-06-15 14:48:28 +00:00
}
// HINZUFÜGEN
2024-07-07 21:07:56 +00:00
/**
* Adds an edge to the graph.
* @param e The edge to add.
*/
2024-06-25 15:18:34 +00:00
public void addEdge(MarkedEdge<U> e) {
2024-06-15 14:48:28 +00:00
this.edges.add(e);
}
2024-07-07 21:07:56 +00:00
/**
* Adds a vertex to the graph.
* @param n The vertex to add.
*/
2024-06-15 14:48:28 +00:00
public void addVertex(MarkedVertex<T> n) {
this.vertexes.add(n);
}
// LÖSCHEN
2024-07-07 21:07:56 +00:00
/**
* Removes an edge from the graph.
* @param e The edge to remove.
*/
2024-06-25 15:18:34 +00:00
public void removeEdge(MarkedEdge<U> e) {
2024-06-15 14:48:28 +00:00
this.edges.remove(e);
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Removes an edge from the graph based on its name.
* @param s The name of the edge to remove.
* @throws NameDoesNotExistException If the edge with the specified name does not exist.
*/
public void removeEdge(String s) throws NameDoesNotExistException {
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.edges) {
2024-06-15 14:48:28 +00:00
if (Objects.equals(i.getName(), s)) {
2024-07-02 22:46:21 +00:00
this.removeEdge(i);
2024-06-15 14:48:28 +00:00
return;
}
}
throw new NameDoesNotExistException("One of the Edges might not exist");
}
2024-07-07 21:07:56 +00:00
/**
* Removes a vertex from the graph.
* @param n The vertex to remove.
*/
2024-06-15 14:48:28 +00:00
public void removeVertex(MarkedVertex<T> n) {
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == n || i.getDestination() == n) {
this.removeEdge(i);
}
}
2024-06-15 14:48:28 +00:00
this.vertexes.remove(n);
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Removes a vertex from the graph based on its name.
* @param s The name of the vertex to remove.
* @throws NameDoesNotExistException If the vertex with the specified name does not exist.
*/
public void removeVertex(String s) throws NameDoesNotExistException {
2024-06-15 14:48:28 +00:00
for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) {
2024-07-02 22:46:21 +00:00
this.removeVertex(i);
2024-06-15 14:48:28 +00:00
return;
}
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
}
// GRAPH EIGENSCHAFTEN
2024-06-15 14:48:28 +00:00
2024-07-07 21:07:56 +00:00
/**
* Returns the number of edges in the graph.
* @return The number of edges in the graph.
*/
public int numberOfEdges() {
return this.edges.size();
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00
/**
* Returns the number of vertices in the graph.
* @return The number of vertices in the graph.
*/
public int numberOfVertexes() {
return this.vertexes.size();
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00
/**
* Computes and returns the degree of the graph.
* @return The degree of the graph.
*/
public int degree() {
return 2 * this.edges.size();
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00
/**
* Checks if a vertex is present in the graph.
* @param n The vertex to check.
* @return True if the vertex is present, false otherwise.
*/
public boolean hasVertex(MarkedVertex<T> n) {
return this.vertexes.contains(n);
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00
/**
* Checks if a vertex with a specific name is present in the graph.
* @param s The name of the vertex to check.
* @return True if the vertex with the given name is present, false otherwise.
*/
2024-06-15 14:48:28 +00:00
public boolean hasVertex(String s) {
for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) {
return true;
}
}
return false;
}
2024-07-07 21:07:56 +00:00
/**
* Checks if an edge is present in the graph.
* @param e The edge to check.
* @return True if the edge is present, false otherwise.
*/
2024-06-25 15:18:34 +00:00
public boolean hasEdge(MarkedEdge<U> e) {
return this.edges.contains(e);
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00
/**
* Checks if an edge with a specific name is present in the graph.
* @param s The name of the edge to check.
* @return True if the edge with the given name is present, false otherwise.
*/
2024-06-15 14:48:28 +00:00
public boolean hasEdge(String s) {
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.edges) {
2024-06-15 14:48:28 +00:00
if (Objects.equals(i.getName(), s)) {
return true;
}
}
return false;
}
// KNOTEN EIGENSCHAFTEN
2024-07-07 21:07:56 +00:00
/**
* Checks if there is an edge between two vertices in the graph.
* @param v1 First vertex.
* @param v2 Second vertex.
* @return True if there is an edge between the vertices, false otherwise.
*/
public boolean hasEdge(MarkedVertex<T> v1, MarkedVertex<T> v2) {
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.edges) {
2024-07-07 21:07:56 +00:00
if ((i.getSource() == v1 && i.getDestination() == v2) || (i.getSource() == v2 && i.getDestination() == v1)) {
return true;
}
}
return false;
2024-06-15 14:48:28 +00:00
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Checks if there is an edge between two vertices identified by their names.
* @param s1 Name of the first vertex.
* @param s2 Name of the second vertex.
* @return True if there is an edge between the vertices, false otherwise.
* @throws NameDoesNotExistException If one of the vertex names does not exist in the graph.
*/
2024-06-15 14:48:28 +00:00
public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException {
MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null;
for (MarkedVertex<T> 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);
}
}
2024-07-07 21:07:56 +00:00
/**
* Checks if two vertices are adjacent (connected by an edge) in the graph.
* @param n1 First vertex.
* @param n2 Second vertex.
* @return True if the vertices are adjacent, false otherwise.
*/
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.edges) {
if ((i.getSource() == n1 && i.getDestination() == n2) || (i.getSource() == n2 && i.getDestination() == n1)) {
2024-06-15 14:48:28 +00:00
return true;
}
}
return false;
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Checks if two vertices identified by their names are adjacent (connected by an edge) in the graph.
* @param s1 Name of the first vertex.
* @param s2 Name of the second vertex.
* @return True if the vertices are adjacent, false otherwise.
* @throws NameDoesNotExistException If one of the vertex names does not exist in the graph.
*/
public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException {
MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null;
for (MarkedVertex<T> 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);
}
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00
/**
* Checks if a vertex has a loop (an edge connecting it to itself) in the graph.
* @param n The vertex to check.
* @return True if the vertex has a loop, false otherwise.
*/
2024-06-15 14:48:28 +00:00
public boolean hasLoop(MarkedVertex<T> n) {
2024-06-25 15:18:34 +00:00
for (MarkedEdge<U> i: this.edges) {
2024-06-15 14:48:28 +00:00
if (i.getSource() == i.getDestination() && i.getSource() == n) {
return true;
}
}
return false;
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Checks if a vertex identified by its name has a loop (an edge connecting it to itself) in the graph.
* @param s The name of the vertex to check.
* @return True if the vertex has a loop, false otherwise.
* @throws NameDoesNotExistException If the vertex name does not exist in the graph.
*/
public boolean hasLoop(String s) throws NameDoesNotExistException {
2024-06-15 14:48:28 +00:00
for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) {
return hasLoop(i);
}
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
}
2024-06-25 15:18:34 +00:00
2024-07-03 17:41:46 +00:00
2024-07-07 21:07:56 +00:00
/**
* Resets the screen graph colors of all vertices to black.
*/
public void clearScreenGraphColor() {
for (visualizationElements.Vertex screenVertexes : this.getScreenGraph().getVertexes()) {
2024-07-07 14:29:45 +00:00
screenVertexes.setColor(Color.BLACK);
}
}
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Abstract method to find the shortest path between two vertices using Dijkstra's algorithm.
* @param n1 Starting vertex.
* @param n2 Destination vertex.
* @return Length of the shortest path between the vertices.
*/
2024-07-03 17:41:46 +00:00
public abstract int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2);
2024-07-07 22:11:04 +00:00
2024-07-07 21:07:56 +00:00
/**
* Abstract method to find the shortest path between two vertices using A* algorithm.
* @param n1 Starting vertex.
* @param n2 Destination vertex.
* @return Length of the shortest path between the vertices.
*/
2024-07-03 19:49:51 +00:00
public abstract int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2);
2024-06-15 14:48:28 +00:00
}
2024-07-07 21:07:56 +00:00