Edge-Klasse:
public class Edge { String v1; String v2; Edge(String v1, String v2) { this.v1 = v1; this.v2 = v2; } public boolean isNeighbour(Edge e) { if (e.v1 == this.v1 || e.v1 == this.v2 || e.v2 == this.v1 || e.v2 == this.v2) { return true; } return false; } }
Graph-Klasse
public class Graph { private List vertices = new ArrayList<>(); private List edges = new ArrayList<>(); public void setEdges(Edge[] edges) { this.edges.clear(); for (Edge e : edges) { this.edges.add(e); } } public void setEdges(String[] edges) { if (edges.length % 2 != 0) { System.out.println("wrong Array length"); } else { for (int i = 0; i < edges.length; i++) { this.addEdge(new Edge(edges[i], edges[i + 1])); i++; } } } public void addEdge(Edge e) { this.edges.add(e); this.vertices.add(e.v1); this.vertices.add(e.v2); } public List getVertices() { return this.vertices; } public List getEdges() { return this.edges; } public List getMaximum(Graph g) { List e = g.edges; List eDel = new ArrayList<>(); List m = new ArrayList<>(); int index = e.size() - 1; while (!e.isEmpty()) { Edge element = e.get(index); m.add(element); e.remove(index); for (Edge elem : e) { if (element.isNeighbour(elem)) { eDel.add(elem); } } e.removeAll(eDel); index = e.size() - 1; } return m; } }