Neuster Stand
This commit is contained in:
commit
6b86b6c64f
124
graph/DirectedGraph.java
Normal file
124
graph/DirectedGraph.java
Normal file
@ -0,0 +1,124 @@
|
||||
package graph;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
|
||||
public class DirectedGraph<T, U> extends Graph<T, U> {
|
||||
|
||||
public DirectedGraph() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
public DirectedGraph(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
|
||||
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2) {
|
||||
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) {
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
if (i.getDestination() == n) {
|
||||
degree += 1;
|
||||
}
|
||||
}
|
||||
return degree;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
int degree = 0;
|
||||
for (MarkedEdge<U> i: this.getAllEdges()) {
|
||||
if (i.getSource() == n) {
|
||||
degree += 1;
|
||||
}
|
||||
}
|
||||
return degree;
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
public Vector<MarkedVertex<T>> getPredecessors(MarkedVertex<T> n) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public Vector<MarkedVertex<T>> getSuccessors(MarkedVertex<T> n) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
52
graph/Edge.java
Normal file
52
graph/Edge.java
Normal file
@ -0,0 +1,52 @@
|
||||
package graph;
|
||||
|
||||
public abstract class Edge {
|
||||
|
||||
private String name;
|
||||
private Vertex source;
|
||||
private Vertex destination;
|
||||
|
||||
|
||||
public Edge() {
|
||||
this.name = "";
|
||||
this.source = null;
|
||||
this.destination = null;
|
||||
}
|
||||
|
||||
|
||||
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
4
graph/EdgeMarking.java
Normal file
@ -0,0 +1,4 @@
|
||||
package graph;
|
||||
|
||||
public abstract class EdgeMarking extends Marking{
|
||||
}
|
191
graph/Graph.java
Normal file
191
graph/Graph.java
Normal file
@ -0,0 +1,191 @@
|
||||
package graph;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
|
||||
public abstract 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 void removeEdge(MarkedEdge<U> e) {
|
||||
this.edges.remove(e);
|
||||
}
|
||||
|
||||
|
||||
public void removeEdge(String s) throws NameDoesNotExistException{
|
||||
for (MarkedEdge<U> i: this.edges) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
removeEdge(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new NameDoesNotExistException("One of the Edges might not exist");
|
||||
}
|
||||
|
||||
|
||||
public void removeVertex(MarkedVertex<T> n) {
|
||||
this.vertexes.remove(n);
|
||||
}
|
||||
|
||||
|
||||
public void removeVertex(String s) throws NameDoesNotExistException{
|
||||
for (MarkedVertex<T> i: this.vertexes) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
removeVertex(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
|
||||
|
||||
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(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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
public abstract boolean areAdjacent(String s1, String s2) throws NameDoesNotExistException;
|
||||
|
||||
|
||||
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) throws NameDoesNotExistException{
|
||||
for (MarkedVertex<T> i: this.vertexes) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
return hasLoop(i);
|
||||
}
|
||||
}
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
}
|
32
graph/MarkedEdge.java
Normal file
32
graph/MarkedEdge.java
Normal 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
32
graph/MarkedVertex.java
Normal 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
4
graph/Marking.java
Normal file
@ -0,0 +1,4 @@
|
||||
package graph;
|
||||
|
||||
public abstract class Marking {
|
||||
}
|
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);
|
||||
}
|
||||
}
|
68
graph/UndirectedGraph.java
Normal file
68
graph/UndirectedGraph.java
Normal file
@ -0,0 +1,68 @@
|
||||
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 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 "";
|
||||
}
|
||||
|
||||
|
||||
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) throws NameDoesNotExistException{
|
||||
for (MarkedVertex<T> i: this.getAllVertexes()) {
|
||||
if (Objects.equals(i.getName(), s)) {
|
||||
return degree(i);
|
||||
}
|
||||
}
|
||||
throw new NameDoesNotExistException("One of the Vertexes might not exist");
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
25
graph/Vertex.java
Normal file
25
graph/Vertex.java
Normal file
@ -0,0 +1,25 @@
|
||||
package graph;
|
||||
|
||||
public abstract class Vertex {
|
||||
|
||||
private String name;
|
||||
|
||||
public Vertex() {
|
||||
this.name = "";
|
||||
}
|
||||
|
||||
|
||||
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
4
graph/VertexMarking.java
Normal file
@ -0,0 +1,4 @@
|
||||
package graph;
|
||||
|
||||
public abstract class VertexMarking extends Marking {
|
||||
}
|
Loading…
Reference in New Issue
Block a user