2024-07-05 13:08:33 +00:00
|
|
|
package graph;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class ExampleGraphs {
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
public DirectedGraph<VertexMarking, EdgeMarking> example1() {
|
2024-07-05 13:08:33 +00:00
|
|
|
|
|
|
|
/*
|
2024-07-07 00:24:27 +00:00
|
|
|
* Beispiel 1 zeigt ein Gitter aus Knoten und Kanten, bei dem jeder Knoten mit
|
|
|
|
* seinen horizontal und vertikal benachbarten Knoten verbunden ist. Alle Kanten
|
|
|
|
* haben die gleiche Gewichtung. Der Startknoten befindet sich in einer Ecke
|
|
|
|
* des Gitters und der Zielknoten in der gegenüberliegenden Ecke.
|
2024-07-05 23:34:51 +00:00
|
|
|
*
|
|
|
|
* Ziel:
|
2024-07-07 00:24:27 +00:00
|
|
|
* Demonstrieren, wie der A*-Algorithmus durch gezieltere Suche effizienter ist
|
|
|
|
* als der Dijkstra-Algorithmus in einem strukturierten Gittergraphen.
|
|
|
|
*
|
|
|
|
* Startknoten: A
|
|
|
|
* Endknoten: Y
|
2024-07-05 13:08:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
DirectedGraph<VertexMarking, EdgeMarking> example1 = new DirectedGraph<>();
|
|
|
|
|
|
|
|
int size = 5;
|
2024-07-06 18:37:42 +00:00
|
|
|
MarkedVertex<VertexMarking>[][] vertices = new MarkedVertex[size][size];
|
2024-07-05 13:08:33 +00:00
|
|
|
|
|
|
|
// Knoten erstellen und zum Graph hinzufügen
|
|
|
|
for (int row = 0; row < size; row++) {
|
|
|
|
for (int col = 0; col < size; col++) {
|
|
|
|
String name = String.valueOf((char) ('A' + row * size + col));
|
|
|
|
vertices[row][col] = new MarkedVertex<>(50 + col * 100, 50 + row * 100, name, null, null);
|
|
|
|
example1.addVertex(vertices[row][col]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kanten horizontal und vertikal verbinden
|
|
|
|
for (int row = 0; row < size; row++) {
|
|
|
|
for (int col = 0; col < size; col++) {
|
|
|
|
if (col < size - 1) {
|
2024-07-06 18:37:42 +00:00
|
|
|
addBidirectionalEdge(example1, vertices[row][col], vertices[row][col + 1], new EdgeWeightMarking(1));
|
2024-07-05 13:08:33 +00:00
|
|
|
}
|
|
|
|
if (row < size - 1) {
|
2024-07-06 18:37:42 +00:00
|
|
|
addBidirectionalEdge(example1, vertices[row][col], vertices[row + 1][col], new EdgeWeightMarking(1));
|
2024-07-05 13:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return example1;
|
|
|
|
}
|
|
|
|
|
2024-07-06 18:37:42 +00:00
|
|
|
private void addBidirectionalEdge(DirectedGraph<VertexMarking, EdgeMarking> graph, MarkedVertex<VertexMarking> from, MarkedVertex<VertexMarking> to, EdgeWeightMarking weight) {
|
|
|
|
MarkedEdge<EdgeMarking> forwardEdge = new MarkedEdge<>("edge" + from.getName() + "_" + to.getName(), from, to, weight, weight.getWeight());
|
|
|
|
MarkedEdge<EdgeMarking> backwardEdge = new MarkedEdge<>("edge" + to.getName() + "_" + from.getName(), to, from, weight, weight.getWeight());
|
2024-07-05 13:08:33 +00:00
|
|
|
graph.addEdge(forwardEdge);
|
|
|
|
graph.addEdge(backwardEdge);
|
|
|
|
}
|
2024-07-05 23:34:51 +00:00
|
|
|
|
|
|
|
public DirectedGraph<VertexMarking, EdgeMarking> example2() {
|
|
|
|
DirectedGraph<VertexMarking, EdgeMarking> example2 = new DirectedGraph<>();
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Beispiel 2 zeigt zwei Wege von einem Startknoten zu einem Zielknoten mit unterschiedlichen Eigenschaften:
|
|
|
|
* Ein Weg (oben) hat wenige Knoten mit Kanten geringer Gewichtung, außer der letzten Kante, die hohe Gewichtung hat.
|
|
|
|
* Der andere Weg (unten) hat mehr Knoten mit höherer Gewichtung der Kanten. Der Algorithmus sollte zuerst den oberen
|
|
|
|
* Weg erkunden, bis er die hohe Gewichtung der letzten Kante des unteren Wegs berücksichtigt.
|
|
|
|
*
|
|
|
|
* Startknoten: A
|
|
|
|
* Endknoten: E
|
|
|
|
*/
|
|
|
|
|
|
|
|
MarkedVertex A = new MarkedVertex<>(100, 100, "A", null, null);
|
|
|
|
MarkedVertex B = new MarkedVertex<>(250, 50, "B", null, null);
|
|
|
|
MarkedVertex C = new MarkedVertex<>(400, 100, "C", null, null);
|
|
|
|
MarkedVertex D = new MarkedVertex<>(550, 50, "D", null, null);
|
|
|
|
MarkedVertex E = new MarkedVertex<>(700, 100, "E", null, null);
|
|
|
|
MarkedVertex F = new MarkedVertex<>(250, 200, "F", null, null);
|
|
|
|
MarkedVertex G = new MarkedVertex<>(550, 200, "G", null, null);
|
2024-07-05 23:34:51 +00:00
|
|
|
|
|
|
|
example2.addVertex(A);
|
|
|
|
example2.addVertex(B);
|
|
|
|
example2.addVertex(C);
|
|
|
|
example2.addVertex(D);
|
|
|
|
example2.addVertex(E);
|
|
|
|
example2.addVertex(F);
|
|
|
|
example2.addVertex(G);
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
example2.addEdge(new MarkedEdge<>("AB", A, B, new EdgeWeightMarking(1), 1));
|
|
|
|
example2.addEdge(new MarkedEdge<>("BC", B, C, new EdgeWeightMarking(1), 1));
|
|
|
|
example2.addEdge(new MarkedEdge<>("CD", C, D, new EdgeWeightMarking(10), 10));
|
|
|
|
example2.addEdge(new MarkedEdge<>("DE", D, E, new EdgeWeightMarking(1), 1));
|
|
|
|
|
|
|
|
example2.addEdge(new MarkedEdge<>("AF", A, F, new EdgeWeightMarking(5), 5));
|
|
|
|
example2.addEdge(new MarkedEdge<>("FG", F, G, new EdgeWeightMarking(5), 5));
|
|
|
|
example2.addEdge(new MarkedEdge<>("GE", G, E, new EdgeWeightMarking(5), 5));
|
2024-07-05 23:34:51 +00:00
|
|
|
|
|
|
|
return example2;
|
|
|
|
}
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
|
|
|
|
|
2024-07-05 23:34:51 +00:00
|
|
|
public DirectedGraph<VertexMarking, EdgeMarking> example3() {
|
2024-07-07 00:24:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Beispiel 3 zeigt ein Labyrinth mit vier horizontalen Wegen, die jeweils aus vier Knoten bestehen.
|
|
|
|
* Jeder Weg führt zum Endpunkt E1, E2, E3 bzw. E4. Ziel ist es zu zeigen, dass die Algorithmen auch
|
|
|
|
* potenziell falsche Wege erkunden können, bevor sie den richtigen Endpunkt erreichen.
|
|
|
|
*
|
|
|
|
* Startknoten: A
|
|
|
|
* Endknoten: E2
|
|
|
|
*/
|
|
|
|
|
2024-07-05 23:34:51 +00:00
|
|
|
DirectedGraph<VertexMarking, EdgeMarking> example3 = new DirectedGraph<>();
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
// Startpunkt A in der Mitte der Y-Koordinate
|
|
|
|
MarkedVertex A = new MarkedVertex<>(100, 250, "A", null, null);
|
|
|
|
|
|
|
|
// Erster Weg
|
|
|
|
MarkedVertex B1 = new MarkedVertex<>(200, 100, "B1", null, null);
|
|
|
|
MarkedVertex C1 = new MarkedVertex<>(300, 100, "C1", null, null);
|
|
|
|
MarkedVertex D1 = new MarkedVertex<>(400, 100, "D1", null, null);
|
|
|
|
MarkedVertex E1 = new MarkedVertex<>(500, 100, "E1", null, null); // Endpunkt des ersten Weges
|
|
|
|
|
|
|
|
// Zweiter Weg
|
|
|
|
MarkedVertex B2 = new MarkedVertex<>(200, 200, "B2", null, null);
|
|
|
|
MarkedVertex C2 = new MarkedVertex<>(300, 200, "C2", null, null);
|
|
|
|
MarkedVertex D2 = new MarkedVertex<>(400, 200, "D2", null, null);
|
|
|
|
MarkedVertex E2 = new MarkedVertex<>(500, 200, "E2", null, null); // Endpunkt des zweiten Weges
|
|
|
|
|
|
|
|
// Dritter Weg
|
|
|
|
MarkedVertex B3 = new MarkedVertex<>(200, 300, "B3", null, null);
|
|
|
|
MarkedVertex C3 = new MarkedVertex<>(300, 300, "C3", null, null);
|
|
|
|
MarkedVertex D3 = new MarkedVertex<>(400, 300, "D3", null, null);
|
|
|
|
MarkedVertex E3 = new MarkedVertex<>(500, 300, "E3", null, null); // Endpunkt des dritten Weges
|
|
|
|
|
|
|
|
// Vierter Weg
|
|
|
|
MarkedVertex B4 = new MarkedVertex<>(200, 400, "B4", null, null);
|
|
|
|
MarkedVertex C4 = new MarkedVertex<>(300, 400, "C4", null, null);
|
|
|
|
MarkedVertex D4 = new MarkedVertex<>(400, 400, "D4", null, null);
|
|
|
|
MarkedVertex E4 = new MarkedVertex<>(500, 400, "E4", null, null); // Endpunkt des vierten Weges
|
2024-07-05 23:34:51 +00:00
|
|
|
|
|
|
|
example3.addVertex(A);
|
2024-07-07 00:24:27 +00:00
|
|
|
example3.addVertex(B1);
|
|
|
|
example3.addVertex(C1);
|
|
|
|
example3.addVertex(D1);
|
|
|
|
example3.addVertex(B2);
|
|
|
|
example3.addVertex(C2);
|
|
|
|
example3.addVertex(D2);
|
|
|
|
example3.addVertex(B3);
|
|
|
|
example3.addVertex(C3);
|
|
|
|
example3.addVertex(D3);
|
|
|
|
example3.addVertex(B4);
|
|
|
|
example3.addVertex(C4);
|
|
|
|
example3.addVertex(D4);
|
|
|
|
example3.addVertex(E1);
|
|
|
|
example3.addVertex(E2);
|
|
|
|
example3.addVertex(E3);
|
|
|
|
example3.addVertex(E4);
|
|
|
|
|
|
|
|
example3.addEdge(new MarkedEdge<>("AB1", A, B1, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("AB2", A, B2, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("AB3", A, B3, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("AB4", A, B4, new EdgeWeightMarking(1), 1));
|
|
|
|
|
|
|
|
example3.addEdge(new MarkedEdge<>("B1C1", B1, C1, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("B2C2", B2, C2, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("B3C3", B3, C3, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("B4C4", B4, C4, new EdgeWeightMarking(1), 1));
|
|
|
|
|
|
|
|
example3.addEdge(new MarkedEdge<>("C1D1", C1, D1, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("C2D2", C2, D2, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("C3D3", C3, D3, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("C4D4", C4, D4, new EdgeWeightMarking(1), 1));
|
|
|
|
|
|
|
|
example3.addEdge(new MarkedEdge<>("D1E1", D1, E1, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("D2E2", D2, E2, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("D3E3", D3, E3, new EdgeWeightMarking(1), 1));
|
|
|
|
example3.addEdge(new MarkedEdge<>("D4E4", D4, E4, new EdgeWeightMarking(1), 1));
|
2024-07-05 23:34:51 +00:00
|
|
|
|
|
|
|
return example3;
|
|
|
|
}
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
|
|
|
|
|
2024-07-05 23:34:51 +00:00
|
|
|
public DirectedGraph<VertexMarking, EdgeMarking> example4() {
|
2024-07-07 00:24:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Beispiel 4 zeigt einen gerichteten Graphen mit deutschen Städten als Knoten und
|
|
|
|
* zufällig gewichteten Kanten.
|
|
|
|
*/
|
|
|
|
|
2024-07-05 23:34:51 +00:00
|
|
|
DirectedGraph<VertexMarking, EdgeMarking> example4 = new DirectedGraph<>();
|
|
|
|
|
2024-07-07 00:24:27 +00:00
|
|
|
// Städte als Knoten
|
|
|
|
MarkedVertex Berlin = new MarkedVertex<>(100, 100, "Berlin", null, null);
|
|
|
|
MarkedVertex Hamburg = new MarkedVertex<>(200, 100, "Hamburg", null, null);
|
|
|
|
MarkedVertex München = new MarkedVertex<>(300, 100, "München", null, null);
|
|
|
|
MarkedVertex Köln = new MarkedVertex<>(100, 200, "Köln", null, null);
|
|
|
|
MarkedVertex Frankfurt = new MarkedVertex<>(200, 200, "Frankfurt", null, null);
|
|
|
|
MarkedVertex Stuttgart = new MarkedVertex<>(300, 200, "Stuttgart", null, null);
|
|
|
|
MarkedVertex Leipzig = new MarkedVertex<>(100, 300, "Leipzig", null, null);
|
|
|
|
MarkedVertex Dresden = new MarkedVertex<>(200, 300, "Dresden", null, null);
|
|
|
|
MarkedVertex Hannover = new MarkedVertex<>(300, 300, "Hannover", null, null);
|
|
|
|
|
|
|
|
// Kanten mit zufälligen Gewichtungen
|
|
|
|
example4.addVertex(Berlin);
|
|
|
|
example4.addVertex(Hamburg);
|
|
|
|
example4.addVertex(München);
|
|
|
|
example4.addVertex(Köln);
|
|
|
|
example4.addVertex(Frankfurt);
|
|
|
|
example4.addVertex(Stuttgart);
|
|
|
|
example4.addVertex(Leipzig);
|
|
|
|
example4.addVertex(Dresden);
|
|
|
|
example4.addVertex(Hannover);
|
|
|
|
|
|
|
|
example4.addEdge(new MarkedEdge<>("Berlin-Hamburg", Berlin, Hamburg, new EdgeWeightMarking(1), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Berlin-München", Berlin, München, new EdgeWeightMarking(2), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Berlin-Köln", Berlin, Köln, new EdgeWeightMarking(3), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Hamburg-Frankfurt", Hamburg, Frankfurt, new EdgeWeightMarking(2), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Hamburg-Leipzig", Hamburg, Leipzig, new EdgeWeightMarking(3), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("München-Stuttgart", München, Stuttgart, new EdgeWeightMarking(1), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("München-Dresden", München, Dresden, new EdgeWeightMarking(2), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Köln-Hannover", Köln, Hannover, new EdgeWeightMarking(3), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Frankfurt-Stuttgart", Frankfurt, Stuttgart, new EdgeWeightMarking(1), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Stuttgart-Leipzig", Stuttgart, Leipzig, new EdgeWeightMarking(2), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Leipzig-Dresden", Leipzig, Dresden, new EdgeWeightMarking(1), 1));
|
|
|
|
example4.addEdge(new MarkedEdge<>("Dresden-Hannover", Dresden, Hannover, new EdgeWeightMarking(3), 1));
|
2024-07-05 23:34:51 +00:00
|
|
|
|
|
|
|
return example4;
|
|
|
|
}
|
2024-07-05 13:08:33 +00:00
|
|
|
}
|
2024-07-07 00:24:27 +00:00
|
|
|
|
|
|
|
|