예제 #1
0
//Traverse the graph from vertex v
void GraphAdjArray::DFSTree(int v, PCSTreeNode t, bool* visited)
{
	visited[v] = true;
	bool first = true;

	CSTreeNode *p = NULL, *q = NULL;
	int w;
	for(w = FirstAdjVertex(v); w >= 0; w = NextAdjVertex(v, w))
	{
		if(visited[w])
		{
			continue;
		}

		p = new CSTreeNode(&vexs[w]);
		if(first)
		{
			t->firstchild = p;
			first = false;
		}
		else
		{
			assert(q != NULL);
			q->nextsibling = p;
		}
		q = p;

		DFSTree(w, p, visited);
	}
}
예제 #2
0
void GraphAdjArray::BFSTraverse(GraphEnumProc pProc, void* lParam)
{
	if(vexnum <= 0)
	{
		return;
	}
	bool* visited = new bool[vexnum];
	memset(visited, 0, sizeof(bool) * vexnum);
	
	std::queue<int> que;
	
	int v, u, w;
	for(v = 0; v < vexnum; v++)
	{
		if(visited[v])
		{
			continue;
		}

		//visit before the vertex is enqueued
		visited[v] = true;
		if(!pProc(&vexs[v], lParam))
		{
			return;
		}
		//enqueue
		que.push(v);


		while(!que.empty())
		{
			//the vertex must be visited already
			u = que.front();
			que.pop();

			//push all the adjacent vertices of "u" to the queue
			for(w = FirstAdjVertex(u); w >= 0; w = NextAdjVertex(u, w))
			{
				if(!visited[w])
				{
					//visit before the vertex is enqueued
					visited[w] = true;
					if(!pProc(&vexs[w], lParam))
					{
						return;
					}
					//enqueue
					que.push(w);
				}
			}
		}
	}

	delete visited;
}
예제 #3
0
void GraphAdjList::BFSTraverse(GraphEnumProc pProc, void* lParam)
{
	if(vexnum <= 0)
	{
		return;
	}
	
	bool* visited = new bool[vexnum];
	memset(visited, 0, sizeof(bool) * vexnum);
	
	std::queue<int> que;
	int u, w;
	for(int v = 0; v < vexnum; v++)
	{
		if(visited[v])
		{
			continue;
		}

		//visit before enqueueing
		visited[v] = true;
		if(!pProc(&vexs[v].vertexinfo, lParam))
		{
			return;
		}
		que.push(v);
	
		while(!que.empty())
		{
			u = que.front();
			que.pop();

			for(ArcNode* pArc = FirstAdjVertex(u); pArc != NULL; pArc = NextAdjVertex(u, pArc))
			{
				w = pArc->arcinfo.to;
				if(visited[w])
				{
					continue;
				}

				visited[w] = true;
				if(!pProc(&vexs[w].vertexinfo, lParam))
				{
					return;
				}
				que.push(w);
			}
		}
	}
	
	delete visited;
}
예제 #4
0
bool GraphAdjArray::DFS(GraphEnumProc pProc, void* lParam, int vertex, bool* visited)
{
	visited[vertex] = true;
	if(!pProc(&vexs[vertex], lParam))
	{
		return false;
	}

	int w;
	for(w = FirstAdjVertex(vertex); w >= 0; w = NextAdjVertex(vertex, w))
	{
		if(!visited[w])
		{
			if(!DFS(pProc, lParam, w, visited))
			{
				return false;
			}
		}
	}
	return true;
}
예제 #5
0
bool GraphAdjList::DFS(GraphEnumProc pProc, void* lParam, int v, bool* visited)
{
	//visit vertex v now
	visited[v] = true;
	if(!pProc(&vexs[v].vertexinfo, lParam))
	{
		return false;
	}

	for(ArcNode* pArc = FirstAdjVertex(v); pArc != NULL; pArc = NextAdjVertex(v, pArc))
	{
		if(!visited[pArc->arcinfo.to])
		{
			if(!DFS(pProc, lParam, pArc->arcinfo.to, visited))
			{
				return false;
			}
		}
	}
	return true;
}
예제 #6
0
파일: DFS_d.c 프로젝트: zmr961006/DSAA
void  DFS(Graph *G,int v){
    stack *S;
    S = INIT_stack();
    int vex[MAXVEX];

    push(S,v);
    while(!isEmpty(S)){
        v = pop(S);
        if(!vex[v]){
            visit(v);
            vex[v] = 1;
        }
        w = FirstAdjVertex(G,v);
        while(w != -1){
            if(!vex[w])
                push(S,w);
            w = NextAdjVertex(G,v,w);
        }
    }

}