int connectedComponents(const Graph &G, NodeArray<int> &component) { int nComponent = 0; component.fill(-1); StackPure<node> S; for(node v : G.nodes) { if (component[v] != -1) continue; S.push(v); component[v] = nComponent; while(!S.empty()) { node w = S.pop(); edge e; forall_adj_edges(e,w) { node x = e->opposite(w); if (component[x] == -1) { component[x] = nComponent; S.push(x); } } } ++nComponent; }
/** * Copy the solution (Graph, Tour, Edges, and Headings) into those given. * @note Existing nodes and edges are cleared. */ void DubinsPathPlanner::copySolution(ogdf::Graph &G, ogdf::GraphAttributes &GA, ogdf::List<ogdf::node> &Tour, ogdf::List<ogdf::edge> &Edges, NodeArray<double> &Headings, double &cost) { DPP_ASSERT(m_haveSolution); // Copy the graph and attributes NodeArray<node> nodeCopyTable(m_G); EdgeArray<edge> edgeCopyTable(m_G); int n = copyGraph(m_G, m_GA, G, GA, nodeCopyTable, edgeCopyTable); // Copy the tour ogdf::ListIterator<ogdf::node> tourIter; for ( tourIter = m_Tour.begin(); tourIter != m_Tour.end(); tourIter++ ) { node u = *tourIter; node ucopy = nodeCopyTable(u); Tour.pushBack(ucopy); } // Copy the edge list ogdf::ListIterator<ogdf::edge> edgeIter; for ( edgeIter = m_Edges.begin(); edgeIter != m_Edges.end(); edgeIter++ ) { edge e = *edgeIter; edge ecopy = edgeCopyTable(e); Edges.pushBack(ecopy); } // Copy the headings Headings.init(G); node u; forall_nodes(u,m_G) { node ucopy = nodeCopyTable(u); Headings(ucopy) = m_Headings(u); }
void print_graph(Graph & G, NodeArray<int> & ind) { int n = G.numberOfNodes(); int m = G.numberOfEdges(); printf("%d %d", n, m); Array<edge> list(m); ind.init(G, 0); node u; int a = 0; forall_nodes(u, G) ind[u] = a++; int i = 0; edge e; forall_edges(e, G) list[i++] = e; EdgeCmp cmp; list.quicksort(cmp); for (i=0; i<m; i++) print_ord(ind[list[i]->source()], ind[list[i]->target()]); printf("\n"); }
void PivotMDS::copySPSS(Array<double>& copyTo, NodeArray<double>& copyFrom) { const Graph &G = *copyFrom.graphOf(); int i = 0; for (node v : G.nodes) { copyTo[i++] = copyFrom[v]; } }
NodePointer Parser::procedureCall() { NodeArray arguments; SymbolIndex definitionIndex = currentSymbol_.value_.toString(); getNextSymbol(); accept(LEFT_PARENTHESIS); if (currentSymbol_.symbolType_ != RIGHT_PARENTHESIS) { arguments.push_back(expression()); while (currentSymbol_.symbolType_ == COMMA) { getNextSymbol(); arguments.push_back(expression()); } } accept(RIGHT_PARENTHESIS); NodePointer subroutineCall = NodePointer(new SubroutineCall(tables_, definitionIndex, arguments, getLineNumber())); return subroutineCall; }
void Graph::copy(const Graph &G, NodeArray<node> &mapNode, EdgeArray<edge> &mapEdge) { if (G.m_nNodes == 0) return; mapNode.init(G,0); node vG; forall_nodes(vG,G) { node v = mapNode[vG] = pureNewNode(); v->m_indeg = vG->m_indeg; v->m_outdeg = vG->m_outdeg; }
void StressMinimization::replaceInfinityDistances( NodeArray<NodeArray<double> >& shortestPathMatrix, double newVal) { const Graph &G = *shortestPathMatrix.graphOf(); for(node v : G.nodes) { for(node w : G.nodes) { if (v != w && isinf(shortestPathMatrix[v][w])) { shortestPathMatrix[v][w] = newVal; } } } }
int readFile(NodeArray &Array, string fileName) { ifstream read; string word; read.open(fileName.c_str()); if (read.fail()){ return 0; } while (read >> word){ for (int i = 0; i < word.length(); i++){ if (ispunct(word[i])) { word.erase(i--, 1); } } Array.placeintolist(word); } read.close(); }
void TopologicSSSP::computeSSSP( const std::vector<node> &topSortArray, NodeArray<int> &sccArray) { resetArrays(); assert(m_G.getNodesNumber() == topSortArray.size()); assert(m_G.getNodesNumber() == sccArray.getSize()); assert(checkIfTopologicOrder(topSortArray, sccArray)); //m_componentDistanceArray[5] = 0; //m_componentDistanceArray[m_G.getNodesNumber()-1] = 0; for(int i = 0; i < topSortArray.size(); i++) { node v = topSortArray[i]; for(edge e = v->getFirstEdge(); e !=0; e = e->getNext()) { node u = e->getTarget(); if(e->getWeight() <= WEIGHT_BOUND && sccArray[u] != sccArray[v] ) { relaxEdge(sccArray[v], sccArray[u], e); } } } for(node v = m_G.getFirstNode(); v!= 0; v = v->getNext()) { m_distanceArray[v] = m_componentDistanceArray[ sccArray[v] ]; } }
bool partition_gas_tubes(Digraph& dg, ArcFilter& arc_filter, NodeArray& pumps, GasTubeTypeMap& gtt) { //标记所有分支为负压管 for( Digraph::ArcIt e( dg ); e != INVALID; ++e ) { gtt[e] = GTT_NEGATIVE; } //用FilterArc适配器过滤一部分分支 typedef FilterArcs<Digraph, ArcFilter> Adaptor; Adaptor adaptor(dg, arc_filter); PositiveTubeVisitorFunctor func(gtt); typedef PositiveTubeVisitor<Adaptor, PositiveTubeVisitorFunctor> VisitoAdaptor; typedef DfsVisit<Adaptor, VisitoAdaptor> DfsVisitAdaptor; bool ret = true; //从所有的泵分支的末点开始,做一次dfs //遍历到的所有分支均视作正压管 for( int i = 0; i < pumps.size(); i++ ) { //初始化假设没有出现泵串联的情况 func.inSeriable = false; //定义dfs visitor,用于在dfs遍历的过程中对分支进行标记 VisitoAdaptor visitor( func ); DfsVisitAdaptor dv( adaptor, visitor ); //从泵分支的末节点开始dfs Digraph::Node u = pumps[i]; dv.run(u); //如果串联,则标记结果为false if(func.inSeriable) { ret = false; } } return ret; }
void StressMinimization::minimizeStress( GraphAttributes& GA, NodeArray<NodeArray<double> >& shortestPathMatrix, NodeArray<NodeArray<double> >& weightMatrix) { const Graph& G = GA.constGraph(); int numberOfPerformedIterations = 0; double prevStress = numeric_limits<double>::max(); double curStress = numeric_limits<double>::max(); if (m_terminationCriterion == STRESS) { curStress = calcStress(GA, shortestPathMatrix, weightMatrix); } NodeArray<double> newX; NodeArray<double> newY; NodeArray<double> newZ; if (m_terminationCriterion == POSITION_DIFFERENCE) { newX.init(G); newY.init(G); if (GA.has(GraphAttributes::threeD)) newZ.init(G); } do { if (m_terminationCriterion == POSITION_DIFFERENCE) { if (GA.has(GraphAttributes::threeD)) copyLayout(GA, newX, newY, newZ); else copyLayout(GA, newX, newY); } nextIteration(GA, shortestPathMatrix, weightMatrix); if (m_terminationCriterion == STRESS) { prevStress = curStress; curStress = calcStress(GA, shortestPathMatrix, weightMatrix); } } while (!finished(GA, ++numberOfPerformedIterations, newX, newY, prevStress, curStress)); Logger::slout() << "Iteration count:\t" << numberOfPerformedIterations << "\tStress:\t" << calcStress(GA, shortestPathMatrix, weightMatrix) << endl; }
void OptimalRanking::doCall( const Graph& G, NodeArray<int> &rank, EdgeArray<bool> &reversed, const EdgeArray<int> &length, const EdgeArray<int> &costOrig) { MinCostFlowReinelt<int> mcf; // construct min-cost flow problem GraphCopy GC; GC.createEmpty(G); // compute connected component of G NodeArray<int> component(G); int numCC = connectedComponents(G,component); // intialize the array of lists of nodes contained in a CC Array<List<node> > nodesInCC(numCC); for(node v : G.nodes) nodesInCC[component[v]].pushBack(v); EdgeArray<edge> auxCopy(G); rank.init(G); for(int i = 0; i < numCC; ++i) { GC.initByNodes(nodesInCC[i], auxCopy); makeLoopFree(GC); for(edge e : GC.edges) if(reversed[GC.original(e)]) GC.reverseEdge(e); // special cases: if(GC.numberOfNodes() == 1) { rank[GC.original(GC.firstNode())] = 0; continue; } else if(GC.numberOfEdges() == 1) { edge e = GC.original(GC.firstEdge()); rank[e->source()] = 0; rank[e->target()] = length[e]; continue; } EdgeArray<int> lowerBound(GC,0); EdgeArray<int> upperBound(GC,mcf.infinity()); EdgeArray<int> cost(GC); NodeArray<int> supply(GC); for(edge e : GC.edges) cost[e] = -length[GC.original(e)]; for(node v : GC.nodes) { int s = 0; edge e; forall_adj_edges(e,v) { if(v == e->source()) s += costOrig[GC.original(e)]; else s -= costOrig[GC.original(e)]; } supply[v] = s; } OGDF_ASSERT(isAcyclic(GC) == true); // find min-cost flow EdgeArray<int> flow(GC); NodeArray<int> dual(GC); #ifdef OGDF_DEBUG bool feasible = #endif mcf.call(GC, lowerBound, upperBound, cost, supply, flow, dual); OGDF_ASSERT(feasible); for(node v : GC.nodes) rank[GC.original(v)] = dual[v]; } }
static void CriticalActivity(DGR& dg, Weight& w, EdgeArray& criticalEdges) { //过滤后的图 typedef typename DGR::template NodeMap<double> EventTimeMap; typedef typename DGR::template ArcMap<double> ActivityTimeMap; //正向拓扑排序,计算ve //事件最早发生时间vt EventTimeMap ve(dg,0); NodeArray nodes; GraphUtils::TopologySort(dg, nodes); // nodes[0] == sn for(size_t i=0;i<nodes.size();i++) { Digraph::Node uu = nodes[i]; double maxTime = DBL_MIN; int count = 0; for(typename DGR::InArcIt e(dg,uu);e!=INVALID;++e) { count++; double t = ve[dg.source(e)] + w[e]; if(t > maxTime) { maxTime = t; } } if(count > 0) { ve[uu]=maxTime; } } //逆向拓扑排序,计算vt //vt默认初始话为ve拓扑序列的最后一个节点值 //事件最晚发生时间vt EventTimeMap vt(dg,ve[nodes.back()]); //拓扑序列反向 GraphUtils::ReverseArray(nodes); for(size_t i=0;i<nodes.size();i++) { Digraph::Node uu = nodes[i]; double minTime = DBL_MAX; int count = 0; for(typename DGR::OutArcIt e(dg,uu);e!=INVALID;++e) { double t = ve[dg.target(e)] - w[e]; if(t < minTime) { minTime = t; } count++; } if(count > 0) { vt[uu] = minTime; } } //活动最早开始和最晚开始时间 ActivityTimeMap et(dg), lt(dg); for(typename DGR::ArcIt e(dg);e!=INVALID;++e) { Digraph::Node u = dg.source(e); Digraph::Node v = dg.target(e); et[e] = ve[dg.source(e)]; lt[e] = vt[dg.target(e)] - w[e]; //如果et[e] == lt[e],则该分支为关键活动分支 if(abs(et[e] - lt[e]) < 0.001) { criticalEdges.push_back(e); } } }
void BasicPageRank::call( const Graph& graph, const EdgeArray<double>& edgeWeight, NodeArray<double>& pageRankResult) { const double initialPageRank = 1.0 / (double)graph.numberOfNodes(); const double maxPageRankDeltaBound = initialPageRank * m_threshold; // the two ping pong buffer NodeArray<double> pageRankPing(graph, 0.0); NodeArray<double> pageRankPong(graph, 0.0); NodeArray<double>* pCurrPageRank = &pageRankPing; NodeArray<double>* pNextPageRank = &pageRankPong; NodeArray<double> nodeNorm(graph); for (node v = graph.firstNode(); v; v = v->succ()) { double sum = 0.0; for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) { edge e = adj->theEdge(); sum += edgeWeight[e]; } nodeNorm[v] = 1.0 / sum; } pCurrPageRank->init(graph, initialPageRank); // main iteration loop int numIterations = 0; bool converged = false; // check conditions while ( !converged && (numIterations < m_maxNumIterations) ) { // init the result of this iteration pNextPageRank->init(graph, (1.0 - m_dampingFactor) / (double)graph.numberOfNodes()); // calculate the transfer between each node for (edge e = graph.firstEdge(); e; e = e->succ()) { node v = e->source(); node w = e->target(); double vwTransfer = (edgeWeight[e] * nodeNorm[v] * (*pCurrPageRank)[v]); double wvTransfer = (edgeWeight[e] * nodeNorm[w] * (*pCurrPageRank)[w]); (*pNextPageRank)[w] += vwTransfer; (*pNextPageRank)[v] += wvTransfer; } // damping and calculating change double maxPageRankDelta = 0.0; for (node v = graph.firstNode(); v; v = v->succ()) { (*pNextPageRank)[v] *= m_dampingFactor; double pageRankDelta = fabs((*pNextPageRank)[v] - (*pCurrPageRank)[v]); maxPageRankDelta = std::max(maxPageRankDelta, pageRankDelta); } // swap ping and pong, pong ping, ping pong, lalalala std::swap(pNextPageRank, pCurrPageRank); numIterations++; // check if the change is small enough converged = (maxPageRankDelta < maxPageRankDeltaBound); } // normalization double maxPageRank = (*pCurrPageRank)[graph.firstNode()]; double minPageRank = (*pCurrPageRank)[graph.firstNode()]; for (node v = graph.firstNode(); v; v = v->succ()) { maxPageRank = std::max(maxPageRank, (*pCurrPageRank)[v]); minPageRank = std::min(minPageRank, (*pCurrPageRank)[v]); } // init result pageRankResult.init(graph); for (node v = graph.firstNode(); v; v = v->succ()) { double r = ((*pCurrPageRank)[v] - minPageRank) / (maxPageRank - minPageRank); pageRankResult[v] = r; } // result is now between 0 and 1 }
T randomOptimalSteiner( int n, EdgeWeightedGraph<T> &graph, List<node> &terminals, NodeArray<bool> &isTerminal, EdgeWeightedGraphCopy<T> &tree ) { OGDF_ASSERT(n >= 0); T result = 0; graph.clear(); terminals.clear(); tree.clear(); tree.createEmpty(graph); isTerminal.init(graph, false); if(n > 0) { node source = graph.newNode(); tree.newNode(source); isTerminal[source] = true; terminals.pushFront(source); if(n > 1) { int numberOfAdditionalNodes = randomNumber(0, n-2); int numberOfNodes = n - numberOfAdditionalNodes; int numberOfEdges = randomNumber(numberOfNodes-1 + numberOfAdditionalNodes*2, (n*(n-1))/2); for(int i = 1; i < numberOfNodes; i++) { node v = graph.chooseNode(); node u = graph.newNode(); tree.newNode(u); edge e = graph.newEdge(v, u, 1); result++; tree.newEdge(e, 1); if(isTerminal[v] && v != source) { isTerminal[v] = false; terminals.del(terminals.search(v)); } isTerminal[u] = true; terminals.pushFront(u); } for(int i = numberOfNodes-1; i < numberOfEdges;) { node v = graph.chooseNode(); node u = graph.chooseNode(); while(u == v) { u = graph.chooseNode(); } if(numberOfAdditionalNodes > 0) { node w = graph.newNode(); graph.newEdge(v, w, n); graph.newEdge(w, u, n); numberOfAdditionalNodes--; i += 2; } else { if(graph.searchEdge(v, u) == NULL && graph.searchEdge(u, v) == NULL) { graph.newEdge(v, u, n); i++; } } } OGDF_ASSERT(graph.numberOfEdges() == numberOfEdges); OGDF_ASSERT(tree.numberOfNodes() == numberOfNodes); OGDF_ASSERT(tree.numberOfEdges() == numberOfNodes - 1); } } OGDF_ASSERT(graph.numberOfNodes() == n); OGDF_ASSERT(isSimpleUndirected(graph)); OGDF_ASSERT(isConnected(graph)); return result; }
Node* root() { if (!nodes_.empty()) return &nodes_[0]; return 0; }
void VerticalEdgeLengthCompacter::computeTransitiveHull(const Graph& g, NodeArray<NodeArray<bool> >& transitiveHull){ transitiveHull.init(g); node n; forall_nodes(n, g){ transitiveHull[n].init(g, false); }