#include<stdio.h> 
#include<stdlib.h> 
#include<string.h>
#include<limits.h> 

#include"ALGraph.h"

void DFS(ALGraph G,int v);          //从第v个顶点出发递归地深度优先遍历图G
void DFSTraverse(ALGraph G);        // 对图G作深度优先遍历
int  visited[MAX_VERTEX_NUM];       // 访问标志数组(全局量)

int main()
{
	ALGraph g;
	CreateGraphF (g); // 利用数据文件创建图
	Display(g);       // 输出图
	printf("深度优先遍历序列:\n"); 
	DFSTraverse(g);	
	return 0;
}


void DFS(ALGraph G,int v)
{   //从第v个顶点出发递归地深度优先遍历图G
	/********** Begin **********/
    ArcNode *p;
	p=G.vertices[v].firstarc;
	visited[v]=1;
	visit(G.vertices[v].data);
	for(int w=FirstAdjVex(G,G.vertices[v].data);w>=0;w=NextAdjVex(G,G.vertices[v].data,G.vertices[w].data)){
		if(!visited[w])
			DFS(G,w);
	}
	/********** End **********/
}

void DFSTraverse(ALGraph G)
{   // 对图G作深度优先遍历
	/********** Begin **********/
    int v;
	for(v=0;v<G.vexnum;v++)
		visited[v]=0;
	for(v=0;v<G.vexnum;v++)
		if(!visited[v])
			DFS(G,v);
	printf("\n");
	/********** End **********/
}
posted on 2024-06-20 17:36  findscripter  阅读(9)  评论(0)    收藏  举报