Ejemplo n.º 1
0
  /**
   * Compute a distance-2 coloring of a bipartite graph G_b
   *
   * <p>Input: - G_b bipartite graph - V contained vertices are colored in the given ordering v_1,
   * ..., v_n
   *
   * <p>Output: - G_b bipartite graph with colors as weights vertex_color
   */
  public static int PartialD2Coloring(GraphModel g, Vector<Integer> V) {
    Vector<Integer> N2 = new Vector<>();
    Vector<Integer> forbidden = new Vector<>(g.numOfVertices());
    for (int i = 0; i < g.numOfVertices(); i++) forbidden.add(-1);
    for (int v : V) g.getVertex(v).setColor(0);
    for (int v : V) {
      if (g.getDegree(g.getVertex(v)) > 0) {
        forbidden.set(0, v);
        N2 = Neighbors.N_2(g, v);
        for (int n2 : N2) {
          if (g.getVertex(n2).getColor() > 0) {
            forbidden.set(g.getVertex(n2).getColor(), v);
          }
        }
        for (int i = 0; i < forbidden.size(); i++) {
          if (forbidden.get(i) != v) {
            g.getVertex(v).setColor(i);
            break;
          }
        }
      } else {
        g.getVertex(v).setColor(0);
      }
    }

    return getNumOfCols(g);
  }
Ejemplo n.º 2
0
  public static boolean StarBicoloringDynamicOrdering(
      GraphModel g, Vector<Integer> V_r, Vector<Integer> V_c, int Ordering) {
    Vector<Integer> forbiddenColors = new Vector<>(V_r.size());
    for (int i = 0; i < V_r.size(); i++) forbiddenColors.add(-1);
    Vector<Integer> Vertices = new Vector<>(V_r.size() + V_c.size());

    // GraphModel gg = g.getCopy();
    for (int i = 0; i < V_r.size(); i++) {
      Vertices.add(g.getVertex(i).getId());
    }

    for (int i = 0; i < V_c.size(); i++) {
      Vertices.add(g.getVertex(i).getId());
    }

    if (Ordering != 0) {
      if (Ordering == 1) {
        Vertices = OrderingHeuristics.LFO(g, Vertices);
      }
      if (Ordering == 2) {
        Vertices = OrderingHeuristics.SLO(g, Vertices);
      }
      if (Ordering == 3) {
        Vertices = OrderingHeuristics.IDO(g, Vertices);
      }
    }

    for (int v : Vertices) {
      if (g.getVertex(v).getColor() != 0) {
        forbiddenColors.set(0, v);
        for (Vertex w : g.getNeighbors(g.getVertex(v))) {
          if (w.getColor() <= 0) {
            for (Vertex x : g.getNeighbors(w)) {
              if (x.getColor() > 0) forbiddenColors.set(x.getColor(), v);
            }
          } else {
            for (Vertex x : g.getNeighbors(w)) {
              if (x.getColor() > 0) {
                for (Vertex y : g.getNeighbors(x)) {
                  if (y.getColor() > 0) {
                    if (w.getColor() == y.getColor() && w.getId() != y.getId()) {
                      forbiddenColors.set(x.getColor(), v);
                    }
                  }
                }
              }
            }
          }
        }
      }

      for (int i = 0; i < forbiddenColors.size(); i++) {
        if (forbiddenColors.get(i) != v) g.getVertex(v).setColor(i);
      }
    }
    return true;
  }
Ejemplo n.º 3
0
  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();
    }
  }
Ejemplo n.º 4
0
  /**
   * Compute a star bicoloring of a bipartite graph G_b restricted to the required edges (this
   * version was implemented for Michael Luelfesmann's diploma thesis)
   *
   * <p>Input: - G_b bipartite graph with required egdes given as weights edge_weight - V_r
   * contained row vertices are colored in the given ordering v_1, ..., v_n - V_c contained column
   * vertices are colored in the given ordering v_1, ..., v_n - Mode parameter \rho (\rho=Mode/2) -
   * Mode2 parameter \rho if using independent set algorithm of Coleman (Mode==1)
   *
   * <p>Output: - G_b bipartite graph with colors given as weights vertex_color
   */
  public static int StarBicoloringSchemeRestricted(
      GraphModel g, Vector<Integer> V_r, Vector<Integer> V_c, int Mode, int Mode2) {
    Vector<Integer> IS = new Vector<>();

    // Compute independent set
    System.out.println("Mode " + Mode);
    if (Mode == 1) {
      IS = IndSet.ISetRestricted(g, V_r, V_c, Mode2); // ISet = IS_Coleman(G_b,V_r,V_c);
    } else if (Mode == 2) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 1.0f);
    } else if (Mode == 3) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 1.5f);
    } else if (Mode == 4) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 2);
    } else if (Mode == 5) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 2.5f);
    } else if (Mode == 6) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 3.0f);
    } else if (Mode == 7) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 3.5f);
    }

    // Color vertices in independent set with color 0
    for (int IS_it : IS) {
      g.getVertex(IS_it).setColor(0);
    }

    // Color all vertices which are not colored
    StarBicoloringRestricted(g, V_r, V_c);

    return getNumOfCols(g);
  }
