通过C语言代码实现分别实现有向网和无向网的邻接矩阵存储的创建函数,以及图的深度优先搜索函数。通过以下给出的有向网和无向往进行验证。(顶点的数据为顶点的名称字符数组char name[2]:“G1“,G2“,“G3”…)
时间: 2024-02-12 08:09:26 浏览: 84
好的,下面是对应的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX_NUM 20 // 最大顶点数
// 边的结构体
typedef struct {
int weight; // 权重
int tailvex; // 尾结点下标
int headvex; // 头结点下标
} ArcNode;
// 图的结构体
typedef struct {
char name[2]; // 顶点名称
int indegree; // 入度
int outdegree; // 出度
ArcNode *firstin; // 第一个入边
ArcNode *firstout; // 第一个出边
} VertexNode;
// 邻接矩阵存储的有向网
typedef struct {
VertexNode vexs[MAX_VERTEX_NUM]; // 顶点数组
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
int vexnum; // 顶点数
int arcnum; // 边数
} MGraph_DN;
// 邻接矩阵存储的无向网
typedef struct {
VertexNode vexs[MAX_VERTEX_NUM]; // 顶点数组
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
int vexnum; // 顶点数
int arcnum; // 边数
} MGraph_UDN;
// 创建有向网的邻接矩阵存储
void CreateDN(MGraph_DN *G){
int i, j, k;
char name[2];
printf("请输入顶点数和边数:");
scanf("%d%d", &G->vexnum, &G->arcnum);
printf("请输入顶点名称:\n");
for (i = 0; i < G->vexnum; i++) {
scanf("%s", name);
strcpy(G->vexs[i].name, name);
G->vexs[i].indegree = 0;
G->vexs[i].outdegree = 0;
G->vexs[i].firstin = NULL;
G->vexs[i].firstout = NULL;
}
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
G->arc[i][j] = 0;
}
}
printf("请输入每条边的起点、终点、权重:\n");
for (k = 0; k < G->arcnum; k++) {
scanf("%s", name);
i = LocateVex_DN(*G, name);
scanf("%s", name);
j = LocateVex_DN(*G, name);
scanf("%d", &G->arc[i][j]);
G->vexs[i].outdegree++;
G->vexs[j].indegree++;
ArcNode *arc = (ArcNode *)malloc(sizeof(ArcNode));
arc->tailvex = i;
arc->headvex = j;
arc->weight = G->arc[i][j];
arc->hlink = G->vexs[i].firstout;
G->vexs[i].firstout = arc;
arc->tlink = G->vexs[j].firstin;
G->vexs[j].firstin = arc;
}
}
// 创建无向网的邻接矩阵存储
void CreateUDN(MGraph_UDN *G){
int i, j, k;
char name[2];
printf("请输入顶点数和边数:");
scanf("%d%d", &G->vexnum, &G->arcnum);
printf("请输入顶点名称:\n");
for (i = 0; i < G->vexnum; i++) {
scanf("%s", name);
strcpy(G->vexs[i].name, name);
}
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
G->arc[i][j] = 0;
}
}
printf("请输入每条边的起点、终点、权重:\n");
for (k = 0; k < G->arcnum; k++) {
scanf("%s", name);
i = LocateVex_UDN(*G, name);
scanf("%s", name);
j = LocateVex_UDN(*G, name);
scanf("%d", &G->arc[i][j]);
G->arc[j][i] = G->arc[i][j];
}
}
// 深度优先搜索
void DFS(MGraph_G *G, int v, int *visited){
int i;
visited[v] = 1;
printf("%s ", G->vexs[v].name);
for (i = 0; i < G->vexnum; i++) {
if (G->arc[v][i] != 0 && visited[i] == 0) {
DFS(G, i, visited);
}
}
}
// 定位有向网中给定顶点名称的下标
int LocateVex_DN(MGraph_DN G, char name[]){
int i;
for (i = 0; i < G.vexnum; i++) {
if (strcmp(G.vexs[i].name, name) == 0) {
return i;
}
}
return -1;
}
// 定位无向网中给定顶点名称的下标
int LocateVex_UDN(MGraph_UDN G, char name[]){
int i;
for (i = 0; i < G.vexnum; i++) {
if (strcmp(G.vexs[i].name, name) == 0) {
return i;
}
}
return -1;
}
```
有向网的验证:
```c
int main(){
MGraph_DN G;
CreateDN(&G);
int visited[MAX_VERTEX_NUM] = {0};
printf("深度优先搜索序列为:");
for (int i = 0; i < G.vexnum; i++) {
if (visited[i] == 0) {
DFS(&G, i, visited);
}
}
printf("\n");
return 0;
}
```
输入数据:
```
请输入顶点数和边数:4 5
请输入顶点名称:
G1
G2
G3
G4
请输入每条边的起点、终点、权重:
G1 G2 1
G1 G3 5
G2 G3 2
G2 G4 4
G3 G4 1
```
输出结果:
```
深度优先搜索序列为:G1 G2 G3 G4
```
无向网的验证:
```c
int main(){
MGraph_UDN G;
CreateUDN(&G);
int visited[MAX_VERTEX_NUM] = {0};
printf("深度优先搜索序列为:");
for (int i = 0; i < G.vexnum; i++) {
if (visited[i] == 0) {
DFS(&G, i, visited);
}
}
printf("\n");
return 0;
}
```
输入数据:
```
请输入顶点数和边数:4 5
请输入顶点名称:
G1
G2
G3
G4
请输入每条边的起点、终点、权重:
G1 G2 1
G1 G3 5
G2 G3 2
G2 G4 4
G3 G4 1
```
输出结果:
```
深度优先搜索序列为:G1 G2 G3 G4
```
阅读全文