pat最小生成树(普里姆算法)

pat最小生成树(普里姆算法)_第1张图片

void Prim(AMGraph G, char u) 
{
    int index = LocateVex(G, u);
   
    for (int i = 0; i < G.vexnum; i++) 
    {
        if (i != index)
        {
            closedge[i].adjvex = u;
            closedge[i].lowcost = G.arcs[index][i];
        }
    }
    //u到u距离为0
    closedge[index].lowcost = 0;

    for (int i = 1; i < G.vexnum; i++)
    {
        index = Min(G);
        cout << closedge[index].adjvex << "->" << G.vexs[index] << endl;
        closedge[index].lowcost = 0;

        for (int j = 0; j < G.vexnum; j++) 
        {
            if (G.arcs[index][j] < closedge[j].lowcost)
            {
                closedge[j].adjvex = G.vexs[index];
                closedge[j].lowcost = G.arcs[index][j];
            }
        }
    }
}

你可能感兴趣的:(算法,c++,数据结构,图论)