# Conflicts:
#	graph/DirectedGraph.java
#	graph/Graph.java
#	graph/NameDoesNotExistException.java
#	graph/UndirectedGraph.java
#	out/production/ProjektGraph/graph/Graph.class
#	out/production/ProjektGraph/graph/WrapperComparator.class
#	out/production/ProjektGraph/graph/WrapperElement.class
This commit is contained in:
i23007 2024-07-08 17:35:55 +02:00
commit deaffff595
33 changed files with 858 additions and 176 deletions

View File

@ -6,21 +6,28 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="70f8ea87-9ffc-471a-8059-ebbfc323adcc" name="Changes" comment="LegendArea gestaltet"> <list default="true" id="70f8ea87-9ffc-471a-8059-ebbfc323adcc" name="Changes" comment="LegendArea gestaltet">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/OurApplication/OurApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/OurApplication/OurApplication.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/DirectedGraph.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/DirectedGraph.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/graph/DirectedGraph.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/DirectedGraph.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/EdgeMarking.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/EdgeMarking.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/EdgeWeightMarking.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/EdgeWeightMarking.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/Graph.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/Graph.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/MarkedEdge.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/MarkedEdge.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/MarkedVertex.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/MarkedVertex.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/NameDoesNotExistException.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/NameDoesNotExistException.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/graph/UndirectedGraph.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/UndirectedGraph.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/graph/UndirectedGraph.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/UndirectedGraph.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjektGraph/OurApplication/OurAlgorithm.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjektGraph/OurApplication/OurAlgorithm.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/graph/VertexMarking.java" beforeDir="false" afterPath="$PROJECT_DIR$/graph/VertexMarking.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjektGraph/OurApplication/OurApplication.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjektGraph/OurApplication/OurApplication.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjektGraph/OurApplication/OurParameterArea.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjektGraph/OurApplication/OurParameterArea.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjektGraph/graph/DirectedGraph.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjektGraph/graph/DirectedGraph.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjektGraph/graph/ExampleGraphs.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjektGraph/graph/ExampleGraphs.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjektGraph/graph/UndirectedGraph.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjektGraph/graph/UndirectedGraph.class" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" /> <option name="LAST_RESOLUTION" value="IGNORE" />
</component> </component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY"> <option name="RECENT_BRANCH_BY_REPOSITORY">
<map> <map>

View File

@ -6,6 +6,7 @@ import visualisation.HybridWindow;
import visualizationElements.Vertex; import visualizationElements.Vertex;
import javax.swing.*; import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.util.Random; import java.util.Random;
@ -47,13 +48,13 @@ public class OurApplication {
DirectedGraph<VertexMarking, EdgeMarking> myGraph = new DirectedGraph<>(); DirectedGraph<VertexMarking, EdgeMarking> myGraph = new DirectedGraph<>();
ExampleGraphs temp = new ExampleGraphs(); ExampleGraphs temp = new ExampleGraphs();
myGraph = temp.example1(); //myGraph = temp.example2();
//sean: Ich wollte erst hier dann das ausgewählte Beispiel reinhauen, jedoch wird das hier nur einmal am Anfang aufgerufen //sean: Ich wollte erst hier dann das ausgewählte Beispiel reinhauen, jedoch wird das hier nur einmal am Anfang aufgerufen
System.out.println(myGraph.toString()); System.out.println(myGraph.toString());
LogElementList<OurLogElement> logList=new LogElementList<OurLogElement>(); LogElementList<OurLogElement> logList = new LogElementList<>();
OurParameterArea parameterArea = new OurParameterArea(); OurParameterArea parameterArea = new OurParameterArea();
OurMethodButtons methodButtons = new OurMethodButtons(); OurMethodButtons methodButtons = new OurMethodButtons();
OurDrawArea drawArea = new OurDrawArea(logList,"GraphVisualization"); OurDrawArea drawArea = new OurDrawArea(logList,"GraphVisualization");
@ -76,5 +77,4 @@ public class OurApplication {
frame.setVisible(true); frame.setVisible(true);
} }
} }

View File

@ -1,9 +1,6 @@
package OurApplication; package OurApplication;
import graph.DirectedGraph; import graph.*;
import graph.EdgeMarking;
import graph.ExampleGraphs;
import graph.VertexMarking;
import visualisation.ParameterArea; import visualisation.ParameterArea;
import javax.swing.*; import javax.swing.*;
@ -22,13 +19,19 @@ public class OurParameterArea extends ParameterArea{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private DirectedGraph<VertexMarking, EdgeMarking> customGraph;
private JRadioButton button1; private JRadioButton button1;
private JRadioButton button2; private JRadioButton button2;
private JRadioButton button3; private JRadioButton button3;
private JRadioButton button4; private JRadioButton button4;
private JRadioButton button5; private JRadioButton button5;
private ExampleGraphs temp; private JButton buttonAddGraph;
private JTextField textField1;
private JTextField textField2;
private int selectedExample; private int selectedExample;
@ -41,8 +44,8 @@ public class OurParameterArea extends ParameterArea{
*/ */
public OurParameterArea() { public OurParameterArea() {
super(); super();
temp = new ExampleGraphs();
selectedExample = 1; // Standardmäßig Beispiel 1 selectedExample = 1; // Standardmäßig Beispiel 1
this.customGraph = new DirectedGraph<>();
setBorder(BorderFactory.createTitledBorder("ParameterArea")); setBorder(BorderFactory.createTitledBorder("ParameterArea"));
@ -56,6 +59,11 @@ public class OurParameterArea extends ParameterArea{
button4 = new JRadioButton("Beispiel 4"); button4 = new JRadioButton("Beispiel 4");
button5 = new JRadioButton("Eigener Graph"); button5 = new JRadioButton("Eigener Graph");
buttonAddGraph = new JButton("Graph einfügen");
// Eingabefelder
textField1 = new JTextField("Knoten");
textField2 = new JTextField("Kanten");
// ButtonGroup erstellen und Buttons hinzufügen, um die gegenseitige Ausschließung zu gewährleisten // ButtonGroup erstellen und Buttons hinzufügen, um die gegenseitige Ausschließung zu gewährleisten
ButtonGroup group = new ButtonGroup(); ButtonGroup group = new ButtonGroup();
@ -66,47 +74,33 @@ public class OurParameterArea extends ParameterArea{
group.add(button5); group.add(button5);
// ActionListener hinzufügen // ActionListener hinzufügen
button1.addActionListener(new ActionListener() { button1.addActionListener(e -> selectedExample = 1);
@Override
public void actionPerformed(ActionEvent e) { button2.addActionListener(e -> selectedExample = 2);
selectedExample = 1;
} button3.addActionListener(e -> selectedExample = 3);
button4.addActionListener(e -> selectedExample = 4);
button5.addActionListener(e -> selectedExample = 5);
buttonAddGraph.addActionListener(e -> {
String input1 = textField1.getText();
String input2 = textField2.getText();
this.customGraph = createGraph(input1, input2);
}); });
button2.addActionListener(new ActionListener() { // Buttons zum Panel hinzufügen
@Override add(button1);
public void actionPerformed(ActionEvent e) { add(button2);
selectedExample = 2; add(button3);
} add(button4);
}); add(button5);
button3.addActionListener(new ActionListener() { add(buttonAddGraph);
@Override add(textField1);
public void actionPerformed(ActionEvent e) { add(textField2);
selectedExample = 3;
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedExample = 4;
}
});
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedExample = 5;
}
});
// Buttons zum Panel hinzufügen
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
} }
public DirectedGraph<VertexMarking, EdgeMarking> getSelectedGraph() { public DirectedGraph<VertexMarking, EdgeMarking> getSelectedGraph() {
@ -119,11 +113,43 @@ public class OurParameterArea extends ParameterArea{
case 4: case 4:
return temp.example4(); return temp.example4();
case 5: case 5:
return new DirectedGraph<>(); return this.customGraph;
case 1: case 1:
default: default:
return temp.example1(); return temp.example1();
} }
} }
// Vertex Eingabe: A-23-23;A-23-23
// Edge Eingabe: A-B-15;A-B-15
public DirectedGraph<VertexMarking, EdgeMarking> createGraph(String vertices, String edges) {
DirectedGraph<VertexMarking, EdgeMarking> newGraph = new DirectedGraph<>();
String[] allVertices = vertices.split(";");
String[] allEdges = edges.split(";");
for (String i: allVertices) {
String[] current = i.split("-");
newGraph.addVertex(new MarkedVertex<>(Integer.parseInt(current[1]), Integer.parseInt(current[2]), current[0], null, Color.BLACK));
}
MarkedVertex<VertexMarking> start = null;
MarkedVertex<VertexMarking> end = null;
for (String i: allEdges) {
String[] current = i.split("-");
for (MarkedVertex<VertexMarking> j: newGraph.getAllVertexes()) {
if (j.getName() == current[0]) {
start = j;
} else if (j.getName() == current[1]) {
end = j;
}
}
newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2]))));
}
return newGraph;
}
} }

