深度优先遍历
作者:互联网
深度优先遍历
#include<stdio.h>
#include<stdlib.h>
#define MAX_VER_NUM 20
//DG有向图 UDG无向图
typedef enum { DG, UDG }GraphKind;
typedef struct ArcNode
{
int adjvex; //节点数值
struct ArcNode* next; //指向邻接节点的指针
}ArcNode;
typedef struct VertexNode
{
char vexdata; //节点的数值
ArcNode* head; //头指针
}VertexNode;
typedef struct
{
VertexNode vertex[MAX_VER_NUM];
int vexnum, arcnum;
GraphKind Type;
}LGraph;
//索引vex位置并返回
int index(char vex, LGraph* lg)
{
int i;
for (i = 1;i <= lg->vexnum;i++)
{
if (lg->vertex[i].vexdata == vex)
{
return i;
}
}
return 0;
}
void Creat_LG(LGraph* lg)
{
int i, j, v1_index, v2_index, type;
char v1, v2;
ArcNode* p1, * p2;
printf("please input graph type 0.DG 1.UDG. input nums:");
scanf_s("%d", &type);
if (type == 0)
{
lg->Type = DG;
}
else if (type == 1)
{
lg->Type = UDG;
}
else
{
printf("please input 0 or 1!!!");
return;
}
printf("Please input vexnum:");
//顶点数
scanf_s("%d", &lg->vexnum);
printf("Please input arcnum:");
//弧数
scanf_s("%d", &lg->arcnum);
getchar();
for (i = 1;i <= lg->vexnum;i++)
{
lg->vertex[i].head = NULL;
printf("Please input %d vex:", i);
scanf_s("%c", &lg->vertex[i].vexdata,1);
getchar();
}
for (j = 1;j <= lg->arcnum;j++)
{
printf("Please input %d arc(vex1 vex2):", j);
scanf_s("%c %c", &v1,1,&v2,1);
getchar();
v1_index = index(v1, lg);
v2_index = index(v2, lg);
if (lg->Type == 1)
{
p1 = (ArcNode*)malloc(sizeof(ArcNode));
p1->adjvex = v2_index;
//前插
p1->next = lg->vertex[v1_index].head;
lg->vertex[v1_index].head = p1;
p2 = (ArcNode*)malloc(sizeof(ArcNode));
p2->adjvex = v1_index;
//前插
p2->next = lg->vertex[v2_index].head;
lg->vertex[v2_index].head = p2;
}
else
{
p1 = (ArcNode*)malloc(sizeof(ArcNode));
p1->adjvex = v2_index;
p1->next = lg->vertex[v1_index].head;
lg->vertex[v1_index].head = p1;
}
}
}
void Print_LG(LGraph* lg)
{
int i;
ArcNode* p;
if (lg->Type == 0)
printf("Is Direct Graph\n");
else
printf("Is UNDirect Graph\n");
for (i = 1;i <= lg->vexnum;i++)
{
printf("%c", lg->vertex[i].vexdata);
p = lg->vertex[i].head;
while (p)
{
printf("->%c", lg->vertex[p->adjvex].vexdata);
p = p->next;
}
printf("\n");
}
}
//深度优先遍历递归
int visit1[MAX_VER_NUM];
void visit(LGraph* lg, int v)
{
printf("%c", lg->vertex[v].vexdata);
}
void DFS1(LGraph* lg, int v0)
{
ArcNode* p;
visit1[v0] = 1;
visit(lg, v0);
p = lg->vertex[v0].head;
while (p)
{
if (!visit1[p->adjvex])
DFS1(lg, p->adjvex);
p = p->next;
}
}
void DFS1Traverse(LGraph* lg)
{
int v;
for (v = 1;v <= lg->vexnum;v++)
visit1[v] = 0;
for (v = 1; v <= lg->vexnum; v++)
{
if (!visit1[v])
DFS1(lg, v);
}
}
//图的深度优先遍历非递归 数组栈
void DFS2(LGraph* lg, int v0)
{
int stack[MAX_VER_NUM];
ArcNode* p;
int visit[MAX_VER_NUM] = { 0 }, top = -1, k = 0;
p = lg->vertex[v0].head;
stack[++top] = v0;
visit[v0] = 1;
printf("%c", lg->vertex[v0].vexdata);
while (top != -1)
{
while (p != NULL &&visit[p->adjvex] != 1)
{ //将该顶点的所有邻边元素进栈
stack[++top] = p->adjvex;
p = p->next;
}
k = stack[top--]; //取栈顶元素
p = lg->vertex[k].head; //指向该顶点的第一条边
//如果到最后一个都已经返问则出栈,去上一个栈元素
if (p == NULL && lg->vertex[k].head != NULL)
{
--top; //出栈
}
else
{
if (lg->vertex[k].head != NULL && !visit[k])
{
printf("%c", lg->vertex[k].vexdata);
visit[k] = 1;
stack[++top] = p->adjvex; //在进栈
}
else
{
if (!visit[k])
{
printf("%c", lg->vertex[k].vexdata);
visit[k] = 1;
}
}
}
}
}
int main()
{
LGraph LG;
Creat_LG(&LG);
Print_LG(&LG);
printf("DFS1:");
DFS1Traverse(&LG);
printf("\n");
printf("DFS2:");
DFS2(&LG, 1);
printf("\n");
return 0;
}
标签:lg,优先,遍历,index,深度,vertex,head,ArcNode,printf 来源: https://blog.csdn.net/qq_45768871/article/details/104847854