コード例 #1
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
  /**
   * the level for the given network
   *
   * @param network
   * @return
   */
  public static int computeLevel(GraphModel network) {
    // first mark reticulation vertices
    // boolean isRetic[] = new boolean[network.getVerticesCount()];
    for (VertexModel v : network) {
      if (network.getOutDegree(v) == 1 && network.getInDegree(v) == 2)
        v.setUserDefinedAttribute("retic", true);
      // isRetic[v.getId()] = true;
    }

    // then make an undirected copy
    GraphModel U = createUndirectedCopy(network);
    BiconnectedComponents bc = new BiconnectedComponents();
    Vector<HashSet<VertexModel>> comps =
        bc.biconnected_components(U, U.getAVertex(), U.getVerticesCount());
    int maxk = 0;
    for (HashSet<VertexModel> component : comps) {
      int k = 0;
      for (VertexModel scan : component)
        if (scan.getUserDefinedAttribute("retic") != null)
          // isRetic[((VertexModel) (scan.getProp().obj)).getId()])
          k++;
      maxk = Math.max(maxk, k);
    }
    return maxk;
  }
コード例 #2
0
ファイル: GraphPower.java プロジェクト: sayiho/GraphLab
  public void action(GraphData graphData) {
    toInsert.clear();
    GraphModel g = graphData.getGraph();

    AlgorithmUtils.clearVertexMarks(g);
    for (Vertex v : g) {
      subtree.clear();
      aStar(v, v, k, g);
      for (Vertex vv : subtree) vv.setMark(false);
    }
    g.insertEdges(toInsert);
  }
コード例 #3
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 public static void dumpResultGraph(GraphModel result) {
   for (VertexModel v : result) {
     TripNet.println(v);
   }
   for (EdgeModel e : result.getEdges()) {
     TripNet.println(e.source.getLabel() + "->" + e.target.getLabel());
   }
 }
コード例 #4
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 static GraphModel createUndirectedCopy(GraphModel network) {
   GraphModel U = new GraphModel(false);
   for (VertexModel scan : network) {
     VertexModel dup = new VertexModel(scan);
     U.addVertex(dup);
     scan.getProp().obj = dup;
     dup.getProp().obj = scan;
   }
   for (EdgeModel scan : network.edges()) {
     EdgeModel dup =
         new EdgeModel(
             scan,
             (VertexModel) scan.source.getProp().obj,
             (VertexModel) scan.target.getProp().obj);
     U.addEdge(dup);
     setDup(scan, dup);
   }
   return U;
 }
コード例 #5
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 public static SubGraph getABackup(GraphModel g) {
   SubGraph ret = new SubGraph(g);
   for (VertexModel v : g) {
     ret.vertices.add(v);
   }
   for (EdgeModel e : g.edges()) {
     ret.edges.add(e);
   }
   return ret;
 }
コード例 #6
0
ファイル: GraphPower.java プロジェクト: sayiho/GraphLab
  void aStar(Vertex root, Vertex v, int k, GraphModel g) {
    if (k == 0) return;
    v.setMark(true);
    subtree.add(v);

    for (Vertex vv : g.getNeighbors(v)) {
      if (!vv.getMark()) {
        toInsert.add(new Edge(root, vv));
        aStar(root, vv, k - 1, g);
      }
    }
  }
コード例 #7
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 public static VertexModel getVertex(
     Object i, GraphModel g, HashMap<Object, VertexModel> vertices) {
   if (vertices.containsKey(i)) {
     return vertices.get(i);
   } else {
     VertexModel v = new VertexModel();
     v.setLabel(i + "");
     TripNet.setVertexId(v, i);
     vertices.put(i, v);
     g.insertVertex(v);
     return v;
   }
 }
コード例 #8
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 public static Vector<EdgeModel> getPathAsEdges(GraphModel graph, Collection<VertexModel> p) {
   Iterator<VertexModel> it = p.iterator();
   Vector<EdgeModel> ret = new Vector<EdgeModel>();
   VertexModel prev = null;
   boolean firstTime = true;
   while (it.hasNext()) {
     VertexModel cur = it.next();
     if (firstTime) {
       firstTime = false;
       prev = cur;
       continue;
     }
     ret.add(graph.getEdge(prev, cur));
     prev = cur;
   }
   return ret;
 }
コード例 #9
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 public static GraphModel getACopy(GraphModel g) {
   GraphModel ret = new GraphModel(g.isDirected());
   VertexModel[] map = new VertexModel[g.getVerticesCount()];
   for (VertexModel v : g) {
     VertexModel t = new VertexModel(v);
     map[v.getId()] = t;
     ret.insertVertex(t);
   }
   for (EdgeModel e : g.edges()) {
     EdgeModel t = new EdgeModel(e, map[e.source.getId()], map[e.target.getId()]);
     ret.insertEdge(t);
     setDup(e, t);
   }
   return ret;
 }
