From ba548f6c1ee9ed438a9e3d5d1380b7ef5ac1d453 Mon Sep 17 00:00:00 2001 From: cmerkens Date: Wed, 26 Jun 2024 02:20:36 +0200 Subject: [PATCH] Stable Version Christian --- graph/DirectedGraph.java | 39 +++++++++++++++++++++++++--- graph/Display.java | 25 ++++++++++++++++++ graph/Graph.java | 11 +++++++- graph/MarkedVertex.java | 2 -- graph/NameDoesNotExistException.java | 9 +++++-- graph/UndirectedGraph.java | 8 ------ graph/Vertex.java | 2 -- 7 files changed, 78 insertions(+), 18 deletions(-) diff --git a/graph/DirectedGraph.java b/graph/DirectedGraph.java index a482ff4..9c0a6e0 100644 --- a/graph/DirectedGraph.java +++ b/graph/DirectedGraph.java @@ -11,7 +11,7 @@ public class DirectedGraph exten public DirectedGraph() { super(); - } //TestSeans + } public DirectedGraph(String s) { @@ -126,6 +126,7 @@ public class DirectedGraph exten // Dijkstra-Algorithmus public int getShortestPathDijkstra(MarkedVertex n1, MarkedVertex n2) { + // 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) @@ -136,6 +137,7 @@ public class DirectedGraph exten distance.put(i, -1); visited.put(i, false); } + // Erstelle Schlange wo die nächsten Verbindungen drin sind PriorityQueue> queue = new PriorityQueue<>(new WrapperComparator()); @@ -144,16 +146,47 @@ public class DirectedGraph exten distance.put(n1, 0); queue.add(new WrapperElement<>(n1, 0)); + // Variable, die Distanz zwischen aktuellem Knoten und Nachfolger speichert + int dist = 0; + while (!queue.isEmpty()) { // Den nächsten Knoten, der am wenigsten kostet, besuchen WrapperElement nextVertex = queue.poll(); + + // Knoten als besucht makieren visited.put(nextVertex.getElement(), true); + System.out.println("Visit " + nextVertex.getElement().getName()); + + // Gehe von diesem Knoten aus alle erreichbaren Knoten durch for (MarkedVertex i: this.getSuccessors(nextVertex.getElement())) { - + + // Kante finde, die den jetzigen und nächsten Knoten verbindet + for (MarkedEdge j: this.getAllEdges()) { + if (j.getSource() == nextVertex.getElement() && j.getDestination() == i) { + + // Berechne Distanz zu nächstem Knoten + dist = distance.get(nextVertex.getElement()) + j.getWeighting(); + 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); + + System.out.println("Add " + i.getName() + " with " + dist + " weight to queue."); + + // Nehme nächsten Knoten in die Queue auf + queue.add(new WrapperElement<>(i, dist)); } } - return 1; + // Gibt Distanz zu gefragtem Knoten zurück + return distance.get(n2); } } diff --git a/graph/Display.java b/graph/Display.java index c4a268f..93a3861 100644 --- a/graph/Display.java +++ b/graph/Display.java @@ -1,7 +1,32 @@ package graph; +import java.util.Random; + public class Display { public static void main(String[] args) { + DirectedGraph myGraph = new DirectedGraph<>(); + + for (int i = 0; i < 10; i++) { + myGraph.addVertex(new MarkedVertex<>(String.valueOf(i), null)); + } + Random random = new Random(); + + for (MarkedVertex i: myGraph.getAllVertexes()) { + myGraph.addEdge(new MarkedEdge<>("a", i, myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size())), null, random.nextInt(1, 10))); + } + + for (MarkedVertex i: myGraph.getAllVertexes()) { + myGraph.addEdge(new MarkedEdge<>("a", i, myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size())), null, random.nextInt(1, 10))); + } + + System.out.println(myGraph.toString()); + + MarkedVertex start = myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size())); + MarkedVertex end = myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size())); + System.out.println(start.getName() + " to " + end.getName()); + + System.out.println(myGraph.getShortestPathDijkstra(start, end)); + } } diff --git a/graph/Graph.java b/graph/Graph.java index fa5ee22..4e27005 100644 --- a/graph/Graph.java +++ b/graph/Graph.java @@ -51,7 +51,16 @@ public abstract class Graph { // Ausgabe public String toString() { - return ""; + String output = ""; + for (Vertex i: this.vertexes) { + output += i.toString(); + output += "\n"; + } + for (Edge i: this.edges) { + output += i.toString(); + output += "\n"; + } + return output; } diff --git a/graph/MarkedVertex.java b/graph/MarkedVertex.java index af8ffda..73081bb 100644 --- a/graph/MarkedVertex.java +++ b/graph/MarkedVertex.java @@ -1,7 +1,5 @@ package graph; -import java.util.Vector; - public class MarkedVertex extends Vertex{ // ATTRIBUTE diff --git a/graph/NameDoesNotExistException.java b/graph/NameDoesNotExistException.java index 2c2f010..65834fc 100644 --- a/graph/NameDoesNotExistException.java +++ b/graph/NameDoesNotExistException.java @@ -1,10 +1,9 @@ package graph; - import java.util.Comparator; // Exception, das User nach einem ungültigen Knoten sucht -class NameDoesNotExistException extends Exception { +public class NameDoesNotExistException extends Exception { public NameDoesNotExistException() { super(); } @@ -50,6 +49,12 @@ class WrapperElement { public int getPrio() { return this.prio; } + + + // Ausgabe + public String toString() { + return "Wrapper with " + this.n1 + " with prio " + this.prio; + } } diff --git a/graph/UndirectedGraph.java b/graph/UndirectedGraph.java index 685153b..d7874f1 100644 --- a/graph/UndirectedGraph.java +++ b/graph/UndirectedGraph.java @@ -1,8 +1,6 @@ package graph; -import java.util.HashMap; import java.util.Objects; -import java.util.PriorityQueue; import java.util.Vector; public class UndirectedGraph extends Graph { @@ -19,12 +17,6 @@ public class UndirectedGraph ext } - // Ausgabe - public String toString() { - return ""; - } - - // KNOTEN EIGENSCHAFTEN // Prüfung des Grades eines Knotens diff --git a/graph/Vertex.java b/graph/Vertex.java index 6b33661..cdf7654 100644 --- a/graph/Vertex.java +++ b/graph/Vertex.java @@ -1,7 +1,5 @@ package graph; -import java.util.Vector; - public abstract class Vertex { // ATTRIBUTE