Ejemplo n.º 5
0
  public Object calculate(GraphModel g) {
    SubGraph sg = new SubGraph();
    MSTPrim mp = new MSTPrim();
    double[][] adj = g.getAdjacencyMatrix().getArray();
    int[][] adjMatrix = new int[g.getVerticesCount()][g.getVerticesCount()];

    for (int i = 0; i < g.getVerticesCount(); i++) {
      for (int j = 0; j < g.getVerticesCount(); j++) {
        if (adj[i][j] == 0) adjMatrix[i][j] = 0;
        else adjMatrix[i][j] = 1;
      }
    }
    int[] parent = mp.prim(adjMatrix);
    for (int i = 0; i < g.getVerticesCount(); i++) {
      if (parent[i] != -1) {
        sg.edges.add(g.getEdge(g.getVertex(i), g.getVertex(parent[i])));
      }
    }
    return sg;
  }
 public static String getEigenValues(GraphModel g) {
   Matrix A = g.getWeightedAdjacencyMatrix();
   EigenvalueDecomposition ed = A.eig();
   double rv[] = ed.getRealEigenvalues();
   double iv[] = ed.getImagEigenvalues();
   String res = "";
   for (int i = 0; i < rv.length; i++) {
     if (iv[i] != 0) res += "" + round(rv[i], 3) + " + " + round(iv[i], 3) + "i";
     else res += "" + round(rv[i], 3);
     if (i != rv.length - 1) {
       res += ",";
     }
   }
   return res;
 }
Ejemplo n.º 7
0
  public static int StarBicoloringSchemeDynamicOrderingRestricted(
      GraphModel g, Vector<Integer> V_r, Vector<Integer> V_c, int Mode, int sort, int Mode2) {
    Vector<Integer> IS = new Vector<>();
    Vector<Integer> num_colors = new Vector<>();

    // Compute independent set
    if (Mode == 1) {
      IS = IndSet.ISetRestricted(g, V_r, V_c, Mode2); // ISet = IS_Coleman(G_b,V_r,V_c);
    } else if (Mode == 2) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 1.0f);
    } else if (Mode == 3) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 1.5f);
    } else if (Mode == 4) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 2);
    } else if (Mode == 5) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 2.5f);
    } else if (Mode == 6) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 3.0f);
    } else if (Mode == 7) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 3.5f);
    } else if (Mode == 8) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 4.0f);
    } else if (Mode == 9) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 4.5f);
    } else if (Mode == 10) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 5.0f);
    } else if (Mode == 11) {
      IS = IndSet.ISetVariantRestricted(g, V_r, V_c, 5.5f);
    }

    // Color vertices in independent set with color 0
    for (int IS_it : IS) {
      g.getVertex(IS_it).setColor(0);
    }

    // Color all vertices which are not colored
    StarBicoloringDynamicOrderingRestricted(g, V_r, V_c, sort);
    return getNumOfCols(g);
  }
