Weighting gefixt

This commit is contained in:
cmerkens 2024-07-07 16:29:45 +02:00
parent 6060796ddd
commit 5e4eec4e5c
7 changed files with 265 additions and 72 deletions

View File

@ -63,6 +63,7 @@ public class OurApplication {
drawArea.setCurrentGraph(myGraph.getScreenGraph()); drawArea.setCurrentGraph(myGraph.getScreenGraph());
algorithm.setCurrentGraph(myGraph); algorithm.setCurrentGraph(myGraph);
JFrame frame = new JFrame("Visualise"); JFrame frame = new JFrame("Visualise");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet); frame.getContentPane().add(applet);

View File

@ -261,7 +261,8 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) { if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) {
// Berechne Distanz zu nächstem Knoten // Berechne Distanz zu nächstem Knoten
dist = distance.get(nextVertex.getElement()) + j.getWeighting(); EdgeWeightMarking marking = (EdgeWeightMarking) j.getMarking();
dist = distance.get(nextVertex.getElement()) + marking.getWeight();
break; break;
} }
} }
@ -361,8 +362,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
+ Math.pow((i.getCords()[1] - n2.getCords()[1]), 2)); + Math.pow((i.getCords()[1] - n2.getCords()[1]), 2));
// Berechne Distanz zu nächstem Knoten // Berechne Distanz zu nächstem Knoten
dist = distance.get(nextVertex.getElement()) + j.getWeighting(); EdgeWeightMarking marking = (EdgeWeightMarking) j.getMarking();
distToFinish = distance.get(nextVertex.getElement()) + j.getWeighting() + airDist; dist = distance.get(nextVertex.getElement()) + marking.getWeight();
distToFinish = distance.get(nextVertex.getElement()) + marking.getWeight() + airDist;
break; break;
} }
} }
@ -393,11 +395,4 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
// Gibt Distanz zu gefragtem Knoten zurück // Gibt Distanz zu gefragtem Knoten zurück
return distance.get(n2); return distance.get(n2);
} }
// Methode für das Zurücksetzten der Knotenfarben
public void clearScreenGraphColor(){
for(visualizationElements.Vertex screenVertexes : this.getScreenGraph().getVertexes()){
screenVertexes.setColor(Color.BLACK);
}
}
} }

View File

