ProjektGraph/graph/ExampleGraphs.java

165 lines
6.7 KiB
Java
Raw Normal View History

2024-07-05 13:08:33 +00:00
package graph;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class ExampleGraphs {
public DirectedGraph<VertexMarking, EdgeMarking> example1 (){
/*
* Beispiel 1 ist ein "Gitter" an Knoten und Kanten. Dabei hat jede Kante die selbe Gewichtung und Start und Endknoten sind in
* entgegengesetzten Ecken
*
* Ziel:
* Zeigen wie A* direkter sucht als Dikstra
2024-07-05 13:08:33 +00:00
*/
/*
*-*-*-*-*
| | | | |
*-*-*-*-*
| | | | |
*-*-*-*-*
| | | | |
*-*-*-*-*
| | | | |
* *-*-*-*
*/
DirectedGraph<VertexMarking, EdgeMarking> example1 = new DirectedGraph<>();
int size = 5;
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) {
addBidirectionalEdge(example1, vertices[row][col], vertices[row][col + 1], new EdgeWeightMarking(1));
2024-07-05 13:08:33 +00:00
}
if (row < size - 1) {
addBidirectionalEdge(example1, vertices[row][col], vertices[row + 1][col], new EdgeWeightMarking(1));
2024-07-05 13:08:33 +00:00
}
}
}
return example1;
}
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);
}
public DirectedGraph<VertexMarking, EdgeMarking> example2() {
DirectedGraph<VertexMarking, EdgeMarking> example2 = new DirectedGraph<>();
MarkedVertex A = new MarkedVertex<>(50, 50, "A", null, null);
MarkedVertex B = new MarkedVertex<>(150, 50, "B", null, null);
MarkedVertex C = new MarkedVertex<>(250, 50, "C", null, null);
MarkedVertex D = new MarkedVertex<>(50, 150, "D", null, null);
MarkedVertex E = new MarkedVertex<>(150, 150, "E", null, null);
MarkedVertex F = new MarkedVertex<>(250, 150, "F", null, null);
MarkedVertex G = new MarkedVertex<>(150, 250, "G", null, null);
example2.addVertex(A);
example2.addVertex(B);
example2.addVertex(C);
example2.addVertex(D);
example2.addVertex(E);
example2.addVertex(F);
example2.addVertex(G);
example2.addEdge(new MarkedEdge<>("AB", A, B, null, 2));
example2.addEdge(new MarkedEdge<>("AC", A, C, null, 4));
example2.addEdge(new MarkedEdge<>("BD", B, D, null, 5));
example2.addEdge(new MarkedEdge<>("BE", B, E, null, 3));
example2.addEdge(new MarkedEdge<>("CE", C, E, null, 1));
example2.addEdge(new MarkedEdge<>("DF", D, F, null, 2));
example2.addEdge(new MarkedEdge<>("EF", E, F, null, 2));
example2.addEdge(new MarkedEdge<>("EG", E, G, null, 4));
example2.addEdge(new MarkedEdge<>("FG", F, G, null, 3));
return example2;
}
public DirectedGraph<VertexMarking, EdgeMarking> example3() {
DirectedGraph<VertexMarking, EdgeMarking> example3 = new DirectedGraph<>();
MarkedVertex A = new MarkedVertex<>(50, 50, "A", null, null);
MarkedVertex B = new MarkedVertex<>(150, 50, "B", null, null);
MarkedVertex C = new MarkedVertex<>(250, 50, "C", null, null);
MarkedVertex D = new MarkedVertex<>(50, 150, "D", null, null);
MarkedVertex E = new MarkedVertex<>(150, 150, "E", null, null);
example3.addVertex(A);
example3.addVertex(B);
example3.addVertex(C);
example3.addVertex(D);
example3.addVertex(E);
example3.addEdge(new MarkedEdge<>("AB", A, B, null, 1));
example3.addEdge(new MarkedEdge<>("AC", A, C, null, 1));
example3.addEdge(new MarkedEdge<>("BD", B, D, null, 1));
example3.addEdge(new MarkedEdge<>("BE", B, E, null, 2));
example3.addEdge(new MarkedEdge<>("CD", C, D, null, 1));
example3.addEdge(new MarkedEdge<>("CE", C, E, null, 2));
example3.addEdge(new MarkedEdge<>("DE", D, E, null, 1));
return example3;
}
public DirectedGraph<VertexMarking, EdgeMarking> example4() {
DirectedGraph<VertexMarking, EdgeMarking> example4 = new DirectedGraph<>();
MarkedVertex A = new MarkedVertex<>(50, 50, "A", null, null);
MarkedVertex B = new MarkedVertex<>(150, 50, "B", null, null);
MarkedVertex C = new MarkedVertex<>(250, 50, "C", null, null);
MarkedVertex D = new MarkedVertex<>(50, 150, "D", null, null);
MarkedVertex E = new MarkedVertex<>(150, 150, "E", null, null);
MarkedVertex F = new MarkedVertex<>(250, 150, "F", null, null);
MarkedVertex G = new MarkedVertex<>(50, 250, "G", null, null);
MarkedVertex H = new MarkedVertex<>(150, 250, "H", null, null);
MarkedVertex I = new MarkedVertex<>(250, 250, "I", null, null);
example4.addVertex(A);
example4.addVertex(B);
example4.addVertex(C);
example4.addVertex(D);
example4.addVertex(E);
example4.addVertex(F);
example4.addVertex(G);
example4.addVertex(H);
example4.addVertex(I);
example4.addEdge(new MarkedEdge<>("AB", A, B, null, 2));
example4.addEdge(new MarkedEdge<>("AC", A, C, null, 4));
example4.addEdge(new MarkedEdge<>("BD", B, D, null, 3));
example4.addEdge(new MarkedEdge<>("BE", B, E, null, 2));
example4.addEdge(new MarkedEdge<>("CF", C, F, null, 2));
example4.addEdge(new MarkedEdge<>("DG", D, G, null, 2));
example4.addEdge(new MarkedEdge<>("EH", E, H, null, 3));
example4.addEdge(new MarkedEdge<>("FI", F, I, null, 4));
example4.addEdge(new MarkedEdge<>("GH", G, H, null, 1));
example4.addEdge(new MarkedEdge<>("HI", H, I, null, 1));
example4.addEdge(new MarkedEdge<>("GI", G, I, null, 3));
return example4;
}
2024-07-05 13:08:33 +00:00
}