public MapFileReader(String path) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File(path.replace(".data", ".xml"))); NodeList nList = document.getElementsByTagName("map"); background = ((Element) nList.item(0)).getElementsByTagName("File").item(0).getTextContent(); nList = document.getElementsByTagName("vertex"); GraphModel g2 = new GraphModel(false); g2.setAllowLoops(true); Vertex root = new Vertex(); root.setLocation(new GraphPoint(0, 0)); g2.addVertex(root); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; boolean isRoot = eElement.getAttribute("type").equals("root"); String id = eElement.getAttribute("id"); int x = Integer.parseInt(eElement.getElementsByTagName("x").item(0).getTextContent()); int y = Integer.parseInt(eElement.getElementsByTagName("y").item(0).getTextContent()); int value = 0; Vertex newVertex = new Vertex(); newVertex.setLocation(new GraphPoint(x, y)); newVertex.setLabel(id); if (!isRoot) { g2.addVertex(newVertex); value = Integer.parseInt(eElement.getElementsByTagName("value").item(0).getTextContent()); Edge e = new Edge(newVertex, root); e.setWeight(value); g2.addEdge(e); } else { root.setLocation(newVertex.getLocation()); root.setLabel(newVertex.getLabel()); // root = newVertex; } } } this.setGraph(g2); } catch (DOMException | ParserConfigurationException | SAXException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static boolean IncidentToReqEdge(GraphModel g, int Vertex) { for (Edge e : g.edges(g.getVertex(Vertex))) if (e.getWeight() == 1) return true; return false; }
public static int color( int Coloring, int Ordering, int Mode, int RequiredPattern, int rho, int blockSize, File file) { int Mode2 = 0; if (Ordering != 0) { if (Coloring == 3) { Ordering += 3; } } if (Mode == 3) { if (rho != 0) { Mode = 2 * rho; } } if (Mode == 0) { if (rho != 0) { Mode2 = 2 * rho; } } Matrix mm = new Matrix(5, 5); try { mm = MM.loadMatrixFromSPARSE(file); } catch (IOException e) { e.printStackTrace(); } int rows = mm.getRowDimension(); int cols = mm.getColumnDimension(); GraphModel g = new GraphModel(false); Vector<Integer> V_r = new Vector<>(); Vector<Integer> V_c = new Vector<>(); for (int i = 0; i < rows; i++) { Vertex v = new Vertex(); v.setColor(-1); g.addVertex(v); V_r.add(v.getId()); } for (int i = 0; i < cols; i++) { Vertex v = new Vertex(); v.setColor(-1); g.addVertex(v); V_c.add(v.getId()); } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (mm.get(i, j) == 1) { g.addEdge(new Edge(g.getVertex(i), g.getVertex(rows + j))); } } } // new GraphData(Application.getBlackBoard()).core.showGraph(g); // Initialize required pattern int entries_pattern = 0; // If edge e \in E_S then edge_property edge_weight=1 else switch (RequiredPattern) { case 0: break; case 1: for (Edge e : g.getEdges()) { if (Math.abs(e.source.getId() - e.target.getId()) == rows) { e.setWeight(1); entries_pattern++; } } break; case 2: for (Edge e : g.getEdges()) { e.setWeight(1); entries_pattern++; } break; case 3: for (Edge e : g.getEdges()) { int RowCoordinate = e.source.getId() + rows; int ColumnCoordinate = e.target.getId(); int RelativeDistance = RowCoordinate - ColumnCoordinate; int RowBlock = RowCoordinate / blockSize; int ColumnBlock = ColumnCoordinate / blockSize; if ((RelativeDistance < blockSize) && (RelativeDistance > -blockSize) && (RowBlock == ColumnBlock)) { e.setWeight(1); entries_pattern++; } } break; } // Presorting of the vertices if (Coloring == 1 || Coloring == 2 || Coloring == 3 || Coloring == 4 || Coloring == 5 || Coloring == 6) { switch (Ordering) { case 0: break; case 1: if (Coloring == 1) { V_c = OrderingHeuristics.LFO(g, V_c); } else if (Coloring == 2) { V_r = OrderingHeuristics.LFO(g, V_r); } else if (Coloring == 5 || Coloring == 6) { V_r = OrderingHeuristics.LFO(g, V_r); V_c = OrderingHeuristics.LFO(g, V_c); } break; case 2: if (Coloring == 1) { V_c = OrderingHeuristics.SLO(g, V_c); } else if (Coloring == 2) { V_r = OrderingHeuristics.SLO(g, V_r); } else if (Coloring == 5) { V_r = OrderingHeuristics.SLO(g, V_r); V_c = OrderingHeuristics.SLO(g, V_c); } break; case 3: if (Coloring == 1) { V_c = OrderingHeuristics.IDO(g, V_c); } else if (Coloring == 2) { V_r = OrderingHeuristics.IDO(g, V_r); } else if (Coloring == 5) { V_r = OrderingHeuristics.IDO(g, V_r); V_c = OrderingHeuristics.IDO(g, V_c); } break; case 4: if (Coloring == 3) { V_c = OrderingHeuristics.LFOrestricted(g, V_c); } else if (Coloring == 4) { V_r = OrderingHeuristics.LFOrestricted(g, V_r); } else if (Coloring == 6) { V_r = OrderingHeuristics.LFOrestricted(g, V_r); V_c = OrderingHeuristics.LFOrestricted(g, V_c); } break; case 5: if (Coloring == 3) { V_c = OrderingHeuristics.SLOrestricted(g, V_c); } else if (Coloring == 4) { V_r = OrderingHeuristics.SLOrestricted(g, V_r); } else if (Coloring == 6) { V_r = OrderingHeuristics.SLOrestricted(g, V_r); V_c = OrderingHeuristics.SLOrestricted(g, V_c); } break; case 6: if (Coloring == 3) { V_c = OrderingHeuristics.IDOrestricted(g, V_c); } else if (Coloring == 4) { V_r = OrderingHeuristics.IDOrestricted(g, V_r); } else if (Coloring == 6) { V_r = OrderingHeuristics.IDOrestricted(g, V_r); V_c = OrderingHeuristics.IDOrestricted(g, V_c); } break; } } int num_colors = 0; // Coloring of the vertices switch (Coloring) { case 1: System.out.println("PartialD2ColoringCols"); num_colors = ColorAlgs.PartialD2Coloring(g, V_c); break; case 2: System.out.println("PartialD2ColoringRows"); num_colors = ColorAlgs.PartialD2Coloring(g, V_r); break; case 3: System.out.println("PartialD2ColoringRestrictedCols"); num_colors = ColorAlgs.PartialD2ColoringRestricted(g, V_c); break; case 4: System.out.println("PartialD2ColoringRestrictedRows"); num_colors = ColorAlgs.PartialD2ColoringRestricted(g, V_r); break; case 5: System.out.println("StarBicoloringScheme"); num_colors = ColorAlgs.StarBicoloringScheme(g, V_r, V_c, Mode, Mode2); break; case 6: System.out.println("StarBicoloringSchemeRestricted " + V_r.size() + " " + V_c.size()); num_colors = ColorAlgs.StarBicoloringSchemeRestricted(g, V_r, V_c, Mode, Mode2); break; case 7: System.out.println("StarBicoloringSchemeDynamicOrdering"); num_colors = ColorAlgs.StarBicoloringSchemeDynamicOrdering(g, V_r, V_c, Mode, Ordering, Mode2); break; case 8: System.out.println("StarBicoloringSchemeCombinedVertexCoverColoring"); num_colors = ColorAlgs.StarBicoloringSchemeCombinedVertexCoverColoring(g, V_r, V_c, Mode); break; case 9: System.out.println("StarBicoloringSchemeDynamicOrderingRestricted"); num_colors = ColorAlgs.StarBicoloringSchemeDynamicOrderingRestricted( g, V_r, V_c, Mode, Ordering, Mode2); break; case 10: System.out.println("StarBicoloringSchemeCombinedVertexCoverColoringRestricted"); num_colors = ColorAlgs.StarBicoloringSchemeCombinedVertexCoverColoringRestricted(g, V_r, V_c, Mode); break; } return num_colors; }