Compare commits
No commits in common. "dd4ad4e5749533315b6922c13d39f398a5716144" and "6684e14bf5bb2cebad11c70f423caa164d75d411" have entirely different histories.
dd4ad4e574
...
6684e14bf5
9
.idea/ProjektGraph.iml
generated
9
.idea/ProjektGraph.iml
generated
@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/misc.xml
generated
6
.idea/misc.xml
generated
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/ProjektGraph.iml" filepath="$PROJECT_DIR$/.idea/ProjektGraph.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
40
.idea/workspace.xml
generated
40
.idea/workspace.xml
generated
@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="70f8ea87-9ffc-471a-8059-ebbfc323adcc" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectColorInfo"><![CDATA[{
|
||||
"associatedIndex": 2
|
||||
}]]></component>
|
||||
<component name="ProjectId" id="2hxt4apXjGaBR1xypJAsjIrgc8R" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"git-widget-placeholder": "main",
|
||||
"kotlin-language-version-configured": "true",
|
||||
"last_opened_file_path": "C:/Git/ProjektGraph"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="70f8ea87-9ffc-471a-8059-ebbfc323adcc" name="Changes" comment="" />
|
||||
<created>1718547365327</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1718547365327</updated>
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
</project>
|
@ -1,8 +1,6 @@
|
||||
package graph;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Vector;
|
||||
|
||||
public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
|
||||
@ -25,7 +23,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
boolean n1ton2 = false;
|
||||
boolean n2ton1 = false;
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getSource() == n1 && i.getDestination() == n2) {
|
||||
n1ton2 = true;
|
||||
} else if (i.getSource() == n2 && i.getDestination() == n1) {
|
||||
@ -57,7 +55,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Prüfung des Eingangsgrades eines Knotens
|
||||
public int inDegree(MarkedVertex<T> n) {
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getDestination() == n) {
|
||||
degree += 1;
|
||||
}
|
||||
@ -79,7 +77,7 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Prüfung des Ausgangsgrades eines Knotens
|
||||
public int outDegree(MarkedVertex<T> n) {
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getSource() == n) {
|
||||
degree += 1;
|
||||
}
|
||||
@ -101,9 +99,9 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Prüfung, welche Knoten Vorgänger sind
|
||||
public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) {
|
||||
Vector<MarkedVertex<T>> predecessors = new Vector<>();
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getDestination() == n) {
|
||||
predecessors.add((MarkedVertex<T>) i.getSource());
|
||||
predecessors.add(i.getSource());
|
||||
}
|
||||
}
|
||||
return predecessors;
|
||||
@ -113,47 +111,12 @@ public class DirectedGraph<T extends VertexMarking, U extends EdgeMarking> exten
|
||||
// Prüfung, welche Knoten Nachfolger sind
|
||||
public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) {
|
||||
Vector<MarkedVertex<T>> successors = new Vector<>();
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getSource() == n) {
|
||||
successors.add((MarkedVertex<T>) i.getDestination());
|
||||
successors.add(i.getDestination());
|
||||
}
|
||||
}
|
||||
return successors;
|
||||
}
|
||||
|
||||
|
||||
// AUFGABE 2
|
||||
|
||||
// Dijkstra-Algorithmus
|
||||
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 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));
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
// Den nächsten Knoten, der am wenigsten kostet, besuchen
|
||||
WrapperElement<T> nextVertex = queue.poll();
|
||||
visited.put(nextVertex.getElement(), true);
|
||||
|
||||
for (MarkedVertex<T> i: this.getSuccessors(nextVertex.getElement())) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package graph;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
|
||||
public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
@ -8,7 +9,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
private String name;
|
||||
private Vector<MarkedVertex<T>> vertexes;
|
||||
private Vector<MarkedEdge<U>> edges;
|
||||
private Vector<MarkedEdge<U, T>> edges;
|
||||
|
||||
|
||||
// KONSTRUKTOREN
|
||||
@ -32,7 +33,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
}
|
||||
|
||||
|
||||
public Vector<MarkedEdge<U>> getAllEdges() {
|
||||
public Vector<MarkedEdge<U, T>> getAllEdges() {
|
||||
return this.edges;
|
||||
}
|
||||
|
||||
@ -58,7 +59,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
// HINZUFÜGEN
|
||||
|
||||
// Kante hinzufügen
|
||||
public void addEdge(MarkedEdge<U> e) {
|
||||
public void addEdge(MarkedEdge<U, T> e) {
|
||||
this.edges.add(e);
|
||||
}
|
||||
|
||||
@ -72,13 +73,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
// LÖSCHEN
|
||||
|
||||
// Kante löschen
|
||||
public void removeEdge(MarkedEdge<U> e) {
|
||||
public void removeEdge(MarkedEdge<U, T> e) {
|
||||
this.edges.remove(e);
|
||||
}
|
||||
|
||||
|
||||
public void removeEdge(String s) throws NameDoesNotExistException{
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
for (MarkedEdge<U, T> i: this.edges) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
removeEdge(i);
|
||||
return;
|
||||
@ -90,11 +91,6 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
// Knoten löschen
|
||||
public void removeVertex(MarkedVertex<T> n) {
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
if (i.getSource() == n || i.getDestination() == n) {
|
||||
this.removeEdge(i);
|
||||
}
|
||||
}
|
||||
this.vertexes.remove(n);
|
||||
}
|
||||
|
||||
@ -148,13 +144,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
|
||||
// Prüfung, ob Kante im Graph
|
||||
public boolean hasEdge(MarkedEdge<U> e) {
|
||||
public boolean hasEdge(MarkedEdge<U, T> e) {
|
||||
return this.edges.contains(e);
|
||||
}
|
||||
|
||||
|
||||
public boolean hasEdge(String s) {
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
for (MarkedEdge<U, T> i: this.edges) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
return true;
|
||||
}
|
||||
@ -168,7 +164,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
// Prüfung, ob Kante zwischen zwei Knoten
|
||||
// TODO schauen, ob es Aufgabe entspricht
|
||||
public boolean hasEdge(MarkedVertex<T> v1, MarkedVertex<T> v2) {
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
for (MarkedEdge<U, T> i: this.edges) {
|
||||
if (i.getSource() == v1 && i.getDestination() == v2) {
|
||||
return true;
|
||||
} else if (i.getSource() == v2 && i.getDestination() == v1) {
|
||||
@ -199,7 +195,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
// Prüfung, ob zwei Knoten adjazent sind
|
||||
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
for (MarkedEdge<U, T> i: this.edges) {
|
||||
if ((i.getSource() == n1 && i.getDestination() == n2) || (i.getSource() == n2 && i.getDestination() == n1)) {
|
||||
return true;
|
||||
}
|
||||
@ -228,7 +224,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
|
||||
// Prüfung, ob Knoten eine Schlinge besitzt
|
||||
public boolean hasLoop(MarkedVertex<T> n) {
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
for (MarkedEdge<U, T> i: this.edges) {
|
||||
if (i.getSource() == i.getDestination() && i.getSource() == n) {
|
||||
return true;
|
||||
}
|
||||
@ -245,5 +241,4 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
|
||||
}
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
package graph;
|
||||
|
||||
public class MarkedEdge<U extends EdgeMarking> extends Edge{
|
||||
public class MarkedEdge<U extends EdgeMarking, T extends VertexMarking> extends Edge{
|
||||
|
||||
// ATTRIBUTE
|
||||
|
||||
// private MarkedVertex<T> source;
|
||||
// private MarkedVertex<T> destination;
|
||||
private U marking;
|
||||
// Für Aufgabe 2
|
||||
private int weighting;
|
||||
|
||||
|
||||
// KONSTRUKTOREN
|
||||
@ -22,26 +22,23 @@ public class MarkedEdge<U extends EdgeMarking> extends Edge{
|
||||
}
|
||||
|
||||
|
||||
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<T> getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
|
||||
// GET-ER
|
||||
public MarkedVertex<T> 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) {
|
||||
@ -49,12 +46,6 @@ public class MarkedEdge<U extends EdgeMarking> extends Edge{
|
||||
}
|
||||
|
||||
|
||||
// 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();
|
||||
|
@ -1,7 +1,5 @@
|
||||
package graph;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public class MarkedVertex<T extends VertexMarking> extends Vertex{
|
||||
|
||||
// ATTRIBUTE
|
||||
|
@ -1,10 +1,6 @@
|
||||
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();
|
||||
}
|
||||
@ -21,42 +17,3 @@ class NameDoesNotExistException extends Exception {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Element in der PriorityQueue
|
||||
class WrapperElement<T extends VertexMarking> {
|
||||
|
||||
// ATTRIBUTE
|
||||
|
||||
private MarkedVertex<T> n1;
|
||||
private int prio;
|
||||
|
||||
|
||||
// KONSTRUKTOR
|
||||
|
||||
public WrapperElement(MarkedVertex<T> n1, int prio) {
|
||||
this.n1 = n1;
|
||||
this.prio = prio;
|
||||
}
|
||||
|
||||
|
||||
// GET-ER
|
||||
|
||||
public MarkedVertex<T> getElement() {
|
||||
return this.n1;
|
||||
}
|
||||
|
||||
|
||||
public int getPrio() {
|
||||
return this.prio;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 2 Elemente in der PriorityQueue Vergleichen
|
||||
class WrapperComparator<T extends VertexMarking> implements Comparator<WrapperElement<T>> {
|
||||
|
||||
public int compare(WrapperElement<T> element1, WrapperElement<T> element2) {
|
||||
return Integer.compare(element1.getPrio(), element2.getPrio());
|
||||
}
|
||||
}
|
||||
|
@ -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<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
|
||||
@ -30,7 +28,7 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
// Prüfung des Grades eines Knotens
|
||||
public int degree(MarkedVertex<T> n) {
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getSource() == n) {
|
||||
degree += 1;
|
||||
}
|
||||
@ -55,14 +53,13 @@ public class UndirectedGraph<T extends VertexMarking, U extends EdgeMarking> ext
|
||||
// Prüfung, welche Knoten Nachbarn sind
|
||||
public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) {
|
||||
Vector<MarkedVertex<T>> neighbours = new Vector<>();
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
for (MarkedEdge<U, T> i: this.getAllEdges()) {
|
||||
if (i.getSource() == n && !neighbours.contains(i.getDestination())) {
|
||||
neighbours.add((MarkedVertex<T>) i.getDestination());
|
||||
neighbours.add(i.getDestination());
|
||||
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
|
||||
neighbours.add((MarkedVertex<T>) i.getSource());
|
||||
neighbours.add(i.getSource());
|
||||
}
|
||||
}
|
||||
return neighbours;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package graph;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public abstract class Vertex {
|
||||
|
||||
// ATTRIBUTE
|
||||
@ -28,11 +26,9 @@ public abstract class Vertex {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// SET-ER
|
||||
|
||||
public void setName(String s) {
|
||||
this.name = s;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user