void Kosaraju(GRAPH * G, stack * s) { transfer_graph(G); unsigned int temp_edge; unsigned int i; if (s->top < G->size) { printf("Graf nie jest silnie spójny!\n"); return; } while (stack_empty(s) != TRUE) { temp_edge = Pop(s); //printf("Pop:%d\n",temp_edge); if (temp_edge != 0) { stack *stacks = allocation_stack(G->size, 1); DFS_VISIT(G, temp_edge, stacks); printf("Skladowa spojnosc: "); //view_edges(G); //printf("Stack top == %d\n",stacks->top); while (stack_empty(stacks) != TRUE) { temp_edge = Pop(stacks); printf("%d ", temp_edge); delete_vertex(temp_edge, G); pop_false(temp_edge, s, G->size); } putchar('\n'); free_stack(stacks); } } }
void DFS_VISIT(size_t u, int path_type) { DFS_in_stack[u] = path_type; DFS_stack.push_back(u); bool found_new_node = false; for (size_t i = 0; i < (*graph_ptr)[u].size(); ++i) { //adjacent node if ((*graph_ptr)[u][i] > 0) { if (DFS_in_stack[i] == 0) { found_new_node = true; DFS_VISIT(i, (*graph_ptr)[u][i]); } } } if (!found_new_node)//node can't find new node, print path { vector<int> cur_path; for(size_t i = 0; i < DFS_stack.size(); ++i) { if (DFS_in_stack[DFS_stack[i]] == 1) cur_path.push_back(static_cast<int> (DFS_stack[i] + 1)); else if (DFS_in_stack[DFS_stack[i]] == 2) { if (cur_path.back() > 0) cur_path[cur_path.size() - 1] = -cur_path.back(); cur_path.push_back(- (static_cast<int> (DFS_stack[i] + 1))); } else cout << "graph abnormal"<<endl; //cout << DFS_stack[i] <<'\t'; } (*stored_path_ptr).push_back(cur_path); //cout << endl; } //pop stack DFS_in_stack[u] = 0; DFS_stack.pop_back(); }
void DFS(vector<vector<int> >& graph, vector<vector<int> >& stored_path, size_t u) { DFS_stack.clear(); DFS_in_stack.clear(); graph_ptr = &graph; stored_path_ptr = &stored_path; DFS_in_stack.resize(graph.size(), 0); DFS_VISIT(u, 1); //for (size_t i = 0; i < (*graph_ptr)[u].size(); ++i) //{ // //if ((*graph_ptr)[u][i] != 0) //} }
void DFS() { for(int i=0; i<nVertex; i++) { color[i] = WHITE; parent[i] = -1; //NIL } time = 0; for(int u=0; u<nVertex; u++) { if( color[u]==WHITE ) DFS_VISIT(u); } }
void DFS() { for(tVIter it = vertex.begin(); it != vertex.end(); ++it) { it->state = NOT_VISITED; it->pre = -1; } time = 0; for(int it = 0; it != vertex.size(); ++it) { if(vertex[it].state == NOT_VISITED) { DFS_VISIT(it); } } }
bool DFS_VISIT(int u) { Nodes[u].state = 1; for(int i = 0;i < Nodes[u].edges.size();i++) { int num = Nodes[u].edges[i]; if(Nodes[num].state == 0) { bool fff = DFS_VISIT(num); if(!fff) return false; } else if(Nodes[num].state == 1) { return false; } } Nodes[u].state = 2; return true; }
void DFS_VISIT(int u) { time++; vertex[u].dTime = time; vertex[u].state = VISITING; for(tEIter it = vertex[u].edge.begin(); it !=vertex[u].edge.end();++it) { Vertex &nextVertex = vertex[it->vertex]; if(nextVertex.state == NOT_VISITED) { nextVertex.pre = u; DFS_VISIT(nextVertex.number); } } vertex[u].state = VISITED; time++; vertex[u].fTime = time; }
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) { for(int i = 0;i < numCourses;i++) Nodes.push_back(Node(0)); for(int i = 0;i < prerequisites.size();i++) Nodes[prerequisites[i][0]].edges.push_back(prerequisites[i][1]); for(int i = 0;i < numCourses;i++) { vector<int> edges = Nodes[i].edges; if(edges.empty()) continue; if(Nodes[i].state != 0) continue; bool fff = DFS_VISIT(i); if(!fff) return false; } return true; }
void DFS_VISIT(int u) { color[u] = GRAY; sequence[time]=u; d[u] = ++time; for(int v=0; v<nVertex; v++) { if( G[u][v] && color[v] == WHITE ) { parent[v] = u; DFS_VISIT(v); } } color[u]=BLACK; sequence[time]=u; f[u] = ++time; }