/* * stronglyConnectedComponents() finds strongly-connected components within system, and gives all Vertices within each strongly- * connected component the same Vertex.sccNumber value, which will differ from that within all Vertices not within that strongly- * connected component. Implements STRONGLY-CONNECTED-COMPONENTS() and DFS(), as defined in Introduction to Algorithms, Third * Edition. * * system - pointer to the System struct in which strongly-connected components are to be found */ static void stronglyConnectedComponents(System * system){ int time = 0; int sccNumber = 0; for(VertexSign i = POSITIVE; i <= NEGATIVE; i++){ for(int j = 0; j < system->n; j++){ if( system->graph[i][j].dfsColor == WHITE ){ dfsVisit( &system->graph[i][j], &time, sccNumber, NULL ); } } } System transpose; transposeSystem( system, &transpose ); int vertexSortArrayLength = system->n * VERTEX_SIGN_COUNT; Vertex * vertexSortArray[ vertexSortArrayLength ]; for(VertexSign i = POSITIVE; i <= NEGATIVE; i++){ for(int j = 0; j < system->n; j++){ transpose.graph[i][j].finishingTime = system->graph[i][j].finishingTime; vertexSortArray[ i * system->n + j ] = &transpose.graph[i][j]; } } qsort( vertexSortArray, vertexSortArrayLength, sizeof(Vertex *), vertexCompareFinishingTimes); time = 0; for(int i = vertexSortArrayLength - 1; i >= 0; i--){ if( vertexSortArray[i]->dfsColor == WHITE ){ dfsVisit( vertexSortArray[i], &time, sccNumber, NULL ); sccNumber++; } } for(VertexSign i = POSITIVE; i <= NEGATIVE; i++){ for(int j = 0; j < system->n; j++){ system->graph[i][j].sccNumber = transpose.graph[i][j].sccNumber; } } freeSystem( &transpose ); }
/* * generateProof() generates a proof of integral infeasibility, as defined in An Efficient Decision Procedure for UTVPI * Constraints - Lahiri, Musuvathi. Returns a pointer to a ConstraintRefList storing this proof. * * Gphi - pointer to the System struct containing the original graph representation * GphiPrime - pointer to the system struct containing only the slackless edges from Gphi * infeasibleVertexIndex - integer representing the index of the variable causing the contradiction */ static ConstraintRefList * generateProof(System * Gphi, System * GphiPrime, int infeasibleVertexIndex){ ConstraintRefList * proof = generateConstraintRefList(); int k = Gphi->graph[NEGATIVE][infeasibleVertexIndex].D - Gphi->graph[POSITIVE][infeasibleVertexIndex].D; Constraint * constraint; Edge * edge; for(VertexSign i = POSITIVE; i <= NEGATIVE; i++){ for(int j = 0; j < GphiPrime->n; j++){ GphiPrime->graph[i][j].dfsColor = WHITE; } } int time = 0; GphiPrime->graph[NEGATIVE][infeasibleVertexIndex].L = NULL; dfsVisit( &GphiPrime->graph[NEGATIVE][infeasibleVertexIndex], &time, 0, &GphiPrime->graph[POSITIVE][infeasibleVertexIndex] ); edge = GphiPrime->graph[POSITIVE][infeasibleVertexIndex].L; while( edge != NULL ){ constraint = (Constraint *) malloc( sizeof(Constraint) ); edgeToConstraint(edge, constraint); constraintRefListPrepend(proof, constraint); edge = edge->tail->L; } constraint = (Constraint *) malloc( sizeof(Constraint) ); constraint->sign[0] = CONSTRAINT_PLUS; constraint->index[0] = infeasibleVertexIndex + 1; constraint->sign[1] = CONSTRAINT_NONE; constraint->index[1] = 0; constraint->weight = (-k-1)/2; constraintRefListPrepend(proof, constraint); for(VertexSign i = POSITIVE; i <= NEGATIVE; i++){ for(int j = 0; j < GphiPrime->n; j++){ GphiPrime->graph[i][j].dfsColor = WHITE; } } time = 0; GphiPrime->graph[POSITIVE][infeasibleVertexIndex].L = NULL; dfsVisit( &GphiPrime->graph[POSITIVE][infeasibleVertexIndex], &time, 0, &GphiPrime->graph[NEGATIVE][infeasibleVertexIndex] ); edge = GphiPrime->graph[NEGATIVE][infeasibleVertexIndex].L; while( edge != NULL ){ constraint = (Constraint *) malloc( sizeof(Constraint) ); edgeToConstraint(edge, constraint); constraintRefListPrepend(proof, constraint); edge = edge->tail->L; } constraint = (Constraint *) malloc( sizeof(Constraint) ); constraint->sign[0] = CONSTRAINT_MINUS; constraint->index[0] = infeasibleVertexIndex + 1; constraint->sign[1] = CONSTRAINT_NONE; constraint->index[1] = 0; constraint->weight = (k-1)/2; constraintRefListPrepend(proof, constraint); return proof; }
void dfsVisit(TreeNode* root, int sum, int curSum, vector<int> tmp, vector< vector<int> >& res) { if (!root) return; tmp.push_back(root->val); curSum += root->val; if (isLeaf(root) && sum == curSum) { res.push_back(tmp); } dfsVisit(root->left, sum, curSum, tmp, res); dfsVisit(root->right, sum, curSum, tmp, res); }
void dfs(vertex *g, int n) { int time = 0, u; for (u = 0; u < n; u++) { if (g[u].color == 0) { dfsVisit(g, u, &time); } } }
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { vector<vector<int>> adjList(numCourses); for (auto &edge : prerequisites) { adjList[edge.second].push_back(edge.first); } vector<int> color(numCourses, WHITE); for (int u = 0; u < numCourses; ++u) if (color[u]==WHITE && !dfsVisit(adjList, color, u)) { return false; } return true; }
vector<vector<int>> pathSum(TreeNode* root, int sum) { //do a recursive dfs, cal the sum along the way vector<vector<int>> res; if (!root) return res; int curSum = 0; vector<int> tmp; dfsVisit(root, sum, curSum, tmp, res); return res; }
void dfs(){ int i; scc = 0; time = 0; for(i = 1; i <= n;i++){ if(vertexList[i].color == WHITE){ dfsVisit(i); scc++; } } }
/** * returns a list of variables with total ordering determined by the constraint * DAG */ list<Variable*> *Blocks::totalOrder() { list<Variable*> *order = new list<Variable*>; for(int i=0;i<nvs;i++) { vs[i]->visited=false; } for(int i=0;i<nvs;i++) { if(vs[i]->in.size()==0) { dfsVisit(vs[i],order); } } return order; }
void dfsVisit(int current) { visited[current] = true; for(int i = 0; i < n ; i++) { if(!visited[i] && (points[current][0] == points[i][0] || points[current][1] == points[i][1])) { dfsVisit(i); } } }
void dfs(GRAPH *g) { VERTEX *adj = g->adj; int i, vet = g->vetNum; for (i = 0; i < vet; i++) { adj[i].color = 0; } for (i = 0; i < vet; i++) { if (adj[i].color == 0) { dfsVisit(g, i); } } printf("\n"); }
static void dfsVisit(GRAPH *g, int v) { printf("%c ", v + 'A'); g->adj[v].color = 1; EDGE *p = g->adj[v].adj; while (p != NULL) { int next = p->dest; if (g->adj[next].color == 0) { dfsVisit(g, next); } p = p->link; } g->adj[v].color = 2; }
void Solve() { answer = 0; memset(visited, 0, sizeof(visited)); for(int i = 0; i < n; i++) { if(!visited[i]) { answer++; dfsVisit(i); } } }
/** * returns a list of variables with total ordering determined by the constraint * DAG */ list<Variable *> *Blocks::totalOrder() { list<Variable *> *order = new list<Variable *>; for (int i = 0; i < nvs; i++) { vs[i].visited = false; } for (int i = 0; i < nvs; i++) { if (vs[i].in.empty()) { dfsVisit(&vs[i], order); } } return order; }
// Recursive depth first search giving total order by pushing nodes in the DAG // onto the front of the list when we finish searching them void Blocks::dfsVisit(Variable *v, list<Variable*> *order) { v->visited=true; vector<Constraint*>::iterator it=v->out.begin(); for(;it!=v->out.end();++it) { Constraint *c=*it; if(!c->right->visited) { dfsVisit(c->right, order); } } #ifdef RECTANGLE_OVERLAP_LOGGING ofstream f(LOGFILE,ios::app); f<<" order="<<*v<<endl; #endif order->push_front(v); }
void dfsVisit(int u){ int v, i; time++; vertexList[u].start = time; vertexList[u].color = GRAY; for(i = 0;vertexList[u].egde[i] != 0;i++){ v = vertexList[u].egde[i]; if(vertexList[v].color == WHITE){ vertexList[v].father = u; dfsVisit(v); } } vertexList[u].color = BLACK; time++; vertexList[u].finish = time; }
void dfsVisit(vertex *g, int u, int *time) { (*time)++; /*white vertex u has just been discovered*/ g[u].d = *time; g[u].color = 1; node *v = g[u].next; vetor[listLenth++] = u; while (v) {/*explore edge (u,v)*/ if (g[v->v].color == 0) { g[v->v].pred = u; dfsVisit(g, v->v, time); } else if (g[v->v].color == 1) { aciclico = 0; } v = v->next; } g[u].color = 2; (*time)++; g[u].f = *time; }
/* * dfsVisit() implements DFS-VISIT(), as defined in Introduction to Algorithms, Third Edition. Also sets Vertex.sccNumber for all * Vertices within a set of recurrences with the input sccNumber. This value is not useful unless this function is called on a * transposed System being examined in order of decreasing Vertex.finishingTime, as found when searching the original System, and * the input sccNumber is set to be different for each set of recurrences. Additionally, the set of recurrences of this function * will stop and return as soon as the destination Vertex is found. If this destination Vertex is found, the function returns * true. Otherwise, returns false. * * vertex - pointer to the Vertex to visit * time - pointer to an integer value for time, a value defined as global in Introduction to Algorithms * sccNumber - an integer value to label each Vertex within each strongly-connected component with * destination - pointer to a Vertex to be found. Set NULL if all Vertices must be found. */ static bool dfsVisit(Vertex * vertex, int * time, int sccNumber, Vertex * destination){ if( vertex == destination ){ return true; } (*time)++; vertex->dfsColor = GRAY; vertex->sccNumber = sccNumber; Edge * edge = vertex->first; while( edge != NULL ){ if( edge->head->dfsColor == WHITE ){ edge->head->L = edge; bool destinationFound = dfsVisit( edge->head, time, sccNumber, destination ); if( destinationFound ){ return true; } } edge = edge->next; } vertex->dfsColor = BLACK; (*time)++; vertex->finishingTime = *time; return false; }
bool dfsVisit(vector<vector<int>> &adjList, vector<int> &color, int u) { color[u] = GRAY; for (auto &v : adjList[u]) if (color[v]==GRAY || color[v]==WHITE && !dfsVisit(adjList, color, v)) { return false; } color[u] = BLACK; return true; }