diff --git a/.idea/ProjektGraph.iml b/.idea/ProjektGraph.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/.idea/ProjektGraph.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..639900d
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..cf75027
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..3c30cd6
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1718547365327
+
+
+ 1718547365327
+
+
+
+
\ No newline at end of file
diff --git a/graph/DirectedGraph.java b/graph/DirectedGraph.java
index 8dffaf9..2dfa28b 100644
--- a/graph/DirectedGraph.java
+++ b/graph/DirectedGraph.java
@@ -1,6 +1,8 @@
package graph;
+import java.util.HashMap;
import java.util.Objects;
+import java.util.PriorityQueue;
import java.util.Vector;
public class DirectedGraph extends Graph {
@@ -23,7 +25,7 @@ public class DirectedGraph exten
public boolean areStrongAdjacent(MarkedVertex n1, MarkedVertex n2) {
boolean n1ton2 = false;
boolean n2ton1 = false;
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getSource() == n1 && i.getDestination() == n2) {
n1ton2 = true;
} else if (i.getSource() == n2 && i.getDestination() == n1) {
@@ -55,7 +57,7 @@ public class DirectedGraph exten
// Prüfung des Eingangsgrades eines Knotens
public int inDegree(MarkedVertex n) {
int degree = 0;
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getDestination() == n) {
degree += 1;
}
@@ -77,7 +79,7 @@ public class DirectedGraph exten
// Prüfung des Ausgangsgrades eines Knotens
public int outDegree(MarkedVertex n) {
int degree = 0;
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
@@ -99,9 +101,9 @@ public class DirectedGraph exten
// Prüfung, welche Knoten Vorgänger sind
public Vector> getPredecessors(MarkedVertex n) {
Vector> predecessors = new Vector<>();
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getDestination() == n) {
- predecessors.add(i.getSource());
+ predecessors.add((MarkedVertex) i.getSource());
}
}
return predecessors;
@@ -111,12 +113,47 @@ public class DirectedGraph exten
// Prüfung, welche Knoten Nachfolger sind
public Vector> getSuccessors(MarkedVertex n) {
Vector> successors = new Vector<>();
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getSource() == n) {
- successors.add(i.getDestination());
+ successors.add((MarkedVertex) i.getDestination());
}
}
return successors;
}
+
+ // AUFGABE 2
+
+ // 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)
+ // Initialisierung, dass kein Knoten besucht wurde
+ HashMap, Integer> distance = new HashMap<>();
+ HashMap, Boolean> visited = new HashMap<>();
+ for (MarkedVertex i: this.getAllVertexes()) {
+ distance.put(i, -1);
+ visited.put(i, false);
+ }
+ // Erstelle Schlange wo die nächsten Verbindungen drin sind
+ PriorityQueue> queue = new PriorityQueue<>(new WrapperComparator());
+
+ // Distanz zu Startknoten auf 0
+ // Weg zu Startknoten in die Schlange aufnehmen
+ distance.put(n1, 0);
+ queue.add(new WrapperElement<>(n1, 0));
+
+ while (!queue.isEmpty()) {
+ // Den nächsten Knoten, der am wenigsten kostet, besuchen
+ WrapperElement nextVertex = queue.poll();
+ visited.put(nextVertex.getElement(), true);
+
+ for (MarkedVertex i: this.getSuccessors(nextVertex.getElement())) {
+
+ }
+ }
+
+ return 1;
+ }
}
diff --git a/graph/Graph.java b/graph/Graph.java
index 92257aa..fa5ee22 100644
--- a/graph/Graph.java
+++ b/graph/Graph.java
@@ -1,7 +1,6 @@
package graph;
-import java.util.Objects;
-import java.util.Vector;
+import java.util.*;
public abstract class Graph {
@@ -9,7 +8,7 @@ public abstract class Graph {
private String name;
private Vector> vertexes;
- private Vector> edges;
+ private Vector> edges;
// KONSTRUKTOREN
@@ -33,7 +32,7 @@ public abstract class Graph {
}
- public Vector> getAllEdges() {
+ public Vector> getAllEdges() {
return this.edges;
}
@@ -59,7 +58,7 @@ public abstract class Graph {
// HINZUFÜGEN
// Kante hinzufügen
- public void addEdge(MarkedEdge e) {
+ public void addEdge(MarkedEdge e) {
this.edges.add(e);
}
@@ -73,13 +72,13 @@ public abstract class Graph {
// LÖSCHEN
// Kante löschen
- public void removeEdge(MarkedEdge e) {
+ public void removeEdge(MarkedEdge e) {
this.edges.remove(e);
}
public void removeEdge(String s) throws NameDoesNotExistException{
- for (MarkedEdge i: this.edges) {
+ for (MarkedEdge i: this.edges) {
if (Objects.equals(i.getName(), s)) {
removeEdge(i);
return;
@@ -91,6 +90,11 @@ public abstract class Graph {
// Knoten löschen
public void removeVertex(MarkedVertex n) {
+ for (MarkedEdge i: this.edges) {
+ if (i.getSource() == n || i.getDestination() == n) {
+ this.removeEdge(i);
+ }
+ }
this.vertexes.remove(n);
}
@@ -144,13 +148,13 @@ public abstract class Graph {
// Prüfung, ob Kante im Graph
- public boolean hasEdge(MarkedEdge e) {
+ public boolean hasEdge(MarkedEdge e) {
return this.edges.contains(e);
}
public boolean hasEdge(String s) {
- for (MarkedEdge i: this.edges) {
+ for (MarkedEdge i: this.edges) {
if (Objects.equals(i.getName(), s)) {
return true;
}
@@ -164,7 +168,7 @@ public abstract class Graph {
// Prüfung, ob Kante zwischen zwei Knoten
// TODO schauen, ob es Aufgabe entspricht
public boolean hasEdge(MarkedVertex v1, MarkedVertex v2) {
- for (MarkedEdge i: this.edges) {
+ for (MarkedEdge i: this.edges) {
if (i.getSource() == v1 && i.getDestination() == v2) {
return true;
} else if (i.getSource() == v2 && i.getDestination() == v1) {
@@ -195,7 +199,7 @@ public abstract class Graph {
// Prüfung, ob zwei Knoten adjazent sind
public boolean areAdjacent(MarkedVertex n1, MarkedVertex n2) {
- for (MarkedEdge i: this.edges) {
+ for (MarkedEdge i: this.edges) {
if ((i.getSource() == n1 && i.getDestination() == n2) || (i.getSource() == n2 && i.getDestination() == n1)) {
return true;
}
@@ -224,7 +228,7 @@ public abstract class Graph {
// Prüfung, ob Knoten eine Schlinge besitzt
public boolean hasLoop(MarkedVertex n) {
- for (MarkedEdge i: this.edges) {
+ for (MarkedEdge i: this.edges) {
if (i.getSource() == i.getDestination() && i.getSource() == n) {
return true;
}
@@ -241,4 +245,5 @@ public abstract class Graph {
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
}
+
}
diff --git a/graph/MarkedEdge.java b/graph/MarkedEdge.java
index a55d169..dfe3795 100644
--- a/graph/MarkedEdge.java
+++ b/graph/MarkedEdge.java
@@ -1,12 +1,12 @@
package graph;
-public class MarkedEdge extends Edge{
+public class MarkedEdge extends Edge{
// ATTRIBUTE
- // private MarkedVertex source;
- // private MarkedVertex destination;
private U marking;
+ // Für Aufgabe 2
+ private int weighting;
// KONSTRUKTOREN
@@ -22,23 +22,26 @@ public class MarkedEdge extends
}
+ public MarkedEdge(String s, Vertex n1, Vertex n2, U u, int w) {
+ super(s, n1, n2);
+ this.marking = u;
+ this.weighting = w;
+ }
+
+
// GET-ER
- public MarkedVertex getSource() {
- return this.source;
- }
-
-
- public MarkedVertex getDestination() {
- return this.destination;
- }
-
-
public U getMarking() {
return this.marking;
}
+ // Für Aufgabe 2
+ public int getWeighting() {
+ return this.weighting;
+ }
+
+
// SET-ER
public void setMarking(U u) {
@@ -46,6 +49,12 @@ public class MarkedEdge extends
}
+ // Für Aufgabe 2
+ public void setWeighting(int w) {
+ this.weighting = w;
+ }
+
+
// Ausgabe
public String toString() {
return "MarkedEdge " + this.getName() + " from " + this.getSource().getName() + " to " + this.getDestination().getName();
diff --git a/graph/MarkedVertex.java b/graph/MarkedVertex.java
index 73081bb..af8ffda 100644
--- a/graph/MarkedVertex.java
+++ b/graph/MarkedVertex.java
@@ -1,5 +1,7 @@
package graph;
+import java.util.Vector;
+
public class MarkedVertex extends Vertex{
// ATTRIBUTE
diff --git a/graph/NameDoesNotExistException.java b/graph/NameDoesNotExistException.java
index 984f705..2c2f010 100644
--- a/graph/NameDoesNotExistException.java
+++ b/graph/NameDoesNotExistException.java
@@ -1,6 +1,10 @@
package graph;
-public class NameDoesNotExistException extends Exception {
+
+import java.util.Comparator;
+
+// Exception, das User nach einem ungültigen Knoten sucht
+class NameDoesNotExistException extends Exception {
public NameDoesNotExistException() {
super();
}
@@ -17,3 +21,42 @@ public class NameDoesNotExistException extends Exception {
super(cause);
}
}
+
+
+// Element in der PriorityQueue
+class WrapperElement {
+
+ // ATTRIBUTE
+
+ private MarkedVertex n1;
+ private int prio;
+
+
+ // KONSTRUKTOR
+
+ public WrapperElement(MarkedVertex n1, int prio) {
+ this.n1 = n1;
+ this.prio = prio;
+ }
+
+
+ // GET-ER
+
+ public MarkedVertex getElement() {
+ return this.n1;
+ }
+
+
+ public int getPrio() {
+ return this.prio;
+ }
+}
+
+
+// 2 Elemente in der PriorityQueue Vergleichen
+class WrapperComparator implements Comparator> {
+
+ public int compare(WrapperElement element1, WrapperElement element2) {
+ return Integer.compare(element1.getPrio(), element2.getPrio());
+ }
+}
diff --git a/graph/UndirectedGraph.java b/graph/UndirectedGraph.java
index d7328fc..685153b 100644
--- a/graph/UndirectedGraph.java
+++ b/graph/UndirectedGraph.java
@@ -1,6 +1,8 @@
package graph;
+import java.util.HashMap;
import java.util.Objects;
+import java.util.PriorityQueue;
import java.util.Vector;
public class UndirectedGraph extends Graph {
@@ -28,7 +30,7 @@ public class UndirectedGraph ext
// Prüfung des Grades eines Knotens
public int degree(MarkedVertex n) {
int degree = 0;
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
@@ -53,13 +55,14 @@ public class UndirectedGraph ext
// Prüfung, welche Knoten Nachbarn sind
public Vector> getNeighbours(MarkedVertex n) {
Vector> neighbours = new Vector<>();
- for (MarkedEdge i: this.getAllEdges()) {
+ for (MarkedEdge i: this.getAllEdges()) {
if (i.getSource() == n && !neighbours.contains(i.getDestination())) {
- neighbours.add(i.getDestination());
+ neighbours.add((MarkedVertex) i.getDestination());
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
- neighbours.add(i.getSource());
+ neighbours.add((MarkedVertex) i.getSource());
}
}
return neighbours;
}
+
}
diff --git a/graph/Vertex.java b/graph/Vertex.java
index 4ef4906..6b33661 100644
--- a/graph/Vertex.java
+++ b/graph/Vertex.java
@@ -1,5 +1,7 @@
package graph;
+import java.util.Vector;
+
public abstract class Vertex {
// ATTRIBUTE
@@ -26,9 +28,11 @@ public abstract class Vertex {
}
+
// SET-ER
public void setName(String s) {
this.name = s;
}
+
}