C语言 图的深度 遍历

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define GVexnum 8
int visited[GVexnum];//define as the global array

//---------define the graphic--------------
typedef struct ArcNode{
	int adjvex;//该斛指向的顶点的位置
	struct ArcNode *nextarc;//指向下一条弧的指针
}ArcNode;

typedef struct VNode{
	int data;//定点信息
	ArcNode *firstarc;//指向第一条依附于该顶点的弧的指针
}VNode,AdjList[20];

typedef struct{
	AdjList vertices;
	int vexnum,arcnum;//弧的当前顶点数和弧数
	int kind;//图的种类和标记
}Graphic;

int DFS_Traverse(Graphic,int,int (* visit)(Graphic,int));
int DFS_Traverse_version2(Graphic,int,int (* visit)(Graphic,int));
int DFS_visit(Graphic,int,int(* visit)(Graphic,int));
int visit_print(Graphic,int);
int main(){
	//-------------the created graphic-----------------------------
	Graphic G;//create a graphic
	G.vexnum=8;G.arcnum=9;G.kind=0;// kind=0 means 无向图
	ArcNode V12,V13;
	ArcNode	V21,V24,V25;
	ArcNode V31,V36,V37;
	ArcNode V42,V48;
	ArcNode V52,V56;
	ArcNode V63,V67;
	ArcNode V73,V76;
	ArcNode V84,V85;
	G.vertices[0].data=11;G.vertices[0].firstarc=&V12;V12.adjvex=1;V12.nextarc=&V13;V13.adjvex=2;V13.nextarc=NULL;
	G.vertices[1].data=12;G.vertices[1].firstarc=&V21;V21.adjvex=0;V21.nextarc=&V24;V24.adjvex=3;V24.nextarc=&V25;V25.adjvex=4;V25.nextarc=NULL;
	G.vertices[2].data=13;G.vertices[2].firstarc=&V31;V31.adjvex=0;V31.nextarc=&V36;V36.adjvex=5;V36.nextarc=&V37;V37.adjvex=6;V37.nextarc=NULL;
	G.vertices[3].data=14;G.vertices[3].firstarc=&V42;V42.adjvex=1;V42.nextarc=&V48;V48.adjvex=7;V48.nextarc=NULL;
	G.vertices[4].data=15;G.vertices[4].firstarc=&V52;V52.adjvex=1;V52.nextarc=&V56;V56.adjvex=5;V56.nextarc=NULL;
	G.vertices[5].data=16;G.vertices[5].firstarc=&V63;V63.adjvex=2;V63.nextarc=&V67;V67.adjvex=6;V67.nextarc=NULL;
	G.vertices[6].data=17;G.vertices[6].firstarc=&V73;V73.adjvex=2;V73.nextarc=&V76;V76.adjvex=5;V76.nextarc=NULL;
	G.vertices[7].data=18;G.vertices[7].firstarc=&V84;V84.adjvex=3;V84.nextarc=&V85;V85.adjvex=4;V85.nextarc=NULL;
	//------------the created graphic--------------------------------


	DFS_Traverse(G,0,visit_print);//the output should be 11 12 14 18 15 16 13 17
//	DFS_Traverse_version2(G,0,visit_print);//the output should be 11 12 14 18 15 13 16 17
//	printf("\n");
//	DFS_Traverse_version2(G,1,visit_print);//the output should be 12 11 13 16 17 14 18 15
	return OK;
}


//这个函数和行参v没有关系。这是因为,有一些有向图,从某一个起点可能没有办法遍历全部的点。不过我们可以做些改进,让从某个起点开始,仍然可以遍历
//全部节点
int DFS_Traverse(Graphic G,int v,int (* visit)(Graphic,int)){//v is the node that we come in
	int i;
	for(i=0;i<G.vexnum;i++)
		visited[i]=0;
	for(i=0;i<G.vexnum;i++)
		if(!visited[i])
			DFS_visit(G,i,visit);

	return 1;
}

int DFS_Traverse_version2(Graphic G,int v,int (* visit)(Graphic,int)){//v is the node that we come in
	int i;
	for(i=0;i<G.vexnum;i++)
		visited[i]=0;
	if(!visited[v])
		DFS_visit(G,v,visit);
	for(i=0;i<G.vexnum;i++){
		if(i!=v && !visited[i])
			DFS_visit(G,i,visit);
	}
	return 1;
}

int DFS_visit(Graphic G,int i,int (* visit)(Graphic,int)){
	visited[i]=1;
	visit(G,i);
	ArcNode * pr=G.vertices[i].firstarc;
	while(pr!=NULL){
		int w=pr->adjvex;
		if(!visited[w])
			DFS_visit(G,w,visit);
		pr=pr->nextarc;
	}
	return 1;
}


int visit_print(Graphic G,int i){
	printf("%d ",G.vertices[i].data);
	return 0;//0 means the i-th node has been visited
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值