graph neuster Stand
This commit is contained in:
parent
44a33b7720
commit
2da3f2b591
@ -1,5 +1,6 @@
|
||||
package graph;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
public boolean areStrongAdjacent(String s1, String s2) {
|
||||
return true;
|
||||
public boolean areAdjacent(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 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) {
|
||||
return 1;
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> 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<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) {
|
||||
return 1;
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> 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<T> i: this.getAllVertexes()) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
return outDegree(i);
|
||||
}
|
||||
}
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -3,7 +3,7 @@ package graph;
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
|
||||
public class Graph<T, U> {
|
||||
public abstract class Graph<T, U> {
|
||||
|
||||
private String name;
|
||||
private Vector<MarkedVertex<T>> vertexes;
|
||||
@ -41,33 +41,35 @@ public class Graph<T, U> {
|
||||
}
|
||||
|
||||
|
||||
public boolean removeEdge(MarkedEdge<U> e) {
|
||||
return this.edges.remove(e);
|
||||
public void removeEdge(MarkedEdge<U> e) {
|
||||
this.edges.remove(e);
|
||||
}
|
||||
|
||||
|
||||
public boolean removeEdge(String s) {
|
||||
public void removeEdge(String s) throws NameDoesNotExistException{
|
||||
for (MarkedEdge<U> 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<T> n) {
|
||||
return this.vertexes.remove(n);
|
||||
public void removeVertex(MarkedVertex<T> n) {
|
||||
this.vertexes.remove(n);
|
||||
}
|
||||
|
||||
|
||||
public boolean removeVertex(String s) {
|
||||
public void removeVertex(String s) throws NameDoesNotExistException{
|
||||
for (MarkedVertex<T> 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<T, U> {
|
||||
}
|
||||
|
||||
|
||||
public boolean hasEdge(String s1, String s2) {
|
||||
return true;
|
||||
public boolean hasEdge(MarkedEdge<U> e) {
|
||||
return this.edges.contains(e);
|
||||
}
|
||||
|
||||
|
||||
public boolean hasEdge(MarkedEdge<U> e) {
|
||||
return this.edges.contains(e);
|
||||
public boolean hasEdge(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 hasEdge(n1, n2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -149,14 +164,10 @@ public class Graph<T, U> {
|
||||
}
|
||||
|
||||
|
||||
public abstract boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
return true;
|
||||
}
|
||||
public abstract boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> 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<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) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
return hasLoop(i);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
}
|
||||
|
19
graph/NameDoesNotExistException.java
Normal file
19
graph/NameDoesNotExistException.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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() {
|
||||
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()) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
return degree(i);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,7 +4,9 @@ public abstract class Vertex {
|
||||
|
||||
private String name;
|
||||
|
||||
public Vertex() {}
|
||||
public Vertex() {
|
||||
this.name = "";
|
||||
}
|
||||
|
||||
|
||||
public Vertex(String s) {
|
||||
|
Loading…
Reference in New Issue
Block a user