Stable Version Christian
This commit is contained in:
parent
dd4ad4e574
commit
ba548f6c1e
@ -11,7 +11,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
|||||||
|
|
||||||
public DirectedGraph() {
|
public DirectedGraph() {
|
||||||
super();
|
super();
|
||||||
} //TestSeans
|
}
|
||||||
|
|
||||||
|
|
||||||
public DirectedGraph(String s) {
|
public DirectedGraph(String s) {
|
||||||
@ -126,6 +126,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
|||||||
|
|
||||||
// Dijkstra-Algorithmus
|
// Dijkstra-Algorithmus
|
||||||
public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
public int getShortestPathDijkstra(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||||
|
|
||||||
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
|
// Erstellt Hashmap um Distanz von Startnoten zu jedem Knoten auf dem Graph zu tracken
|
||||||
// Erstellt Hashmap um zu tracken welche Knoten schon besucht wurden
|
// Erstellt Hashmap um zu tracken welche Knoten schon besucht wurden
|
||||||
// Initialisierung aller Distanzen auf UNENDLICH (= -1)
|
// Initialisierung aller Distanzen auf UNENDLICH (= -1)
|
||||||
@ -136,6 +137,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
|||||||
distance.put(i, -1);
|
distance.put(i, -1);
|
||||||
visited.put(i, false);
|
visited.put(i, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erstelle Schlange wo die nächsten Verbindungen drin sind
|
// Erstelle Schlange wo die nächsten Verbindungen drin sind
|
||||||
PriorityQueue<WrapperElement<T>> queue = new PriorityQueue<>(new WrapperComparator<T>());
|
PriorityQueue<WrapperElement<T>> queue = new PriorityQueue<>(new WrapperComparator<T>());
|
||||||
|
|
||||||
@ -144,16 +146,47 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
|||||||
distance.put(n1, 0);
|
distance.put(n1, 0);
|
||||||
queue.add(new WrapperElement<>(n1, 0));
|
queue.add(new WrapperElement<>(n1, 0));
|
||||||
|
|
||||||
|
// Variable, die Distanz zwischen aktuellem Knoten und Nachfolger speichert
|
||||||
|
int dist = 0;
|
||||||
|
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
||||||
WrapperElement<T> nextVertex = queue.poll();
|
WrapperElement<T> nextVertex = queue.poll();
|
||||||
|
|
||||||
|
// Knoten als besucht makieren
|
||||||
visited.put(nextVertex.getElement(), true);
|
visited.put(nextVertex.getElement(), true);
|
||||||
|
|
||||||
|
System.out.println("Visit " + nextVertex.getElement().getName());
|
||||||
|
|
||||||
|
// Gehe von diesem Knoten aus alle erreichbaren Knoten durch
|
||||||
for (MarkedVertex<T> i: this.getSuccessors(nextVertex.getElement())) {
|
for (MarkedVertex<T> i: this.getSuccessors(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
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,32 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class Display {
|
public class Display {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
DirectedGraph<VertexMarking, EdgeMarking> myGraph = new DirectedGraph<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
myGraph.addVertex(new MarkedVertex<>(String.valueOf(i), null));
|
||||||
|
}
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
for (MarkedVertex<VertexMarking> i: myGraph.getAllVertexes()) {
|
||||||
|
myGraph.addEdge(new MarkedEdge<>("a", i, myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size())), null, random.nextInt(1, 10)));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (MarkedVertex<VertexMarking> 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<VertexMarking> start = myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size()));
|
||||||
|
MarkedVertex<VertexMarking> end = myGraph.getAllVertexes().get(random.nextInt(myGraph.getAllVertexes().size()));
|
||||||
|
System.out.println(start.getName() + " to " + end.getName());
|
||||||
|
|
||||||
|
System.out.println(myGraph.getShortestPathDijkstra(start, end));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,16 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
|||||||
|
|
||||||
// Ausgabe
|
// Ausgabe
|
||||||
public String toString() {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
public class MarkedVertex<T extends VertexMarking> extends Vertex{
|
public class MarkedVertex<T extends VertexMarking> extends Vertex{
|
||||||
|
|
||||||
// ATTRIBUTE
|
// ATTRIBUTE
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
// Exception, das User nach einem ungültigen Knoten sucht
|
// Exception, das User nach einem ungültigen Knoten sucht
|
||||||
class NameDoesNotExistException extends Exception {
|
public class NameDoesNotExistException extends Exception {
|
||||||
public NameDoesNotExistException() {
|
public NameDoesNotExistException() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@ -50,6 +49,12 @@ class WrapperElement<T extends VertexMarking> {
|
|||||||
public int getPrio() {
|
public int getPrio() {
|
||||||
return this.prio;
|
return this.prio;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Ausgabe
|
||||||
|
public String toString() {
|
||||||
|
return "Wrapper with " + this.n1 + " with prio " + this.prio;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
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> {
|
||||||
@ -19,12 +17,6 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Ausgabe
|
|
||||||
public String toString() {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// KNOTEN EIGENSCHAFTEN
|
// KNOTEN EIGENSCHAFTEN
|
||||||
|
|
||||||
// Prüfung des Grades eines Knotens
|
// Prüfung des Grades eines Knotens
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package graph;
|
package graph;
|
||||||
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
public abstract class Vertex {
|
public abstract class Vertex {
|
||||||
|
|
||||||
// ATTRIBUTE
|
// ATTRIBUTE
|
||||||
|
Loading…
Reference in New Issue
Block a user