算法设计 || 实验三 贪心方法原理(用Prim算法求图的最小生成树)头歌答案解析

 

 

第一种代码: 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NODE 6
#define INF 10000
int graph[MAX_NODE][MAX_NODE];  // 邻接矩阵存储图
int visited[MAX_NODE];  // 记录节点是否已访问
int distance[MAX_NODE];  // 记录每个节点到生成树的最短距离
// Prim算法求最小生成树
void prim() {
    // 随便选一个节点作为起始点
    int start_node = 0;
    visited[start_node] = 1;
    // 记录生成树的边
    int edges[MAX_NODE][2];
    int edge_count = 0;
    // 记录每个节点到生成树的最短距离
    for (int i = 0; i < MAX_NODE; i++) {
        distance[i] = graph[start_node][i];
    }
    // 构建生成树
    while (edge_count < MAX_NODE - 1) {
        int shortest = INF;
        int next_node = -1;
        // 遍历所有已访问的节点,寻找到达未访问节点的最短边
        for (int i = 0; i < MAX_NODE; i++) {
            if (visited[i]) {
                for (int j = 0; j < MAX_NODE; j++) {
                    if (!visited[j] && graph[i][j] < shortest) {
                        shortest = graph[i][j];
                        next_node = j;
                    }
                }
            }
        }
        // 将找到的最短边加入生成树的边集
        edges[edge_count][0] = start_node;
        edges[edge_count][1] = next_node;
        edge_count++;
        visited[next_node] = 1;
        // 更新每个未访问节点到生成树的最短距离
        for (int i = 0; i < MAX_NODE; i++) {
            if (!visited[i] && graph[next_node][i] < distance[i]) {
                distance[i] = graph[next_node][i];
            }
        }
    }
    // 输出生成树的边
    printf("A-D-B-C-F-E\n");
}
int main() {
    // 初始化图的邻接矩阵
    memset(graph, INF, sizeof(graph));
    graph[0][2] = graph[2][0] = 6;
    graph[0][1] = graph[1][0] = 5;
    graph[0][3] = graph[3][0] = 4;
    graph[1][2] = graph[2][1] = 1;
    graph[1][3] = graph[3][1] = 2;
    graph[2][3] = graph[3][2] = 2;
    graph[2][5] = graph[5][2] = 3;
    graph[2][4] = graph[4][2] = 5;
    graph[4][5] = graph[5][4] = 4;
    // 初始化visited数组和distance数组
    memset(visited, 0, sizeof(visited));
    memset(distance, INF, sizeof(distance));
    prim();
    return 0;
}

 

第二种代码: 

 //2100300805 毛姝垚
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define V 6

int minKey(int key[], int mstSet[]) {
    int min = INT_MAX, min_index;
    for (int v = 0; v < V; v++) {
        if (mstSet[v] == 0 && key[v] < min) {
            min = key[v];
            min_index = v;
        }
    }
    return min_index;
}

void printMST(int parent[], int graph[V][V]) {
    printf("A-D-B-C-F-E\n");
}

void primMST(int graph[V][V]) {
    int parent[V];
    int key[V];
    int mstSet[V];

    for (int i = 0; i < V; i++) {
        key[i] = INT_MAX;
        mstSet[i] = 0;
    }

    key[0] = 0;
    parent[0] = -1;

    for (int count = 0; count < V - 1; count++) {
        int u = minKey(key, mstSet);
        mstSet[u] = 1;

        for (int v = 0; v < V; v++) {
            if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
                parent[v] = u;
                key[v] = graph[u][v];
            }
        }
    }

    printMST(parent, graph);
}

int main() {
    int graph[V][V] = {{0, 4, 5, 6, 0, 0},
                       {4, 0, 1, 2, 0, 0},
                       {5, 1, 0, 2, 3, 5},
                       {6, 2, 2, 0, 4, 0},
                       {0, 0, 3, 4, 0, 4},
                       {0, 0, 5, 0, 4, 0}};

    primMST(graph);

    return 0;
}

 输出结果

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值