7.3 图的遍历-深度优先搜索
邻接矩阵:
//邻接矩阵
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define FALSE 0
typedef int VRType;
typedef int VertexType;
typedef int Status;
//-------图的数组(邻接矩阵)存储表示--------
#define Infinity 10 //最大值
#define MaxVertexNum 20 //最大顶点个数
typedef enum {DG,DN,UDG,UDN} GraphKind;
typedef struct ArcCell{
VRType adj;//顶点关系类型。无权图:1或0表示是否相邻;带权图:为权值类型
}ArcCell,AdjMatrix[MaxVertexNum][MaxVertexNum];
typedef struct{
VertexType vex[MaxVertexNum]; //顶点向量
AdjMatrix arcs; //邻接矩阵
int vexnum,arcnum; //图的当前顶点数和弧数
GraphKind kind; //图的种类标志
}MGraph;
int LocateVex(MGraph G,int v){
for(int j=0;j<G.vexnum;j++){
if(v==G.vex[j]){
return j;
break;
}
}
}
Status CreateDG(MGraph &G){
printf("请输入图的顶点数目:\n");
scanf("%d",&G.vexnum);
printf("请输入图的边的数目:\n");
scanf("%d",&G.arcnum);
printf("请依次输入顶点:\n");
for(int i=0;i<G.vexnum;i++){
scanf("%d",&G.vex[i]); //顶点向量
}
//初始化邻接矩阵
for(int i=0;i<G.vexnum;i++){
for(int j=0;j<G.vexnum;j++){
G.arcs[i][j].adj=0;
}
}
for(int k=0;k<G.arcnum;k++){
int v1,v2;
printf("请输入第%d条弧依附的两个顶点v1->v2:\n",k+1);
scanf("%d",&v1);
scanf("%d",&v2);
int i=LocateVex(G,v1);
int j=LocateVex(G,v2);
G.arcs[i][j].adj=1;
}
return OK;
}
void DFS(MGraph G,int v,int visited[]){
visited[v]=1; //将该顶点由未被访问变为已访问
printf("%d ",G.vex[v]);
for(int i=0;i<G.vexnum;i++){
if(visited[i]==0&&G.arcs[v][i].adj==1){
DFS(G,i,visited); //对未访问的邻接顶点调用DFS
}
}
}
//对G作深度优先遍历
void DFSTraverse(MGraph G,int visited[]){
for(int i=0;i<G.vexnum;i++){
visited[i]=0; //初始都未被访问
}
for(int i=0;i<G.vexnum;i++){
if(visited[i]==0){
DFS(G,i,visited); //对未访问的顶点调用DFS
}
}
}
int main(){
MGraph G;
int visited[8]; //访问标志数组
CreateDG(G);
printf("\n构造的有向图为:\n");
for(int i=0;i<G.vexnum;i++){
for(int j=0;j<G.vexnum;j++){
printf("%d ",G.arcs[i][j].adj);
}
printf("\n");
}
printf("\n顶点的访问序列为:\n");
DFSTraverse(G,visited);
return 0;
}
运行结果:
请输入图的顶点数目:
8
请输入图的边的数目:
7
请依次输入顶点:
1 2 3 4 5 6 7 8
请输入第1条弧依附的两个顶点v1->v2:
1 2
请输入第2条弧依附的两个顶点v1->v2:
2 4
请输入第3条弧依附的两个顶点v1->v2:
4 8
请输入第4条弧依附的两个顶点v1->v2:
8 5
请输入第5条弧依附的两个顶点v1->v2:
1 3
请输入第6条弧依附的两个顶点v1->v2:
3 6
请输入第7条弧依附的两个顶点v1->v2:
6 7
构造的有向图为:
0 1 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
顶点的访问序列为:
1 2 4 8 5 3 6 7
邻接表:
//邻接表
#include<stdio.h>
#include<stdlib.h>
#define OK 1
typedef char VertexType;
typedef int Status;
//-------图的邻接表存储表示------
#define MaxVertexNum 20
typedef enum {DG,DN,UDG,UDN} GraphKind; //{有向图,有向网,无向图,无向网 }
typedef struct ArcNode{
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
// InfoType *info; //该弧相关信息的指针
}ArcNode;
typedef struct VNode{
VertexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
}VNode,AdjList[MaxVertexNum];
typedef struct{
AdjList vertices;
int vexnum,arcnum;
int kind;
}ALGraph;
Status CreateDG(ALGraph &G){
G.kind=DG;
printf("请输入图的顶点数目:\n");
scanf("%d",&G.vexnum);
printf("请输入图的边的数目:\n");
scanf("%d",&G.arcnum);
return OK;
}
void dfs(ALGraph G,int x,int visited[]){
ArcNode *pArc;
visited[x]=1;
printf("%c ",G.vertices[x].data);
for(pArc=G.vertices[x].firstarc;pArc!=NULL;pArc=pArc->nextarc)
if(visited[pArc->adjvex]==0)
dfs(G,pArc->adjvex,visited);
}
int main(){
ALGraph G;
int visited[8]={0,0,0,0,0,0,0,0};
CreateDG(G);
printf("请依次输入顶点:\n");
for(int i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
}
struct ArcNode *pArc1, *pArc2;
pArc1=(struct ArcNode*)malloc(sizeof(struct ArcNode));
pArc1->adjvex=2; G.vertices[0].firstarc=pArc1;
pArc2=(struct ArcNode*)malloc(sizeof(struct ArcNode));
pArc2->adjvex=1; pArc1->nextarc=pArc2;
pArc2->nextarc = NULL;
printf("\n邻接表为:\n");
printf("%c->",G.vertices[0].data);
while(pArc1){
printf("%d",pArc1->adjvex);
if(pArc1->nextarc!=NULL){
printf("->");
}
pArc1=pArc1->nextarc;
}
printf("^\n");
G.vertices[1].firstarc=NULL;
printf("%c^",G.vertices[1].data);
pArc1=(struct ArcNode*)malloc(sizeof(struct ArcNode));
pArc1->adjvex=3; G.vertices[2].firstarc=pArc1;
pArc1->nextarc=NULL;
printf("\n%c->",G.vertices[2].data);
while(pArc1){
printf("%d",pArc1->adjvex);
if(pArc1->nextarc!=NULL){
printf("->");
}
pArc1=pArc1->nextarc;
}
printf("^\n");
pArc1=(struct ArcNode*)malloc(sizeof(struct ArcNode));
pArc1->adjvex=0; G.vertices[3].firstarc=pArc1;
pArc1->nextarc=NULL;
printf("%c->",G.vertices[3].data);
while(pArc1){
printf("%d",pArc1->adjvex);
if(pArc1->nextarc!=NULL){
printf("->");
}
pArc1=pArc1->nextarc;
}
printf("^\n");
printf("\n顶点的访问序列为:\n");
dfs(G,0,visited);
return 0;
}
运行结果请输入图的顶点数目:
4
请输入图的边的数目:
4
请依次输入顶点:
A B C D
邻接表为:
A->2->1^
B^
C->3^
D->0^
顶点的访问序列为:
A C D B