68 lines
2.3 KiB
Java
68 lines
2.3 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<VertexMarking>[][] 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], new EdgeWeightMarking(1));
|
|
}
|
|
if (row < size - 1) {
|
|
addBidirectionalEdge(example1, vertices[row][col], vertices[row + 1][col], new EdgeWeightMarking(1));
|
|
}
|
|
}
|
|
}
|
|
|
|
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());
|
|
graph.addEdge(forwardEdge);
|
|
graph.addEdge(backwardEdge);
|
|
}
|
|
}
|