/** * 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; }
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; } }
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; }
static void completeEdges(SubGraph reminder) { for (VertexModel v : reminder.vertices) { for (VertexModel u : reminder.vertices) { if (u.getId() > v.getId()) if (SubGraph.getEdge(reminder, u, v) == null) { Integer w = TripNet.getW(TripNet.Xi(u), TripNet.Xi(v)); if (w != null) { EdgeModel e = new EdgeModel(u, v); e.setWeight(w); reminder.graph.insertEdge(e); reminder.edges.add(e); } } } } }
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; }
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; }
static SubGraph getRelatedSubGraph(VertexModel v) { return v.getUserDefinedAttribute("Related SubGraph"); }
private static Object getVId(VertexModel vi) { return vi.getUserDefinedAttribute("Xi"); }