View File

@ -12,6 +12,14 @@ import java.util.Objects;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Vector; import java.util.Vector;
/**
* Represents a directed graph with vertex and edge markings. This class extends the Graph class and
* provides additional functionality for directed graphs, including visualization and logging capabilities.
*
* @param <T> the type of vertex marking
* @param <U> the type of edge marking
*/
public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> { public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
// ATTRIBUTE // ATTRIBUTE
@ -22,6 +30,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// KONSTRUKTOREN // KONSTRUKTOREN
/**
* Constructs an empty directed graph.
*/
public DirectedGraph() { public DirectedGraph() {
super(); super();
this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), true, EdgeStyle.Direct); this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), true, EdgeStyle.Direct);
@ -29,6 +40,11 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
/**
* Constructs a directed graph from a string representation.
*
* @param s the string representation of the graph
*/
public DirectedGraph(String s) { public DirectedGraph(String s) {
super(s); super(s);
this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), true, EdgeStyle.Direct); this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), true, EdgeStyle.Direct);
@ -38,10 +54,21 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// GET-ER // GET-ER
/**
* Returns the screen graph associated with this directed graph.
*
* @return the screen graph
*/
public visualizationElements.Graph getScreenGraph() { public visualizationElements.Graph getScreenGraph() {
return this.screenGraph; return this.screenGraph;
} }
/**
* Returns a copy of the screen graph associated with this directed graph.
*
* @return a copy of the screen graph
*/
public visualizationElements.Graph getScreenGraphCopy() { public visualizationElements.Graph getScreenGraphCopy() {
visualizationElements.Graph graphCopy = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), true, EdgeStyle.Direct); visualizationElements.Graph graphCopy = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), true, EdgeStyle.Direct);
Vector<visualizationElements.Vertex> copiedVertexes = new Vector<>(); Vector<visualizationElements.Vertex> copiedVertexes = new Vector<>();
@ -60,6 +87,11 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
/**
* Returns the log list associated with this directed graph.
*
* @return the log list
*/
public LogElementList<OurLogElement> getLogList() { public LogElementList<OurLogElement> getLogList() {
return this.logList; return this.logList;
} }
@ -67,14 +99,22 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// HINZUFÜGEN // HINZUFÜGEN
// Kante hinzufügen /**
* Adds an edge to the graph.
*
* @param e the edge to be added
*/
public void addEdge(MarkedEdge<U> e) { public void addEdge(MarkedEdge<U> e) {
super.addEdge(e); super.addEdge(e);
this.screenGraph.getEdges().add(e.getScreenEdge()); this.screenGraph.getEdges().add(e.getScreenEdge());
} }
// Knoten hinzufügen /**
* Adds a vertex to the graph.
*
* @param n the vertex to be added
*/
public void addVertex(MarkedVertex<T> n) { public void addVertex(MarkedVertex<T> n) {
super.addVertex(n); super.addVertex(n);
this.screenGraph.getVertexes().add(n.getScreenVertex()); this.screenGraph.getVertexes().add(n.getScreenVertex());
@ -83,14 +123,22 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// LÖSCHEN // LÖSCHEN
// Kante löschen /**
* Removes an edge from the graph.
*
* @param e the edge to be removed
*/
public void removeEdge(MarkedEdge<U> e) { public void removeEdge(MarkedEdge<U> e) {
super.removeEdge(e); super.removeEdge(e);
this.screenGraph.getEdges().remove(e.getScreenEdge()); this.screenGraph.getEdges().remove(e.getScreenEdge());
} }
// Knoten löschen /**
* Removes a vertex from the graph.
*
* @param n the vertex to be removed
*/
public void removeVertex(MarkedVertex<T> n) { public void removeVertex(MarkedVertex<T> n) {
super.removeVertex(n); super.removeVertex(n);
this.screenGraph.getVertexes().remove(n.getScreenVertex()); this.screenGraph.getVertexes().remove(n.getScreenVertex());
@ -99,7 +147,13 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// KNOTEN EIGENSCHAFTEN // KNOTEN EIGENSCHAFTEN
// Prüfung, ob zwei Knoten stark adjazent sind /**
* Checks if two vertices are strongly adjacent (i.e., there is a bidirectional edge between them).
*
* @param n1 the first vertex
* @param n2 the second vertex
* @return true if the vertices are strongly adjacent, false otherwise
*/
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) { public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
boolean n1ton2 = false; boolean n1ton2 = false;
boolean n2ton1 = false; boolean n2ton1 = false;
@ -114,6 +168,14 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
/**
* Checks if two vertices are strongly adjacent (i.e., there is a bidirectional edge between them) by their names.
*
* @param s1 the name of the first vertex
* @param s2 the name of the second vertex
* @return true if the vertices are strongly adjacent, false otherwise
* @throws NameDoesNotExistException if one of the vertices does not exist
*/
public boolean areStrongAdjacent(String s1, String s2) throws NameDoesNotExistException { public boolean areStrongAdjacent(String s1, String s2) throws NameDoesNotExistException {
MarkedVertex<T> n1 = null; MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null; MarkedVertex<T> n2 = null;
@ -132,7 +194,12 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
// Prüfung des Eingangsgrades eines Knotens /**
* Returns the in-degree of a vertex (i.e., the number of edges directed towards the vertex).
*
* @param n the vertex
* @return the in-degree of the vertex
*/
public int inDegree(MarkedVertex<T> n) { public int inDegree(MarkedVertex<T> n) {
int degree = 0; int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) { for (MarkedEdge<U> i: this.getAllEdges()) {
@ -144,6 +211,13 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
/**
* Returns the in-degree of a vertex by its name.
*
* @param s the name of the vertex
* @return the in-degree of the vertex
* @throws NameDoesNotExistException if the vertex does not exist
*/
public int inDegree(String s) throws NameDoesNotExistException{ public int inDegree(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.getAllVertexes()) { for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
@ -154,7 +228,12 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
// Prüfung des Ausgangsgrades eines Knotens /**
* Returns the out-degree of a vertex (i.e., the number of edges directed away from the vertex).
*
* @param n the vertex
* @return the out-degree of the vertex
*/
public int outDegree(MarkedVertex<T> n) { public int outDegree(MarkedVertex<T> n) {
int degree = 0; int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) { for (MarkedEdge<U> i: this.getAllEdges()) {
@ -166,6 +245,13 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
/**
* Returns the out-degree of a vertex by its name.
*
* @param s the name of the vertex
* @return the out-degree of the vertex
* @throws NameDoesNotExistException if the vertex does not exist
*/
public int outDegree(String s) throws NameDoesNotExistException{ public int outDegree(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.getAllVertexes()) { for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
@ -176,7 +262,12 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
// Prüfung, welche Knoten Vorgänger sind /**
* Returns a vector of predecessor vertices (i.e., vertices with edges directed towards the specified vertex).
*
* @param n the vertex
* @return a vector of predecessor vertices
*/
public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) { public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> predecessors = new Vector<>(); Vector<MarkedVertex<T>> predecessors = new Vector<>();
for (MarkedEdge<U> i: this.getAllEdges()) { for (MarkedEdge<U> i: this.getAllEdges()) {
@ -188,7 +279,12 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
// Prüfung, welche Knoten Nachfolger sind /**
* Returns a vector of successor vertices (i.e., vertices with edges directed away from the specified vertex).
*
* @param n the vertex
* @return a vector of successor vertices
*/
public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) { public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> successors = new Vector<>(); Vector<MarkedVertex<T>> successors = new Vector<>();
for (MarkedEdge<U> i: this.getAllEdges()) { for (MarkedEdge<U> i: this.getAllEdges()) {
@ -202,7 +298,13 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// AUFGABE 2 // AUFGABE 2
// Dijkstra-Algorithmus /**
* Finds the shortest path between two vertices using Dijkstra's algorithm.
*
* @param n1 the starting vertex
* @param n2 the ending vertex
* @return the shortest distance from n1 to n2, or -1 if no path is found
*/
public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) { public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) {
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken // Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
@ -254,7 +356,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
textDescription = "Visit " + nextVertex.getElement().getName(); textDescription = "Visit " + nextVertex.getElement().getName();
// Logging // Logging
System.out.println(textDescription); System.out.println(textDescription);
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE); if (nextVertex.getElement().getScreenVertex().getColor() != Color.RED) {
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
} }
@ -305,7 +409,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// Logging // Logging
textDescription = "Add " + i.getName() + " with " + dist + " weight to queue."; textDescription = "Add " + i.getName() + " with " + dist + " weight to queue.";
System.out.println(textDescription); System.out.println(textDescription);
i.getScreenVertex().setColor(Color.YELLOW); if (i.getScreenVertex().getColor() != Color.RED) {
i.getScreenVertex().setColor(Color.YELLOW);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Nehme nächsten Knoten in die Queue auf // Nehme nächsten Knoten in die Queue auf
@ -332,7 +438,13 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
} }
/**
* Finds the shortest path between two vertices using the A* algorithm.
*
* @param n1 the starting vertex
* @param n2 the ending vertex
* @return the shortest distance from n1 to n2, or -1 if no path is found
*/
public double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) { public double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken // Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
@ -388,7 +500,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
textDescription = "Visit " + nextVertex.getElement().getName(); textDescription = "Visit " + nextVertex.getElement().getName();
// Logging // Logging
System.out.println(textDescription); System.out.println(textDescription);
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE); if (nextVertex.getElement().getScreenVertex().getColor() != Color.RED) {
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
} }
@ -444,7 +558,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// Logging // Logging
textDescription = "Add " + i.getName() + " with " + distToFinish + " weight to queue."; textDescription = "Add " + i.getName() + " with " + distToFinish + " weight to queue.";
System.out.println(textDescription); System.out.println(textDescription);
i.getScreenVertex().setColor(Color.YELLOW); if (i.getScreenVertex().getColor() != Color.RED) {
i.getScreenVertex().setColor(Color.YELLOW);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Nehme nächsten Knoten in die Queue auf // Nehme nächsten Knoten in die Queue auf

View File

@ -1,63 +1,108 @@
package graph; package graph;
/**
* Abstract class representing an edge in a graph.
*/
public abstract class Edge { public abstract class Edge {
// ATTRIBUTE // ATTRIBUTES
/** The name of the edge. */
private String name; private String name;
/** The source vertex of the edge. */
private Vertex source; private Vertex source;
/** The destination vertex of the edge. */
private Vertex destination; private Vertex destination;
// CONSTRUCTORS
// KONSTRUKTOREN /**
* Default constructor initializes the edge with empty name and null source and destination vertices.
*/
public Edge() { public Edge() {
this.name = ""; this.name = "";
this.source = null; this.source = null;
this.destination = null; this.destination = null;
} }
/**
* Constructor initializes the edge with a specified name, source, and destination vertices.
*
* @param s The name of the edge.
* @param n1 The source vertex of the edge.
* @param n2 The destination vertex of the edge.
*/
public Edge(String s, Vertex n1, Vertex n2) { public Edge(String s, Vertex n1, Vertex n2) {
this.name = s; this.name = s;
this.source = n1; this.source = n1;
this.destination = n2; this.destination = n2;
} }
// GETTERS
// GET-ER /**
* Retrieves the name of the edge.
*
* @return The name of the edge.
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Retrieves the source vertex of the edge.
*
* @return The source vertex of the edge.
*/
public Vertex getSource() { public Vertex getSource() {
return this.source; return this.source;
} }
/**
* Retrieves the destination vertex of the edge.
*
* @return The destination vertex of the edge.
*/
public Vertex getDestination() { public Vertex getDestination() {
return this.destination; return this.destination;
} }
/**
* Abstract method to retrieve the screen representation of the edge for visualization.
*
* @return The visualizationElements.Edge representing the screen edge.
*/
public abstract visualizationElements.Edge getScreenEdge(); public abstract visualizationElements.Edge getScreenEdge();
// SETTERS
// SET-ER /**
* Sets the name of the edge.
*
* @param s The name to set for the edge.
*/
public void setName(String s) { public void setName(String s) {
this.name = s; this.name = s;
} }
/**
* Sets the source vertex of the edge.
*
* @param n The source vertex to set for the edge.
*/
public void setSource(Vertex n) { public void setSource(Vertex n) {
this.source = n; this.source = n;
} }
/**
* Sets the destination vertex of the edge.
*
* @param n The destination vertex to set for the edge.
*/
public void setDestination(Vertex n) { public void setDestination(Vertex n) {
this.destination = n; this.destination = n;
} }
} }

View File

@ -1,19 +1,8 @@
package graph; package graph;
public abstract class EdgeMarking extends Marking{ /**
} * Abstract class representing an edge marking.
* Extends the Marking class.
class EdgeWeightMarking extends EdgeMarking{ */
private int weight; public abstract class EdgeMarking extends Marking {
EdgeWeightMarking(int weight){
this.weight = weight;
}
public void setWeight(int weight){
this.weight=weight;
}
public int getWeight(){
return this.weight;
}
} }

View File

@ -0,0 +1,39 @@
package graph;
/**
* Class representing a specific type of edge marking: edge weight.
* Inherits from EdgeMarking.
*/
public class EdgeWeightMarking extends EdgeMarking {
/** The weight value associated with this edge marking. */
private int weight;
/**
* Constructor to initialize the EdgeWeightMarking with a specified weight.
*
* @param weight The weight value to set.
*/
public EdgeWeightMarking(int weight) {
this.weight = weight;
}
/**
* Sets the weight value of this edge marking.
*
* @param weight The weight value to set.
*/
public void setWeight(int weight) {
this.weight = weight;
}
/**
* Retrieves the weight value of this edge marking.
*
* @return The weight value of this edge marking.
*/
public int getWeight() {
return this.weight;
}
}

View File

@ -6,23 +6,40 @@ import logging.LogElementList;
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
/**
* 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> { public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// ATTRIBUTE // ATTRIBUTE
/** The name of the graph. */
private String name; private String name;
/** Vector containing all marked vertices in the graph. */
private Vector<MarkedVertex<T>> vertexes; private Vector<MarkedVertex<T>> vertexes;
/** Vector containing all marked edges in the graph. */
private Vector<MarkedEdge<U>> edges; private Vector<MarkedEdge<U>> edges;
// KONSTRUKTOREN // KONSTRUKTOREN
/**
* Default constructor initializes an empty graph.
*/
public Graph() { public Graph() {
this.edges = new Vector<>(); this.edges = new Vector<>();
this.vertexes = new Vector<>(); this.vertexes = new Vector<>();
} }
/**
* Constructor that initializes the graph with a given name.
* @param s The name of the graph.
*/
public Graph(String s) { public Graph(String s) {
this(); this();
this.name = s; this.name = s;
@ -31,35 +48,62 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// GET-ER // GET-ER
/**
* Returns the name of the graph.
* @return The name of the graph.
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Returns all edges in the graph.
* @return Vector containing all edges in the graph.
*/
public Vector<MarkedEdge<U>> getAllEdges() { public Vector<MarkedEdge<U>> getAllEdges() {
return this.edges; return this.edges;
} }
/**
* Returns all vertices in the graph.
* @return Vector containing all vertices in the graph.
*/
public Vector<MarkedVertex<T>> getAllVertexes() { public Vector<MarkedVertex<T>> getAllVertexes() {
return this.vertexes; return this.vertexes;
} }
/**
* Abstract method to get the visualization representation of the graph.
* @return Visualization representation of the graph.
*/
public abstract visualizationElements.Graph getScreenGraph(); public abstract visualizationElements.Graph getScreenGraph();
/**
* Abstract method to get the log list associated with the graph.
* @return LogElementList containing log elements associated with the graph.
*/
public abstract LogElementList<OurLogElement> getLogList(); public abstract LogElementList<OurLogElement> getLogList();
// SET-ER // SET-ER
/**
* Sets the name of the graph.
* @param s The name to set for the graph.
*/
public void setName(String s) { public void setName(String s) {
this.name = s; this.name = s;
} }
// Ausgabe /**
* Returns a string representation of the graph.
* @return String representation of the graph.
*/
public String toString() { public String toString() {
String output = ""; String output = "";
for (Vertex i: this.vertexes) { for (Vertex i: this.vertexes) {
@ -76,13 +120,19 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// HINZUFÜGEN // HINZUFÜGEN
// Kante hinzufügen /**
* Adds an edge to the graph.
* @param e The edge to add.
*/
public void addEdge(MarkedEdge<U> e) { public void addEdge(MarkedEdge<U> e) {
this.edges.add(e); this.edges.add(e);
} }
// Knoten hinzufügen /**
* Adds a vertex to the graph.
* @param n The vertex to add.
*/
public void addVertex(MarkedVertex<T> n) { public void addVertex(MarkedVertex<T> n) {
this.vertexes.add(n); this.vertexes.add(n);
} }
@ -90,13 +140,21 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// LÖSCHEN // LÖSCHEN
// Kante löschen /**
* Removes an edge from the graph.
* @param e The edge to remove.
*/
public void removeEdge(MarkedEdge<U> e) { public void removeEdge(MarkedEdge<U> e) {
this.edges.remove(e); this.edges.remove(e);
} }
public void removeEdge(String s) throws NameDoesNotExistException{ /**
* 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 {
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
this.removeEdge(i); this.removeEdge(i);
@ -107,7 +165,10 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
// Knoten löschen /**
* Removes a vertex from the graph.
* @param n The vertex to remove.
*/
public void removeVertex(MarkedVertex<T> n) { public void removeVertex(MarkedVertex<T> n) {
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == n || i.getDestination() == n) { if (i.getSource() == n || i.getDestination() == n) {
@ -118,7 +179,12 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
public void removeVertex(String s) throws NameDoesNotExistException{ /**
* 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 {
for (MarkedVertex<T> i: this.vertexes) { for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
this.removeVertex(i); this.removeVertex(i);
@ -131,31 +197,47 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// GRAPH EIGENSCHAFTEN // GRAPH EIGENSCHAFTEN
// Kantenmenge /**
* Returns the number of edges in the graph.
* @return The number of edges in the graph.
*/
public int numberOfEdges() { public int numberOfEdges() {
return this.edges.size(); return this.edges.size();
} }
// Knotenmenge /**
* Returns the number of vertices in the graph.
* @return The number of vertices in the graph.
*/
public int numberOfVertexes() { public int numberOfVertexes() {
return this.vertexes.size(); return this.vertexes.size();
} }
// Grad des Graphen /**
// https://loeh.app.uni-regensburg.de/teaching/discmath_ws0910/graphentheorie_ueberblick.pdf * Computes and returns the degree of the graph.
* @return The degree of the graph.
*/
public int degree() { public int degree() {
return 2 * this.edges.size(); return 2 * this.edges.size();
} }
// Prüfung, ob Knoten im Graph /**
* 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) { public boolean hasVertex(MarkedVertex<T> n) {
return this.vertexes.contains(n); return this.vertexes.contains(n);
} }
/**
* 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.
*/
public boolean hasVertex(String s) { public boolean hasVertex(String s) {
for (MarkedVertex<T> i: this.vertexes) { for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
@ -166,12 +248,20 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
// Prüfung, ob Kante im Graph /**
* Checks if an edge is present in the graph.
* @param e The edge to check.
* @return True if the edge is present, false otherwise.
*/
public boolean hasEdge(MarkedEdge<U> e) { public boolean hasEdge(MarkedEdge<U> e) {
return this.edges.contains(e); return this.edges.contains(e);
} }
/**
* 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.
*/
public boolean hasEdge(String s) { public boolean hasEdge(String s) {
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
@ -184,13 +274,15 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// KNOTEN EIGENSCHAFTEN // KNOTEN EIGENSCHAFTEN
// Prüfung, ob Kante zwischen zwei Knoten /**
// TODO schauen, ob es Aufgabe entspricht * 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) { public boolean hasEdge(MarkedVertex<T> v1, MarkedVertex<T> v2) {
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == v1 && i.getDestination() == v2) { if ((i.getSource() == v1 && i.getDestination() == v2) || (i.getSource() == v2 && i.getDestination() == v1)) {
return true;
} else if (i.getSource() == v2 && i.getDestination() == v1) {
return true; return true;
} }
} }
@ -198,6 +290,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
/**
* 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.
*/
public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException { public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException {
MarkedVertex<T> n1 = null; MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null; MarkedVertex<T> n2 = null;
@ -216,7 +315,12 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
// Prüfung, ob zwei Knoten adjazent sind /**
* 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) { public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if ((i.getSource() == n1 && i.getDestination() == n2) || (i.getSource() == n2 && i.getDestination() == n1)) { if ((i.getSource() == n1 && i.getDestination() == n2) || (i.getSource() == n2 && i.getDestination() == n1)) {
@ -227,6 +331,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
/**
* 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 { public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException {
MarkedVertex<T> n1 = null; MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null; MarkedVertex<T> n2 = null;
@ -245,7 +356,11 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
// Prüfung, ob Knoten eine Schlinge besitzt /**
* 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.
*/
public boolean hasLoop(MarkedVertex<T> n) { public boolean hasLoop(MarkedVertex<T> n) {
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == i.getDestination() && i.getSource() == n) { if (i.getSource() == i.getDestination() && i.getSource() == n) {
@ -256,7 +371,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
public boolean hasLoop(String s) throws NameDoesNotExistException{ /**
* 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 {
for (MarkedVertex<T> i: this.vertexes) { for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
return hasLoop(i); return hasLoop(i);
@ -266,15 +387,31 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
// Methode für das Zurücksetzten der Knotenfarben /**
public void clearScreenGraphColor(){ * Resets the screen graph colors of all vertices to black.
for(visualizationElements.Vertex screenVertexes : this.getScreenGraph().getVertexes()){ */
public void clearScreenGraphColor() {
for (visualizationElements.Vertex screenVertexes : this.getScreenGraph().getVertexes()) {
screenVertexes.setColor(Color.BLACK); screenVertexes.setColor(Color.BLACK);
} }
} }
/**
* 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.
*/
public abstract int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2); public abstract int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2);
/**
* 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.
*/
public abstract double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2); public abstract double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2);
} }

View File

@ -2,46 +2,80 @@ package graph;
import java.awt.*; import java.awt.*;
public class MarkedEdge<U extends EdgeMarking> extends Edge{ /**
* The MarkedEdge class extends the Edge class and includes additional attributes
* for marking and visualization on a screen. It supports constructors to initialize
* these attributes and provides getter and setter methods for accessing and modifying them.
*
* @param <U> A type that extends EdgeMarking, used for marking the edge.
*/
public class MarkedEdge<U extends EdgeMarking> extends Edge {
// ATTRIBUTE // ATTRIBUTES
private U marking; private U marking;
private visualizationElements.Edge screenEdge; private visualizationElements.Edge screenEdge;
// KONSTRUKTOREN // CONSTRUCTORS
/**
* Default constructor initializes the edge with default values.
*/
public MarkedEdge() { public MarkedEdge() {
super(); super();
this.screenEdge = new visualizationElements.Edge(null, null); this.screenEdge = new visualizationElements.Edge(null, null);
} }
/**
* Constructor initializes the edge with a name, vertices, and marking.
*
* @param s The name of the edge.
* @param n1 The source vertex of the edge.
* @param n2 The destination vertex of the edge.
* @param u The marking of the edge.
*/
public MarkedEdge(String s, Vertex n1, Vertex n2, U u) { public MarkedEdge(String s, Vertex n1, Vertex n2, U u) {
super(s, n1, n2); super(s, n1, n2);
this.marking = u; this.marking = u;
EdgeWeightMarking m = (EdgeWeightMarking) this.marking; EdgeWeightMarking m = (EdgeWeightMarking) this.marking;
this.screenEdge = new visualizationElements.Edge(n1.getScreenVertex(), n2.getScreenVertex(), Integer.toString(m.getWeight()), Color.BLACK); this.screenEdge = new visualizationElements.Edge(
n1.getScreenVertex(),
n2.getScreenVertex(),
Integer.toString(m.getWeight()),
Color.BLACK);
} }
// GETTERS
// GET-ER /**
* Gets the marking of the edge.
*
* @return The marking of the edge.
*/
public U getMarking() { public U getMarking() {
return this.marking; return this.marking;
} }
/**
* Gets the screen edge used for visualization.
*
* @return The screen edge.
*/
public visualizationElements.Edge getScreenEdge() { public visualizationElements.Edge getScreenEdge() {
return this.screenEdge; return this.screenEdge;
} }
// SET-ER // SETTERS
/**
* Sets the marking of the edge.
*
* @param u The new marking of the edge.
*/
public void setMarking(U u) { public void setMarking(U u) {
this.marking = u; this.marking = u;
EdgeWeightMarking m = (EdgeWeightMarking) this.marking; EdgeWeightMarking m = (EdgeWeightMarking) this.marking;
@ -49,9 +83,16 @@ public class MarkedEdge<U extends EdgeMarking> extends Edge{
} }
// OUTPUT
// Ausgabe /**
* Returns a string representation of the MarkedEdge.
*
* @return A string representing the MarkedEdge.
*/
@Override
public String toString() { public String toString() {
return "MarkedEdge " + this.getName() + " from " + this.getSource().getName() + " to " + this.getDestination().getName(); return "MarkedEdge " + this.getName() + " from " + this.getSource().getName() + " to " + this.getDestination().getName();
} }
} }

View File

@ -2,20 +2,29 @@ package graph;
import java.awt.*; import java.awt.*;
public class MarkedVertex<T extends VertexMarking> extends Vertex{ /**
* The MarkedVertex class extends the Vertex class and includes additional attributes
* for marking, coordinates, and visualization on a screen. It supports various
* constructors to initialize these attributes and provides getter and setter methods
* for accessing and modifying them.
*
* @param <T> A type that extends VertexMarking, used for marking the vertex.
*/
public class MarkedVertex<T extends VertexMarking> extends Vertex {
// ATTRIBUTE // ATTRIBUTE
private T marking; private T marking;
private int xCord; private int xCord;
private int yCord; private int yCord;
private visualizationElements.Vertex screenVertex; private visualizationElements.Vertex screenVertex;
// KONSTRUKTOREN // CONSTRUCTORS
/**
* Default constructor initializes the vertex with default values.
*/
public MarkedVertex() { public MarkedVertex() {
super(); super();
this.screenVertex = new visualizationElements.Vertex(0, 0); this.screenVertex = new visualizationElements.Vertex(0, 0);
@ -24,6 +33,12 @@ public class MarkedVertex<T extends VertexMarking> extends Vertex{
} }
/**
* Constructor initializes the vertex with a name and marking.
*
* @param s The name of the vertex.
* @param t The marking of the vertex.
*/
public MarkedVertex(String s, T t) { public MarkedVertex(String s, T t) {
super(s); super(s);
this.marking = t; this.marking = t;
@ -33,6 +48,15 @@ public class MarkedVertex<T extends VertexMarking> extends Vertex{
} }
/**
* Constructor initializes the vertex with coordinates, name, marking, and color.
*
* @param xCord The x-coordinate of the vertex.
* @param yCord The y-coordinate of the vertex.
* @param name The name of the vertex.
* @param t The marking of the vertex.
* @param color The color of the vertex for visualization purposes.
*/
public MarkedVertex(int xCord, int yCord, String name, T t, Color color) { public MarkedVertex(int xCord, int yCord, String name, T t, Color color) {
super(name); super(name);
this.marking = t; this.marking = t;
@ -42,30 +66,55 @@ public class MarkedVertex<T extends VertexMarking> extends Vertex{
} }
// GET-ER // GETTERS
/**
* Gets the marking of the vertex.
*
* @return The marking of the vertex.
*/
public T getMarking() { public T getMarking() {
return this.marking; return this.marking;
} }
/**
* Gets the screen vertex used for visualization.
*
* @return The screen vertex.
*/
public visualizationElements.Vertex getScreenVertex() { public visualizationElements.Vertex getScreenVertex() {
return this.screenVertex; return this.screenVertex;
} }
/**
* Gets the coordinates of the vertex.
*
* @return An array containing the x and y coordinates of the vertex.
*/
public int[] getCords() { public int[] getCords() {
return new int[]{this.xCord, this.yCord}; return new int[]{this.xCord, this.yCord};
} }
// SET-ER // SETTERS
/**
* Sets the marking of the vertex.
*
* @param t The new marking of the vertex.
*/
public void setMarking(T t) { public void setMarking(T t) {
this.marking = t; this.marking = t;
} }
/**
* Sets the coordinates of the vertex and updates the screen vertex's position.
*
* @param cords An array containing the new x and y coordinates of the vertex.
*/
public void setCords(int[] cords) { public void setCords(int[] cords) {
this.xCord = cords[0]; this.xCord = cords[0];
this.yCord = cords[1]; this.yCord = cords[1];
@ -74,7 +123,14 @@ public class MarkedVertex<T extends VertexMarking> extends Vertex{
} }
// Ausgabe // OUTPUT
/**
* Returns a string representation of the MarkedVertex.
*
* @return A string representing the MarkedVertex.
*/
@Override
public String toString() { public String toString() {
return "MarkedVertex " + this.getName(); return "MarkedVertex " + this.getName();
} }

View File

@ -1,4 +1,10 @@
package graph; package graph;
/**
* Abstract class representing a marking for vertices or edges in a graph.
* This class serves as a base class for specific types of markings.
*/
public abstract class Marking { public abstract class Marking {
// No additional attributes or methods are defined in this abstract class.
// Specific types of markings (VertexMarking, EdgeMarking) should extend this class.
} }

View File

@ -1,67 +1,135 @@
package graph; package graph;
import java.util.Comparator; import java.util.Comparator;
// Exception, das User nach einem ungültigen Knoten sucht /**
* Exception thrown when a user attempts to access a non-existent vertex by name.
*/
public class NameDoesNotExistException extends Exception { public class NameDoesNotExistException extends Exception {
/**
* Constructs a new NameDoesNotExistException with no detail message.
*/
public NameDoesNotExistException() { public NameDoesNotExistException() {
super(); super();
} }
/**
* Constructs a new NameDoesNotExistException with the specified detail message.
*
* @param message The detail message.
*/
public NameDoesNotExistException(String message) { public NameDoesNotExistException(String message) {
super(message); super(message);
} }
/**
* Constructs a new NameDoesNotExistException with the specified detail message and cause.
*
* @param message The detail message.
* @param cause The cause of the exception.
*/
public NameDoesNotExistException(String message, Throwable cause) { public NameDoesNotExistException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
/**
* Constructs a new NameDoesNotExistException with the specified cause and a detail message
* that includes the cause's description.
*
* @param cause The cause of the exception.
*/
public NameDoesNotExistException(Throwable cause) { public NameDoesNotExistException(Throwable cause) {
super(cause); super(cause);
} }
} }
// Element in der PriorityQueue
/**
* Represents an element stored in a priority queue for graph algorithms.
*
* @param <T> The type of vertex marking associated with the element.
*/
class WrapperElement<T extends VertexMarking> { class WrapperElement<T extends VertexMarking> {
// ATTRIBUTE // ATTRIBUTE
private MarkedVertex<T> n1; private MarkedVertex<T> element;
private double prio; private double priority;
// KONSTRUKTOR // KONSTRUKTOR
public WrapperElement(MarkedVertex<T> n1, double prio) { /**
this.n1 = n1; * Constructs a WrapperElement with the specified marked vertex and priority.
this.prio = prio; *
* @param element The marked vertex to wrap.
* @param priority The priority associated with the element.
*/
public WrapperElement(MarkedVertex<T> element, double priority) {
this.element = element;
this.priority = priority;
} }
// GET-ER // GET-ER
/**
* Retrieves the marked vertex stored in this wrapper element.
*
* @return The marked vertex.
*/
public MarkedVertex<T> getElement() { public MarkedVertex<T> getElement() {
return this.n1; return this.element;
} }
public double getPrio() { /**
return this.prio; * Retrieves the priority associated with this wrapper element.
*
* @return The priority.
*/
public double getPriority() {
return this.priority;
} }
// Ausgabe // Ausgabe
/**
* Returns a string representation of this WrapperElement.
*
* @return A string representation containing the element and its priority.
*/
public String toString() { public String toString() {
return "Wrapper with " + this.n1 + " with prio " + this.prio; return "WrapperElement{" +
"element=" + element +
", priority=" + priority +
'}';
} }
} }
// 2 Elemente in der PriorityQueue Vergleichen
/**
* Comparator for comparing WrapperElement objects based on their priority.
*
* @param <T> The type of vertex marking associated with the WrapperElement.
*/
class WrapperComparator<T extends VertexMarking> implements Comparator<WrapperElement<T>> { class WrapperComparator<T extends VertexMarking> implements Comparator<WrapperElement<T>> {
/**
* Compares two WrapperElement objects based on their priorities.
*
* @param element1 The first WrapperElement to compare.
* @param element2 The second WrapperElement to compare.
* @return A negative integer, zero, or a positive integer as the first element's
* priority is less than, equal to, or greater than the second element's priority.
*/
public int compare(WrapperElement<T> element1, WrapperElement<T> element2) { public int compare(WrapperElement<T> element1, WrapperElement<T> element2) {
return Double.compare(element1.getPrio(), element2.getPrio()); return Double.compare(element1.getPriority(), element2.getPriority());
} }
} }

View File

@ -12,6 +12,13 @@ import java.util.Objects;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Vector; import java.util.Vector;
/**
* Represents an undirected graph with vertices marked by T and edges marked by U.
* Inherits from the Graph class and provides additional functionality specific to undirected graphs.
*
* @param <T> Type of marking for vertices in the graph.
* @param <U> Type of marking for edges in the graph.
*/
public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> { public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
// ATTRIBUTE // ATTRIBUTE
@ -22,6 +29,9 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
// KONSTRUKTOREN // KONSTRUKTOREN
/**
* Constructs an empty undirected graph with default properties.
*/
public UndirectedGraph() { public UndirectedGraph() {
super(); super();
this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct); this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct);
@ -29,6 +39,11 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
} }
/**
* Constructs an undirected graph with a specified name.
*
* @param s The name of the graph.
*/
public UndirectedGraph(String s) { public UndirectedGraph(String s) {
super(s); super(s);
this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct); this.screenGraph = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct);
@ -38,16 +53,31 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
// GET-ER // GET-ER
/**
* Retrieves the visualization graph associated with this undirected graph.
*
* @return The visualization graph.
*/
public visualizationElements.Graph getScreenGraph() { public visualizationElements.Graph getScreenGraph() {
return this.screenGraph; return this.screenGraph;
} }
/**
* Retrieves the log list containing logging elements related to operations on this graph.
*
* @return The log element list.
*/
public LogElementList<OurLogElement> getLogList() { public LogElementList<OurLogElement> getLogList() {
return this.logList; return this.logList;
} }
/**
* Creates and retrieves a deep copy of the visualization graph associated with this undirected graph.
*
* @return A deep copy of the visualization graph.
*/
public visualizationElements.Graph getScreenGraphCopy() { public visualizationElements.Graph getScreenGraphCopy() {
visualizationElements.Graph graphCopy = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct); visualizationElements.Graph graphCopy = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct);
Vector<visualizationElements.Vertex> copiedVertexes = new Vector<>(); Vector<visualizationElements.Vertex> copiedVertexes = new Vector<>();
@ -68,14 +98,22 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
// HINZUFÜGEN // HINZUFÜGEN
// Kante hinzufügen /**
* Adds an edge to the undirected graph and updates the associated visualization graph.
*
* @param e The edge to be added.
*/
public void addEdge(MarkedEdge<U> e) { public void addEdge(MarkedEdge<U> e) {
super.addEdge(e); super.addEdge(e);
this.screenGraph.getEdges().add(e.getScreenEdge()); this.screenGraph.getEdges().add(e.getScreenEdge());
} }
// Knoten hinzufügen /**
* Adds a vertex to the undirected graph and updates the associated visualization graph.
*
* @param n The vertex to be added.
*/
public void addVertex(MarkedVertex<T> n) { public void addVertex(MarkedVertex<T> n) {
super.addVertex(n); super.addVertex(n);
this.screenGraph.getVertexes().add(n.getScreenVertex()); this.screenGraph.getVertexes().add(n.getScreenVertex());
@ -84,14 +122,22 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
// LÖSCHEN // LÖSCHEN
// Kante löschen /**
* Removes an edge from the undirected graph and updates the associated visualization graph.
*
* @param e The edge to be removed.
*/
public void removeEdge(MarkedEdge<U> e) { public void removeEdge(MarkedEdge<U> e) {
super.removeEdge(e); super.removeEdge(e);
this.screenGraph.getEdges().remove(e.getScreenEdge()); this.screenGraph.getEdges().remove(e.getScreenEdge());
} }
// Knoten löschen /**
* Removes a vertex from the undirected graph and updates the associated visualization graph.
*
* @param n The vertex to be removed.
*/
public void removeVertex(MarkedVertex<T> n) { public void removeVertex(MarkedVertex<T> n) {
super.removeVertex(n); super.removeVertex(n);
this.screenGraph.getVertexes().remove(n.getScreenVertex()); this.screenGraph.getVertexes().remove(n.getScreenVertex());
@ -100,7 +146,13 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
// KNOTEN EIGENSCHAFTEN // KNOTEN EIGENSCHAFTEN
// Prüfung des Grades eines Knotens /**
* Returns the degree of a specified vertex in the undirected graph.
* The degree of a vertex is the number of edges incident to it.
*
* @param n The vertex for which to determine the degree.
* @return The degree of the vertex.
*/
public int degree(MarkedVertex<T> n) { public int degree(MarkedVertex<T> n) {
int degree = 0; int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) { for (MarkedEdge<U> i: this.getAllEdges()) {
@ -115,7 +167,14 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
} }
public int degree(String s) throws NameDoesNotExistException{ /**
* Returns the degree of a specified vertex by its name in the undirected graph.
*
* @param s The name of the vertex for which to determine the degree.
* @return The degree of the vertex.
* @throws NameDoesNotExistException If the vertex with the specified name does not exist in the graph.
*/
public int degree(String s) throws NameDoesNotExistException {
for (MarkedVertex<T> i: this.getAllVertexes()) { for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
return degree(i); return degree(i);
@ -125,7 +184,12 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
} }
// Prüfung, welche Knoten Nachbarn sind /**
* Returns a vector containing all neighbors (adjacent vertices) of a specified vertex in the undirected graph.
*
* @param n The vertex for which to retrieve neighbors.
* @return A vector of neighboring vertices.
*/
public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) { public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> neighbours = new Vector<>(); Vector<MarkedVertex<T>> neighbours = new Vector<>();
for (MarkedEdge<U> i: this.getAllEdges()) { for (MarkedEdge<U> i: this.getAllEdges()) {
@ -139,6 +203,13 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
} }
/**
* Computes the shortest path between two vertices using Dijkstra's algorithm in the undirected graph.
*
* @param n1 The starting vertex of the shortest path.
* @param n2 The ending vertex of the shortest path.
* @return The length of the shortest path between n1 and n2.
*/
public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) { public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) {
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken // Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
@ -189,7 +260,9 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
textDescription = "Visit " + nextVertex.getElement().getName(); textDescription = "Visit " + nextVertex.getElement().getName();
// Logging // Logging
System.out.println(textDescription); System.out.println(textDescription);
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE); if (nextVertex.getElement().getScreenVertex().getColor() != Color.RED) {
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
} }
@ -241,7 +314,9 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
// Logging // Logging
textDescription = "Add " + i.getName() + " with " + dist + " weight to queue."; textDescription = "Add " + i.getName() + " with " + dist + " weight to queue.";
System.out.println(textDescription); System.out.println(textDescription);
i.getScreenVertex().setColor(Color.YELLOW); if (i.getScreenVertex().getColor() != Color.RED) {
i.getScreenVertex().setColor(Color.YELLOW);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Nehme nächsten Knoten in die Queue auf // Nehme nächsten Knoten in die Queue auf
@ -268,6 +343,13 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
} }
/**
* Computes the shortest path between two vertices using A* algorithm in the undirected graph.
*
* @param n1 The starting vertex of the shortest path.
* @param n2 The ending vertex of the shortest path.
* @return The length of the shortest path between n1 and n2.
*/
public double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) { public double getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken // Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
@ -322,7 +404,9 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
textDescription = "Visit " + nextVertex.getElement().getName(); textDescription = "Visit " + nextVertex.getElement().getName();
// Logging // Logging
System.out.println(textDescription); System.out.println(textDescription);
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE); if (nextVertex.getElement().getScreenVertex().getColor() != Color.RED) {
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
} }
@ -369,16 +453,15 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
continue; continue;
} }
// Vorgänger aktualisieren
predecessors.put(i, nextVertex.getElement());
// Aktualisiere Distanz von Start zu nächstem Knoten // Aktualisiere Distanz von Start zu nächstem Knoten
distance.put(i, dist); distance.put(i, dist);
// Logging // Logging
textDescription = "Add " + i.getName() + " with " + distToFinish + " weight to queue."; textDescription = "Add " + i.getName() + " with " + distToFinish + " weight to queue.";
System.out.println(textDescription); System.out.println(textDescription);
i.getScreenVertex().setColor(Color.YELLOW); if (i.getScreenVertex().getColor() != Color.RED) {
i.getScreenVertex().setColor(Color.YELLOW);
}
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy())); this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Nehme nächsten Knoten in die Queue auf // Nehme nächsten Knoten in die Queue auf

View File

@ -1,5 +1,9 @@
package graph; package graph;
/**
* Represents a vertex in a graph.
* This class serves as a base abstraction for vertices in various types of graphs.
*/
public abstract class Vertex { public abstract class Vertex {
// ATTRIBUTE // ATTRIBUTE
@ -9,11 +13,19 @@ public abstract class Vertex {
// KONSTRUKTOREN // KONSTRUKTOREN
/**
* Constructs a vertex with an empty name.
*/
public Vertex() { public Vertex() {
this.name = ""; this.name = "";
} }
/**
* Constructs a vertex with a specified name.
*
* @param s The name of the vertex.
*/
public Vertex(String s) { public Vertex(String s) {
this.name = s; this.name = s;
} }
@ -21,16 +33,32 @@ public abstract class Vertex {
// GET-ER // GET-ER
/**
* Retrieves the name of the vertex.
*
* @return The name of the vertex.
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Abstract method to retrieve the visualization representation of the vertex.
* Concrete subclasses will provide specific implementations.
*
* @return The visualization representation of the vertex.
*/
public abstract visualizationElements.Vertex getScreenVertex(); public abstract visualizationElements.Vertex getScreenVertex();
// SET-ER // SET-ER
/**
* Sets the name of the vertex.
*
* @param s The new name for the vertex.
*/
public void setName(String s) { public void setName(String s) {
this.name = s; this.name = s;
} }

View File

@ -1,19 +1,60 @@
package graph; package graph;
/**
* Represents a marking associated with a vertex in a graph.
* This class serves as a base abstraction for vertex markings.
*/
public abstract class VertexMarking extends Marking { public abstract class VertexMarking extends Marking {
// This class currently does not have any additional attributes or methods.
// It extends Marking, inheriting its properties and behaviors.
} }
class VertexWeightMarking extends VertexMarking{
/**
* Represents a weight marking associated with a vertex in a graph.
* Extends {@link VertexMarking}.
*/
class VertexWeightMarking extends VertexMarking {
// ATTRIBUTE
private int weight; private int weight;
VertexWeightMarking(int weight){
// KONSTRUKTOREN
/**
* Constructs a vertex weight marking with the specified weight.
*
* @param weight The weight value associated with the vertex.
*/
VertexWeightMarking(int weight) {
this.weight = weight; this.weight = weight;
} }
public void setWeight(int weight){
this.weight=weight; // GET-ER
}
public int getWeight(){ /**
* Retrieves the weight associated with this vertex weight marking.
*
* @return The weight of the vertex marking.
*/
public int getWeight() {
return this.weight; return this.weight;
} }
// SET-ER
/**
* Sets the weight associated with this vertex weight marking.
*
* @param weight The new weight value to set.
*/
public void setWeight(int weight) {
this.weight = weight;
}
} }