#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; } 补充代码
时间: 2024-01-07 09:02:53 浏览: 156
在代码中,需要完成以下部分的补充:
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;
}
```
阅读全文