最小生成树之prim算法和克鲁斯卡尔算法

prim算法:
1)思想: //创建edg[][]二维数组储存图表,low[i]记录未加入点到加入点集合的最小距离,visited数组标记某点是否已加入集合,en[i]表示使i加入Enew的点。(恩人节点)
已知V和E,求Vnew和Enew
初始时令:vnew={v1}
在v-vnew中找出和vnew中点距离最近的点,加入vnew,
2)步骤
step1: 取节点1加入集合vnew,标记visit[1]=1,由节点1初始化low[],en[];
step2:进行n-1次如下操作,每进行一次加入一个点到vnew
2.1 找出low[i]中最小值对应下标pos,
2.2 标记visit[pos]=1;
2.3 更新low[]
初始化–》找–》标记–》更新

void prim(int edg[][n],int low[],int visited[],int en[],int enew[][n])
 {
    int i,j,pos,min;
    memset(visited,0,sizeof(visited));
    visited[1]=1;pos=1;//取第一个点,分别标记和记录该点
    for(i=1;i<=n;i++)
        if(i!=pos) 
        {
            low[i]=edg[pos][i];   //第一次给low数组赋值
            en[i]=pos;        
        }
    for(i=1;i<n;i++)//再运行n-1次
    {
        min=MaxInt;
        for(j=1;j<=n;j++)//找出最小权值并记录位置
            if(visited[j]==0&&min>low[j])
            {
                min=low[j];pos=j;
            }
        visited[pos]=1;//标记该点
        for(j=1;j<=n;j++)//更新权值
            if(visited[j]==0&&low[j]>map[pos][j])
            {
                low[j]=map[pos][j];
                en[j]=pos;
            }
    }
    memset(enew,0,sizeof(enew)); //二位数组enew初始化为0
    for(i=2;i<=n;i++)
        enew[en[i]][i]=1;//为1表示是最小生成树的边;  
}
以下是最小生成树的普里姆算法克鲁斯卡尔算法的C语言实现: 1. 普里姆算法 ```c #include <stdio.h> #include <limits.h> #define V 5 int minKey(int key[], bool mstSet[]) { int min = INT_MAX, min_index; for (int v = 0; v < V; v++) if (mstSet[v] == false && key[v] < min) min = key[v], min_index = v; return min_index; } void printMST(int parent[], int graph[V][V]) { printf("Edge \tWeight\n"); for (int i = 1; i < V; i++) printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]); } void primMST(int graph[V][V]) { int parent[V]; int key[V]; bool mstSet[V]; for (int i = 0; i < V; i++) key[i] = INT_MAX, mstSet[i] = false; key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = minKey(key, mstSet); mstSet[u] = true; for (int v = 0; v < V; v++) if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) parent[v] = u, key[v] = graph[u][v]; } printMST(parent, graph); } int main() { int graph[V][V] = { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 }, { 0, 3, 0, 0, 7 }, { 6, 8, 0, 0, 9 }, { 0, 5, 7, 9, 0 } }; primMST(graph); return 0; } ``` 2. 克鲁斯卡尔算法 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define V 5 #define E 9 struct Edge { int src, dest, weight; }; struct Graph { int V, E; struct Edge* edge; }; struct Graph* createGraph(int V, int E) { struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); graph->V = V; graph->E = E; graph->edge = (struct Edge*)malloc(graph->E * sizeof(struct Edge)); return graph; } struct subset { int parent; int rank; }; int find(struct subset subsets[], int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; } void Union(struct subset subsets[], int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank < subsets[yroot].rank) subsets[xroot].parent = yroot; else if (subsets[xroot].rank > subsets[yroot].rank) subsets[yroot].parent = xroot; else { subsets[yroot].parent = xroot; subsets[xroot].rank++; } } int myComp(const void* a, const void* b) { struct Edge* a1 = (struct Edge*)a; struct Edge* b1 = (struct Edge*)b; return a1->weight > b1->weight; } void KruskalMST(struct Graph* graph) { int V = graph->V; struct Edge result[V]; int e = 0; int i = 0; qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); struct subset* subsets = (struct subset*)malloc(V * sizeof(struct subset)); for (int v = 0; v < V; ++v) { subsets[v].parent = v; subsets[v].rank = 0; } while (e < V - 1 && i < graph->E) { struct Edge next_edge = graph->edge[i++]; int x = find(subsets, next_edge.src); int y = find(subsets, next_edge.dest); if (x != y) { result[e++] = next_edge; Union(subsets, x, y); } } printf("Following are the edges in the constructed MST\n"); for (i = 0; i < e; ++i) printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight); return; } int main() { int V = 5; int E = 9; struct Graph* graph = createGraph(V, E); graph->edge[0].src = 0; graph->edge[0].dest = 1; graph->edge[0].weight = 2; graph->edge[1].src = 0; graph->edge[1].dest = 3; graph->edge[1].weight = 6; graph->edge[2].src = 1; graph->edge[2].dest = 2; graph->edge[2].weight = 3; graph->edge[3].src = 1; graph->edge[3].dest = 4; graph->edge[3].weight = 5; graph->edge[4].src = 2; graph->edge[4].dest = 4; graph->edge[4].weight = 7; graph->edge[5].src = 3; graph->edge[5].dest = 4; graph->edge[5].weight = 9; KruskalMST(graph); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值