Programmieren_Projekt/graph/Graph.java

198 lines
5.2 KiB
Java

package graph;
import logging.LogElement;
import logging.LogElementList;
import java.util.Vector;
public abstract class Graph<T extends VertexMarking, U extends EdgeMarking> {
private String name;
private Vector<MarkedVertex<T>> vertexes = new Vector<>();
private Vector<MarkedEdge<U>> edges = new Vector<>();
public Graph() {}
public Graph(String s) {
this.name = s;
}
public void addEdge(MarkedEdge<U> e) {
edges.add(e);
}
public void addVertex(MarkedVertex<T> n) {
vertexes.add(n);
}
public boolean areAdjacent(MarkedVertex<T> n1, MarkedVertex<T> n2){
for (MarkedEdge<U> edge: edges){
if (edge.getSource() == n1 && edge.getDestination() == n2 || edge.getDestination() == n1 && edge.getSource() == n2){
return true;
}
}
return false;
}
public boolean areAdjacent(String s1, String s2){
for (MarkedEdge<U> edge: edges){
if (edge.getSource().getName().equals(s1) && edge.getDestination().getName().equals(s2)){
return true;
}
else if (edge.getSource().getName().equals(s2) && edge.getDestination().getName().equals(s1)){
return true;
}
}
return false;
}
public int degree() {
return edges.size();
}
public Vector<MarkedEdge<U>> getAllEdges() {
return this.edges;
}
public Vector<MarkedVertex<T>> getAllVertexes() {
return this.vertexes;
}
public String getName() {
return this.name;
}
public abstract visualizationElements.Graph getVisualizedGraph();
public abstract LogElementList<LogElement> getLogList();
/*
public Vector <Vertex> getNeighbors(Vertex n1){
Vector<Vertex> neighbors = new Vector<>();
for (MarkedEdge<U> edge : edges) {
if (edge.getSource().equals(n1)) {
neighbors.add(edge.getDestination());
}
else if (edge.getDestination().equals(n1)){
neighbors.add(edge.getSource());
}
}
return neighbors;
}
*/
public boolean hasEdge(MarkedEdge<U> e) {
return edges.contains(e);
}
public boolean hasEdge(String s) {
for (MarkedEdge<U> edge : edges) {
if (edge.getName().equals(s)) {
return true;
}
}
return false;
}
public boolean hasEdge(MarkedVertex<T> v1, MarkedVertex<T> v2) {
for (MarkedEdge<U> edge : edges) {
if (edge.getSource().equals(v1) && edge.getDestination().equals(v2)) {
return true;
}
else if (edge.getSource().equals(v2) && edge.getDestination().equals(v1)) {
return true;
}
}
return false;
}
public boolean hasEdge(String s1, String s2) {
for (MarkedEdge<U> edge : edges) {
if (edge.getSource().getName().equals(s1) && edge.getDestination().getName().equals(s2)) {
return true;
}
else if (edge.getSource().getName().equals(s2) && edge.getDestination().getName().equals(s1)) {
return true;
}
}
return false;
}
public boolean hasLoop(MarkedVertex<T> n) {
for (MarkedEdge<U> edge : edges) {
if (edge.getSource().equals(n) && edge.getDestination().equals(n)) {
return true;
}
}
return false;
}
public boolean hasLoop(String s) {
for (MarkedEdge<U> edge : edges) {
if (edge.getSource().getName().equals(s) && edge.getDestination().getName().equals(s)) {
return true;
}
}
return false;
}
public Boolean hasVertex(MarkedVertex<T> n) {
return vertexes.contains(n);
}
public boolean hasVertex(String s) {
for (MarkedVertex<T> vertex : vertexes) {
if (vertex.getName().equals(s)) {
return true;
}
}
return false;
}
public int numberOfEdges() {
return edges.size();
}
public int numberOfVertexes() {
return vertexes.size();
}
public boolean removeEdge(MarkedEdge<U> e) {
return edges.remove(e);
}
public boolean removeEdge(String s) {
for (MarkedEdge<U> edge : edges) {
if (edge.getName().equals(s)) {
return edges.remove(edge);
}
}
return false;
}
public boolean removeVertex(MarkedVertex<T> n) {
return vertexes.remove(n);
}
public boolean removeVertex(String s) {
for (MarkedVertex<T> vertex : vertexes) {
if (vertex.getName().equals(s)) {
return vertexes.remove(vertex);
}
}
return false;
}
public void setName(String n) {
this.name = n;
}
@Override
public String toString() {
return "Graph{" + "name='" + name + '\'' + ", vertexes=" + vertexes + ", edges=" + edges + '}';
}
}