Graphenteorie alle Methoden grundlegend implementiert

This commit is contained in:
cmerkens 2024-06-11 23:48:59 +02:00
parent eadea1ea5f
commit 44a33b7720
10 changed files with 441 additions and 0 deletions

56
graph/DirectedGraph.java Normal file
View File

@ -0,0 +1,56 @@
package graph;
import java.util.Vector;
public class DirectedGraph<T, U> extends Graph<T, U> {
public DirectedGraph() {
super();
}
public DirectedGraph(String s) {
super(s);
}
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return true;
}
public boolean areStrongAdjacent(String s1, String s2) {
return true;
}
public int inDegree(MarkedVertex<T> n) {
return 1;
}
public int inDegree(String s) {
return 1;
}
public int outDegree(MarkedVertex<T> n) {
return 1;
}
public int outDegree(String s) {
return 1;
}
public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) {
return null;
}
public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) {
return null;
}
}

48
graph/Edge.java Normal file
View File

@ -0,0 +1,48 @@
package graph;
public abstract class Edge {
private String name;
private Vertex source;
private Vertex destination;
public Edge() {}
public Edge(String s, Vertex n1, Vertex n2) {
this.name = s;
this.source = n1;
this.destination = n2;
}
public String getName() {
return this.name;
}
public Vertex getSource() {
return this.source;
}
public Vertex getDestination() {
return this.destination;
}
public void setName(String s) {
this.name = s;
}
public void setSource(Vertex n) {
this.source = n;
}
public void setDestination(Vertex n) {
this.destination = n;
}
}

4
graph/EdgeMarking.java Normal file
View File

@ -0,0 +1,4 @@
package graph;
public abstract class EdgeMarking extends Marking{
}

180
graph/Graph.java Normal file
View File

@ -0,0 +1,180 @@
package graph;
import java.util.Objects;
import java.util.Vector;
public class Graph<T, U> {
private String name;
private Vector<MarkedVertex<T>> vertexes;
private Vector<MarkedEdge<U>> edges;
public Graph() {
this.edges = new Vector<>();
this.vertexes = new Vector<>();
}
public Graph(String s) {
this();
this.name = s;
}
public void setName(String s) {
this.name = s;
}
public String toString() {
return "";
}
public void addEdge(MarkedEdge<U> e) {
this.edges.add(e);
}
public void addVertex(MarkedVertex<T> n) {
this.vertexes.add(n);
}
public boolean removeEdge(MarkedEdge<U> e) {
return this.edges.remove(e);
}
public boolean removeEdge(String s) {
for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) {
return removeEdge(i);
}
}
return false;
}
public boolean removeVertex(MarkedVertex<T> n) {
return this.vertexes.remove(n);
}
public boolean removeVertex(String s) {
for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) {
return removeVertex(i);
}
}
return false;
}
public String getName() {
return this.name;
}
public Vector<MarkedEdge<U>> getAllEdges() {
return this.edges;
}
public Vector<MarkedVertex<T>> getAllVertexes() {
return this.vertexes;
}
public int numberOfEdges() {
return this.edges.size();
}
public int numberOfVertexes() {
return this.vertexes.size();
}
public boolean hasVertex(String s) {
for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) {
return true;
}
}
return false;
}
public boolean hasVertex(MarkedVertex<T> n) {
return this.vertexes.contains(n);
}
public boolean hasEdge(String s) {
for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) {
return true;
}
}
return false;
}
public boolean hasEdge(String s1, String s2) {
return true;
}
public boolean hasEdge(MarkedEdge<U> e) {
return this.edges.contains(e);
}
// TODO schauen, ob es Aufgabe entspricht
public boolean hasEdge(MarkedVertex<T> v1, MarkedVertex<T> v2) {
for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == v1 && i.getDestination() == v2) {
return true;
} else if (i.getSource() == v2 && i.getDestination() == v1) {
return true;
}
}
return false;
}
public int degree() {
return 1;
}
public abstract boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return true;
}
public abstract boolean areAdjacent(String s1, String s2) {
return true;
}
public boolean hasLoop(MarkedVertex<T> n) {
for (MarkedEdge<U> i: this.edges) {
if (i.getSource() == i.getDestination() && i.getSource() == n) {
return true;
}
}
return false;
}
public boolean hasLoop(String s) {
for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) {
return hasLoop(i);
}
}
return false;
}
}

32
graph/MarkedEdge.java Normal file
View File

@ -0,0 +1,32 @@
package graph;
public class MarkedEdge<U> extends Edge{
private U marking;
public MarkedEdge() {
super();
}
public MarkedEdge(String s, Vertex n1, Vertex n2, U u) {
super(s, n1, n2);
this.marking = u;
}
public U getMarking() {
return this.marking;
}
public void setMarking(U u) {
this.marking = u;
}
public String toString() {
return "";
}
}

32
graph/MarkedVertex.java Normal file
View File

@ -0,0 +1,32 @@
package graph;
public class MarkedVertex<T> extends Vertex{
private T marking;
public MarkedVertex() {
super();
}
public MarkedVertex(String s, T t) {
super(s);
this.marking = t;
}
public T getMarking() {
return this.marking;
}
public void setMarking(T t) {
this.marking = t;
}
public String toString() {
return "";
}
}

4
graph/Marking.java Normal file
View File

@ -0,0 +1,4 @@
package graph;
public abstract class Marking {
}

View File

@ -0,0 +1,58 @@
package graph;
import java.util.Objects;
import java.util.Vector;
public class UndirectedGraph<T, U> extends Graph<T, U> {
public UndirectedGraph() {
super();
}
public UndirectedGraph(String s) {
super(s);
}
public String toString() {
return "";
}
public int degree(MarkedVertex<T> n) {
int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
if (i.getDestination() == n) {
degree += 1;
}
}
return degree;
}
public int degree(String s) {
for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) {
return degree(i);
}
}
return 0;
}
public Vector<MarkedVertex<T>> getNeighbours(MarkedVertex<T> n) {
Vector<MarkedVertex<T>> neighbours = new Vector<>();
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n && !neighbours.contains(i.getDestination())) {
neighbours.add(i.getDestination());
} else if (i.getDestination() == n && !neighbours.contains(i.getSource())) {
neighbours.add(i.getSource());
}
}
return neighbours;
}
}

23
graph/Vertex.java Normal file
View File

@ -0,0 +1,23 @@
package graph;
public abstract class Vertex {
private String name;
public Vertex() {}
public Vertex(String s) {
this.name = s;
}
public String getName() {
return this.name;
}
public void setName(String s) {
this.name = s;
}
}

4
graph/VertexMarking.java Normal file
View File

@ -0,0 +1,4 @@
package graph;
public abstract class VertexMarking extends Marking {
}