void prim(adjmgraph g,minspantree closevertex[]) { vert x; int n=g.vertices.size,mincost; int *lowcost=(int *)malloc(sizeof(int)*n); int i,j,k=0; for(i=1;i<n;i++) lowcost[i]=g.edge[0][i]; Listget(g.vertices,0,&x); closevertex[0].vertex=x; lowcost[0]=-1; for(i=1;i<n;i++) { mincost=maxweight; for(j=1;j<n;j++) { if(lowcost[j]<mincost&&lowcost[j]>0) { mincost=lowcost[j]; k=j; } } } Listget(g.vertices,k,&x); closevertex[i].vertex=x; closevertex[i].weight=mincost; lowcost[k]=-1; for(j=1;j<n;j++) { if(g.edge[k][j]<lowcost[j]) lowcost[j]=g.edge[k][j]; } }
时间: 2024-04-02 15:33:36 浏览: 23
这是一个实现Prim最小生成树算法的代码。算法的基本思想是从图中的某个顶点开始,每次找到与当前生成树集合相邻的权值最小的顶点,将其加入生成树集合中,直到生成树集合包含所有顶点为止。具体实现中,代码首先初始化lowcost数组,表示当前生成树集合与其余顶点的最小权值。然后从第一个顶点开始寻找权值最小的相邻顶点,将其加入生成树集合中,并更新lowcost数组。重复该过程,直到生成树集合包含所有顶点为止。
注:由于代码缩进不够规范,有可能存在语法错误。
相关问题
void MiniSpanTree_Prim(AMGraph* G, int u) { int k = 0; k = LocateVex(G, u); for (int j = 0; j < G->vexnum; j++) { if (j != k) closedge[j] = { u,G->arcs[k][j] }; } closedge[k].lowcost = 0; for (int i = 0; i < G->vexnum; i++) { k = Min(closedge); int u0 = closedge[k].adjvex; int v0 = G->vexs[k]; printf("%d %d", u0, v0); closedge[k].lowcost = 0; for (int j = 0; j < G->vexnum; j++) { if (G->arcs[i][j] < closedge[j].lowcost) { closedge[i] = { G->vexs[k],G->arcs[i][j] }; } } } }
以下是将该段代码转换为C语言的代码:
```c
void MiniSpanTree_Prim(AMGraph* G, int u) {
int k = 0;
k = LocateVex(G, u);
for (int j = 0; j < G->vexnum; j++) {
if (j != k) {
closedge[j].adjvex = u;
closedge[j].lowcost = G->arcs[k][j];
}
}
closedge[k].lowcost = 0;
for (int i = 0; i < G->vexnum; i++) {
k = Min(closedge);
int u0 = closedge[k].adjvex;
int v0 = G->vexs[k];
printf("%d %d", u0, v0);
closedge[k].lowcost = 0;
for (int j = 0; j < G->vexnum; j++) {
if (G->arcs[i][j] < closedge[j].lowcost) {
closedge[j].adjvex = G->vexs[k];
closedge[j].lowcost = G->arcs[i][j];
}
}
}
}
```
#include <iostream> #define MVNum 100 #define MaxInt 32767 using namespace std; struct edge{ char adjvex; int lowcost; }closedge[MVNum]; typedef struct{ char vexs[MVNum]; int arcs[MVNum][MVNum]; int vexnum,arcnum; }AMGraph; int LocateVex(AMGraph G , char v);//实现细节隐藏 int CreateUDN(AMGraph &G);//实现细节隐藏 int Min(AMGraph G){ int i; int index = -1; int min = MaxInt; for(i = 0 ; i < G.vexnum ; ++i){ if( ){ min = closedge[i].lowcost; index = i; } } return index; } void MiniSpanTree_Prim(AMGraph G, char u){ int k , j , i; char u0 , v0; k =LocateVex(G, u); for(j = 0; j < G.vexnum; ++j){ if(j != k){ closedge[j].adjvex = ; closedge[j].lowcost = ; } } closedge[k].lowcost = ; for(i = 1; i < G.vexnum; ++i){ k = ; u0 = closedge[k].adjvex; v0 = G.vexs[k]; cout << u0 << "->" << v0 << endl; closedge[k].lowcost = ; for(j = 0; j < G.vexnum; ++j) if(G.arcs[k][j] < closedge[j].lowcost){ closedge[j].adjvex = ; closedge[j].lowcost = ; } } } int main(){ AMGraph G; CreateUDN(G); char u; cin >> u; MiniSpanTree_Prim(G , u); return 0; } 补充代码
在代码中,需要完成以下部分的补充:
1. 在 Min 函数中,需要添加判断条件来判断 closedge[i].lowcost 是否小于 min。
2. 在 MiniSpanTree_Prim 函数中,需要根据 Prim 算法的步骤补充代码,并且需要实现 LocateVex 函数来找到对应顶点在图中的位置。
补充后的代码如下:
```
#include <iostream>
#define MVNum 100
#define MaxInt 32767
using namespace std;
struct edge{
char adjvex;
int lowcost;
}closedge[MVNum];
typedef struct{
char vexs[MVNum];
int arcs[MVNum][MVNum];
int vexnum,arcnum;
}AMGraph;
int LocateVex(AMGraph G , char v);//实现细节隐藏
int CreateUDN(AMGraph &G);//实现细节隐藏
int Min(AMGraph G){
int i;
int index = -1;
int min = MaxInt;
for(i = 0 ; i < G.vexnum ; ++i){
if(closedge[i].lowcost < min){
min = closedge[i].lowcost;
index = i;
}
}
return index;
}
void MiniSpanTree_Prim(AMGraph G, char u){
int k , j , i;
char u0 , v0;
k =LocateVex(G, u);
for(j = 0; j < G.vexnum; ++j){
if(j != k){
closedge[j].adjvex = G.vexs[k];
closedge[j].lowcost = G.arcs[k][j];
}
}
closedge[k].lowcost = 0;
for(i = 1; i < G.vexnum; ++i){
k = Min(G);
u0 = closedge[k].adjvex;
v0 = G.vexs[k];
cout << u0 << "->" << v0 << endl;
closedge[k].lowcost = 0;
for(j = 0; j < G.vexnum; ++j)
if(G.arcs[k][j] < closedge[j].lowcost){
closedge[j].adjvex = G.vexs[k];
closedge[j].lowcost = G.arcs[k][j];
}
}
}
int LocateVex(AMGraph G , char v){
int i;
for(i = 0 ; i < G.vexnum ; ++i){
if(G.vexs[i] == v){
return i;
}
}
return -1;
}
int CreateUDN(AMGraph &G){
int i , j , k;
cout << "请输入顶点数和边数:";
cin >> G.vexnum >> G.arcnum;
cout << "请依次输入每个顶点的信息:";
for(i = 0 ; i < G.vexnum ; ++i){
cin >> G.vexs[i];
}
for(i = 0 ; i < G.vexnum ; ++i){
for(j = 0 ; j < G.vexnum ; ++j){
G.arcs[i][j] = MaxInt;
}
}
cout << "请依次输入每条边的两个顶点及其权值:";
for(k = 0 ; k < G.arcnum ; ++k){
char v1 , v2;
int w;
cin >> v1 >> v2 >> w;
i = LocateVex(G , v1);
j = LocateVex(G , v2);
G.arcs[i][j] = w;
G.arcs[j][i] = w;
}
return 1;
}
int main(){
AMGraph G;
CreateUDN(G);
char u;
cin >> u;
MiniSpanTree_Prim(G , u);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)