//图的基本操作:1,深度优先遍历 2,广度优先遍历 #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 int visited[MAX_VERTEX_NUM]; //标志数组作为全局变量 typedef enum { DG, DN, UDG, UDN }GraphKind; typedef char VertexType; typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; }ArcNode; typedef struct VNode { VertexType data; ArcNode *firstarc; }VNode,AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vexnum,arcnum; GraphKind kind; }ALGraph; void CreateAdjGraph(ALGraph &G) { int i,j,k; ArcNode *s; printf("Please input n and e:\n"); //输入顶点数与边数 scanf("%d,%d",&G.vexnum,&G.arcnum); getchar(); printf("Please input %d vertex:",G.vexnum); for(i=0;i<G.vexnum;i++) { scanf("%c",&G.vertices[i].data); //读入顶点信息 G.vertices[i].firstarc = NULL; //边表置为空表 } printf("Please input %d edges:\n",G.arcnum); for(k=0;k<G.arcnum;k++) //循环e次建立边表 { scanf("%d,%d",&i,&j); //逆序输入无序对(i,j) s = (ArcNode*)malloc(sizeof(ArcNode)); s->adjvex = j; //邻接点序号为j s->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc = s; //将新结点*s插入顶点vi的边表头部(头插法) s = (ArcNode*)malloc(sizeof(ArcNode)); s->adjvex = i; //邻接点序号为i s->nextarc = G.vertices[j].firstarc; G.vertices[j].firstarc = s; //将新结点*s插入顶点vj的边表头部(头插法) } printf("\n"); for(i=0;i<G.vexnum;i++) //显示已建立的邻接表信息 { printf("%2c",G.vertices[i].data); //顶点结点 s = G.vertices[i].firstarc; while(s) { printf("->%2d",s->adjvex); //边表结点 s = s->nextarc; } printf("\n"); } } void dfs(ALGraph G,int i) {//从vi出发 ArcNode *w; //先访问顶点i printf("->%c",G.vertices[i].data); visited[i] = 1; w = G.vertices[i].firstarc; while(w) {//从w的邻接点出发深度优先搜索 if(!visited[w->adjvex]) dfs(G,w->adjvex); //递归调用 w = w->nextarc; } } void dfstraverse(ALGraph G) {//深度优先遍历 int i; for(i=0;i<G.vexnum;i++) { visited[i] = 0; //初始化标志数组 } printf("dfstraverse visit vertex:"); for(i=0;i<G.vexnum;i++) if(!visited[i]) //vi未访问过 dfs(G,i); }//dfstraverse void bfs(ALGraph G,int v) { int qu[MAX_VERTEX_NUM],f=0,r=0; ArcNode *w; printf("%c",G.vertices[v].data); visited[v]=1; qu[0]=v; r++; //初始化队列 while(f<r) { v = qu[f++]; //出队 w = G.vertices[v].firstarc; while(w) { v = w->adjvex; if(visited[v]==0) { visited[v]=1; printf("->%c",G.vertices[v].data); qu[r++]=v; //入队 } w = w->nextarc; } } } void bfstraverse(ALGraph G) {//广度优先遍历 int i; for(i=0;i<G.vexnum;i++) visited[i] = 0; //初始化标志数组 printf("bfstraverse visit vertex:"); for(i=0;i<G.vexnum;i++) if(visited[i]==0) //vi未访问过 bfs(G,i); }//bftraverse void main() { ALGraph G; int xz = 1; while(xz) { printf(" 图的建立及其遍历\n"); printf("==============================\n"); printf("1,建立无向图的邻接表\n"); printf("2,图的深度优先遍历\n"); printf("3,图的广度优先遍历\n"); printf("0,退出系统\n"); printf("==============================\n"); printf("请选择:(0~3)\n"); scanf("%d",&xz); getchar(); switch(xz) { //顶点数为8,边数为9 //顶点为:12345678或ABCDEFGH //边为:7,4、7,3/6,5/6,2/5,2/4,1/3,1/2,0/1,0 case 1:printf("输入无向图的相关信息:\n"); G.kind = UDN; CreateAdjGraph(G); printf("图的邻接表存储结构建立完成\n"); printf("\n"); break; case 2:printf("该图的深度优先遍历序列是:\n"); dfstraverse(G); //1->2->4->8->5->3->6->7 //或A->B->D->H->E->C->F->G printf("\n"); break; case 3:printf("该图的广度优先遍历序列是:\n"); bfstraverse(G); //1->2->3->4->5->6-7->8 //或A->B->C->D->E->F-G printf("\n"); break; case 0:break; default:break; } } }