Ejemplo n.º 8
0
  public Object calculate(GraphModel g) {
    ZagrebIndexFunctions zif = new ZagrebIndexFunctions(g);
    ZagrebIndexFunctions zifL = new ZagrebIndexFunctions(LineGraph.createLineGraph(g));

    RendTable ret = new RendTable();
    ret.add(new Vector<Object>());
    ret.get(0).add(" M^3_1(G) ");
    ret.get(0).add("Z1");
    ret.get(0).add("Z2");
    ret.get(0).add("Z3");
    ret.get(0).add("Z4");
    ret.get(0).add("Ep3");
    ret.get(0).add("Ep4");
    ret.get(0).add("Psi1");
    ret.get(0).add("Psi2");
    ret.get(0).add("illc");
    ret.get(0).add("N2");
    ret.get(0).add("N1");
    double maxDeg = 0;
    double maxDeg2 = 0;
    double minDeg = Integer.MAX_VALUE;

    ArrayList<Integer> al = AlgorithmUtils.getDegreesList(g);
    Collections.sort(al);
    maxDeg = al.get(al.size() - 1);
    if (al.size() - 2 >= 0) maxDeg2 = al.get(al.size() - 2);
    else maxDeg2 = maxDeg;
    minDeg = al.get(0);

    if (maxDeg2 == 0) maxDeg2 = maxDeg;

    double a = 0;
    double b = 0;

    for (Vertex v : g) {
      if (g.getDegree(v) == maxDeg) a++;
      if (g.getDegree(v) == minDeg) b++;
    }
    if (maxDeg == minDeg) b = 0;

    double m = g.getEdgesCount();
    double n = g.getVerticesCount();

    double M12 = zif.getSecondZagreb(1);
    double M21 = zif.getFirstZagreb(1);
    double M31 = zif.getFirstZagreb(2);
    double M41 = zif.getFirstZagreb(3);
    double M22 = zif.getSecondZagreb(2);
    double Mm31 = zif.getFirstZagreb(-4);
    double Mm11 = zif.getFirstZagreb(-2);
    double EM1 = zifL.getFirstZagreb(1);

    double Psi1 =
        (Math.pow(
                (2 * (m + 1)
                    - (n + maxDeg + maxDeg2)
                    + Math.sqrt(
                        (2 * m - maxDeg - maxDeg2) * (Mm11 - ((1 / maxDeg) + (1 / maxDeg2))))),
                2)
            / (n - 2));

    double Psi2 =
        (Math.pow(
                (2 * (m + 1)
                    - (n + maxDeg + minDeg)
                    + Math.sqrt(
                        (2 * m - maxDeg - minDeg) * (Mm11 - ((1 / maxDeg) + (1 / minDeg))))),
                2)
            / (n - 2));

    double Zeta1 =
        2 * m
            - maxDeg
            - maxDeg2
            + (M21 - maxDeg * maxDeg - maxDeg2 * maxDeg2 - n + 2)
                * (M21 - maxDeg * maxDeg - maxDeg2 * maxDeg2 - n + 2)
                / (2 * m - maxDeg - maxDeg2 - Mm11 + (1 / maxDeg) + (1 / maxDeg2));

    double Zeta2 =
        2 * m
            - (maxDeg)
            - (minDeg)
            + Math.pow((M21 - (maxDeg * maxDeg) - (minDeg * minDeg) - (n - 2)), 2)
                / (2 * m - maxDeg - minDeg - Mm11 + (1 / maxDeg) + (1 / minDeg));

    double Zeta3 =
        M21
            - maxDeg * maxDeg
            - maxDeg2 * maxDeg2
            + (Math.pow((M21 - maxDeg * maxDeg - maxDeg2 * maxDeg2 - 2 * m + maxDeg + maxDeg2), 2)
                / (2 * m - maxDeg - maxDeg2 - n + 2));

    double Zeta4 =
        M21
            - maxDeg * maxDeg
            - minDeg * minDeg
            + (Math.pow((M21 - maxDeg * maxDeg - minDeg * minDeg - 2 * m + maxDeg + minDeg), 2)
                / (2 * m - maxDeg - minDeg - n + 2));

    double Eps3 =
        (Math.pow(
                (M21 - (maxDeg * maxDeg) - (maxDeg2 * maxDeg2))
                    + Math.sqrt((n - 2) * (M21 - (maxDeg * maxDeg) - (maxDeg2 * maxDeg2)))
                    - (2 * m - maxDeg - maxDeg2),
                2)
            / (2 * m - maxDeg - maxDeg2));

    double Eps4 =
        (Math.pow(
                (M21 - (maxDeg * maxDeg) - (minDeg * minDeg))
                    + Math.sqrt((n - 2) * (M21 - (maxDeg * maxDeg) - (minDeg * minDeg)))
                    - (2 * m - maxDeg - minDeg),
                2)
            / (2 * m - maxDeg - minDeg));

    ret.add(new Vector<Object>());

    ret.get(1).add(zifL.getFirstZagreb(1));

    // new3
    ret.get(1)
        .add(
            (maxDeg * maxDeg * maxDeg)
                + (maxDeg2 * maxDeg2 * maxDeg2)
                + (Zeta3)
                - (4 * M21)
                + (2 * M12)
                + (4 * m));

    // new4
    ret.get(1)
        .add(
            (maxDeg * maxDeg * maxDeg)
                + (minDeg * minDeg * minDeg)
                + (Zeta4)
                - (4 * M21)
                + (2 * M12)
                + (4 * m));

    // new1
    ret.get(1)
        .add(
            (maxDeg * maxDeg * maxDeg)
                + (maxDeg2 * maxDeg2 * maxDeg2)
                + (Zeta1)
                - (4 * M21)
                + (2 * M12)
                + (4 * m));

    // new2
    ret.get(1)
        .add(
            (maxDeg * maxDeg * maxDeg)
                + (minDeg * minDeg * minDeg)
                + (Zeta2)
                - (4 * M21)
                + (2 * M12)
                + (4 * m));

    // Eps3
    ret.get(1)
        .add(
            (maxDeg * maxDeg * maxDeg)
                + (maxDeg2 * maxDeg2 * maxDeg2)
                + (Eps3)
                - (4 * M21)
                + (2 * M12)
                + (4 * m));

    // Eps4
    ret.get(1)
        .add(
            (maxDeg * maxDeg * maxDeg)
                + (minDeg * minDeg * minDeg)
                + (Eps4)
                - (4 * M21)
                + (2 * M12)
                + (4 * m));

    // Psi1
    ret.get(1)
        .add(
            M31
                - (4 * M21)
                + (2 * maxDeg * maxDeg)
                + (2 * maxDeg2 * maxDeg2)
                + (2 * Psi1)
                + 2 * (M12 - M21 + m)
                + 2 * m);

    // Psi2
    ret.get(1)
        .add(
            M31
                - (4 * M21)
                + (2 * maxDeg * maxDeg)
                + (2 * minDeg * minDeg)
                + (2 * Psi2)
                + 2 * (M12 - M21 + m)
                + 2 * m);

    // illc
    ret.get(1).add(((2 * m / n) * M21) + (4 * m) + (2 * M12) - (4 * M21));
    // nilanjan de 2
    ret.get(1).add(Math.pow(M21 - (2 * m), 2) / m);
    // nilanjan de 1
    ret.get(1).add(4 * m * (minDeg - 1) * (minDeg - 1));
    return ret;
  }
  public Object calculate(GraphModel g) {
    ZagrebIndexFunctions zif = new ZagrebIndexFunctions(g);
    RenderTable ret = new RenderTable();
    Vector<String> titles = new Vector<>();
    titles.add(" E(G) ");
    titles.add(" 1.1 ");
    titles.add(" 1.2 ");
    titles.add(" 1.3 ");
    titles.add(" 1.4 ");
    titles.add(" 1.5 ");
    titles.add(" 1.6 ");
    titles.add(" 1.7 ");
    titles.add(" Eigenvalues ");
    titles.add(" 2-degree sum ");
    titles.add("new query");
    ret.setTitles(titles);

    Matrix A = g.getWeightedAdjacencyMatrix();
    EigenvalueDecomposition ed = A.eig();
    double rv[] = ed.getRealEigenvalues();
    double sum = 0;
    double detA = Math.abs(A.det());

    // positiv RV
    Double[] prv = new Double[rv.length];
    for (int i = 0; i < rv.length; i++) {
      prv[i] = Math.abs(rv[i]);
      prv[i] = (double) Math.round(prv[i] * 100000d) / 100000d;
      sum += prv[i];
    }

    Arrays.sort(prv, Collections.reverseOrder());

    double maxDeg = 0;
    double maxDeg2 = 0;
    double minDeg = Integer.MAX_VALUE;

    ArrayList<Integer> al = AlgorithmUtils.getDegreesList(g);
    Collections.sort(al);
    maxDeg = al.get(al.size() - 1);
    if (al.size() - 2 >= 0) maxDeg2 = al.get(al.size() - 2);
    else maxDeg2 = maxDeg;
    minDeg = al.get(0);

    if (maxDeg2 == 0) maxDeg2 = maxDeg;

    double a = 0;
    double b = 0;

    for (Vertex v : g) {
      if (g.getDegree(v) == maxDeg) a++;
      if (g.getDegree(v) == minDeg) b++;
    }
    if (maxDeg == minDeg) b = 0;

    double m = g.getEdgesCount();
    double n = g.getVerticesCount();

    double M12 = zif.getSecondZagreb(1);
    double M21 = zif.getFirstZagreb(1);
    double M22 = zif.getSecondZagreb(2);
    double Mm11 = zif.getFirstZagreb(-2);

    Vector<Object> v = new Vector<>();
    v.add(sum);
    // 1
    v.add(Math.sqrt(2 * m));
    // 2
    v.add(
        (2 * Math.sqrt(2 * m * n) * Math.sqrt(prv[0] * prv[prv.length - 1]))
            / (prv[0] + prv[prv.length - 1]));
    // 3
    v.add((prv[0] * prv[prv.length - 1] * n + 2 * m) / (prv[0] + prv[prv.length - 1]));
    // 4
    v.add(Math.sqrt(2 * m * n - (Math.pow(n * (prv[0] - prv[prv.length - 1]), 2) / 4)));
    // 5
    double alpha = n * Math.floor(n / 2) * (1 - (1 / n) * Math.floor(n / 2));
    v.add(Math.sqrt(2 * m * n - (Math.pow((prv[0] - prv[prv.length - 1]), 2) * alpha)));
    // 6
    if (detA == 0) v.add(0);
    else v.add(Math.sqrt(2 * m + n * (n - 1) * Math.pow(detA, 2 / n)));

    // 7
    double up = n * Math.pow(prv[0] - prv[prv.length - 1], 2);
    double down = 4 * (prv[0] + prv[prv.length - 1]);
    v.add(Math.sqrt(2 * m * n) - (up / down));

    // eigenvalues
    v.add(getEigenValues(g));

    // 2-degree sum
    v.add(Utils.getDegreeSum(g, 1));

    // new query
    double eigenVal_k = prv[prv.length - 1];
    int cnt = prv.length - 1;

    if (eigenVal_k == 0) {
      while (eigenVal_k == 0) {
        eigenVal_k = prv[--cnt];
      }
    }

    int numOfNZEigenValue = 0;
    for (int i = 0; i < prv.length; i++) {
      if (prv[i] != 0) numOfNZEigenValue++;
    }

    double alpha_k =
        numOfNZEigenValue
            * Math.floor(numOfNZEigenValue / 2)
            * (1 - (1 / numOfNZEigenValue) * Math.floor(numOfNZEigenValue / 2));
    System.out.println(alpha_k + "  " + numOfNZEigenValue);
    System.out.println(prv[0] + "  " + eigenVal_k);
    v.add(Math.sqrt(2 * m * numOfNZEigenValue - (Math.pow((prv[0] - eigenVal_k), 2) * alpha_k)));

    ret.add(v);
    return ret;
  }