@ -52,8 +52,8 @@ public class ExampleGraphs {
} }
private void addBidirectionalEdge(DirectedGraph<VertexMarking, EdgeMarking> graph, MarkedVertex<VertexMarking> from, MarkedVertex<VertexMarking> to, EdgeWeightMarking weight) { 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> forwardEdge = new MarkedEdge<>("edge" + from.getName() + "_" + to.getName(), from, to, weight);
MarkedEdge<EdgeMarking> backwardEdge = new MarkedEdge<>("edge" + to.getName() + "_" + from.getName(), to, from, weight, weight.getWeight()); MarkedEdge<EdgeMarking> backwardEdge = new MarkedEdge<>("edge" + to.getName() + "_" + from.getName(), to, from, weight);
graph.addEdge(forwardEdge); graph.addEdge(forwardEdge);
graph.addEdge(backwardEdge); graph.addEdge(backwardEdge);
} }
@ -88,14 +88,14 @@ public class ExampleGraphs {
example2.addVertex(F); example2.addVertex(F);
example2.addVertex(G); example2.addVertex(G);
example2.addEdge(new MarkedEdge<>("AB", A, B, new EdgeWeightMarking(1), 1)); example2.addEdge(new MarkedEdge<>("AB", A, B, new EdgeWeightMarking(1)));
example2.addEdge(new MarkedEdge<>("BC", B, C, new EdgeWeightMarking(1), 1)); example2.addEdge(new MarkedEdge<>("BC", B, C, new EdgeWeightMarking(1)));
example2.addEdge(new MarkedEdge<>("CD", C, D, new EdgeWeightMarking(10), 10)); example2.addEdge(new MarkedEdge<>("CD", C, D, new EdgeWeightMarking(10)));
example2.addEdge(new MarkedEdge<>("DE", D, E, new EdgeWeightMarking(1), 1)); example2.addEdge(new MarkedEdge<>("DE", D, E, new EdgeWeightMarking(1)));
example2.addEdge(new MarkedEdge<>("AF", A, F, new EdgeWeightMarking(5), 5)); example2.addEdge(new MarkedEdge<>("AF", A, F, new EdgeWeightMarking(5)));
example2.addEdge(new MarkedEdge<>("FG", F, G, new EdgeWeightMarking(5), 5)); example2.addEdge(new MarkedEdge<>("FG", F, G, new EdgeWeightMarking(5)));
example2.addEdge(new MarkedEdge<>("GE", G, E, new EdgeWeightMarking(5), 5)); example2.addEdge(new MarkedEdge<>("GE", G, E, new EdgeWeightMarking(5)));
return example2; return example2;
} }
@ -160,25 +160,25 @@ public class ExampleGraphs {
example3.addVertex(E3); example3.addVertex(E3);
example3.addVertex(E4); example3.addVertex(E4);
example3.addEdge(new MarkedEdge<>("AB1", A, B1, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("AB1", A, B1, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("AB2", A, B2, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("AB2", A, B2, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("AB3", A, B3, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("AB3", A, B3, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("AB4", A, B4, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("AB4", A, B4, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("B1C1", B1, C1, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("B1C1", B1, C1, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("B2C2", B2, C2, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("B2C2", B2, C2, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("B3C3", B3, C3, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("B3C3", B3, C3, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("B4C4", B4, C4, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("B4C4", B4, C4, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("C1D1", C1, D1, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("C1D1", C1, D1, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("C2D2", C2, D2, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("C2D2", C2, D2, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("C3D3", C3, D3, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("C3D3", C3, D3, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("C4D4", C4, D4, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("C4D4", C4, D4, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("D1E1", D1, E1, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("D1E1", D1, E1, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("D2E2", D2, E2, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("D2E2", D2, E2, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("D3E3", D3, E3, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("D3E3", D3, E3, new EdgeWeightMarking(1)));
example3.addEdge(new MarkedEdge<>("D4E4", D4, E4, new EdgeWeightMarking(1), 1)); example3.addEdge(new MarkedEdge<>("D4E4", D4, E4, new EdgeWeightMarking(1)));
return example3; return example3;
} }
@ -216,18 +216,18 @@ public class ExampleGraphs {
example4.addVertex(Dresden); example4.addVertex(Dresden);
example4.addVertex(Hannover); example4.addVertex(Hannover);
example4.addEdge(new MarkedEdge<>("Berlin-Hamburg", Berlin, Hamburg, new EdgeWeightMarking(1), 1)); example4.addEdge(new MarkedEdge<>("Berlin-Hamburg", Berlin, Hamburg, new EdgeWeightMarking(1)));
example4.addEdge(new MarkedEdge<>("Berlin-München", Berlin, München, new EdgeWeightMarking(2), 1)); example4.addEdge(new MarkedEdge<>("Berlin-München", Berlin, München, new EdgeWeightMarking(2)));
example4.addEdge(new MarkedEdge<>("Berlin-Köln", Berlin, Köln, new EdgeWeightMarking(3), 1)); example4.addEdge(new MarkedEdge<>("Berlin-Köln", Berlin, Köln, new EdgeWeightMarking(3)));
example4.addEdge(new MarkedEdge<>("Hamburg-Frankfurt", Hamburg, Frankfurt, new EdgeWeightMarking(2), 1)); example4.addEdge(new MarkedEdge<>("Hamburg-Frankfurt", Hamburg, Frankfurt, new EdgeWeightMarking(2)));
example4.addEdge(new MarkedEdge<>("Hamburg-Leipzig", Hamburg, Leipzig, new EdgeWeightMarking(3), 1)); example4.addEdge(new MarkedEdge<>("Hamburg-Leipzig", Hamburg, Leipzig, new EdgeWeightMarking(3)));
example4.addEdge(new MarkedEdge<>("München-Stuttgart", München, Stuttgart, new EdgeWeightMarking(1), 1)); example4.addEdge(new MarkedEdge<>("München-Stuttgart", München, Stuttgart, new EdgeWeightMarking(1)));
example4.addEdge(new MarkedEdge<>("München-Dresden", München, Dresden, new EdgeWeightMarking(2), 1)); example4.addEdge(new MarkedEdge<>("München-Dresden", München, Dresden, new EdgeWeightMarking(2)));
example4.addEdge(new MarkedEdge<>("Köln-Hannover", Köln, Hannover, new EdgeWeightMarking(3), 1)); example4.addEdge(new MarkedEdge<>("Köln-Hannover", Köln, Hannover, new EdgeWeightMarking(3)));
example4.addEdge(new MarkedEdge<>("Frankfurt-Stuttgart", Frankfurt, Stuttgart, new EdgeWeightMarking(1), 1)); example4.addEdge(new MarkedEdge<>("Frankfurt-Stuttgart", Frankfurt, Stuttgart, new EdgeWeightMarking(1)));
example4.addEdge(new MarkedEdge<>("Stuttgart-Leipzig", Stuttgart, Leipzig, new EdgeWeightMarking(2), 1)); example4.addEdge(new MarkedEdge<>("Stuttgart-Leipzig", Stuttgart, Leipzig, new EdgeWeightMarking(2)));
example4.addEdge(new MarkedEdge<>("Leipzig-Dresden", Leipzig, Dresden, new EdgeWeightMarking(1), 1)); example4.addEdge(new MarkedEdge<>("Leipzig-Dresden", Leipzig, Dresden, new EdgeWeightMarking(1)));
example4.addEdge(new MarkedEdge<>("Dresden-Hannover", Dresden, Hannover, new EdgeWeightMarking(3), 1)); example4.addEdge(new MarkedEdge<>("Dresden-Hannover", Dresden, Hannover, new EdgeWeightMarking(3)));
return example4; return example4;
} }

View File

@ -3,6 +3,7 @@ package graph;
import OurApplication.OurLogElement; import OurApplication.OurLogElement;
import logging.LogElementList; import logging.LogElementList;
import java.awt.*;
import java.util.*; import java.util.*;
public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> { public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
@ -265,6 +266,14 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
} }
// Methode für das Zurücksetzten der Knotenfarben
public void clearScreenGraphColor(){
for(visualizationElements.Vertex screenVertexes : this.getScreenGraph().getVertexes()){
screenVertexes.setColor(Color.BLACK);
}
}
public abstract int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2); public abstract int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2);
public abstract int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2); public abstract int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2);

View File

@ -7,8 +7,6 @@ public class MarkedEdge<U extends EdgeMarking> extends Edge{
// ATTRIBUTE // ATTRIBUTE
private U marking; private U marking;
// Für Aufgabe 2
private int weighting;
private visualizationElements.Edge screenEdge; private visualizationElements.Edge screenEdge;
@ -24,18 +22,11 @@ public class MarkedEdge<U extends EdgeMarking> extends 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;
this.screenEdge = new visualizationElements.Edge(n1.getScreenVertex(), n2.getScreenVertex()); EdgeWeightMarking m = (EdgeWeightMarking) this.marking;
this.screenEdge = new visualizationElements.Edge(n1.getScreenVertex(), n2.getScreenVertex(), Integer.toString(m.getWeight()), Color.BLACK);
} }
// TODO ACHTUNG DEBUG!!!!
public MarkedEdge(String s, Vertex n1, Vertex n2, U u, int weighting) {
super(s, n1, n2);
this.marking = u;
this.weighting = weighting;
this.screenEdge = new visualizationElements.Edge(n1.getScreenVertex(), n2.getScreenVertex(), Integer.toString(weighting), Color.BLACK);
}
// GET-ER // GET-ER
@ -44,12 +35,6 @@ public class MarkedEdge<U extends EdgeMarking> extends Edge{
} }
// Für Aufgabe 2
public int getWeighting() {
return this.weighting;
}
public visualizationElements.Edge getScreenEdge() { public visualizationElements.Edge getScreenEdge() {
return this.screenEdge; return this.screenEdge;
} }
@ -59,14 +44,11 @@ public class MarkedEdge<U extends EdgeMarking> extends Edge{
public void setMarking(U u) { public void setMarking(U u) {
this.marking = u; this.marking = u;
EdgeWeightMarking m = (EdgeWeightMarking) this.marking;
this.screenEdge.setMarking(Integer.toString(m.getWeight()));
} }
// Für Aufgabe 2
public void setWeighting(int w) {
this.weighting = w;
}
// Ausgabe // Ausgabe
public String toString() { public String toString() {

View File

@ -33,7 +33,6 @@ public class MarkedVertex<T extends VertexMarking> extends Vertex{
} }
// TODO ACHTUNG DEBUG!!!!
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;

View File

@ -6,7 +6,10 @@ import visualizationElements.Edge;
import visualizationElements.EdgeStyle; import visualizationElements.EdgeStyle;
import visualizationElements.Vertex; import visualizationElements.Vertex;
import java.awt.*;
import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Vector; import java.util.Vector;
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> {
@ -45,6 +48,24 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
} }
public visualizationElements.Graph getScreenGraphCopy() {
visualizationElements.Graph graphCopy = new visualizationElements.Graph(new Vector<Vertex>(), new Vector<Edge>(), false, EdgeStyle.Direct);
Vector<visualizationElements.Vertex> copiedVertexes = new Vector<>();
Vector<visualizationElements.Edge> copiedEdges = new Vector<>();
for (visualizationElements.Vertex vertexCopy : this.screenGraph.getVertexes()) {
visualizationElements.Vertex newCopiedVertex = new visualizationElements.Vertex(vertexCopy.getXpos(), vertexCopy.getYpos(), vertexCopy.getMarking(), vertexCopy.getColor());
copiedVertexes.add(newCopiedVertex);
graphCopy.setVertexes(copiedVertexes);
}
for (visualizationElements.Edge edgeCopy : this.screenGraph.getEdges()) {
visualizationElements.Edge newCopiedEdge = new visualizationElements.Edge(edgeCopy.getSource(), edgeCopy.getDestination(), edgeCopy.getMarking(), edgeCopy.getColor());
copiedEdges.add(newCopiedEdge);
graphCopy.setEdges(copiedEdges);
}
return graphCopy;
}
// HINZUFÜGEN // HINZUFÜGEN
// Kante hinzufügen // Kante hinzufügen
@ -119,10 +140,196 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) { public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return 1;
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
// Erstellt Hashmap um zu tracken welche Knoten schon besucht wurden
// Initialisierung aller Distanzen auf UNENDLICH (= -1)
// Initialisierung, dass kein Knoten besucht wurde
HashMap<MarkedVertex<T>, Integer> distance = new HashMap<>();
HashMap<MarkedVertex<T>, Boolean> visited = new HashMap<>();
for (MarkedVertex<T> i: this.getAllVertexes()) {
distance.put(i, -1);
visited.put(i, false);
}
// Erstelle Schlange wo die nächsten Verbindungen drin sind
PriorityQueue<WrapperElement<T>> queue = new PriorityQueue<>(new WrapperComparator<T>());
// Distanz zu Startknoten auf 0
// Weg zu Startknoten in die Schlange aufnehmen
distance.put(n1, 0);
queue.add(new WrapperElement<>(n1, 0));
// Variable, die Distanz zwischen aktuellem Knoten und Nachfolger speichert
int dist = 0;
// Zähler für LogList
int step = 0;
// String für den Description Inhalt
String textDescription;
// Färben der Start und Ziel Knoten für Visualisierung + hinzufügen zur LogList
n1.getScreenVertex().setColor(Color.RED);
n2.getScreenVertex().setColor(Color.RED);
textDescription = "Startknoten: " + n1.getScreenVertex().getMarking()
+ ", Endknoten: " + n2.getScreenVertex().getMarking();
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
while (!queue.isEmpty()) {
// Den nächsten Knoten, der am wenigsten kostet, besuchen
WrapperElement<T> nextVertex = queue.poll();
// Knoten als besucht makieren
visited.put(nextVertex.getElement(), true);
// Logging
textDescription = "Visit " + nextVertex.getElement().getName();
System.out.println(textDescription);
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Gehe von diesem Knoten aus alle erreichbaren Knoten durch
for (MarkedVertex<T> i: this.getNeighbours(nextVertex.getElement())) {
// Kante finde, die den jetzigen und nächsten Knoten verbindet
for (MarkedEdge<U> j: this.getAllEdges()) {
if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) {
// Berechne Distanz zu nächstem Knoten
EdgeWeightMarking marking = (EdgeWeightMarking) j.getMarking();
dist = distance.get(nextVertex.getElement()) + marking.getWeight();
break;
}
}
// Wenn es schon einen kürzeren Weg zum Knoten gibt, überspringen
if ((distance.get(i) <= dist && distance.get(i) != -1) || visited.get(i)) {
continue;
}
// Aktualisiere Distanz von Start zu nächstem Knoten
distance.put(i, dist);
// Logging
textDescription = "Add " + i.getName() + " with " + dist + " weight to queue.";
System.out.println(textDescription);
i.getScreenVertex().setColor(Color.YELLOW);
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Nehme nächsten Knoten in die Queue auf
queue.add(new WrapperElement<>(i, dist));
}
}
//zurücksetzten der Färbungen
this.clearScreenGraphColor();
System.out.println("Done");
// Gibt Distanz zu gefragtem Knoten zurück
return distance.get(n2);
} }
public int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) { public int getShortestPathAStar(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return 2;
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
// Erstellt Hashmap um zu tracken welche Knoten schon besucht wurden
// Initialisierung aller Distanzen auf UNENDLICH (= -1)
// Initialisierung, dass kein Knoten besucht wurde
HashMap<MarkedVertex<T>, Integer> distance = new HashMap<>();
HashMap<MarkedVertex<T>, Boolean> visited = new HashMap<>();
for (MarkedVertex<T> i: this.getAllVertexes()) {
distance.put(i, -1);
visited.put(i, false);
}
// Erstelle Schlange wo die nächsten Verbindungen drin sind
PriorityQueue<WrapperElement<T>> queue = new PriorityQueue<>(new WrapperComparator<T>());
// Distanz zu Startknoten auf 0
// Weg zu Startknoten in die Schlange aufnehmen
distance.put(n1, 0);
queue.add(new WrapperElement<>(n1, 0));
// Variable, die Distanz zwischen aktuellem Knoten und Nachfolger speichert
int dist = 0;
// Variable, die Distanz zwischen dem potenziell nächsten Knoten und dem Zielknoten speichert
int airDist = 0;
// Variable, die Distanz zwischen dem aktuellen Knoten bis zum Endknoten speichert
int distToFinish = 0;
// Zähler für LogList
int step = 0;
// String für den Description Inhalt
String textDescription;
// Färben der Start und Ziel Knoten für Visualisierung + hinzufügen zur LogList
n1.getScreenVertex().setColor(Color.RED);
n2.getScreenVertex().setColor(Color.RED);
textDescription = "Startknoten: " + n1.getScreenVertex().getMarking()
+ ", Endknoten: " + n2.getScreenVertex().getMarking();
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
while (!queue.isEmpty()) {
// Den nächsten Knoten, der am wenigsten kostet, besuchen
WrapperElement<T> nextVertex = queue.poll();
// Knoten als besucht makieren
visited.put(nextVertex.getElement(), true);
// Logging
textDescription = "Visit " + nextVertex.getElement().getName();
System.out.println(textDescription);
nextVertex.getElement().getScreenVertex().setColor(Color.BLUE);
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Gehe von diesem Knoten aus alle erreichbaren Knoten durch
for (MarkedVertex<T> i: this.getNeighbours(nextVertex.getElement())) {
// Kante finde, die den jetzigen und nächsten Knoten verbindet
for (MarkedEdge<U> j: this.getAllEdges()) {
if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) {
//Berechnung der Heuristik über die Luftdistanz des nächsten Knoten zum Zielknoten
airDist = (int) Math.sqrt(Math.pow((i.getCords()[0] - n2.getCords()[0]), 2)
+ Math.pow((i.getCords()[1] - n2.getCords()[1]), 2));
// Berechne Distanz zu nächstem Knoten
EdgeWeightMarking marking = (EdgeWeightMarking) j.getMarking();
dist = distance.get(nextVertex.getElement()) + marking.getWeight();
distToFinish = distance.get(nextVertex.getElement()) + marking.getWeight() + airDist;
break;
}
}
// Wenn es schon einen kürzeren Weg zum Knoten gibt, überspringen
if ((distance.get(i) <= dist && distance.get(i) != -1) || visited.get(i)) {
continue;
}
// Aktualisiere Distanz von Start zu nächstem Knoten
distance.put(i, dist);
// Logging
textDescription = "Add " + i.getName() + " with " + dist + " weight to queue.";
System.out.println(textDescription);
i.getScreenVertex().setColor(Color.YELLOW);
this.logList.add(new OurLogElement(step, textDescription, 0, this.getScreenGraphCopy()));
// Nehme nächsten Knoten in die Queue auf
queue.add(new WrapperElement<>(i, distToFinish));
}
}
//zurücksetzten der Färbungen
this.clearScreenGraphColor();
System.out.println("Done");
// Gibt Distanz zu gefragtem Knoten zurück
return distance.get(n2);
} }
} }