From a16761a3cc9626c09a06cadc3b24a15f02591768 Mon Sep 17 00:00:00 2001 From: Angel Adrian Cantu Date: Sat, 21 Apr 2018 02:33:35 -0500 Subject: [PATCH 1/2] MinCut: Clean version --- .../asarg/polysim/models/base/Assembly.java | 68 + .../asarg/polysim/models/base/bondGraph.java | 1580 +++++++++++++++++ 2 files changed, 1648 insertions(+) create mode 100644 src/main/java/com/asarg/polysim/models/base/bondGraph.java diff --git a/src/main/java/com/asarg/polysim/models/base/Assembly.java b/src/main/java/com/asarg/polysim/models/base/Assembly.java index 964de43..5f0d71d 100644 --- a/src/main/java/com/asarg/polysim/models/base/Assembly.java +++ b/src/main/java/com/asarg/polysim/models/base/Assembly.java @@ -52,6 +52,7 @@ public class Assembly extends Observable { public Assembly() { tileSystem = new TileSystem(2, 0); frontier = new Frontier(tileSystem); + } public Assembly(TileSystem ts) { @@ -67,6 +68,53 @@ public Assembly(TileSystem ts) { frontier = new Frontier(tileSystem); } + public bondGraph getBondGraph(){ + Set> tiles = Grid.entrySet(); + bondGraph bondGraph = new bondGraph(); + + for (Map.Entry mep : tiles) { + Coordinate pt = mep.getKey(); + Tile tile = mep.getValue(); + if (tile.getGlueW() != null || tile.getGlueN() != null || tile.getGlueE() != null || tile.getGlueS() != null) { + bondGraph.addBondVertex(tile, tile.getLabel(), pt, tile.getGlueN(), + tile.getGlueS(), tile.getGlueW(), tile.getGlueE()); + } + } + for (bondGraph.Vertex v : bondGraph.vertices){ + for (bondGraph.Vertex vertex : bondGraph.vertices){ + if (v == vertex){ + + } + else{ + if (v.location.getEast().equals(vertex.location)){ + int strength = tileSystem.getStrength(v.E, vertex.W); + if (strength != 0){ + v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "E"); + } + } + else if (v.location.getWest().equals(vertex.location)){ + int strength = tileSystem.getStrength(v.W, vertex.E); + if (strength != 0){ + v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "W"); + } + } + else if (v.location.getNorth().equals(vertex.location)){ + int strength = tileSystem.getStrength(v.N, vertex.S); + if (strength != 0){ + v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "N"); + } + } + else if (v.location.getSouth().equals(vertex.location)){ + int strength = tileSystem.getStrength(v.S, vertex.N); + if (strength != 0){ + v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "S"); + } + } + } + } + } + return bondGraph; + } //change tile system stub public void changeTileSystem(TileSystem newTS) { System.out.println("WARNING: CHANGING THE TILE SYSTEM, PREPARE FOR ERRORS!"); @@ -84,6 +132,26 @@ public void changeTileSystem(TileSystem newTS) { public void changeTemperature(int temperature) { tileSystem.setTemperature(temperature); changeTileSystem(tileSystem); + + //////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////// + //For now: + //Changing the temperature gets you the bond graph (works like a button) + bondGraph bond_graph = getBondGraph(); + //Also, computes MinCut on the bond graph (when needed) + //you must pass it the temperature of the system, so it knows what cut to return + ArrayList cutMe = bond_graph.getMinCut(tileSystem.getTemperature()); + //here we print the edges which are to be cut + //each edge in the list cutMe has tileFrom and tileTo, which are of type: Tile + if (!cutMe.isEmpty()){ + System.out.print("\nCut the following edges: "); + for (bondGraph.Edge e : cutMe){ + System.out.print("\n" + e.tileFrom.getLabel() + "->" + e.tileTo.getLabel()); + } + } + //////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////// + System.out.print("\nSuccess"); } public void changeTileConfiguration(TileConfiguration tc) { diff --git a/src/main/java/com/asarg/polysim/models/base/bondGraph.java b/src/main/java/com/asarg/polysim/models/base/bondGraph.java new file mode 100644 index 0000000..e844915 --- /dev/null +++ b/src/main/java/com/asarg/polysim/models/base/bondGraph.java @@ -0,0 +1,1580 @@ +package com.asarg.polysim.models.base; + +import javafx.util.Pair; +import java.util.*; +/** + * Created by Angel A. Cantu in 2018 + */ +public class bondGraph { + private ArrayList> preFaceVertices = new ArrayList>(); + private ArrayList face_vertices = new ArrayList<>(); + private List markedOnes = new ArrayList<>(); + private int totalAmountOfEdges = 0; + public List vertices = new ArrayList<>(); + + public class Vertex { + public Tile thisTile; + public String tileLabel; + public String realTileLabel; + public String N; + public String W; + public String S; + public String E; + public Coordinate location; + + private ArrayList itsBoundary = new ArrayList(); + private List neighbors = new ArrayList<>(); + private int mark = 0; + private int pathMark = 0; + private int edgeAmount = 0; + private int boundaryFlag = 0; + private Edge northNeighbor; + private Edge southNeighbor; + private Edge westNeighbor; + private Edge eastNeighbor; + public void addEdge(Tile from, Tile to, Vertex b, int strength, String where) { + totalAmountOfEdges++; + neighbors.add(new Edge(from, to, b, strength, where)); + Edge e = new Edge(from, to, b, strength, where); + edgeAmount = edgeAmount + 1; + if (where == "N") { + northNeighbor = e; + } else if (where == "S") { + southNeighbor = e; + } else if (where == "W") { + westNeighbor = e; + } else if (where == "E") { + eastNeighbor = e; + } + } + } + public class Edge { + public Tile tileFrom; + public Tile tileTo; + private Vertex toVertex; + private String direction = "X"; + private int bondStrength = 0; + private int inside = -1; + private int taken = 0; + private Edge(Tile from, Tile to, Vertex v, int strength, String dir) { + tileFrom = from; + tileTo = to; + toVertex = v; + bondStrength = strength; + direction = dir; + } + } + private class faceVertex{ + private String label; + private ArrayList> edges = new ArrayList<>(); + private ArrayList neighbors = new ArrayList<>(); + private ArrayList strengths = new ArrayList<>(); + private int inside = -1; + private faceVertex(String name){ + label = name; + } + } + private class faceEdge { + private faceVertex toVertex; + private String source; + private String original_source; + private String original_to; + private int bondStrength = 0; + private faceEdge(String x, String y, String from, faceVertex b, int strength) { + source = from; + original_source = x; + original_to = y; + toVertex = new faceVertex(b.label); + bondStrength = strength; + } + } + private class Border{ + private String ofPath = "X"; + private Pair inout; + private Border(String path, String in, String out){ + ofPath = path; + inout = new Pair<>(in, out); + } + private boolean Compare(String x, String y){ + Pair c = new Pair<>(x, y); + if (inout.equals(c)){ + return true; + } + else { + return false; + } + } + } + private class bellmanGraph{ + private class bellmanEdge { + int src, dest, weight, flag, org_src, org_dest, id, redundantFlag; + bellmanEdge() { + src = dest = weight = flag = org_dest = org_src = id = redundantFlag = 0; + } + } + int V; + int E; + bellmanEdge edge[]; + bellmanGraph(int v, int e) + { + V = v; + E = e; + edge = new bellmanEdge[e]; + for (int i=0; i cutMe = minCut(tao); + + return cutMe; + } + private void handleLeafs(){ + int amountNoFaces = 0; + while (true) { + int amount = markLeafs(); + if (amount >= 2) { + break; + } + else if (amountNoFaces == vertices.size() || amount == -1){ + System.out.print("INVALID ASSEMBLY || NO FACES"); + return; + } + amountNoFaces++; + } + } + private void removeRedundantEdges(){ + for (faceVertex fv : face_vertices){ + for (faceEdge e : fv.neighbors){ + for (faceEdge ee: fv.neighbors){ + if (ee.original_to == e.original_source && ee.original_source == e.original_to){ + ee.original_source = "deleteMe"; + } + } + } + } + for (faceVertex fv : face_vertices){ + Iterator ee = fv.neighbors.iterator(); + while (ee.hasNext()){ + faceEdge e = ee.next(); + if (e.original_source == "deleteMe"){ + ee.remove(); + } + } + } + } + private ArrayList minCut(int tao){ + List s_Belonging = new ArrayList<>(); + List f_Belonging = new ArrayList<>(); + List path = new ArrayList<>(); + for (Vertex s: vertices){ + findFaceOf(s_Belonging, s); + for (Vertex t : vertices){ + if (t.tileLabel == s.tileLabel){ + f_Belonging.clear(); + } + else{ + List s_disjunction = new ArrayList<>(); + s_disjunction.addAll(s_Belonging); + findFaceOf(f_Belonging, t); + s_disjunction.removeAll(f_Belonging); + + List f_disjunction = new ArrayList<>(); + f_disjunction.addAll(s_Belonging); + findFaceOf(f_Belonging, t); + f_disjunction.removeAll(f_Belonging); + + for (Integer s_vertex : s_Belonging){ + for (Integer f_vertex : f_Belonging) { + if (f_vertex.equals(s_vertex)) { + + } else { + if (s_vertex == -1){ + s_vertex = face_vertices.size() - 1; + } + if (f_vertex == -1){ + f_vertex = face_vertices.size() - 1; + } + ArrayList> cutPath = new ArrayList<>(); + int hasCut = bellmanFord(cutPath, s_vertex, path, f_vertex, tao); + if (hasCut == 1){ + ArrayList cutMe = new ArrayList<>(); + for (Pair i : cutPath){ + for (Vertex v : vertices){ + for (Edge e : v.neighbors){ + if (i.getValue().equals(Integer.parseInt(e.toVertex.tileLabel)) && i.getKey().equals(Integer.parseInt(v.tileLabel))){ + cutMe.add(e); + } + } + } + } + return cutMe; + } + else{ + cutPath.clear(); + } + } + } + } + } + f_Belonging.clear(); + } + s_Belonging.clear(); + } + return new ArrayList(); + } + private void findFaceOf(List s_t_Belonging, Vertex s_t){ + for (faceVertex fc : face_vertices){ + for (faceEdge fe : fc.neighbors){ + if (s_t.tileLabel.equals(fe.original_source) || s_t.tileLabel.equals(fe.original_to)){ + s_t_Belonging.add(Integer.parseInt(fc.label)); + } + } + } + Set removeDup = new HashSet<>(); + removeDup.addAll(s_t_Belonging); + s_t_Belonging.clear(); + s_t_Belonging.addAll(removeDup); + } + private int bellmanFord(ArrayList cutPath, int source, List path, int dest, int tao){ + ArrayList>> bellman = new ArrayList>>(); + for (faceVertex fe : face_vertices){ + bellman.add(new ArrayList>()); + } + bellmanGraph graphOfBellmanFord = new bellmanGraph(face_vertices.size(), totalAmountOfEdges); + int k = 0; + for (faceVertex fc : face_vertices){ + for (faceEdge fe : fc.neighbors){ + int neg_source = 0; + int neg_to = 0; + neg_source = Integer.parseInt(fe.source); + neg_to = Integer.parseInt(fe.toVertex.label); + + if (fe.source == "-1"){ + neg_source = face_vertices.size() - 1; + } + if (fe.toVertex.label == "-1"){ + neg_to = face_vertices.size() - 1; + } + graphOfBellmanFord.edge[k].src = neg_source; + graphOfBellmanFord.edge[k].dest = neg_to; + graphOfBellmanFord.edge[k].org_src = Integer.parseInt(fe.original_source); + graphOfBellmanFord.edge[k].org_dest = Integer.parseInt(fe.original_to); + graphOfBellmanFord.edge[k].weight = fe.bondStrength; + graphOfBellmanFord.edge[k].id = k; + k++; + } + } + for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ + for (bellmanGraph.bellmanEdge ee : graphOfBellmanFord.edge){ + if (ee.redundantFlag == 0 && e.org_src == ee.org_dest && e.org_dest == ee.org_src){ + ee.id = e.id; + ee.redundantFlag = 1; + } + } + } + int amountVertices = graphOfBellmanFord.V; + int amountEdges = graphOfBellmanFord.E; + int dist[] = new int[amountVertices]; + int pred[] = new int[amountVertices]; + ArrayList> cameFrom = new ArrayList>(); + for (int i=0; i < amountVertices; ++i){ + dist[i] = Integer.MAX_VALUE; + pred[i] = -1; + cameFrom.add(new Pair<>(0, 0)); + } + dist[source] = 0; + pred[source] = -10; + + for (int i = 1; i < amountVertices; ++i) + { + for (int j = 0; j < amountEdges; j++) + { + int u = graphOfBellmanFord.edge[j].src; + int v = graphOfBellmanFord.edge[j].dest; + int weight = graphOfBellmanFord.edge[j].weight; + int id = graphOfBellmanFord.edge[j].id; + if (dist[u]!=Integer.MAX_VALUE && + dist[u]+weight p = new Pair<>(id, u); + int flagg = 0; + ArrayList values = new ArrayList<>(); + + + for (Pair ppp : bellman.get(v)){ + if (Integer.valueOf((int)ppp.getKey()) == id){ + flagg = 1; + } + values.add(Integer.valueOf((int)ppp.getValue())); + } + + values.add(u); + for (Integer val : values) { + for (Pair ppp : bellman.get(val)) { + if (Integer.valueOf((int)ppp.getKey()) == id){ + flagg = 1; + } + } + } + ArrayList> holder = new ArrayList>(); + if (flagg == 0){ + for (Pair ppp : bellman.get(v)){ + if (Integer.valueOf((int)ppp.getValue()) == u){ + + } + else{ + holder.add(ppp); + } + } + bellman.set(v, holder); + bellman.get(v).add(p); + } + if (flagg == 0){ + dist[v] = dist[u] + weight; + pred[v] = u; + } + + cameFrom.set(v, new Pair<>(graphOfBellmanFord.edge[j].org_dest, graphOfBellmanFord.edge[j].org_src)); + graphOfBellmanFord.edge[j].flag = 2; + for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ + if (e.org_src == graphOfBellmanFord.edge[j].org_dest && e.org_dest == graphOfBellmanFord.edge[j].org_src){ + e.flag = 2; + } + } + for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ + int thing = 0; + Pair destroyer = new Pair<>(e.org_dest, e.org_src); + Pair reyortsed = new Pair<>(e.org_src, e.org_dest); + for (Pair pp : cameFrom){ + if (pp.getKey() == destroyer.getKey() && pp.getValue() == destroyer.getValue() + || pp.getKey() == reyortsed.getKey() && pp.getValue() == reyortsed.getValue()){ + thing = 1; + } + } + if (thing == 1){ + e.flag = 2; + } + else{ + e.flag = 0; + } + } + } + } + } + int counter = 0; + Iterator>> okok = bellman.iterator(); + ArrayList hol = new ArrayList<>(); + while (okok.hasNext()){ + ArrayList> chale = okok.next(); + Iterator> kkk = chale.iterator(); + while(kkk.hasNext()){ + Pair po = kkk.next(); + if (po.getValue() == pred[counter]){ + hol.add(po.getKey()); + } + } + counter++; + } + ArrayList realPath = new ArrayList<>(); + for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ + for (Integer ahh : hol){ + if (e.id == ahh){ + e.flag = 1; + realPath.add(e.id); + } + } + } + dist[source] = Integer.MAX_VALUE; + int finalflag = 0; + for (int j = 0; j < amountEdges; ++j) + { + int u = graphOfBellmanFord.edge[j].src; + int v = graphOfBellmanFord.edge[j].dest; + int weight = graphOfBellmanFord.edge[j].weight; + int f = graphOfBellmanFord.edge[j].flag; + int id = graphOfBellmanFord.edge[j].id; + if (u != source && dist[u] != Integer.MAX_VALUE && + dist[u]+weight < tao && f != 1 && v == source) { + dist[v] = dist[u] + weight; + pred[v] = u; + Pair pp = new Pair<>(id, u); + bellman.get(v).add(pp); + realPath.add(id); + finalflag = 1; + break; + } + } + + path.clear(); + int is_flag = 0; + for (Integer p : pred){ + int previous = pred[dest]; + if (previous == Integer.MAX_VALUE){ + path.add(dest); + if (dest == source){ + is_flag = 1; + } + break; + } + else{ + path.add(previous); + dest = previous; + if (dest == source){ + is_flag = 1; + } + } + dest = previous; + if (dest == -10){ + return 0; + } + } + int countah = 1; + for (Integer ah : path){ + if (countah <= path.size() - 1){ + Iterator> last = bellman.get(ah).iterator(); + while(last.hasNext()){ + Pair po = last.next(); + if(po.getValue() == path.get(countah)){ + + } + } + } + countah++; + } + + ArrayList oneMoreTime = new ArrayList<>(); + ArrayList idPath = new ArrayList<>(); + for (int j = 1; j < path.size(); j++){ + oneMoreTime.add(path.get(j)); + } + for (int s = 0; s < path.size(); s++){ + if (s < oneMoreTime.size()){ + Iterator> com = bellman.get(path.get(s)).iterator(); + while(com.hasNext()){ + Pair po = com.next(); + if (po.getValue() == oneMoreTime.get(s)){ + idPath.add(po.getKey()); + } + } + } + else{ + Iterator> com = bellman.get(path.get(s)).iterator(); + while(com.hasNext()){ + Pair po = com.next(); + if (po.getValue() == pred[path.get(s)]){ + idPath.add(po.getKey()); + } + } + } + } + Set removeDup = new HashSet<>(); + removeDup.addAll(idPath); + idPath.clear(); + idPath.addAll(removeDup); + ArrayList> finalPath = new ArrayList<>(); + for (Integer i : idPath){ + for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ + if (i == e.id){ + finalPath.add(new Pair<>(e.org_dest, e.org_src)); + } + } + } + ArrayList indices = new ArrayList<>(); + int count = 0; + int flag = 0; + for (Pair p : finalPath){ + int counttwo = 0; + for (Pair pp : finalPath){ + if (count == counttwo){ + + } + else if (p.getValue() == pp.getKey() && p.getKey()== pp.getValue()){ + for (Integer i : indices){ + if (i == counttwo){ + flag = 1; + } + } + if (flag == 0){ + indices.add(count); + } + flag = 0; + } + counttwo++; + } + count++; + } + + Set> thefinalPath = new HashSet<>(); + for (Integer i : indices){ + thefinalPath.add(finalPath.get(i)); + } + cutPath.addAll(thefinalPath); + if (finalflag == 1 && dist[source] < tao && is_flag != 0){ + return 1; + } + else{ + return 0; + } + } + private void connectFaceEdges(boolean x){ + if (!x){ + faceVertex outsideFace = new faceVertex("-1"); + face_vertices.add(outsideFace); + } + for (Vertex v : vertices){ + int flag = 0; + if (v.northNeighbor != null && v.northNeighbor.direction != null + && !v.northNeighbor.direction.isEmpty() + && v.northNeighbor.taken == 0){ + Pair check_pair = new Pair<>(v.tileLabel, v.northNeighbor.toVertex.tileLabel); + Pair r_check_pair = new Pair<>(v.northNeighbor.toVertex.tileLabel, v.tileLabel); + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (check_pair.equals(fe)){ + if (v.northNeighbor.inside == -1){ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.northNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.northNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.northNeighbor.bondStrength)); + } + else{ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.northNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.northNeighbor.inside), v.northNeighbor.bondStrength)); + face_vertices.get(v.northNeighbor.inside).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.northNeighbor.inside).label, fc, v.northNeighbor.bondStrength)); + } + } + } + } + if (flag == 0){ + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (r_check_pair.equals(fe)){ + if (v.northNeighbor.inside == -1){ + flag = 1; + } + else{ + flag = 1; + } + } + } + } + if (flag == 0){ + if (v.northNeighbor.inside == -1){ + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.northNeighbor.bondStrength)); + } + else{ + face_vertices.get(v.northNeighbor.inside).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.northNeighbor.inside).label, face_vertices.get(v.northNeighbor.inside), v.northNeighbor.bondStrength)); + } + + } + } + v.northNeighbor.taken = 1; + } + if (v.eastNeighbor != null && v.eastNeighbor.direction != null + && !v.eastNeighbor.direction.isEmpty() + && v.eastNeighbor.taken == 0){ + Pair check_pair = new Pair<>(v.tileLabel, v.eastNeighbor.toVertex.tileLabel); + Pair r_check_pair = new Pair<>(v.eastNeighbor.toVertex.tileLabel, v.tileLabel); + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (check_pair.equals(fe)){ + if (v.eastNeighbor.inside == -1){ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.eastNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.eastNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.eastNeighbor.bondStrength)); + } + else{ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.eastNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.eastNeighbor.inside), v.eastNeighbor.bondStrength)); + face_vertices.get(v.eastNeighbor.inside).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.eastNeighbor.inside).label, fc, v.eastNeighbor.bondStrength)); + } + } + } + } + if (flag == 0){ + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (r_check_pair.equals(fe)){ + if (v.eastNeighbor.inside == -1){ + flag = 1; + } + else{ + flag = 1; + } + } + } + } + if (flag == 0){ + if (v.eastNeighbor.inside == -1){ + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.eastNeighbor.bondStrength)); + } + else{ + face_vertices.get(v.eastNeighbor.inside).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.eastNeighbor.inside).label, face_vertices.get(v.eastNeighbor.inside), v.eastNeighbor.bondStrength)); + } + + } + } + v.eastNeighbor.taken = 1; + } + if (v.westNeighbor != null && v.westNeighbor.direction != null + && !v.westNeighbor.direction.isEmpty() + && v.westNeighbor.taken == 0){ + Pair check_pair = new Pair<>(v.tileLabel, v.westNeighbor.toVertex.tileLabel); + Pair r_check_pair = new Pair<>(v.westNeighbor.toVertex.tileLabel, v.tileLabel); + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (check_pair.equals(fe)){ + if (v.westNeighbor.inside == -1){ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.westNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.westNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.westNeighbor.bondStrength)); + } + else{ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.westNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.westNeighbor.inside), v.westNeighbor.bondStrength)); + face_vertices.get(v.westNeighbor.inside).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.westNeighbor.inside).label, fc, v.westNeighbor.bondStrength)); + } + } + } + } + if (flag == 0){ + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (r_check_pair.equals(fe)){ + if (v.westNeighbor.inside == -1){ + flag = 1; + } + else{ + flag = 1; + } + } + } + } + if (flag == 0){ + if (v.westNeighbor.inside == -1){ + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.westNeighbor.bondStrength)); + } + else{ + face_vertices.get(v.westNeighbor.inside).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.westNeighbor.inside).label, face_vertices.get(v.westNeighbor.inside), v.westNeighbor.bondStrength)); + } + + } + } + v.westNeighbor.taken = 1; + } + if (v.southNeighbor != null && v.southNeighbor.direction != null + && !v.southNeighbor.direction.isEmpty() + && v.southNeighbor.taken == 0){ + Pair check_pair = new Pair<>(v.tileLabel, v.southNeighbor.toVertex.tileLabel); + Pair r_check_pair = new Pair<>(v.southNeighbor.toVertex.tileLabel, v.tileLabel); + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (check_pair.equals(fe)){ + if (v.southNeighbor.inside == -1){ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.southNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.southNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.southNeighbor.bondStrength)); + } + else{ + flag = 1; + fc.neighbors.add(new faceEdge(v.tileLabel, v.southNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.southNeighbor.inside), v.southNeighbor.bondStrength)); + face_vertices.get(v.southNeighbor.inside).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.southNeighbor.inside).label, fc, v.southNeighbor.bondStrength)); + } + } + } + } + if (flag == 0){ + for (faceVertex fc : face_vertices){ + for (Pair fe : fc.edges){ + if (r_check_pair.equals(fe)){ + if (v.southNeighbor.inside == -1){ + flag = 1; + } + else{ + flag = 1; + } + } + } + } + if (flag == 0){ + if (v.southNeighbor.inside == -1){ + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.southNeighbor.bondStrength)); + } + else{ + face_vertices.get(v.southNeighbor.inside).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.southNeighbor.inside).label, face_vertices.get(v.southNeighbor.inside), v.southNeighbor.bondStrength)); + } + + } + } + v.southNeighbor.taken = 1; + } + } + } + private void makeFaceVertices() { + int i = 0; + while(i < preFaceVertices.size()){ + faceVertex face_vertex = new faceVertex(Integer.toString(i)); + face_vertices.add(face_vertex); + makeFaceEdges(i); + i++; + } + } + private void makeFaceEdges(int i){ + for (int j = 0; j < preFaceVertices.get(i).size() - 1; j++){ + + if (preFaceVertices.get(i).get(j).northNeighbor != null && preFaceVertices.get(i).get(j).northNeighbor.direction != null + && !preFaceVertices.get(i).get(j).northNeighbor.direction.isEmpty() + && preFaceVertices.get(i).get(j).northNeighbor.inside != -1){ + if (face_vertices.get(i).inside < preFaceVertices.get(i).get(j).northNeighbor.inside){ + face_vertices.get(i).inside = preFaceVertices.get(i).get(j).northNeighbor.inside; + } + face_vertices.get(i).strengths.add(preFaceVertices.get(i).get(j).northNeighbor.bondStrength); + } + if (preFaceVertices.get(i).get(j).eastNeighbor != null && preFaceVertices.get(i).get(j).eastNeighbor.direction != null + && !preFaceVertices.get(i).get(j).eastNeighbor.direction.isEmpty() + && preFaceVertices.get(i).get(j).eastNeighbor.inside != -1){ + if (face_vertices.get(i).inside < preFaceVertices.get(i).get(j).eastNeighbor.inside){ + face_vertices.get(i).inside = preFaceVertices.get(i).get(j).eastNeighbor.inside; + } + face_vertices.get(i).strengths.add(preFaceVertices.get(i).get(j).eastNeighbor.bondStrength); + } + if (preFaceVertices.get(i).get(j).westNeighbor != null && preFaceVertices.get(i).get(j).westNeighbor.direction != null + && !preFaceVertices.get(i).get(j).westNeighbor.direction.isEmpty() + && preFaceVertices.get(i).get(j).westNeighbor.inside != -1){ + if (face_vertices.get(i).inside < preFaceVertices.get(i).get(j).westNeighbor.inside){ + face_vertices.get(i).inside = preFaceVertices.get(i).get(j).westNeighbor.inside; + } + face_vertices.get(i).strengths.add(preFaceVertices.get(i).get(j).westNeighbor.bondStrength); + } + if (preFaceVertices.get(i).get(j).southNeighbor != null && preFaceVertices.get(i).get(j).southNeighbor.direction != null + && !preFaceVertices.get(i).get(j).southNeighbor.direction.isEmpty() + && preFaceVertices.get(i).get(j).southNeighbor.inside != -1){ + if (face_vertices.get(i).inside < preFaceVertices.get(i).get(j).southNeighbor.inside){ + face_vertices.get(i).inside = preFaceVertices.get(i).get(j).southNeighbor.inside; + } + face_vertices.get(i).strengths.add(preFaceVertices.get(i).get(j).southNeighbor.bondStrength); + } + Pair pair = new Pair(preFaceVertices.get(i).get(j).tileLabel, preFaceVertices.get(i).get(j + 1).tileLabel); + face_vertices.get(i).edges.add(pair); + } + } + private void setUpEdgesDualGraph(){ + int thisone = 0; + for (faceVertex fv : face_vertices){ + for (Pair e : fv.edges){ + preConnectEdges(e, thisone); + } + thisone++; + } + } + private void preConnectEdges(Pair e, int which) { + int count = 0; + Pair e_reversed = new Pair(e.getValue(), e.getKey()); + for (faceVertex fc : face_vertices) { + if (count == which) { + + } else { + for (Pair vv : fc.edges) { + if (e_reversed.equals(vv) || e.equals(vv)){ + preCreateEdgeBetweenFaces(vv.getKey().toString(), vv.getValue().toString(), which, count); + } + } + } + count++; + } + } + private void preCreateEdgeBetweenFaces(String x, String y, int which, int count){ + int str = 0; + String firstName = ""; + String secondName = ""; + for (Vertex v : vertices){ + if (v.tileLabel == x) { + if (v.northNeighbor != null && v.northNeighbor.direction != null + && !v.northNeighbor.direction.isEmpty() + && v.northNeighbor.toVertex.tileLabel.equals(y)) { + str = v.northNeighbor.bondStrength; + v.northNeighbor.taken = 1; + firstName = v.tileLabel; + secondName = v.northNeighbor.toVertex.tileLabel; + } + if (v.eastNeighbor != null && v.eastNeighbor.direction != null + && !v.eastNeighbor.direction.isEmpty() + && v.eastNeighbor.toVertex.tileLabel.equals(y)) { + str = v.eastNeighbor.bondStrength; + v.eastNeighbor.taken = 1; + firstName = v.tileLabel; + secondName = v.eastNeighbor.toVertex.tileLabel; + } + if (v.westNeighbor != null && v.westNeighbor.direction != null + && !v.westNeighbor.direction.isEmpty() + && v.westNeighbor.toVertex.tileLabel.equals(y)) { + str = v.westNeighbor.bondStrength; + v.westNeighbor.taken = 1; + firstName = v.tileLabel; + secondName = v.westNeighbor.toVertex.tileLabel; + } + if (v.southNeighbor != null && v.southNeighbor.direction != null + && !v.southNeighbor.direction.isEmpty() + && v.southNeighbor.toVertex.tileLabel.equals(y)) { + str = v.southNeighbor.bondStrength; + v.southNeighbor.taken = 1; + firstName = v.tileLabel; + secondName = v.southNeighbor.toVertex.tileLabel; + } + break; + } + } + faceEdge e = new faceEdge(firstName, secondName, Integer.toString(which), face_vertices.get(count), str); + face_vertices.get(which).neighbors.add(e); + } + private void markBoundary(List path, boolean which){ + if (which){ + for (Vertex v : path){ + v.boundaryFlag = 1; + } + } + else{ + for (Vertex v : path){ + v.boundaryFlag = 0; + } + } + } + private int getIndexOfAttribute(Vertex v, int boundaryNum){ + if (!v.itsBoundary.isEmpty()){ + for (int i = 0; i < v.itsBoundary.size(); i++) { + if (v.itsBoundary.get(i).ofPath.equals(Integer.toString(boundaryNum))) { + return i; + } + } + return -1; + } + else{ + return 0; + } + } + private void insideWho(){ + int boundaryNum = 0; + int flag; + int index; + for (ArrayList v : preFaceVertices){ + markBoundary(v, true); + for(Vertex vv : v){ + flag = 0; + if (flag == 0 && vv.eastNeighbor != null && vv.eastNeighbor.direction != null + && !vv.eastNeighbor.direction.isEmpty() && vv.eastNeighbor.toVertex.boundaryFlag != 1){ + index = getIndexOfAttribute(vv, boundaryNum); + if (index != -1){ + if (vv.itsBoundary.get(index).Compare("E", "W")){ + vv.eastNeighbor.inside = boundaryNum; + DFSmarking(vv.eastNeighbor.toVertex, boundaryNum); + flag = 1; + } + } + } + if (flag == 0 && vv.westNeighbor != null && vv.westNeighbor.direction != null + && !vv.westNeighbor.direction.isEmpty() && vv.westNeighbor.toVertex.boundaryFlag != 1){ + index = getIndexOfAttribute(vv, boundaryNum); + if (index != -1){ + if (vv.itsBoundary.get(index).Compare("W", "E")){ + vv.westNeighbor.inside = boundaryNum; + DFSmarking(vv.westNeighbor.toVertex, boundaryNum); + flag = 1; + } + } + } + + if (flag == 0 && vv.northNeighbor != null && vv.northNeighbor.direction != null + && !vv.northNeighbor.direction.isEmpty() && vv.northNeighbor.toVertex.boundaryFlag != 1){ + index = getIndexOfAttribute(vv, boundaryNum); + if (index != -1){ + if (vv.itsBoundary.get(index).Compare("N", "S")){ + vv.northNeighbor.inside = boundaryNum; + DFSmarking(vv.northNeighbor.toVertex, boundaryNum); + flag = 1; + } + } + } + if (flag == 0 && vv.southNeighbor != null && vv.southNeighbor.direction != null + && !vv.southNeighbor.direction.isEmpty() && vv.southNeighbor.toVertex.boundaryFlag != 1){ + index = getIndexOfAttribute(vv, boundaryNum); + if (index != -1){ + if (vv.itsBoundary.get(index).Compare("S", "N")){ + vv.southNeighbor.inside = boundaryNum; + DFSmarking(vv.southNeighbor.toVertex, boundaryNum); + } + } + } + } + markBoundary(v, false); + boundaryNum++; + } + } + private void DFSmarking(Vertex v, int inside){ + ArrayList insiders = new ArrayList<>(); + + insiders.add(v); + do { + for (Vertex vv : insiders) { + if (vv.southNeighbor != null && vv.southNeighbor.direction != null + && !vv.southNeighbor.direction.isEmpty() && vv.southNeighbor.inside != inside) { + vv.southNeighbor.inside = inside; + if (vv.southNeighbor.toVertex.boundaryFlag != 1) { + insiders.add(vv.southNeighbor.toVertex); + } + } + if (vv.eastNeighbor != null && vv.eastNeighbor.direction != null + && !vv.eastNeighbor.direction.isEmpty() && vv.eastNeighbor.inside != inside) { + vv.eastNeighbor.inside = inside; + if (vv.eastNeighbor.toVertex.boundaryFlag != 1) { + insiders.add(vv.eastNeighbor.toVertex); + } + } + if (vv.westNeighbor != null && vv.westNeighbor.direction != null + && !vv.westNeighbor.direction.isEmpty() && vv.westNeighbor.inside != inside) { + vv.westNeighbor.inside = inside; + if (vv.westNeighbor.toVertex.boundaryFlag != 1) { + insiders.add(vv.westNeighbor.toVertex); + } + } + if (vv.northNeighbor != null && vv.northNeighbor.direction != null + && !vv.northNeighbor.direction.isEmpty() && vv.northNeighbor.inside != inside) { + vv.northNeighbor.inside = inside; + if (vv.northNeighbor.toVertex.boundaryFlag != 1) { + insiders.add(vv.northNeighbor.toVertex); + } + } + insiders.remove(insiders.indexOf(vv)); + break; + } + }while(!insiders.isEmpty()); + } + private void getFaces() { + ArrayList faces = new ArrayList(); + for (Vertex v : vertices) { + if (v.neighbors.size() == 1) { + } else { + for (Edge e : v.neighbors) { + if (e.direction.equals("N")) { + faces = theTrials(v, e); + if (!faces.isEmpty()){ + preFaceVertices.add(faces); + } + } + } + } + } + if (!preFaceVertices.isEmpty()){ + int flag = 0; + ArrayList> path = new ArrayList>(); + path.add(preFaceVertices.get(0)); + for (ArrayList firstPath : preFaceVertices){ + for (ArrayList secondPath : path){ + if (listEqualsIgnoreOrder(secondPath, firstPath)){ + flag = 1; + } + } + if (flag == 0){ + path.add(firstPath); + } + else{ + flag = 0; + } + } + preFaceVertices = path; + } + Collections.sort(preFaceVertices, new Comparator(){ + public int compare(ArrayList a1, ArrayList a2) { + return a2.size() - a1.size(); + } + }); + buildBoundaries(); + } + private void buildBoundaries(){ + int i = 0; + int j = 0; + for (ArrayList path : preFaceVertices){ + for (Vertex v : path){ + if (i >= path.size() - 1){ + + } + else{ + if (v.northNeighbor != null && v.northNeighbor.direction != null + && !v.northNeighbor.direction.isEmpty() && v.northNeighbor.toVertex == path.get(i + 1)){ + v.itsBoundary.add(new Border(Integer.toString(j), "W", "E")); + } + else if (v.westNeighbor != null && v.westNeighbor.direction != null + && !v.westNeighbor.direction.isEmpty() && v.westNeighbor.toVertex == path.get(i + 1)){ + v.itsBoundary.add(new Border(Integer.toString(j), "S", "N")); + } + else if (v.southNeighbor != null && v.southNeighbor.direction != null + && !v.southNeighbor.direction.isEmpty() && v.southNeighbor.toVertex == path.get(i + 1)){ + v.itsBoundary.add(new Border(Integer.toString(j), "E", "W")); + } + else if (v.eastNeighbor != null && v.eastNeighbor.direction != null + && !v.eastNeighbor.direction.isEmpty() && v.eastNeighbor.toVertex == path.get(i + 1)){ + v.itsBoundary.add(new Border(Integer.toString(j), "N", "S")); + } + i++; + } + } + j++; + i = 0; + } + } + private static boolean listEqualsIgnoreOrder(ArrayList list1, ArrayList list2) { + return new HashSet<>(list1).equals(new HashSet<>(list2)); + } + private int markLeafs() { + int minimum_edge_amount = 5; + for (Vertex v : vertices) { + if (v.edgeAmount == 1 && v.mark == 0) { + v.mark = 1; + markedOnes.add(v); + for (Edge neighbor : v.neighbors) { + if (neighbor.toVertex.mark == 0) { + neighbor.toVertex.edgeAmount -= 1; + } + } + } + } + if (markedOnes.size() == vertices.size() || vertices.size() == 1){ + return -1; + } + for (Vertex v : vertices) { + if (v.edgeAmount <= minimum_edge_amount && v.mark != 1) { + minimum_edge_amount = v.edgeAmount; + } + } + + return minimum_edge_amount; + } + private void removePathMarkings() { + for (Vertex v : vertices) { + v.pathMark = 0; + } + } + private ArrayList theTrials(Vertex v, Edge start) { + int get_out = 0; + if (v.northNeighbor != null && v.northNeighbor.direction != null + && !v.northNeighbor.direction.isEmpty() + && v.westNeighbor != null && v.westNeighbor.direction != null + && !v.westNeighbor.direction.isEmpty()){ + + } + else{ + get_out = 1; + } + Vertex transv = new Vertex(); + Vertex notTransv = new Vertex(); + int round = 0; + int trial = 0; + int flag = 0; + List goBack = new ArrayList(); + List goBackTrial = new ArrayList(); + + ArrayList finalPath = new ArrayList(); + finalPath.add(v); + transv = start.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + int i = 0; + do { + if (get_out == 1){ + break; + } + notTransv = transv; + if (trial == 0) { + if (transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + + && transv.eastNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.mark != 1 + + && transv.eastNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + if (round > 0){ + goBack.add(transv.eastNeighbor.toVertex); + goBackTrial.add(3); + } + goBack.add(transv.northNeighbor.toVertex); + goBackTrial.add(0); + transv = transv.westNeighbor.toVertex; + finalPath.add(transv); + trial = 1; + transv.pathMark = 1; + } + else if (transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.westNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.pathMark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.northNeighbor.toVertex); + goBackTrial.add(0); + transv = transv.westNeighbor.toVertex; + finalPath.add(transv); + trial = 1; + transv.pathMark = 1; + } + else if (transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.eastNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + if (round > 0){ + goBack.add(transv.eastNeighbor.toVertex); + goBackTrial.add(3); + } + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.eastNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + if (round > 0){ + goBack.add(transv.eastNeighbor.toVertex); + goBackTrial.add(3); + } + transv = transv.westNeighbor.toVertex; + finalPath.add(transv); + trial = 1; + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.westNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + transv = transv.westNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + trial = 1; + } + else if (transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.northNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (round > 0 && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.eastNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1) { + transv = transv.eastNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + trial = 3; + } + } else if (trial == 1) { + round++; + if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + + && transv.southNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.mark != 1 + + && transv.southNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.westNeighbor.toVertex); + goBack.add(transv.northNeighbor.toVertex); + goBackTrial.add(1); + goBackTrial.add(0); + transv = transv.southNeighbor.toVertex; + finalPath.add(transv); + trial = 2; + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + + && transv.southNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.mark != 1 + + && transv.southNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.westNeighbor.toVertex); + goBackTrial.add(1); + transv = transv.southNeighbor.toVertex; + finalPath.add(transv); + trial = 2; + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.northNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.northNeighbor.toVertex); + transv = transv.westNeighbor.toVertex; + goBackTrial.add(0); + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + + && transv.southNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.mark != 1 + + && transv.northNeighbor.toVertex.pathMark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.northNeighbor.toVertex); + transv = transv.southNeighbor.toVertex; + goBackTrial.add(0); + finalPath.add(transv); + trial = 2; + transv.pathMark = 1; + } + else if (transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.southNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + transv = transv.southNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + trial = 2; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.westNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + transv = transv.westNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.northNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + trial = 0; + } + } else if (trial == 2) { + round++; + if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + + && transv.eastNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.mark != 1 + + && transv.eastNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.westNeighbor.toVertex); + goBack.add(transv.southNeighbor.toVertex); + goBackTrial.add(1); + goBackTrial.add(2); + transv = transv.eastNeighbor.toVertex; + finalPath.add(transv); + trial = 3; + transv.pathMark = 1; + } + else if (transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + + && transv.eastNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.mark != 1 + + && transv.eastNeighbor.toVertex.pathMark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.southNeighbor.toVertex); + transv = transv.eastNeighbor.toVertex; + goBackTrial.add(2); + finalPath.add(transv); + trial = 3; + transv.pathMark = 1; + } + else if (transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + + && transv.northNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.mark != 1 + + && transv.northNeighbor.toVertex.pathMark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.northNeighbor.toVertex); + goBackTrial.add(0); + transv = transv.southNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + + && transv.westNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.mark != 1 + + && transv.eastNeighbor.toVertex.pathMark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.westNeighbor.toVertex); + transv = transv.eastNeighbor.toVertex; + goBackTrial.add(1); + finalPath.add(transv); + trial = 3; + transv.pathMark = 1; + } + else if (transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.eastNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1) { + transv = transv.eastNeighbor.toVertex; + finalPath.add(transv); + trial = 3; + transv.pathMark = 1; + } + else if (transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.southNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + transv = transv.southNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.westNeighbor.toVertex.mark != 1 + && transv.westNeighbor.toVertex.pathMark != 1) { + transv = transv.westNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + trial = 1; + } + } else if (trial == 3) { + round++; + if (transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + + && transv.northNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.mark != 1 + + && transv.northNeighbor.toVertex.pathMark != 1 + && transv.southNeighbor.toVertex.pathMark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.eastNeighbor.toVertex); + goBack.add(transv.southNeighbor.toVertex); + goBackTrial.add(3); + goBackTrial.add(2); + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + trial = 0; + transv.pathMark = 1; + } + else if (transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + + && transv.northNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.mark != 1 + + && transv.northNeighbor.toVertex.pathMark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.eastNeighbor.toVertex); + goBackTrial.add(3); + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + trial = 0; + transv.pathMark = 1; + } + else if (transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.southNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.pathMark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.southNeighbor.toVertex); + goBackTrial.add(2); + transv = transv.eastNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.westNeighbor != null && transv.westNeighbor.direction != null + && !transv.westNeighbor.direction.isEmpty() + && transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + + && transv.northNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.mark != 1 + + && transv.northNeighbor.toVertex.pathMark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + goBack.add(transv.southNeighbor.toVertex); + goBackTrial.add(2); + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + trial = 0; + transv.pathMark = 1; + } + else if (transv.northNeighbor != null && transv.northNeighbor.direction != null + && !transv.northNeighbor.direction.isEmpty() + && transv.northNeighbor.toVertex.mark != 1 + && transv.northNeighbor.toVertex.pathMark != 1) { + transv = transv.northNeighbor.toVertex; + finalPath.add(transv); + trial = 0; + } + else if (transv.eastNeighbor != null && transv.eastNeighbor.direction != null + && !transv.eastNeighbor.direction.isEmpty() + && transv.eastNeighbor.toVertex.mark != 1 + && transv.eastNeighbor.toVertex.pathMark != 1) { + transv = transv.eastNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + } + else if (transv.southNeighbor != null && transv.southNeighbor.direction != null + && !transv.southNeighbor.direction.isEmpty() + && transv.southNeighbor.toVertex.mark != 1 + && transv.southNeighbor.toVertex.pathMark != 1) { + transv = transv.southNeighbor.toVertex; + finalPath.add(transv); + transv.pathMark = 1; + trial = 2; + } + } + if (notTransv.equals(transv)){ + if (!goBack.isEmpty()){ + trial = goBackTrial.get(goBackTrial.size() - 1); + transv = goBack.get(goBack.size() - 1); + throwPathBack(finalPath, transv); + finalPath.add(transv); + transv.pathMark = 1; + goBack.remove(goBack.size() - 1); + goBackTrial.remove(goBackTrial.size() - 1); + i--; + } + } + i++; + if (i > vertices.size() * vertices.size()) { + flag = 1; + removePathMarkings(); + break; + } + } while (!transv.equals(v)); + removePathMarkings(); + if (flag == 1 || get_out == 1) { + for (Vertex d : finalPath){ + } + if (!goBack.isEmpty()){ + } + finalPath.clear(); + return finalPath; + } else { + return finalPath; + } + } + private void throwPathBack(ArrayList finalPath, Vertex transv){ + int i = 0; + if (!finalPath.isEmpty()) { + ListIterator ok = finalPath.listIterator(finalPath.size()); + while (ok.hasPrevious()) { + Vertex holder = ok.previous(); + + if (holder.eastNeighbor != null && holder.eastNeighbor.direction != null + && !holder.eastNeighbor.direction.isEmpty() + && holder.eastNeighbor.toVertex == transv) { + break; + } else if (holder.northNeighbor != null && holder.northNeighbor.direction != null + && !holder.northNeighbor.direction.isEmpty() + && holder.northNeighbor.toVertex == transv) { + break; + } else if (holder.westNeighbor != null && holder.westNeighbor.direction != null + && !holder.westNeighbor.direction.isEmpty() + && holder.westNeighbor.toVertex == transv) { + break; + } else if (holder.southNeighbor != null && holder.southNeighbor.direction != null + && !holder.southNeighbor.direction.isEmpty() + && holder.southNeighbor.toVertex == transv) { + break; + } + else{ + i++; + } + } + while (i > 0) { + finalPath.remove(finalPath.size() - 1); + i--; + } + } + } +} \ No newline at end of file From 1f86af80914d9d6ee1059f72cab0a8e6c4f0bcf2 Mon Sep 17 00:00:00 2001 From: Angel Adrian Cantu Date: Sat, 28 Apr 2018 15:33:04 -0500 Subject: [PATCH 2/2] MinCut: Final Reviews --- .../asarg/polysim/models/base/Assembly.java | 76 ++- .../base/{bondGraph.java => BondGraph.java} | 481 ++++++++---------- 2 files changed, 256 insertions(+), 301 deletions(-) rename src/main/java/com/asarg/polysim/models/base/{bondGraph.java => BondGraph.java} (82%) diff --git a/src/main/java/com/asarg/polysim/models/base/Assembly.java b/src/main/java/com/asarg/polysim/models/base/Assembly.java index 5f0d71d..c9f9b92 100644 --- a/src/main/java/com/asarg/polysim/models/base/Assembly.java +++ b/src/main/java/com/asarg/polysim/models/base/Assembly.java @@ -68,47 +68,41 @@ public Assembly(TileSystem ts) { frontier = new Frontier(tileSystem); } - public bondGraph getBondGraph(){ + public BondGraph getBondGraph(){ Set> tiles = Grid.entrySet(); - bondGraph bondGraph = new bondGraph(); + BondGraph bondGraph = new BondGraph(); for (Map.Entry mep : tiles) { Coordinate pt = mep.getKey(); Tile tile = mep.getValue(); if (tile.getGlueW() != null || tile.getGlueN() != null || tile.getGlueE() != null || tile.getGlueS() != null) { - bondGraph.addBondVertex(tile, tile.getLabel(), pt, tile.getGlueN(), - tile.getGlueS(), tile.getGlueW(), tile.getGlueE()); + bondGraph.addBondVertex(tile, pt); } } - for (bondGraph.Vertex v : bondGraph.vertices){ - for (bondGraph.Vertex vertex : bondGraph.vertices){ - if (v == vertex){ - - } - else{ - if (v.location.getEast().equals(vertex.location)){ - int strength = tileSystem.getStrength(v.E, vertex.W); - if (strength != 0){ - v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "E"); + for (BondGraph.Vertex v : bondGraph.getListOfVertices()){ + for (BondGraph.Vertex vertex : bondGraph.getListOfVertices()){ + if (!(v == vertex)) { + if (v.getLocation().getEast().equals(vertex.getLocation())) { + int strength = tileSystem.getStrength(v.getTile().getGlueE(), vertex.getTile().getGlueW()); + if (strength != 0) { + v.addEdge(v.getTile(), vertex.getTile(), vertex, strength, "E"); } - } - else if (v.location.getWest().equals(vertex.location)){ - int strength = tileSystem.getStrength(v.W, vertex.E); - if (strength != 0){ - v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "W"); + } else if (v.getLocation().getWest().equals(vertex.getLocation())) { + int strength = tileSystem.getStrength(v.getTile().getGlueW(), vertex.getTile().getGlueE()); + if (strength != 0) { + v.addEdge(v.getTile(), vertex.getTile(), vertex, strength, "W"); } - } - else if (v.location.getNorth().equals(vertex.location)){ - int strength = tileSystem.getStrength(v.N, vertex.S); - if (strength != 0){ - v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "N"); + } else if (v.getLocation().getNorth().equals(vertex.getLocation())) { + int strength = tileSystem.getStrength(v.getTile().getGlueN(), vertex.getTile().getGlueS()); + if (strength != 0) { + v.addEdge(v.getTile(), vertex.getTile(), vertex, strength, "N"); } - } - else if (v.location.getSouth().equals(vertex.location)){ - int strength = tileSystem.getStrength(v.S, vertex.N); - if (strength != 0){ - v.addEdge(v.thisTile, vertex.thisTile, vertex, strength, "S"); + } else if (v.getLocation().getSouth().equals(vertex.getLocation())) { + int strength = tileSystem.getStrength(v.getTile().getGlueS(), vertex.getTile().getGlueN()); + if (strength != 0) { + v.addEdge(v.getTile(), vertex.getTile(), vertex, strength, "S"); } + } } } @@ -132,28 +126,20 @@ public void changeTileSystem(TileSystem newTS) { public void changeTemperature(int temperature) { tileSystem.setTemperature(temperature); changeTileSystem(tileSystem); + getMinCut(); + } - //////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////// - //For now: - //Changing the temperature gets you the bond graph (works like a button) - bondGraph bond_graph = getBondGraph(); - //Also, computes MinCut on the bond graph (when needed) - //you must pass it the temperature of the system, so it knows what cut to return - ArrayList cutMe = bond_graph.getMinCut(tileSystem.getTemperature()); - //here we print the edges which are to be cut - //each edge in the list cutMe has tileFrom and tileTo, which are of type: Tile + public List getMinCut(){ + BondGraph bond_graph = getBondGraph(); + List cutMe = bond_graph.getMinCut(tileSystem.getTemperature()); if (!cutMe.isEmpty()){ - System.out.print("\nCut the following edges: "); - for (bondGraph.Edge e : cutMe){ - System.out.print("\n" + e.tileFrom.getLabel() + "->" + e.tileTo.getLabel()); + for (BondGraph.Edge e : cutMe){ + System.out.print("\n" + e.getTileFrom().getLabel() + "->" + e.getTileTo().getLabel()); } } - //////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////// System.out.print("\nSuccess"); + return cutMe; } - public void changeTileConfiguration(TileConfiguration tc) { tileSystem.loadTileConfiguration(tc); frontier.changeTileSystem(tileSystem); diff --git a/src/main/java/com/asarg/polysim/models/base/bondGraph.java b/src/main/java/com/asarg/polysim/models/base/BondGraph.java similarity index 82% rename from src/main/java/com/asarg/polysim/models/base/bondGraph.java rename to src/main/java/com/asarg/polysim/models/base/BondGraph.java index e844915..84b4c7d 100644 --- a/src/main/java/com/asarg/polysim/models/base/bondGraph.java +++ b/src/main/java/com/asarg/polysim/models/base/BondGraph.java @@ -5,33 +5,27 @@ /** * Created by Angel A. Cantu in 2018 */ -public class bondGraph { +public class BondGraph { private ArrayList> preFaceVertices = new ArrayList>(); private ArrayList face_vertices = new ArrayList<>(); - private List markedOnes = new ArrayList<>(); private int totalAmountOfEdges = 0; - public List vertices = new ArrayList<>(); + private List vertices = new ArrayList<>(); - public class Vertex { - public Tile thisTile; - public String tileLabel; - public String realTileLabel; - public String N; - public String W; - public String S; - public String E; - public Coordinate location; + public class Vertex { + private Tile thisTile; + private String vertexLabel; + private Coordinate location; private ArrayList itsBoundary = new ArrayList(); private List neighbors = new ArrayList<>(); - private int mark = 0; - private int pathMark = 0; - private int edgeAmount = 0; - private int boundaryFlag = 0; private Edge northNeighbor; private Edge southNeighbor; private Edge westNeighbor; private Edge eastNeighbor; + private int mark = 0; + private int pathMark = 0; + private int edgeAmount = 0; + private int boundaryFlag = 0; public void addEdge(Tile from, Tile to, Vertex b, int strength, String where) { totalAmountOfEdges++; neighbors.add(new Edge(from, to, b, strength, where)); @@ -47,10 +41,16 @@ public void addEdge(Tile from, Tile to, Vertex b, int strength, String where) { eastNeighbor = e; } } + public Coordinate getLocation() { + return location; + } + public Tile getTile() { + return thisTile; + } } public class Edge { - public Tile tileFrom; - public Tile tileTo; + private Tile tileFrom; + private Tile tileTo; private Vertex toVertex; private String direction = "X"; private int bondStrength = 0; @@ -63,13 +63,20 @@ private Edge(Tile from, Tile to, Vertex v, int strength, String dir) { bondStrength = strength; direction = dir; } + public Tile getTileFrom() { + return tileFrom; + } + + public Tile getTileTo() { + return tileTo; + } } private class faceVertex{ + private int inside = -1; private String label; private ArrayList> edges = new ArrayList<>(); private ArrayList neighbors = new ArrayList<>(); private ArrayList strengths = new ArrayList<>(); - private int inside = -1; private faceVertex(String name){ label = name; } @@ -90,14 +97,14 @@ private faceEdge(String x, String y, String from, faceVertex b, int strength) { } private class Border{ private String ofPath = "X"; - private Pair inout; + private Pair inAndOutBorder; private Border(String path, String in, String out){ ofPath = path; - inout = new Pair<>(in, out); + inAndOutBorder = new Pair<>(in, out); } private boolean Compare(String x, String y){ Pair c = new Pair<>(x, y); - if (inout.equals(c)){ + if (inAndOutBorder.equals(c)){ return true; } else { @@ -106,50 +113,65 @@ private boolean Compare(String x, String y){ } } private class bellmanGraph{ + private int amountOfV; + private int amountOfE; + private bellmanEdge edge[]; + private class bellmanEdge { - int src, dest, weight, flag, org_src, org_dest, id, redundantFlag; - bellmanEdge() { - src = dest = weight = flag = org_dest = org_src = id = redundantFlag = 0; + private int src; + private int dest; + private int weight; + private int flag; + private int org_src; + private int org_dest; + private int id; + private int redundantFlag; + private bellmanEdge() { + src = 0; + dest = 0; + weight = 0; + flag = 0; + org_dest = 0; + org_src = 0; + id = 0; + redundantFlag = 0; } } - int V; - int E; - bellmanEdge edge[]; - bellmanGraph(int v, int e) - { - V = v; - E = e; - edge = new bellmanEdge[e]; - for (int i=0; i getListOfVertices(){ + return vertices; + } + public void addBondVertex(Tile tile, Coordinate pt) { Vertex v = new Vertex(); - v.tileLabel = Integer.toString(vertices.size() - 1); + v.vertexLabel = Integer.toString(vertices.size() - 1); v.thisTile = tile; - v.realTileLabel = label; v.location = pt; - v.N = N; - v.S = S; - v.W = W; - v.E = E; vertices.add(v); } - public ArrayList getMinCut(int tao) { - handleLeafs(); - getFaces(); + public List getMinCut(int tao) { + boolean validAssembly = handleLeafs(); + if (!validAssembly){ + return new ArrayList(); + } + startGetFaces(); insideWho(); makeFaceVertices(); setUpEdgesDualGraph(); connectFaceEdges(false); connectFaceEdges(true); removeRedundantEdges(); - ArrayList cutMe = minCut(tao); + List cutMe = minCut(tao); return cutMe; } - private void handleLeafs(){ + private boolean handleLeafs(){ int amountNoFaces = 0; while (true) { int amount = markLeafs(); @@ -158,10 +180,11 @@ private void handleLeafs(){ } else if (amountNoFaces == vertices.size() || amount == -1){ System.out.print("INVALID ASSEMBLY || NO FACES"); - return; + return false; } amountNoFaces++; } + return true; } private void removeRedundantEdges(){ for (faceVertex fv : face_vertices){ @@ -183,32 +206,30 @@ private void removeRedundantEdges(){ } } } - private ArrayList minCut(int tao){ + private List minCut(int tao){ List s_Belonging = new ArrayList<>(); - List f_Belonging = new ArrayList<>(); + List t_Belonging = new ArrayList<>(); List path = new ArrayList<>(); for (Vertex s: vertices){ findFaceOf(s_Belonging, s); for (Vertex t : vertices){ - if (t.tileLabel == s.tileLabel){ - f_Belonging.clear(); + if (t.vertexLabel == s.vertexLabel){ + t_Belonging.clear(); } else{ List s_disjunction = new ArrayList<>(); s_disjunction.addAll(s_Belonging); - findFaceOf(f_Belonging, t); - s_disjunction.removeAll(f_Belonging); + findFaceOf(t_Belonging, t); + s_disjunction.removeAll(t_Belonging); - List f_disjunction = new ArrayList<>(); - f_disjunction.addAll(s_Belonging); - findFaceOf(f_Belonging, t); - f_disjunction.removeAll(f_Belonging); + List t_disjunction = new ArrayList<>(); + t_disjunction.addAll(s_Belonging); + findFaceOf(t_Belonging, t); + t_disjunction.removeAll(t_Belonging); for (Integer s_vertex : s_Belonging){ - for (Integer f_vertex : f_Belonging) { - if (f_vertex.equals(s_vertex)) { - - } else { + for (Integer f_vertex : t_Belonging) { + if (!(f_vertex.equals(s_vertex))) { if (s_vertex == -1){ s_vertex = face_vertices.size() - 1; } @@ -222,7 +243,8 @@ private ArrayList minCut(int tao){ for (Pair i : cutPath){ for (Vertex v : vertices){ for (Edge e : v.neighbors){ - if (i.getValue().equals(Integer.parseInt(e.toVertex.tileLabel)) && i.getKey().equals(Integer.parseInt(v.tileLabel))){ + if (i.getValue().equals(Integer.parseInt(e.toVertex.vertexLabel)) + && i.getKey().equals(Integer.parseInt(v.vertexLabel))){ cutMe.add(e); } } @@ -237,7 +259,7 @@ private ArrayList minCut(int tao){ } } } - f_Belonging.clear(); + t_Belonging.clear(); } s_Belonging.clear(); } @@ -246,7 +268,7 @@ private ArrayList minCut(int tao){ private void findFaceOf(List s_t_Belonging, Vertex s_t){ for (faceVertex fc : face_vertices){ for (faceEdge fe : fc.neighbors){ - if (s_t.tileLabel.equals(fe.original_source) || s_t.tileLabel.equals(fe.original_to)){ + if (s_t.vertexLabel.equals(fe.original_source) || s_t.vertexLabel.equals(fe.original_to)){ s_t_Belonging.add(Integer.parseInt(fc.label)); } } @@ -256,19 +278,12 @@ private void findFaceOf(List s_t_Belonging, Vertex s_t){ s_t_Belonging.clear(); s_t_Belonging.addAll(removeDup); } - private int bellmanFord(ArrayList cutPath, int source, List path, int dest, int tao){ - ArrayList>> bellman = new ArrayList>>(); - for (faceVertex fe : face_vertices){ - bellman.add(new ArrayList>()); - } - bellmanGraph graphOfBellmanFord = new bellmanGraph(face_vertices.size(), totalAmountOfEdges); + private void makeBellmanGraph(bellmanGraph graphOfBellmanFord){ int k = 0; for (faceVertex fc : face_vertices){ for (faceEdge fe : fc.neighbors){ - int neg_source = 0; - int neg_to = 0; - neg_source = Integer.parseInt(fe.source); - neg_to = Integer.parseInt(fe.toVertex.label); + int neg_source = Integer.parseInt(fe.source); + int neg_to = Integer.parseInt(fe.toVertex.label); if (fe.source == "-1"){ neg_source = face_vertices.size() - 1; @@ -293,8 +308,17 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d } } } - int amountVertices = graphOfBellmanFord.V; - int amountEdges = graphOfBellmanFord.E; + + } + private int bellmanFord(ArrayList cutPath, int source, List path, int dest, int tao){ + ArrayList>> bellman = new ArrayList>>(); + for (faceVertex _ : face_vertices){ + bellman.add(new ArrayList>()); + } + bellmanGraph graphOfBellmanFord = new bellmanGraph(face_vertices.size(), totalAmountOfEdges); + makeBellmanGraph(graphOfBellmanFord); + int amountVertices = graphOfBellmanFord.amountOfV; + int amountEdges = graphOfBellmanFord.amountOfE; int dist[] = new int[amountVertices]; int pred[] = new int[amountVertices]; ArrayList> cameFrom = new ArrayList>(); @@ -304,52 +328,46 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d cameFrom.add(new Pair<>(0, 0)); } dist[source] = 0; - pred[source] = -10; + pred[source] = -2; - for (int i = 1; i < amountVertices; ++i) - { - for (int j = 0; j < amountEdges; j++) - { + int firstFlag; + for (int i = 1; i < amountVertices; ++i){ + for (int j = 0; j < amountEdges; j++){ int u = graphOfBellmanFord.edge[j].src; int v = graphOfBellmanFord.edge[j].dest; int weight = graphOfBellmanFord.edge[j].weight; int id = graphOfBellmanFord.edge[j].id; - if (dist[u]!=Integer.MAX_VALUE && - dist[u]+weight p = new Pair<>(id, u); - int flagg = 0; + firstFlag = 0; ArrayList values = new ArrayList<>(); - - - for (Pair ppp : bellman.get(v)){ - if (Integer.valueOf((int)ppp.getKey()) == id){ - flagg = 1; + for (Pair p_ : bellman.get(v)){ + if (Integer.valueOf((int)p_.getKey()) == id){ + firstFlag = 1; } - values.add(Integer.valueOf((int)ppp.getValue())); + values.add(Integer.valueOf((int)p_.getValue())); } values.add(u); for (Integer val : values) { - for (Pair ppp : bellman.get(val)) { - if (Integer.valueOf((int)ppp.getKey()) == id){ - flagg = 1; + for (Pair p_ : bellman.get(val)) { + if (Integer.valueOf((int)p_.getKey()) == id){ + firstFlag = 1; } } } ArrayList> holder = new ArrayList>(); - if (flagg == 0){ - for (Pair ppp : bellman.get(v)){ - if (Integer.valueOf((int)ppp.getValue()) == u){ - - } - else{ - holder.add(ppp); + if (firstFlag == 0){ + for (Pair p_ : bellman.get(v)){ + if (!(Integer.valueOf((int)p_.getValue()) == u)){ + holder.add(p_); } } bellman.set(v, holder); bellman.get(v).add(p); } - if (flagg == 0){ + if (firstFlag == 0){ dist[v] = dist[u] + weight; pred[v] = u; } @@ -362,16 +380,16 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d } } for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ - int thing = 0; + int destroyFlag = 0; Pair destroyer = new Pair<>(e.org_dest, e.org_src); Pair reyortsed = new Pair<>(e.org_src, e.org_dest); for (Pair pp : cameFrom){ if (pp.getKey() == destroyer.getKey() && pp.getValue() == destroyer.getValue() || pp.getKey() == reyortsed.getKey() && pp.getValue() == reyortsed.getValue()){ - thing = 1; + destroyFlag = 1; } } - if (thing == 1){ + if (destroyFlag == 1){ e.flag = 2; } else{ @@ -381,31 +399,31 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d } } } - int counter = 0; - Iterator>> okok = bellman.iterator(); - ArrayList hol = new ArrayList<>(); - while (okok.hasNext()){ - ArrayList> chale = okok.next(); - Iterator> kkk = chale.iterator(); - while(kkk.hasNext()){ - Pair po = kkk.next(); - if (po.getValue() == pred[counter]){ - hol.add(po.getKey()); + int iterator = 0; + Iterator>> bellIterator = bellman.iterator(); + ArrayList bellBuffer = new ArrayList<>(); + while (bellIterator.hasNext()){ + ArrayList> nextBellIt = bellIterator.next(); + Iterator> bellItIterator = nextBellIt.iterator(); + while(bellItIterator.hasNext()){ + Pair po = bellItIterator.next(); + if (po.getValue() == pred[iterator]){ + bellBuffer.add(po.getKey()); } } - counter++; + iterator++; } ArrayList realPath = new ArrayList<>(); for (bellmanGraph.bellmanEdge e : graphOfBellmanFord.edge){ - for (Integer ahh : hol){ - if (e.id == ahh){ + for (Integer i : bellBuffer){ + if (e.id == i){ e.flag = 1; realPath.add(e.id); } } } dist[source] = Integer.MAX_VALUE; - int finalflag = 0; + int cycleFlag = 0; for (int j = 0; j < amountEdges; ++j) { int u = graphOfBellmanFord.edge[j].src; @@ -414,25 +432,25 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d int f = graphOfBellmanFord.edge[j].flag; int id = graphOfBellmanFord.edge[j].id; if (u != source && dist[u] != Integer.MAX_VALUE && - dist[u]+weight < tao && f != 1 && v == source) { + dist[u] + weight < tao && f != 1 && v == source) { dist[v] = dist[u] + weight; pred[v] = u; Pair pp = new Pair<>(id, u); bellman.get(v).add(pp); realPath.add(id); - finalflag = 1; + cycleFlag = 1; break; } } path.clear(); - int is_flag = 0; - for (Integer p : pred){ + int secondFlag = 0; + for (Integer _ : pred){ int previous = pred[dest]; if (previous == Integer.MAX_VALUE){ path.add(dest); if (dest == source){ - is_flag = 1; + secondFlag = 1; } break; } @@ -440,39 +458,25 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d path.add(previous); dest = previous; if (dest == source){ - is_flag = 1; + secondFlag = 1; } } dest = previous; - if (dest == -10){ + if (dest == -2){ return 0; } } - int countah = 1; - for (Integer ah : path){ - if (countah <= path.size() - 1){ - Iterator> last = bellman.get(ah).iterator(); - while(last.hasNext()){ - Pair po = last.next(); - if(po.getValue() == path.get(countah)){ - - } - } - } - countah++; - } - - ArrayList oneMoreTime = new ArrayList<>(); + ArrayList buffer = new ArrayList<>(); ArrayList idPath = new ArrayList<>(); for (int j = 1; j < path.size(); j++){ - oneMoreTime.add(path.get(j)); + buffer.add(path.get(j)); } for (int s = 0; s < path.size(); s++){ - if (s < oneMoreTime.size()){ + if (s < buffer.size()){ Iterator> com = bellman.get(path.get(s)).iterator(); while(com.hasNext()){ Pair po = com.next(); - if (po.getValue() == oneMoreTime.get(s)){ + if (po.getValue() == buffer.get(s)){ idPath.add(po.getKey()); } } @@ -500,36 +504,34 @@ private int bellmanFord(ArrayList cutPath, int source, List path, int d } } ArrayList indices = new ArrayList<>(); - int count = 0; + int firstCounter = 0; int flag = 0; for (Pair p : finalPath){ - int counttwo = 0; + int secondCounter = 0; for (Pair pp : finalPath){ - if (count == counttwo){ + if (firstCounter == secondCounter){ } else if (p.getValue() == pp.getKey() && p.getKey()== pp.getValue()){ for (Integer i : indices){ - if (i == counttwo){ + if (i == secondCounter){ flag = 1; } } if (flag == 0){ - indices.add(count); + indices.add(firstCounter); } flag = 0; } - counttwo++; + secondCounter++; } - count++; + firstCounter++; } - Set> thefinalPath = new HashSet<>(); - for (Integer i : indices){ - thefinalPath.add(finalPath.get(i)); - } - cutPath.addAll(thefinalPath); - if (finalflag == 1 && dist[source] < tao && is_flag != 0){ + if (cycleFlag == 1 && dist[source] < tao && secondFlag != 0){ + for (Integer i : indices){ + cutPath.add(finalPath.get(i)); + } return 1; } else{ @@ -546,20 +548,20 @@ private void connectFaceEdges(boolean x){ if (v.northNeighbor != null && v.northNeighbor.direction != null && !v.northNeighbor.direction.isEmpty() && v.northNeighbor.taken == 0){ - Pair check_pair = new Pair<>(v.tileLabel, v.northNeighbor.toVertex.tileLabel); - Pair r_check_pair = new Pair<>(v.northNeighbor.toVertex.tileLabel, v.tileLabel); + Pair check_pair = new Pair<>(v.vertexLabel, v.northNeighbor.toVertex.vertexLabel); + Pair r_check_pair = new Pair<>(v.northNeighbor.toVertex.vertexLabel, v.vertexLabel); for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (check_pair.equals(fe)){ if (v.northNeighbor.inside == -1){ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.northNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.northNeighbor.bondStrength)); - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.northNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.northNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.northNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.northNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.northNeighbor.bondStrength)); } else{ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.northNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.northNeighbor.inside), v.northNeighbor.bondStrength)); - face_vertices.get(v.northNeighbor.inside).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.northNeighbor.inside).label, fc, v.northNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.northNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(v.northNeighbor.inside), v.northNeighbor.bondStrength)); + face_vertices.get(v.northNeighbor.inside).neighbors.add(new faceEdge(v.northNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.northNeighbor.inside).label, fc, v.northNeighbor.bondStrength)); } } } @@ -568,21 +570,16 @@ private void connectFaceEdges(boolean x){ for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (r_check_pair.equals(fe)){ - if (v.northNeighbor.inside == -1){ - flag = 1; - } - else{ - flag = 1; - } + flag = 1; } } } if (flag == 0){ if (v.northNeighbor.inside == -1){ - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.northNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.northNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.northNeighbor.bondStrength)); } else{ - face_vertices.get(v.northNeighbor.inside).neighbors.add(new faceEdge(v.northNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.northNeighbor.inside).label, face_vertices.get(v.northNeighbor.inside), v.northNeighbor.bondStrength)); + face_vertices.get(v.northNeighbor.inside).neighbors.add(new faceEdge(v.northNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.northNeighbor.inside).label, face_vertices.get(v.northNeighbor.inside), v.northNeighbor.bondStrength)); } } @@ -592,20 +589,20 @@ private void connectFaceEdges(boolean x){ if (v.eastNeighbor != null && v.eastNeighbor.direction != null && !v.eastNeighbor.direction.isEmpty() && v.eastNeighbor.taken == 0){ - Pair check_pair = new Pair<>(v.tileLabel, v.eastNeighbor.toVertex.tileLabel); - Pair r_check_pair = new Pair<>(v.eastNeighbor.toVertex.tileLabel, v.tileLabel); + Pair check_pair = new Pair<>(v.vertexLabel, v.eastNeighbor.toVertex.vertexLabel); + Pair r_check_pair = new Pair<>(v.eastNeighbor.toVertex.vertexLabel, v.vertexLabel); for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (check_pair.equals(fe)){ if (v.eastNeighbor.inside == -1){ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.eastNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.eastNeighbor.bondStrength)); - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.eastNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.eastNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.eastNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.eastNeighbor.bondStrength)); } else{ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.eastNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.eastNeighbor.inside), v.eastNeighbor.bondStrength)); - face_vertices.get(v.eastNeighbor.inside).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.eastNeighbor.inside).label, fc, v.eastNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.eastNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(v.eastNeighbor.inside), v.eastNeighbor.bondStrength)); + face_vertices.get(v.eastNeighbor.inside).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.eastNeighbor.inside).label, fc, v.eastNeighbor.bondStrength)); } } } @@ -614,21 +611,16 @@ private void connectFaceEdges(boolean x){ for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (r_check_pair.equals(fe)){ - if (v.eastNeighbor.inside == -1){ - flag = 1; - } - else{ - flag = 1; - } + flag = 1; } } } if (flag == 0){ if (v.eastNeighbor.inside == -1){ - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.eastNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.eastNeighbor.bondStrength)); } else{ - face_vertices.get(v.eastNeighbor.inside).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.eastNeighbor.inside).label, face_vertices.get(v.eastNeighbor.inside), v.eastNeighbor.bondStrength)); + face_vertices.get(v.eastNeighbor.inside).neighbors.add(new faceEdge(v.eastNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.eastNeighbor.inside).label, face_vertices.get(v.eastNeighbor.inside), v.eastNeighbor.bondStrength)); } } @@ -638,20 +630,20 @@ private void connectFaceEdges(boolean x){ if (v.westNeighbor != null && v.westNeighbor.direction != null && !v.westNeighbor.direction.isEmpty() && v.westNeighbor.taken == 0){ - Pair check_pair = new Pair<>(v.tileLabel, v.westNeighbor.toVertex.tileLabel); - Pair r_check_pair = new Pair<>(v.westNeighbor.toVertex.tileLabel, v.tileLabel); + Pair check_pair = new Pair<>(v.vertexLabel, v.westNeighbor.toVertex.vertexLabel); + Pair r_check_pair = new Pair<>(v.westNeighbor.toVertex.vertexLabel, v.vertexLabel); for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (check_pair.equals(fe)){ if (v.westNeighbor.inside == -1){ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.westNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.westNeighbor.bondStrength)); - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.westNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.westNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.westNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.westNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.westNeighbor.bondStrength)); } else{ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.westNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.westNeighbor.inside), v.westNeighbor.bondStrength)); - face_vertices.get(v.westNeighbor.inside).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.westNeighbor.inside).label, fc, v.westNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.westNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(v.westNeighbor.inside), v.westNeighbor.bondStrength)); + face_vertices.get(v.westNeighbor.inside).neighbors.add(new faceEdge(v.westNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.westNeighbor.inside).label, fc, v.westNeighbor.bondStrength)); } } } @@ -660,21 +652,16 @@ private void connectFaceEdges(boolean x){ for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (r_check_pair.equals(fe)){ - if (v.westNeighbor.inside == -1){ - flag = 1; - } - else{ - flag = 1; - } + flag = 1; } } } if (flag == 0){ if (v.westNeighbor.inside == -1){ - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.westNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.westNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.westNeighbor.bondStrength)); } else{ - face_vertices.get(v.westNeighbor.inside).neighbors.add(new faceEdge(v.westNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.westNeighbor.inside).label, face_vertices.get(v.westNeighbor.inside), v.westNeighbor.bondStrength)); + face_vertices.get(v.westNeighbor.inside).neighbors.add(new faceEdge(v.westNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.westNeighbor.inside).label, face_vertices.get(v.westNeighbor.inside), v.westNeighbor.bondStrength)); } } @@ -684,20 +671,20 @@ private void connectFaceEdges(boolean x){ if (v.southNeighbor != null && v.southNeighbor.direction != null && !v.southNeighbor.direction.isEmpty() && v.southNeighbor.taken == 0){ - Pair check_pair = new Pair<>(v.tileLabel, v.southNeighbor.toVertex.tileLabel); - Pair r_check_pair = new Pair<>(v.southNeighbor.toVertex.tileLabel, v.tileLabel); + Pair check_pair = new Pair<>(v.vertexLabel, v.southNeighbor.toVertex.vertexLabel); + Pair r_check_pair = new Pair<>(v.southNeighbor.toVertex.vertexLabel, v.vertexLabel); for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (check_pair.equals(fe)){ if (v.southNeighbor.inside == -1){ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.southNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.southNeighbor.bondStrength)); - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.southNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.southNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(face_vertices.size() - 1), v.southNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.southNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, fc, v.southNeighbor.bondStrength)); } else{ flag = 1; - fc.neighbors.add(new faceEdge(v.tileLabel, v.southNeighbor.toVertex.tileLabel, fc.label, face_vertices.get(v.southNeighbor.inside), v.southNeighbor.bondStrength)); - face_vertices.get(v.southNeighbor.inside).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.southNeighbor.inside).label, fc, v.southNeighbor.bondStrength)); + fc.neighbors.add(new faceEdge(v.vertexLabel, v.southNeighbor.toVertex.vertexLabel, fc.label, face_vertices.get(v.southNeighbor.inside), v.southNeighbor.bondStrength)); + face_vertices.get(v.southNeighbor.inside).neighbors.add(new faceEdge(v.southNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.southNeighbor.inside).label, fc, v.southNeighbor.bondStrength)); } } } @@ -706,21 +693,16 @@ private void connectFaceEdges(boolean x){ for (faceVertex fc : face_vertices){ for (Pair fe : fc.edges){ if (r_check_pair.equals(fe)){ - if (v.southNeighbor.inside == -1){ - flag = 1; - } - else{ - flag = 1; - } + flag = 1; } } } if (flag == 0){ if (v.southNeighbor.inside == -1){ - face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.southNeighbor.bondStrength)); + face_vertices.get(face_vertices.size() - 1).neighbors.add(new faceEdge(v.southNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(face_vertices.size() - 1).label, face_vertices.get(face_vertices.size() - 1), v.southNeighbor.bondStrength)); } else{ - face_vertices.get(v.southNeighbor.inside).neighbors.add(new faceEdge(v.southNeighbor.toVertex.tileLabel, v.tileLabel, face_vertices.get(v.southNeighbor.inside).label, face_vertices.get(v.southNeighbor.inside), v.southNeighbor.bondStrength)); + face_vertices.get(v.southNeighbor.inside).neighbors.add(new faceEdge(v.southNeighbor.toVertex.vertexLabel, v.vertexLabel, face_vertices.get(v.southNeighbor.inside).label, face_vertices.get(v.southNeighbor.inside), v.southNeighbor.bondStrength)); } } @@ -773,33 +755,31 @@ private void makeFaceEdges(int i){ } face_vertices.get(i).strengths.add(preFaceVertices.get(i).get(j).southNeighbor.bondStrength); } - Pair pair = new Pair(preFaceVertices.get(i).get(j).tileLabel, preFaceVertices.get(i).get(j + 1).tileLabel); + Pair pair = new Pair(preFaceVertices.get(i).get(j).vertexLabel, preFaceVertices.get(i).get(j + 1).vertexLabel); face_vertices.get(i).edges.add(pair); } } private void setUpEdgesDualGraph(){ - int thisone = 0; + int iterator = 0; for (faceVertex fv : face_vertices){ for (Pair e : fv.edges){ - preConnectEdges(e, thisone); + preConnectEdges(e, iterator); } - thisone++; + iterator++; } } private void preConnectEdges(Pair e, int which) { - int count = 0; + int iterator = 0; Pair e_reversed = new Pair(e.getValue(), e.getKey()); for (faceVertex fc : face_vertices) { - if (count == which) { - - } else { + if (!(iterator == which)) { for (Pair vv : fc.edges) { if (e_reversed.equals(vv) || e.equals(vv)){ - preCreateEdgeBetweenFaces(vv.getKey().toString(), vv.getValue().toString(), which, count); + preCreateEdgeBetweenFaces(vv.getKey().toString(), vv.getValue().toString(), which, iterator); } } } - count++; + iterator++; } } private void preCreateEdgeBetweenFaces(String x, String y, int which, int count){ @@ -807,38 +787,38 @@ private void preCreateEdgeBetweenFaces(String x, String y, int which, int count) String firstName = ""; String secondName = ""; for (Vertex v : vertices){ - if (v.tileLabel == x) { + if (v.vertexLabel == x) { if (v.northNeighbor != null && v.northNeighbor.direction != null && !v.northNeighbor.direction.isEmpty() - && v.northNeighbor.toVertex.tileLabel.equals(y)) { + && v.northNeighbor.toVertex.vertexLabel.equals(y)) { str = v.northNeighbor.bondStrength; v.northNeighbor.taken = 1; - firstName = v.tileLabel; - secondName = v.northNeighbor.toVertex.tileLabel; + firstName = v.vertexLabel; + secondName = v.northNeighbor.toVertex.vertexLabel; } if (v.eastNeighbor != null && v.eastNeighbor.direction != null && !v.eastNeighbor.direction.isEmpty() - && v.eastNeighbor.toVertex.tileLabel.equals(y)) { + && v.eastNeighbor.toVertex.vertexLabel.equals(y)) { str = v.eastNeighbor.bondStrength; v.eastNeighbor.taken = 1; - firstName = v.tileLabel; - secondName = v.eastNeighbor.toVertex.tileLabel; + firstName = v.vertexLabel; + secondName = v.eastNeighbor.toVertex.vertexLabel; } if (v.westNeighbor != null && v.westNeighbor.direction != null && !v.westNeighbor.direction.isEmpty() - && v.westNeighbor.toVertex.tileLabel.equals(y)) { + && v.westNeighbor.toVertex.vertexLabel.equals(y)) { str = v.westNeighbor.bondStrength; v.westNeighbor.taken = 1; - firstName = v.tileLabel; - secondName = v.westNeighbor.toVertex.tileLabel; + firstName = v.vertexLabel; + secondName = v.westNeighbor.toVertex.vertexLabel; } if (v.southNeighbor != null && v.southNeighbor.direction != null && !v.southNeighbor.direction.isEmpty() - && v.southNeighbor.toVertex.tileLabel.equals(y)) { + && v.southNeighbor.toVertex.vertexLabel.equals(y)) { str = v.southNeighbor.bondStrength; v.southNeighbor.taken = 1; - firstName = v.tileLabel; - secondName = v.southNeighbor.toVertex.tileLabel; + firstName = v.vertexLabel; + secondName = v.southNeighbor.toVertex.vertexLabel; } break; } @@ -930,7 +910,6 @@ private void insideWho(){ } private void DFSmarking(Vertex v, int inside){ ArrayList insiders = new ArrayList<>(); - insiders.add(v); do { for (Vertex vv : insiders) { @@ -967,14 +946,13 @@ private void DFSmarking(Vertex v, int inside){ } }while(!insiders.isEmpty()); } - private void getFaces() { + private void startGetFaces() { ArrayList faces = new ArrayList(); for (Vertex v : vertices) { - if (v.neighbors.size() == 1) { - } else { + if (!(v.neighbors.size() == 1)) { for (Edge e : v.neighbors) { if (e.direction.equals("N")) { - faces = theTrials(v, e); + faces = getFaces(v, e); if (!faces.isEmpty()){ preFaceVertices.add(faces); } @@ -1013,10 +991,7 @@ private void buildBoundaries(){ int j = 0; for (ArrayList path : preFaceVertices){ for (Vertex v : path){ - if (i >= path.size() - 1){ - - } - else{ + if (!(i >= path.size() - 1)){ if (v.northNeighbor != null && v.northNeighbor.direction != null && !v.northNeighbor.direction.isEmpty() && v.northNeighbor.toVertex == path.get(i + 1)){ v.itsBoundary.add(new Border(Integer.toString(j), "W", "E")); @@ -1045,6 +1020,7 @@ private static boolean listEqualsIgnoreOrder(ArrayList list1, ArrayList markedOnes = new ArrayList<>(); for (Vertex v : vertices) { if (v.edgeAmount == 1 && v.mark == 0) { v.mark = 1; @@ -1064,7 +1040,6 @@ private int markLeafs() { minimum_edge_amount = v.edgeAmount; } } - return minimum_edge_amount; } private void removePathMarkings() { @@ -1072,19 +1047,18 @@ private void removePathMarkings() { v.pathMark = 0; } } - private ArrayList theTrials(Vertex v, Edge start) { + private ArrayList getFaces(Vertex v, Edge start) { int get_out = 0; if (v.northNeighbor != null && v.northNeighbor.direction != null && !v.northNeighbor.direction.isEmpty() && v.westNeighbor != null && v.westNeighbor.direction != null && !v.westNeighbor.direction.isEmpty()){ - } else{ get_out = 1; } - Vertex transv = new Vertex(); - Vertex notTransv = new Vertex(); + Vertex transv; + Vertex notTransv; int round = 0; int trial = 0; int flag = 0; @@ -1516,7 +1490,7 @@ else if (transv.southNeighbor != null && transv.southNeighbor.direction != null if (!goBack.isEmpty()){ trial = goBackTrial.get(goBackTrial.size() - 1); transv = goBack.get(goBack.size() - 1); - throwPathBack(finalPath, transv); + faceFinderReturn(finalPath, transv); finalPath.add(transv); transv.pathMark = 1; goBack.remove(goBack.size() - 1); @@ -1533,23 +1507,18 @@ else if (transv.southNeighbor != null && transv.southNeighbor.direction != null } while (!transv.equals(v)); removePathMarkings(); if (flag == 1 || get_out == 1) { - for (Vertex d : finalPath){ - } - if (!goBack.isEmpty()){ - } finalPath.clear(); return finalPath; } else { return finalPath; } } - private void throwPathBack(ArrayList finalPath, Vertex transv){ + private void faceFinderReturn(ArrayList finalPath, Vertex transv){ int i = 0; if (!finalPath.isEmpty()) { ListIterator ok = finalPath.listIterator(finalPath.size()); while (ok.hasPrevious()) { Vertex holder = ok.previous(); - if (holder.eastNeighbor != null && holder.eastNeighbor.direction != null && !holder.eastNeighbor.direction.isEmpty() && holder.eastNeighbor.toVertex == transv) {