Ejemplo n.º 10
0
  public static int StarBicoloringSchemeCombinedVertexCoverColoringRestricted(
      GraphModel g, final Vector<Integer> V_rr, final Vector<Integer> V_cc, final int Mode) {
    Vector<Integer> IS = new Vector<>();
    Vector<Integer> V_r = new Vector<>();
    Vector<Integer> V_c = new Vector<>();
    V_r.addAll(V_rr);
    V_c.addAll(V_cc);
    Vector<Integer> num_colors;
    Vector<Integer> V_r_aux = new Vector<>();
    Vector<Integer> V_c_aux = new Vector<>();
    V_r_aux.addAll(V_r);
    V_c_aux.addAll(V_c);
    GraphModel gaux = g.getCopy();
    float ratio = 1;

    if (Mode != 1) ratio = Mode / 2;

    Vector<Pair<Integer, Integer>> Degree_V_r_aux = new Vector<>();
    Vector<Pair<Integer, Integer>> Degree_V_c_aux = new Vector<>();
    Vector<Pair<Integer, Integer>> copy_real_r = new Vector<>();
    Vector<Pair<Integer, Integer>> copy_real_c = new Vector<>();
    Vector<Integer> forbiddenColors = new Vector<>(V_r.size());
    for (int i = 0; i < V_r.size(); i++) {
      forbiddenColors.add(-1);
    }

    Iterator<Integer> v_r_aux_it = V_r_aux.iterator();
    for (int v_r : V_r) {
      if (v_r_aux_it.hasNext()) {
        int v_r_aux = v_r_aux_it.next();
        copy_real_r.add(new Pair<>(v_r, v_r_aux));
      }
    }

    Iterator<Integer> v_c_aux_it = V_c_aux.iterator();
    for (int v_c : V_c) {
      if (v_c_aux_it.hasNext()) {
        int v_c_aux = v_c_aux_it.next();
        copy_real_c.add(new Pair<>(v_c, v_c_aux));
      }
    }

    for (int v_r : V_r_aux) {
      Degree_V_r_aux.add(new Pair<>(v_r, gaux.getDegree(gaux.getVertex(v_r))));
    }

    for (int v_c : V_c_aux) {
      Degree_V_c_aux.add(new Pair<>(v_c, gaux.getDegree(gaux.getVertex(v_c))));
    }

    while (gaux.getEdgesCount() > 0) {
      int max_degree_V_r_aux = 0;
      for (Pair<Integer, Integer> di : Degree_V_r_aux) {
        di.second = gaux.getDegree(gaux.getVertex(di.first));
        int degree_v_r = di.second;
        if (degree_v_r > max_degree_V_r_aux) {
          max_degree_V_r_aux = degree_v_r;
        }
      }
      System.out.println("1");

      int max_degree_V_c_aux = 0;
      for (Pair<Integer, Integer> di : Degree_V_c_aux) {
        di.second = gaux.getDegree(gaux.getVertex(di.first));
        int degree_v_c = di.second;
        if (degree_v_c > max_degree_V_c_aux) {
          max_degree_V_c_aux = degree_v_c;
        }
      }
      System.out.println("2");

      if (max_degree_V_r_aux > ratio * max_degree_V_c_aux) {
        Vector<Pair<Integer, Integer>> removedEdges = new Vector<>();
        for (Pair<Integer, Integer> di : Degree_V_r_aux) {
          if (max_degree_V_r_aux == di.second) {
            Pair<Integer, Integer> cr = new Pair<>(0, 0);
            for (Pair<Integer, Integer> crtmp : copy_real_r) {
              if (crtmp.second.equals(di.first)) {
                cr = crtmp;
                break;
              }
            }
            int v = 0;
            for (int tmp : V_r) {
              if (cr.first == v) {
                v = tmp;
                break;
              }
            }
            System.out.println("33");

            IndSet.clearVertex(gaux.getVertex(di.first), gaux);
            removedEdges.add(di);

            System.out.println("44");
            forbiddenColors.set(0, v);
            for (Vertex w : g.getNeighbors(g.getVertex(v))) {
              if (w.getColor() <= 0) {
                for (Vertex x : g.getNeighbors(w)) {
                  if (x.getColor() > 0) {
                    if (g.getEdge(w, g.getVertex(v)).getWeight() == 1
                        || g.getEdge(w, x).getWeight() == 1) {
                      forbiddenColors.set(x.getId(), v);
                    }
                  }
                }
              } else { // Color[w]>0
                for (Vertex x : g.getNeighbors(w)) {
                  if (x.getColor() > 0) {
                    if (g.getEdge(w, x).getWeight() == 1) {
                      for (Vertex y : g.getNeighbors(x)) {
                        if (y.getColor() > 0) {
                          if (w.getColor() == y.getColor() && w.getId() != y.getId()) {
                            forbiddenColors.set(x.getId(), v);
                          }
                        }
                      }
                    }
                  }
                }
              }
              System.out.println("45s");
            }
            System.out.printf("456");

            // Find first color which can be assigned to v_c
            for (int i = 0; i < forbiddenColors.size(); i++) {
              if (forbiddenColors.get(i) != v) {
                g.getVertex(v).setColor(forbiddenColors.get(i));
                break;
              }
            }
            System.out.println("500");
          }
        }
        Degree_V_r_aux.removeAll(removedEdges);
      } else {
        Vector<Pair<Integer, Integer>> removedEdges = new Vector<>();
        for (Pair<Integer, Integer> di : Degree_V_c_aux) {
          if (max_degree_V_c_aux == di.second) {
            Pair<Integer, Integer> cr = new Pair<>(0, 0);
            for (Pair<Integer, Integer> crtmp : copy_real_c) {
              if (crtmp.second == di.first) {
                cr = crtmp;
                break;
              }
            }
            int v = 0;
            for (int tmp : V_c) {
              if (cr.first == v) {
                v = tmp;
                break;
              }
            }

            IndSet.clearVertex(gaux.getVertex(di.first), gaux);
            removedEdges.add(di);

            System.out.println("3");
            forbiddenColors.set(0, v);
            System.out.println("3.0q" + v);
            for (Vertex w : g.getNeighbors(g.getVertex(v))) {
              System.out.println("3.1");
              if (w.getColor() <= 0) {
                for (Vertex x : g.getNeighbors(w)) {
                  if (x.getColor() > 0) {
                    if (g.getEdge(w, g.getVertex(v)).getWeight() == 1
                        || g.getEdge(w, x).getWeight() == 1) {
                      forbiddenColors.set(x.getId(), v);
                    }
                  }
                }
                System.out.println("3.2");

              } else { // Color[w]>0
                for (Vertex x : g.getNeighbors(w)) {
                  System.out.println("3.3");
                  if (x.getColor() > 0) {
                    if (g.getEdge(w, x).getWeight() == 1) {
                      for (Vertex y : g.getNeighbors(x)) {
                        if (y.getColor() > 0) {
                          if (w.getColor() == y.getColor() && w.getId() != y.getId()) {
                            forbiddenColors.set(x.getId(), v);
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
            System.out.println("3.5");

            // Find first color which can be assigned to v_c
            for (int i = 0; i < forbiddenColors.size(); i++) {
              if (forbiddenColors.get(i) != v) {
                g.getVertex(v).setColor(forbiddenColors.get(i));
                break;
              }
            }
          }
        }
        System.out.println("4");
        Degree_V_c_aux.removeAll(removedEdges);
      }
    }
    for (Pair<Integer, Integer> di : Degree_V_r_aux) {
      IS.add(di.first);
    }
    for (Pair<Integer, Integer> di : Degree_V_c_aux) {
      IS.add(di.first);
    }

    for (int i = 0; i < IS.size(); i++) {
      g.getVertex(IS.get(i)).setColor(0);
    }

    return getNumOfCols(g);
  }
Ejemplo n.º 11
0
 public static boolean IncidentToReqEdge(GraphModel g, int Vertex) {
   for (Edge e : g.edges(g.getVertex(Vertex))) if (e.getWeight() == 1) return true;
   return false;
 }
Ejemplo n.º 12
0
  public static boolean StarBicoloringRestricted(
      GraphModel g, Vector<Integer> V_r, Vector<Integer> V_c) {
    Vector<Integer> forbiddenColors = new Vector<>(V_r.size());
    for (int i = 0; i < V_r.size(); i++) forbiddenColors.add(-1);
    Vector<Integer> Vertices = new Vector<>(V_r.size() + V_c.size());

    // GraphModel gg = g.getCopy();
    for (int v_r : V_r) Vertices.add(v_r);
    for (int v_c : V_c) Vertices.add(v_c);

    for (int v : Vertices) {
      if (g.getVertex(v).getColor() != 0) {
        forbiddenColors.set(0, v);
        for (Vertex w : g.getNeighbors(g.getVertex(v))) {
          if (w.getColor() <= 0) {
            for (Vertex x : g.getNeighbors(w)) {
              if (x.getColor() > 0) {
                if (g.getEdge(x, w).getWeight() == 1
                    || g.getEdge(w, g.getVertex(v)).getWeight() == 1) {
                  forbiddenColors.set(x.getColor(), v);
                }
              }
            }
          } else {
            for (Vertex x : g.getNeighbors(w)) {
              System.out.println(" jez " + g.getEdge(x, w).getWeight());
              if (g.getEdge(x, w).getWeight() == 1) {
                if (x.getColor() > 0) {
                  for (Vertex y : g.getNeighbors(x)) {
                    if (y.getColor() > 0) {
                      if (w.getColor() == y.getColor() && w.getId() != y.getId()) {
                        forbiddenColors.set(x.getColor(), v);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

      for (int i = 0; i < forbiddenColors.size(); i++) {
        if (forbiddenColors.get(i) != v) {
          g.getVertex(v).setColor(i);
        }
      }
    }
    return true;
  }
Ejemplo n.º 13
0
  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;
  }