예제 #1
0
void DFS(Grapa* Node, int n) {
    srand(unsigned(time(NULL)));
    int count = 0;
    while (count < n) {
        int origin = rand()%n;

        while (!if_whilte(origin)) {
            origin = rand()%n;
        }
        count++; 

        std::cout << "origin = " << origin << std::endl;

        Node[origin]._colour = G;
        Node[origin]._depth = 0;
        Node[origin]._father = 33;
        Next* nextPtr = Node[origin]._next;

        while (nextPtr != NULL && Node[nextPtr->index]._colour != B) {
            DFS_VIST(nextPtr->index, Node[origin]._depth, count);
            Node[nextPtr->index]._colour = B;
            if (nextPtr != NULL && Node[nextPtr->index]._colour == B)
                nextPtr = nextPtr->next;
        }
        Node[origin]._colour = B;
    }
    return;
}
void DFS( std::vector<Node*> &vec )
{
	int t=0;

	for (size_t i=0;i<vec.size();++i)
	{
		if (vec[i]->color==Node::WHITE)
		{
			DFS_VIST(vec[i],t);
		}
	}
}
예제 #3
0
void DFS_VIST(int index, int depth, int & count) {
    count++;
    Node[index]._colour = G;
    Node[index]._depth = depth + 1;
    Next* nextPtr = Node[index]._next;
    while (nextPtr != NULL && Node[nextPtr->index]._colour != B) {
        DFS_VIST(nextPtr->index, Node[index]._depth, count);
        Node[nextPtr->index]._colour = B;
        if (nextPtr != NULL && Node[nextPtr->index]._colour == B)
            nextPtr = nextPtr->next;
    }
    Node[index]._colour = B;
    return;
}
void DFS_VIST( Node* u ,int &t)
{
	Node* v;
	t++;
	u->d=t;
	u->color=Node::GRAY;

	for (size_t i=0;i<u->adj.size();++i)
	{
		v=u->adj[i];
		if (v->color==Node::WHITE)
		{
			v->parent=u;
			DFS_VIST(v,t);
		}
	}
	u->color=Node::BLACK;
	t++;
	u->f=t;
}