C语言邻接矩阵为 {0, 6, 1, 5, 0, 0}, {6, 0, 5, 0, 3, 0}, {1, 5, 0, 5, 6, 4}, {5, 0, 5, 0, 0, 2}, {0, 3, 6, 0, 0, 6}, {0, 0, 4, 2, 6, 0},1、用邻接表和邻接矩阵分别表示该图全代码。 2、基于邻接表或邻接矩阵输出每个结点的度全代码。 3、用普里姆算法和克鲁斯卡尔算法实现本图的最小生成树(此类算法为贪心算法)全代码
时间: 2023-09-26 09:05:02 浏览: 97
c语言邻接矩阵
好的,我会依次回答你的问题:
1. 邻接矩阵和邻接表的代码
邻接矩阵的代码:
```c
#define MAX_VERTEX_NUM 6 // 最大顶点数
typedef struct {
int vexs[MAX_VERTEX_NUM]; // 存放顶点值
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存放边的权值
int vexnum, arcnum; // 顶点数和边数
} MGraph;
void CreateMGraph(MGraph* G) {
// 初始化顶点数和边数
G->vexnum = MAX_VERTEX_NUM;
G->arcnum = 10;
// 初始化顶点
for (int i = 0; i < G->vexnum; i++) {
G->vexs[i] = i;
}
// 初始化边
memset(G->arcs, 0, sizeof(G->arcs));
G->arcs[0][1] = G->arcs[1][0] = 6;
G->arcs[0][2] = G->arcs[2][0] = 1;
G->arcs[0][3] = G->arcs[3][0] = 5;
G->arcs[1][2] = G->arcs[2][1] = 5;
G->arcs[1][4] = G->arcs[4][1] = 3;
G->arcs[2][3] = G->arcs[3][2] = 5;
G->arcs[2][4] = G->arcs[4][2] = 6;
G->arcs[2][5] = G->arcs[5][2] = 4;
G->arcs[3][5] = G->arcs[5][3] = 2;
G->arcs[4][5] = G->arcs[5][4] = 6;
}
```
邻接表的代码:
```c
#define MAX_VERTEX_NUM 6 // 最大顶点数
typedef struct ArcNode {
int adjvex; // 邻接点下标
int weight; // 权值
struct ArcNode* next; // 下一个邻接点
} ArcNode;
typedef struct {
int data; // 顶点值
ArcNode* firstarc; // 第一个邻接点
} VNode;
typedef struct {
VNode adjlist[MAX_VERTEX_NUM]; // 存储各个顶点
int vexnum, arcnum; // 顶点数和边数
} ALGraph;
void CreateALGraph(ALGraph* G) {
// 初始化顶点数和边数
G->vexnum = MAX_VERTEX_NUM;
G->arcnum = 10;
// 初始化顶点
for (int i = 0; i < G->vexnum; i++) {
G->adjlist[i].data = i;
G->adjlist[i].firstarc = NULL;
}
// 初始化边
ArcNode* node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 1;
node->weight = 6;
node->next = G->adjlist[0].firstarc;
G->adjlist[0].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 2;
node->weight = 1;
node->next = G->adjlist[0].firstarc;
G->adjlist[0].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 3;
node->weight = 5;
node->next = G->adjlist[0].firstarc;
G->adjlist[0].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 0;
node->weight = 6;
node->next = G->adjlist[1].firstarc;
G->adjlist[1].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 2;
node->weight = 5;
node->next = G->adjlist[1].firstarc;
G->adjlist[1].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 4;
node->weight = 3;
node->next = G->adjlist[1].firstarc;
G->adjlist[1].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 0;
node->weight = 1;
node->next = G->adjlist[2].firstarc;
G->adjlist[2].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 1;
node->weight = 5;
node->next = G->adjlist[2].firstarc;
G->adjlist[2].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 3;
node->weight = 5;
node->next = G->adjlist[2].firstarc;
G->adjlist[2].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 4;
node->weight = 6;
node->next = G->adjlist[2].firstarc;
G->adjlist[2].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 5;
node->weight = 4;
node->next = G->adjlist[2].firstarc;
G->adjlist[2].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 2;
node->weight = 5;
node->next = G->adjlist[3].firstarc;
G->adjlist[3].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 5;
node->weight = 2;
node->next = G->adjlist[3].firstarc;
G->adjlist[3].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 1;
node->weight = 3;
node->next = G->adjlist[4].firstarc;
G->adjlist[4].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 2;
node->weight = 6;
node->next = G->adjlist[4].firstarc;
G->adjlist[4].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 3;
node->weight = 2;
node->next = G->adjlist[5].firstarc;
G->adjlist[5].firstarc = node;
node = (ArcNode*)malloc(sizeof(ArcNode));
node->adjvex = 2;
node->weight = 4;
node->next = G->adjlist[5].firstarc;
G->adjlist[5].firstarc = node;
}
```
2. 输出每个结点的度的代码
基于邻接矩阵的代码:
```c
void GetDegreeByMGraph(MGraph G, int* indegree, int* outdegree) {
memset(indegree, 0, sizeof(int) * G.vexnum);
memset(outdegree, 0, sizeof(int) * G.vexnum);
for (int i = 0; i < G.vexnum; i++) {
for (int j = 0; j < G.vexnum; j++) {
if (G.arcs[i][j] != 0) {
outdegree[i]++;
indegree[j]++;
}
}
}
}
```
基于邻接表的代码:
```c
void GetDegreeByALGraph(ALGraph G, int* indegree, int* outdegree) {
memset(indegree, 0, sizeof(int) * G.vexnum);
memset(outdegree, 0, sizeof(int) * G.vexnum);
for (int i = 0; i < G.vexnum; i++) {
ArcNode* node = G.adjlist[i].firstarc;
while (node != NULL) {
outdegree[i]++;
indegree[node->adjvex]++;
node = node->next;
}
}
}
```
3. 普里姆算法和克鲁斯卡尔算法的代码
普里姆算法的代码:
```c
void Prim(MGraph G, int* parent) {
int lowcost[MAX_VERTEX_NUM]; // 存放当前节点到已选节点的最小边权值
int closest[MAX_VERTEX_NUM]; // 存放当前节点到已选节点最小边的终点
bool s[MAX_VERTEX_NUM]; // 存放是否已选节点
// 初始化
for (int i = 0; i < G.vexnum; i++) {
lowcost[i] = G.arcs[0][i];
closest[i] = 0;
s[i] = false;
}
s[0] = true;
// 依次选取节点
for (int i = 1; i < G.vexnum; i++) {
int min = INT_MAX;
int j = 0;
// 找到当前未选节点中到已选节点的最小值
for (int k = 1; k < G.vexnum; k++) {
if (!s[k] && lowcost[k] < min) {
min = lowcost[k];
j = k;
}
}
s[j] = true;
parent[j] = closest[j];
// 更新到未选节点的最小值
for (int k = 1; k < G.vexnum; k++) {
if (!s[k] && G.arcs[j][k] < lowcost[k]) {
lowcost[k] = G.arcs[j][k];
closest[k] = j;
}
}
}
}
```
克鲁斯卡尔算法的代码:
```c
typedef struct {
int u, v, weight;
} Edge;
// 边的比较函数,按权值从小到大排序
int compare(const void* a, const void* b) {
return ((Edge*)a)->weight - ((Edge*)b)->weight;
}
int Find(int* parent, int f) {
while (parent[f] > 0) {
f = parent[f];
}
return f;
}
void Kruskal(MGraph G, int* parent) {
Edge edges[MAX_VERTEX_NUM * (MAX_VERTEX_NUM - 1) / 2];
int k = 0;
// 获取所有边
for (int i = 0; i < G.vexnum; i++) {
for (int j = i + 1; j < G.vexnum; j++) {
if (G.arcs[i][j] != 0) {
edges[k].u = i;
edges[k].v = j;
edges[k].weight = G.arcs[i][j];
k++;
}
}
}
// 按权值从小到大排序
qsort(edges, k, sizeof(Edge), compare);
memset(parent, -1, sizeof(int) * G.vexnum);
// 依次加入边
for (int i = 0; i < k; i++) {
int u = Find(parent, edges[i].u);
int v = Find(parent, edges[i].v);
if (u != v) {
parent[u] = v;
}
}
}
```
完整代码如下:
阅读全文