请分别用c语言编程实现图的深度优先遍历和广度优先遍历算法
时间: 2024-05-07 21:21:13 浏览: 166
图的深度优先遍历与广度优先遍历(C语言实现)
5星 · 资源好评率100%
深度优先遍历算法的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
typedef struct ArcNode
{
int adjvex;
struct ArcNode *next;
} ArcNode;
typedef struct VNode
{
int data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
int visited[MAX_VERTEX_NUM]; // 记录顶点是否被访问过
void CreateGraph(ALGraph *G)
{
int i, j, k;
ArcNode *e;
printf("请输入图的顶点数和边数:");
scanf("%d%d", &(G->vexnum), &(G->arcnum));
printf("请输入%d个顶点:", G->vexnum);
for (i = 0; i < G->vexnum; i++)
{
scanf("%d", &(G->vertices[i].data));
G->vertices[i].firstarc = NULL;
}
printf("请输入%d条边,每条边由两个顶点组成:\n", G->arcnum);
for (k = 0; k < G->arcnum; k++)
{
scanf("%d%d", &i, &j);
e = (ArcNode *)malloc(sizeof(ArcNode));
e->adjvex = j;
e->next = G->vertices[i].firstarc;
G->vertices[i].firstarc = e;
e = (ArcNode *)malloc(sizeof(ArcNode));
e->adjvex = i;
e->next = G->vertices[j].firstarc;
G->vertices[j].firstarc = e;
}
}
void DFS(ALGraph G, int i)
{
ArcNode *p;
visited[i] = 1;
printf("%d ", G.vertices[i].data);
p = G.vertices[i].firstarc;
while (p != NULL)
{
if (visited[p->adjvex] == 0)
{
DFS(G, p->adjvex);
}
p = p->next;
}
}
void DFSTraverse(ALGraph G)
{
int i;
for (i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
}
for (i = 0; i < G.vexnum; i++)
{
if (visited[i] == 0)
{
DFS(G, i);
}
}
}
int main()
{
ALGraph G;
CreateGraph(&G);
printf("深度优先遍历序列:");
DFSTraverse(G);
return 0;
}
```
广度优先遍历算法的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
typedef struct ArcNode
{
int adjvex;
struct ArcNode *next;
} ArcNode;
typedef struct VNode
{
int data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
int visited[MAX_VERTEX_NUM]; // 记录顶点是否被访问过
void CreateGraph(ALGraph *G)
{
int i, j, k;
ArcNode *e;
printf("请输入图的顶点数和边数:");
scanf("%d%d", &(G->vexnum), &(G->arcnum));
printf("请输入%d个顶点:", G->vexnum);
for (i = 0; i < G->vexnum; i++)
{
scanf("%d", &(G->vertices[i].data));
G->vertices[i].firstarc = NULL;
}
printf("请输入%d条边,每条边由两个顶点组成:\n", G->arcnum);
for (k = 0; k < G->arcnum; k++)
{
scanf("%d%d", &i, &j);
e = (ArcNode *)malloc(sizeof(ArcNode));
e->adjvex = j;
e->next = G->vertices[i].firstarc;
G->vertices[i].firstarc = e;
e = (ArcNode *)malloc(sizeof(ArcNode));
e->adjvex = i;
e->next = G->vertices[j].firstarc;
G->vertices[j].firstarc = e;
}
}
void BFS(ALGraph G, int i)
{
int front = 0, rear = 0;
int queue[MAX_VERTEX_NUM];
ArcNode *p;
visited[i] = 1;
printf("%d ", G.vertices[i].data);
queue[rear++] = i;
while (front < rear)
{
i = queue[front++];
p = G.vertices[i].firstarc;
while (p != NULL)
{
if (visited[p->adjvex] == 0)
{
visited[p->adjvex] = 1;
printf("%d ", G.vertices[p->adjvex].data);
queue[rear++] = p->adjvex;
}
p = p->next;
}
}
}
void BFSTraverse(ALGraph G)
{
int i;
for (i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
}
for (i = 0; i < G.vexnum; i++)
{
if (visited[i] == 0)
{
BFS(G, i);
}
}
}
int main()
{
ALGraph G;
CreateGraph(&G);
printf("广度优先遍历序列:");
BFSTraverse(G);
return 0;
}
```
阅读全文