From 2da3f2b59131ac2953c92df64f5aae7cf711b69e Mon Sep 17 00:00:00 2001 From: cmerkens Date: Sat, 15 Jun 2024 16:35:42 +0200 Subject: [PATCH] graph neuster Stand --- graph/DirectedGraph.java | 86 +++++++++++++++++++++++++--- graph/Edge.java | 6 +- graph/Graph.java | 57 ++++++++++-------- graph/NameDoesNotExistException.java | 19 ++++++ graph/UndirectedGraph.java | 14 ++++- graph/Vertex.java | 4 +- 6 files changed, 150 insertions(+), 36 deletions(-) create mode 100644 graph/NameDoesNotExistException.java diff --git a/graph/DirectedGraph.java b/graph/DirectedGraph.java index 0131fd0..fb9a36a 100644 --- a/graph/DirectedGraph.java +++ b/graph/DirectedGraph.java @@ -1,5 +1,6 @@ package graph; +import java.util.Objects; import java.util.Vector; public class DirectedGraph extends Graph { @@ -14,33 +15,100 @@ public class DirectedGraph extends Graph { } - public boolean areStrongAdjacent(MarkedVertex n1, MarkedVertex n2) { + public boolean areAdjacent(MarkedVertex n1, MarkedVertex n2) { return true; } - public boolean areStrongAdjacent(String s1, String s2) { - return true; + public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException{ + MarkedVertex n1 = null; + MarkedVertex n2 = null; + for (MarkedVertex 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 n1, MarkedVertex n2) { + boolean n1ton2 = false; + boolean n2ton1 = false; + for (MarkedEdge 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 n1 = null; + MarkedVertex n2 = null; + for (MarkedVertex 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 n) { - return 1; + int degree = 0; + for (MarkedEdge i: this.getAllEdges()) { + if (i.getDestination() == n) { + degree += 1; + } + } + return degree; } - public int inDegree(String s) { - return 1; + public int inDegree(String s) throws NameDoesNotExistException{ + for (MarkedVertex 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 n) { - return 1; + int degree = 0; + for (MarkedEdge i: this.getAllEdges()) { + if (i.getSource() == n) { + degree += 1; + } + } + return degree; } - public int outDegree(String s) { - return 1; + public int outDegree(String s) throws NameDoesNotExistException{ + for (MarkedVertex i: this.getAllVertexes()) { + if (Objects.equals(i.getName(), s)) { + return outDegree(i); + } + } + throw new NameDoesNotExistException("One of the Vertexes might not exist"); } diff --git a/graph/Edge.java b/graph/Edge.java index b89a08c..887953e 100644 --- a/graph/Edge.java +++ b/graph/Edge.java @@ -7,7 +7,11 @@ public abstract class Edge { private Vertex destination; - public Edge() {} + public Edge() { + this.name = ""; + this.source = null; + this.destination = null; + } public Edge(String s, Vertex n1, Vertex n2) { diff --git a/graph/Graph.java b/graph/Graph.java index 3235b2c..bdbdd73 100644 --- a/graph/Graph.java +++ b/graph/Graph.java @@ -3,7 +3,7 @@ package graph; import java.util.Objects; import java.util.Vector; -public class Graph { +public abstract class Graph { private String name; private Vector> vertexes; @@ -41,33 +41,35 @@ public class Graph { } - public boolean removeEdge(MarkedEdge e) { - return this.edges.remove(e); + public void removeEdge(MarkedEdge e) { + this.edges.remove(e); } - public boolean removeEdge(String s) { + public void removeEdge(String s) throws NameDoesNotExistException{ for (MarkedEdge i: this.edges) { 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 n) { - return this.vertexes.remove(n); + public void removeVertex(MarkedVertex n) { + this.vertexes.remove(n); } - public boolean removeVertex(String s) { + public void removeVertex(String s) throws NameDoesNotExistException{ for (MarkedVertex i: this.vertexes) { 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 { } - public boolean hasEdge(String s1, String s2) { - return true; + public boolean hasEdge(MarkedEdge e) { + return this.edges.contains(e); } - public boolean hasEdge(MarkedEdge e) { - return this.edges.contains(e); + public boolean hasEdge(String s1, String s2) throws NameDoesNotExistException { + MarkedVertex n1 = null; + MarkedVertex n2 = null; + for (MarkedVertex 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 { } - public abstract boolean areAdjacent(MarkedVertex n1, MarkedVertex n2) { - return true; - } + public abstract boolean areAdjacent(MarkedVertex n1, MarkedVertex n2); - public abstract boolean areAdjacent(String s1, String s2) { - return true; - } + public abstract boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException; public boolean hasLoop(MarkedVertex n) { @@ -169,12 +180,12 @@ public class Graph { } - public boolean hasLoop(String s) { + public boolean hasLoop(String s) throws NameDoesNotExistException{ for (MarkedVertex i: this.vertexes) { if (Objects.equals(i.getName(), s)) { return hasLoop(i); } } - return false; + throw new NameDoesNotExistException("One of the Vertexes might not exist"); } } diff --git a/graph/NameDoesNotExistException.java b/graph/NameDoesNotExistException.java new file mode 100644 index 0000000..984f705 --- /dev/null +++ b/graph/NameDoesNotExistException.java @@ -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); + } +} diff --git a/graph/UndirectedGraph.java b/graph/UndirectedGraph.java index 7624161..aaf9a15 100644 --- a/graph/UndirectedGraph.java +++ b/graph/UndirectedGraph.java @@ -15,6 +15,16 @@ public class UndirectedGraph extends Graph { } + public boolean areAdjacent(MarkedVertex n1, MarkedVertex n2) { + return true; + } + + + public boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException { + return true; + } + + public String toString() { return ""; } @@ -34,13 +44,13 @@ public class UndirectedGraph extends Graph { } - public int degree(String s) { + public int degree(String s) throws NameDoesNotExistException{ for (MarkedVertex i: this.getAllVertexes()) { if (Objects.equals(i.getName(), s)) { return degree(i); } } - return 0; + throw new NameDoesNotExistException("One of the Vertexes might not exist"); } diff --git a/graph/Vertex.java b/graph/Vertex.java index e51ef3d..6f296ed 100644 --- a/graph/Vertex.java +++ b/graph/Vertex.java @@ -4,7 +4,9 @@ public abstract class Vertex { private String name; - public Vertex() {} + public Vertex() { + this.name = ""; + } public Vertex(String s) {