graph neuster Stand

This commit is contained in:
cmerkens 2024-06-15 16:35:42 +02:00
parent 44a33b7720
commit 2da3f2b591
6 changed files with 150 additions and 36 deletions

View File

@ -1,5 +1,6 @@
package graph; package graph;
import java.util.Objects;
import java.util.Vector; import java.util.Vector;
public class DirectedGraph<T, U> extends Graph<T, U> { public class DirectedGraph<T, U> extends Graph<T, U> {
@ -14,33 +15,100 @@ public class DirectedGraph<T, U> extends Graph<T, U> {
} }
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) { public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return true; return true;
} }
public boolean areStrongAdjacent(String s1, String s2) { public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException{
return true; MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null;
for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s1)) {
n1 = i;
} else if (Objects.equals(i.getName(), s2)) {
n2 = i;
}
}
if (n1 == null || n2 == null) {
throw new NameDoesNotExistException("One of the Vertexes might not exist");
} else {
return areAdjacent(n1, n2);
}
}
public boolean areStrongAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
boolean n1ton2 = false;
boolean n2ton1 = false;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n1 && i.getDestination() == n2) {
n1ton2 = true;
} else if (i.getSource() == n2 && i.getDestination() == n1) {
n2ton1 = true;
}
}
return (n1ton2 && n2ton1);
}
public boolean areStrongAdjacent(String s1, String s2) throws NameDoesNotExistException {
MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null;
for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s1)) {
n1 = i;
} else if (Objects.equals(i.getName(), s2)) {
n2 = i;
}
}
if (n1 == null || n2 == null) {
throw new NameDoesNotExistException("One of the Vertexes might not exist");
} else {
return areStrongAdjacent(n1, n2);
}
} }
public int inDegree(MarkedVertex<T> n) { public int inDegree(MarkedVertex<T> n) {
return 1; int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getDestination() == n) {
degree += 1;
}
}
return degree;
} }
public int inDegree(String s) { public int inDegree(String s) throws NameDoesNotExistException{
return 1; for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) {
return inDegree(i);
}
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
} }
public int outDegree(MarkedVertex<T> n) { public int outDegree(MarkedVertex<T> n) {
return 1; int degree = 0;
for (MarkedEdge<U> i: this.getAllEdges()) {
if (i.getSource() == n) {
degree += 1;
}
}
return degree;
} }
public int outDegree(String s) { public int outDegree(String s) throws NameDoesNotExistException{
return 1; for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) {
return outDegree(i);
}
}
throw new NameDoesNotExistException("One of the Vertexes might not exist");
} }

View File

@ -7,7 +7,11 @@ public abstract class Edge {
private Vertex destination; private Vertex destination;
public Edge() {} public Edge() {
this.name = "";
this.source = null;
this.destination = null;
}
public Edge(String s, Vertex n1, Vertex n2) { public Edge(String s, Vertex n1, Vertex n2) {

View File

@ -3,7 +3,7 @@ package graph;
import java.util.Objects; import java.util.Objects;
import java.util.Vector; import java.util.Vector;
public class Graph<T, U> { public abstract class Graph<T, U> {
private String name; private String name;
private Vector<MarkedVertex<T>> vertexes; private Vector<MarkedVertex<T>> vertexes;
@ -41,33 +41,35 @@ public class Graph<T, U> {
} }
public boolean removeEdge(MarkedEdge<U> e) { public void removeEdge(MarkedEdge<U> e) {
return this.edges.remove(e); this.edges.remove(e);
} }
public boolean removeEdge(String s) { public void removeEdge(String s) throws NameDoesNotExistException{
for (MarkedEdge<U> i: this.edges) { for (MarkedEdge<U> i: this.edges) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
return removeEdge(i); removeEdge(i);
return;
} }
} }
return false; throw new NameDoesNotExistException("One of the Edges might not exist");
} }
public boolean removeVertex(MarkedVertex<T> n) { public void removeVertex(MarkedVertex<T> n) {
return this.vertexes.remove(n); this.vertexes.remove(n);
} }
public boolean removeVertex(String s) { public void removeVertex(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.vertexes) { for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
return removeVertex(i); removeVertex(i);
return;
} }
} }
return false; throw new NameDoesNotExistException("One of the Vertexes might not exist");
} }
@ -121,13 +123,26 @@ public class Graph<T, U> {
} }
public boolean hasEdge(String s1, String s2) { public boolean hasEdge(MarkedEdge<U> e) {
return true; return this.edges.contains(e);
} }
public boolean hasEdge(MarkedEdge<U> e) { public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException {
return this.edges.contains(e); MarkedVertex<T> n1 = null;
MarkedVertex<T> n2 = null;
for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s1)) {
n1 = i;
} else if (Objects.equals(i.getName(), s2)) {
n2 = i;
}
}
if (n1 == null || n2 == null) {
throw new NameDoesNotExistException("One of the Vertexes might not exist");
} else {
return hasEdge(n1, n2);
}
} }
@ -149,14 +164,10 @@ public class Graph<T, U> {
} }
public abstract boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) { public abstract boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2);
return true;
}
public abstract boolean areAdjacent(String s1, String s2) { public abstract boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException;
return true;
}
public boolean hasLoop(MarkedVertex<T> n) { public boolean hasLoop(MarkedVertex<T> n) {
@ -169,12 +180,12 @@ public class Graph<T, U> {
} }
public boolean hasLoop(String s) { public boolean hasLoop(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.vertexes) { for (MarkedVertex<T> i: this.vertexes) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
return hasLoop(i); return hasLoop(i);
} }
} }
return false; throw new NameDoesNotExistException("One of the Vertexes might not exist");
} }
} }

View File

@ -0,0 +1,19 @@
package graph;
public class NameDoesNotExistException extends Exception {
public NameDoesNotExistException() {
super();
}
public NameDoesNotExistException(String message) {
super(message);
}
public NameDoesNotExistException(String message, Throwable cause) {
super(message, cause);
}
public NameDoesNotExistException(Throwable cause) {
super(cause);
}
}

View File

@ -15,6 +15,16 @@ public class UndirectedGraph<T, U> extends Graph<T, U> {
} }
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
return true;
}
public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException {
return true;
}
public String toString() { public String toString() {
return ""; return "";
} }
@ -34,13 +44,13 @@ public class UndirectedGraph<T, U> extends Graph<T, U> {
} }
public int degree(String s) { public int degree(String s) throws NameDoesNotExistException{
for (MarkedVertex<T> i: this.getAllVertexes()) { for (MarkedVertex<T> i: this.getAllVertexes()) {
if (Objects.equals(i.getName(), s)) { if (Objects.equals(i.getName(), s)) {
return degree(i); return degree(i);
} }
} }
return 0; throw new NameDoesNotExistException("One of the Vertexes might not exist");
} }

View File

@ -4,7 +4,9 @@ public abstract class Vertex {
private String name; private String name;
public Vertex() {} public Vertex() {
this.name = "";
}
public Vertex(String s) { public Vertex(String s) {