// computes topological numbering on the segments of the constraint graph. // Usage: If used on the basic (and vertex size) arcs, the numbering can be // used in order to serve as sorting criteria for respecting the given // embedding, e.g., when computing visibility arcs and allowing edges // with length 0. void CompactionConstraintGraphBase::computeTopologicalSegmentNum( NodeArray<int> &topNum) { NodeArray<int> indeg(*this); StackPure<node> sources; for(node v : nodes) { topNum[v] = 0; indeg[v] = v->indeg(); if(indeg[v] == 0) sources.push(v); } while(!sources.empty()) { node v = sources.pop(); edge e; forall_adj_edges(e,v) { if(e->source() != v) continue; node w = e->target(); if (topNum[w] < topNum[v] + 1) topNum[w] = topNum[v] + 1; if (--indeg[w] == 0) sources.push(w); } } }
void resolver_pags_web( conf& args, ifstream& ifile, int cant_nodos, int cant_aristas, vector<double>& resultado, ostream& conv ) { MEDIR_TIEMPO_INICIO(args.timer); switch (args.alg) { case ALG_PAGERANK: { matrize data(ifile, cant_nodos, cant_aristas); vector<double> inicial(cant_nodos, (double) 1/cant_nodos); resultado = data.potencias(inicial, args.c, args.tol, &args.count_iter, conv); break; } case ALG_ALT: { resultado = indeg(ifile, cant_nodos, cant_aristas); break; } } MEDIR_TIEMPO_FIN(args.timer); }
void LongestPathCompaction::applyLongestPaths( const CompactionConstraintGraph<int> &D, NodeArray<int> &pos) { const Graph &Gd = D.getGraph(); m_component.init(Gd); NodeArray<int> indeg(Gd); StackPure<node> sources; for(node v : Gd.nodes) { indeg[v] = v->indeg(); if(indeg[v] == 0) sources.push(v); } while(!sources.empty()) { node v = sources.pop(); int predComp = -1; // means "unset" bool isPseudoSource = true; edge e; forall_adj_edges(e,v) { if(e->source() != v) { // incoming edge if (D.cost(e) > 0) { isPseudoSource = false; node w = e->source(); // is tight? if (pos[w] + D.length(e) == pos[v]) { if (predComp == -1) predComp = m_component[w]; else if (predComp != m_component[w]) predComp = 0; // means "vertex is in no pseudo-comp. } } } else { // outgoing edge node w = e->target(); if (pos[w] < pos[v] + D.length(e)) pos[w] = pos[v] + D.length(e); if (--indeg[w] == 0) sources.push(w); } } if (predComp == -1) predComp = 0; if( isPseudoSource) { m_pseudoSources.pushFront(v); m_component[v] = m_pseudoSources.size(); } else { m_component[v] = predComp; } } }
void IptTracer::mergePartialPaths(omp_lock_t& cmdLock) { struct SearchQuery { const IptPathState* interState; vector<int> mergeIndex; SearchQuery() { mergeIndex.clear(); } void process(const IptPathState& lightState) { mergeIndex.push_back(lightState.index); } }; vector<vec3f> contribs(partialPhotonNum - lightPhotonNum); vector<double> mergedPath(partialPhotonNum - lightPhotonNum); for (int i = 0; i < partialSubPathList.size(); i++) { partialSubPathList[i].index = i; } if (usePPM) return; // preprocess PointKDTree<IptPathState> lightTree(partialSubPathList); revIndex = new int[partialPhotonNum - lightPhotonNum]; int flag = 0; for (int i = 0; i < interPathNum; i++) { if (partPathMergeIndex[i].size() == 0) continue; for (int j = 0; j < partPathMergeIndex[i].size(); j++) { int k = partPathMergeIndex[i][j] - lightPhotonNum; revIndex[k] = i; } } #pragma omp parallel for for (int i = 0; i < interPathNum; i++) { if (partPathMergeIndex[i].size() == 0) continue; SearchQuery query; query.interState = &partialSubPathList[partPathMergeIndex[i][0]]; lightTree.searchInRadius(0 , query.interState->originRay->origin , mergeRadius , query); partPathMergeIndex[i].clear(); for (int j = 0; j < query.mergeIndex.size(); j++) { int k = query.mergeIndex[j]; if (k < lightPhotonNum || revIndex[k - lightPhotonNum] != i) partPathMergeIndex[i].push_back(query.mergeIndex[j]); } } bool f = checkCycle; int checkTime = checkCycleIters; vis.clear(); inStack.clear(); cannotBeCycle.clear(); vis.resize(partialPhotonNum - lightPhotonNum); inStack.resize(partialPhotonNum - lightPhotonNum); cannotBeCycle.resize(partialPhotonNum - lightPhotonNum); // topological sort for (int i = 0; i < vis.size(); i++) { vis[i] = cannotBeCycle[i] = 0; } if (checkCycle) { int totEdges = 0; vector<int> indeg(partialPhotonNum - lightPhotonNum , 0); for (int i = lightPhotonNum; i < partialPhotonNum; i++) { int k = revIndex[i - lightPhotonNum]; for (int j = 0; j < partPathMergeIndex[k].size(); j++) { int pa = partPathMergeIndex[k][j]; if (pa >= lightPhotonNum) { totEdges++; indeg[pa - lightPhotonNum]++; } } } printf("inter path graph has %d edges\n" , totEdges); while (!q.empty()) q.pop(); for (int i = lightPhotonNum; i < partialPhotonNum; i++) if (indeg[i - lightPhotonNum] == 0) q.push(i); while (!q.empty()) { int i = q.front(); cannotBeCycle[i - lightPhotonNum] = true; q.pop(); int k = revIndex[i - lightPhotonNum]; for (int j = 0; j < partPathMergeIndex[k].size(); j++) { int pa = partPathMergeIndex[k][j]; if (pa >= lightPhotonNum) { indeg[pa - lightPhotonNum]--; if (indeg[pa - lightPhotonNum] == 0) q.push(pa); } } } int cnt = 0; for (int i = lightPhotonNum; i < partialPhotonNum; i++) if (cannotBeCycle[i - lightPhotonNum]) cnt++; printf("%d/%d nodes may be in a cycle\n" , partialPhotonNum - lightPhotonNum - cnt , partialPhotonNum - lightPhotonNum); } while (f) { f = false; // check cycle for (int i = 0; i < vis.size(); i++) { vis[i] = 0; inStack[i] = 0; } vector<int> nodePerm(partialPhotonNum - lightPhotonNum); for (int i = lightPhotonNum; i < partialPhotonNum; i++) nodePerm[i - lightPhotonNum] = i; int N = nodePerm.size(); for (int i = 0; i < nodePerm.size() / 10; i++) { int x = rand() % N; int y = rand() % N; swap(nodePerm[x] , nodePerm[y]); } for (int i = 0; i < nodePerm.size(); i++) { int st = nodePerm[i]; if (cannotBeCycle[st - lightPhotonNum] || vis[st - lightPhotonNum]) continue; while (!cycle.empty()) { inStack[cycle.top() - lightPhotonNum] = 0; cycle.pop(); } f |= dfs(1 , st); } // eliminate cycle for (int i = 0; i < edgeToRemove.size(); i++) { int child = edgeToRemove[i].second; int pa = edgeToRemove[i].first; int k = revIndex[child - lightPhotonNum]; int index = -1; for (int j = 0; j < partPathMergeIndex[k].size(); j++) { if (partPathMergeIndex[k][j] == pa) index = j; } if (index != -1) partPathMergeIndex[k].erase(partPathMergeIndex[k].begin() + index); } edgeToRemove.clear(); checkTime--; //printf("eliminating cycles: iteration %d\n" , checkCycleIters - checkTime); if (checkTime == 0) break; } printf("check & eliminate cycles, use %d iterations\n" , checkCycleIters - checkTime); f = true; int totMergeIter = 0; while (f) { ++totMergeIter; #pragma omp parallel for for (int i = lightPhotonNum; i < partialPhotonNum; i++) { mergePartialPaths(contribs , mergedPath , partialSubPathList[i]); } f = false; for (int i = lightPhotonNum; i < partialPhotonNum; i++) { if (!f && abs(intensity(partialSubPathList[i].indirContrib) - intensity(contribs[i - lightPhotonNum])) > 1e-3f) f = true; partialSubPathList[i].indirContrib = contribs[i - lightPhotonNum]; partialSubPathList[i].mergedPath = mergedPath[i - lightPhotonNum]; } if (totMergeIter > mergeIterations) break; } printf("merge done... totMergeIter = %d... tracing eye paths...\n" , totMergeIter); vis.clear(); for (int i = 0; i < partPathMergeIndex.size(); i++) { partPathMergeIndex[i].clear(); partPathMergeIndex[i].shrink_to_fit(); } partPathMergeIndex.clear(); delete[] revIndex; }
void topo_shortest_path(graph_t * g, char s) { list_t * L = g->get_structure(); int N = g->vex_count(); int si = g->id(s); std::vector<int> indeg(N, 0); get_indegree(L, indeg); std::queue<int> qu; for (int i = 0; i < N; ++i) { if (indeg[i] == 0) { qu.push(i); } } std::queue<int> q2; while (!qu.empty()) { int t = qu.front(); q2.push(t); qu.pop(); for (arc_t * p = L->_heads[t]; p; p = p->next) { --indeg[p->adj]; if (indeg[p->adj] == 0) { qu.push(p->adj); } } } int UNL = g->unconnected_value(); std::vector<int> dist(N, UNL); std::vector<int> prev(N, -1); dist[si] = 0; while (!q2.empty()) { int t = q2.front(); q2.pop(); for (arc_t * p = L->_heads[t]; p; p = p->next) { if (dist[t] + p->weight < dist[p->adj]) { dist[p->adj] = dist[t] + p->weight; prev[p->adj] = t; } } } std::stack<char> stk; for (int i = 0; i < N; ++i) { if (i != si) { int j = i; while (j != -1) { stk.push(g->vex(j)); j = prev[j]; } while (!stk.empty()) { std::cout << stk.top() << ' '; stk.pop(); } std::cout << '\n'; } } }
int pred2(Edge *e) { Node *n = &elem(e->tgt); return (indeg(n) >= 1); }
int pred0(Node *n) { return (outdeg(n) >= 1 && indeg(n) >= 1); }
int signature(Node *n) { return (min(1, outdeg(n)) << 1) | min(1, indeg(n)); }