コード例 #10
0
  @Override
  public void doAlgorithm() {

    step("TEST the step");
    GraphModel g = graphData.getGraph();
    Vertex v1 = requestVertex(g, "select the first vertex");
    Vertex v2 = requestVertex(g, "select the second vertex");
    step("color v1 and v2");
    v1.setColor(2);
    v2.setColor(2);

    step("mark v1 neighbours");
    for (Vertex v : g.neighbors(v1)) v.setMark(true);

    step("color v1 edges");
    for (Edge e : g.edges(v1)) e.setColor(3);

    step("connect v2 to the neighbors of its neighbours");
    Vector<Edge> toInsert = new Vector<Edge>();
    for (Vertex v : g.neighbors(v2))
      for (Vertex vv : g.neighbors(v)) toInsert.add(new Edge(v2, vv));
    g.insertEdges(toInsert);

    step("mark the path connecting v1 and v2, using helper methods in AlgorithmUtils");
    Path<Vertex> path = AlgorithmUtils.getPath(g, v1, v2);
    Vertex last = v2;
    for (Vertex v : path) {
      Edge e = g.getEdge(v, last);
      if (e != null) e.setColor(6);
      last = v;
    }

    step("the graph matrix<br>" + getMatrixHTML(g));

    step("That's it!");
    step("Start making your algorithm by modifing this file, and running make.sh");
    step("have fun :)");
  }
コード例 #11
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
  public static GraphModel createIPGraph(Vector<Triplet> tow) {
    // create the DAG
    dag = new GraphModel(true);
    HashMap<Object, VertexModel> V = new HashMap<Object, VertexModel>();
    for (Triplet t : tow) {
      VertexModel ik = getVertex(t.i, t.k, dag, V);
      VertexModel jk = getVertex(t.j, t.k, dag, V);
      VertexModel ij = getVertex(t.i, t.j, dag, V);
      EdgeModel e = new EdgeModel(ij, ik);
      e.getProp().obj = t;
      dag.insertEdge(e);
      e = new EdgeModel(ij, jk);
      e.getProp().obj = t;
      dag.insertEdge(e);
    }

    // convert it to a dag (from each cycle remove an edge)
    // convertToDag(dag);
    boolean first = true;
    while ((cycle = DAG.findACycle(dag)) != null) {
      if (first) {
        TripNet.println("Warning 0: Input triplets are inconsistent!");
        first = false;
      }
      ArrayList<Triplet> cycleTriplets = TripNet.getCycleTriplets(cycle);
      Triplet minT = cycleTriplets.get(0);
      for (Triplet t : cycleTriplets) {
        if (t.w < minT.w) minT = t;
      }
      TripNet.println(
          "removing: "
              + minT
              + " with weight: "
              + minT.w
              + " ::: Resolving the cycle: "
              + cycleTriplets);
      if (TripNet.REAMOVE_CYCLIC_TRIPLETS) {
        TripNet.println("removing Triplet of tow: " + cycleTriplets);
        tow.remove(minT);
      }
      VertexModel ik = getVertex(minT.i, minT.k, dag, V);
      VertexModel jk = getVertex(minT.j, minT.k, dag, V);
      VertexModel ij = getVertex(minT.i, minT.j, dag, V);
      dag.removeEdge(dag.getEdge(ij, jk));
      dag.removeEdge(dag.getEdge(ij, ik));
    }
    // topological sort
    AbstractList<VertexModel> topsort = DAG.doSort(dag);

    if (topsort == null) {
      // the inputs are inconsistent, they are not a dag
      // GraphData gd = new GraphData(Application.blackboard);
      // gd.core.showGraph(dag);
      cycle = DAG.findACycle(dag);
      VertexModel p = cycle.get(0);
      VertexModel c = cycle.get(1);
      EdgeModel e = dag.getEdge(p, c);
      dag.removeEdge(e);
      TripNet.println("Err 0: please contact us for this error! :(");
      return null;
    }
    // set Xij

    // find the length of the longest chain
    int max = -1;
    for (Pair<VertexModel, Integer> p : DAG.findLongestPath(dag)) {
      max = Math.max(max, p.second);
    }

    // set the Xij as large as u can
    for (int i = topsort.size() - 1; i >= 0; i--) {
      VertexModel v = topsort.get(i);
      int min = max + 1;
      for (VertexModel trg : dag.getNeighbors(v)) {
        min = Math.min(min, trg.getColor());
      }
      v.setColor(min - 1); // the color will be related edge's weight (or
      // formally Xij)
    }

    // create the network graph
    HashMap<Object, VertexModel> VR = new HashMap<Object, VertexModel>();
    GraphModel ret = new GraphModel(false);
    if (TripNet.verbose) TripNet.println("Integer Programming Results:");
    TripNet.ths.maxW = max;
    for (VertexModel v : topsort) {
      Pair<Integer, Integer> edge = (Pair<Integer, Integer>) getVId(v);
      VertexModel i = getVertex(edge.first, ret, VR);
      VertexModel j = getVertex(edge.second, ret, VR);
      EdgeModel e = new EdgeModel(i, j);
      int w = v.getColor();
      e.setWeight(w);
      ret.insertEdge(e);
      if (TripNet.verbose) System.out.println(edge.first + "," + edge.second + ": " + w);
      TripNet.ths.F0.put(edge.first + "," + edge.second, w);
      TripNet.ths.weights.put(new Pair<Integer, Integer>(edge.first, edge.second), w);
    }
    // check consistency of results
    for (Triplet t : tow) {
      if ((TripNet.getW(t.i, t.j).intValue() <= TripNet.getW(t.i, t.k).intValue())
          && (TripNet.getW(t.i, t.j).intValue() <= TripNet.getW(t.j, t.k).intValue())) continue;
      System.err.println("inconsistant weight");
    }
    return ret;
  }