68 lines
2.2 KiB
Java
68 lines
2.2 KiB
Java
|
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
|
||
|
*/
|
||
|
|
||
|
|
||
|
/*
|
||
|
*-*-*-*-*
|
||
|
| | | | |
|
||
|
*-*-*-*-*
|
||
|
| | | | |
|
||
|
*-*-*-*-*
|
||
|
| | | | |
|
||
|
*-*-*-*-*
|
||
|
| | | | |
|
||
|
* *-*-*-*
|
||
|
*/
|
||
|
|
||
|
DirectedGraph<VertexMarking, EdgeMarking> example1 = new DirectedGraph<>();
|
||
|
|
||
|
int size = 5;
|
||
|
MarkedVertex[][] vertices = new MarkedVertex[size][size];
|
||
|
|
||
|
// 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], 1);
|
||
|
}
|
||
|
if (row < size - 1) {
|
||
|
addBidirectionalEdge(example1, vertices[row][col], vertices[row + 1][col], 1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return example1;
|
||
|
}
|
||
|
|
||
|
private void addBidirectionalEdge(DirectedGraph<VertexMarking, EdgeMarking> graph, MarkedVertex from, MarkedVertex to, int weight) {
|
||
|
MarkedEdge forwardEdge = new MarkedEdge<>("edge" + from.getName() + "_" + to.getName(), from, to, null, weight);
|
||
|
MarkedEdge backwardEdge = new MarkedEdge<>("edge" + to.getName() + "_" + from.getName(), to, from, null, weight);
|
||
|
graph.addEdge(forwardEdge);
|
||
|
graph.addEdge(backwardEdge);
|
||
|
}
|
||
|
}
|