Compare commits

...

2 Commits

Author SHA1 Message Date
cmerkens
dd4ad4e574 Merge branch 'main' of https://gitea.hb.dhbw-stuttgart.de/i23024/ProjektGraph 2024-06-25 17:25:40 +02:00
cmerkens
7982917710 ... 2024-06-25 17:18:34 +02:00
12 changed files with 209 additions and 37 deletions

9
.idea/ProjektGraph.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?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 Normal file
View File

@ -0,0 +1,6 @@
<?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 Normal file
View File

@ -0,0 +1,8 @@
<?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 Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

40
.idea/workspace.xml generated Normal file
View File

@ -0,0 +1,40 @@
<?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>

View File

@ -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<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
@ -23,7 +25,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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> 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<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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getDestination() == n) {
degree += 1;
}
@ -77,7 +79,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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
@ -99,9 +101,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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getDestination() == n) {
predecessors.add(i.getSource());
predecessors.add((MarkedVertex<T>) i.getSource());
}
}
return predecessors;
@ -111,12 +113,47 @@ 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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n) {
successors.add(i.getDestination());
successors.add((MarkedVertex<T>) 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;
}
}

View File

@ -1,7 +1,6 @@
package graph;
import java.util.Objects;
import java.util.Vector;
import java.util.*;
public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
@ -9,7 +8,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
private String name;
private Vector<MarkedVertex<T>> vertexes;
private Vector<MarkedEdge<U, T>> edges;
private Vector<MarkedEdge<U>> edges;
// KONSTRUKTOREN
@ -33,7 +32,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
}
public Vector<MarkedEdge<U, T>> getAllEdges() {
public Vector<MarkedEdge<U>> getAllEdges() {
return this.edges;
}
@ -59,7 +58,7 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// HINZUFÜGEN
// Kante hinzufügen
public void addEdge(MarkedEdge<U, T> e) {
public void addEdge(MarkedEdge<U> e) {
this.edges.add(e);
}
@ -73,13 +72,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// LÖSCHEN
// Kante löschen
public void removeEdge(MarkedEdge<U, T> e) {
public void removeEdge(MarkedEdge<U> e) {
this.edges.remove(e);
}
public void removeEdge(String s) throws NameDoesNotExistException{
for (MarkedEdge<U, T> i: this.edges) {
for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) {
removeEdge(i);
return;
@ -91,6 +90,11 @@ 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);
}
@ -144,13 +148,13 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
// Prüfung, ob Kante im Graph
public boolean hasEdge(MarkedEdge<U, T> e) {
public boolean hasEdge(MarkedEdge<U> e) {
return this.edges.contains(e);
}
public boolean hasEdge(String s) {
for (MarkedEdge<U, T> i: this.edges) {
for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) {
return true;
}
@ -164,7 +168,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, T> i: this.edges) {
for (MarkedEdge<U> 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<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, T> i: this.edges) {
for (MarkedEdge<U> 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<T extends VertexMarking, U extends EdgeMarking> {
// Prüfung, ob Knoten eine Schlinge besitzt
public boolean hasLoop(MarkedVertex<T> n) {
for (MarkedEdge<U, T> i: this.edges) {
for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == i.getDestination() && i.getSource() == n) {
return true;
}
@ -241,4 +245,5 @@ public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
}
}

View File

@ -1,12 +1,12 @@
package graph;
public class MarkedEdge<U extends EdgeMarking, T extends VertexMarking> extends Edge{
public class MarkedEdge<U extends EdgeMarking> extends Edge{
// ATTRIBUTE
// private MarkedVertex<T> source;
// private MarkedVertex<T> destination;
private U marking;
// Für Aufgabe 2
private int weighting;
// KONSTRUKTOREN
@ -22,23 +22,26 @@ public class MarkedEdge<U extends EdgeMarking, T extends VertexMarking> 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<T> getSource() {
return this.source;
}
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) {
@ -46,6 +49,12 @@ public class MarkedEdge<U extends EdgeMarking, T extends VertexMarking> 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();

View File

@ -1,5 +1,7 @@
package graph;
import java.util.Vector;
public class MarkedVertex<T extends VertexMarking> extends Vertex{
// ATTRIBUTE

View File

@ -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<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());
}
}

View File

@ -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<T extends VertexMarking, U extends EdgeMarking> extends Graph<T, U> {
@ -28,7 +30,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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
@ -53,13 +55,14 @@ 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, T> i: this.getAllEdges()) {
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n && !neighbours.contains(i.getDestination())) {
neighbours.add(i.getDestination());
neighbours.add((MarkedVertex<T>) i.getDestination());
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
neighbours.add(i.getSource());
neighbours.add((MarkedVertex<T>) i.getSource());
}
}
return neighbours;
}
}

View File

@ -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;
}
}