最小生成树 普里姆算法

//普利姆算法 最小生成树
/*
    把所有顶点分为 2 个集合 ,一个表示已经选中的顶点集合 另一个表示未选中的顶点集合
    例如 a,b,c,d,e 五个顶点
    1.任意选择一个顶点 放在 已经选中的顶点集合中 假如 选a


    2.将a  与未选中顶点集合中 选择 一顶点 条件 权值最小的一个顶点 如何权值相同 则任意选择一个最小的


    3.将上轮选中的顶点 重复2 直到所有顶点选择完毕
*/
#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
using namespace std;


#define MAXSIZE 512
#define INVALID -1


struct BaseNode
{
    BaseNode()
    {
        tailIndex = INVALID;
        nWeight = INVALID;
        Next = NULL;
    }


    int tailIndex;//弧尾 下标


    int nWeight; //权值


    BaseNode * Next;
};


struct GraphNode
{
    BaseNode *first;


    int mIndex;


    bool bVisit;
};


struct ArcInfo
{
    ArcInfo()
    {
        headIndex = INVALID;
        tailIndex = INVALID;
        nWeight = INVALID;
    }
    bool operator==(int i)
    {
        return nWeight == i;
    }
    int headIndex;


    int tailIndex;


    int nWeight;
};


class Graph
{
public:
    Graph()
    {
        m_AllNode = NULL;
        m_EdgeNum = 0;
        m_VertexNum = 0;
    }
    bool Init(bool boDirection);


    BaseNode * InsertNode(BaseNode * CurNode,int tailIndex, int nWeight);


    void Print();


    void depth();


    void depthSearch(int index);


    void Breadth();


    void BreadthSearch(int index);


    bool MakeMiniSpanTree();


    void SelectPoint(int i, vector<int> &selectPoint, vector<int> &noSelectPoint, vector<ArcInfo> &selectArc,vector<ArcInfo> &tempList);
public:
    GraphNode *m_AllNode;


    int m_EdgeNum;                //边数


    int m_VertexNum;            //顶点数


    bool  m_boDirection;        //有向 无向
};


BaseNode * Graph::InsertNode(BaseNode * CurNode, int tailIndex, int nWeight)
{
    
    if (CurNode->Next != NULL)
    {
        cout << "Next No Null" << endl;
        return CurNode;
    }
    BaseNode *t = new BaseNode;


    if (t == NULL)
    {
        cout << "New BaseNode Error" << endl;
        return CurNode;
    }


    if ((t)->tailIndex > m_VertexNum)
    {
        cout << "InsertNode Big Error " << endl;
        return CurNode;
    }


    (t)->tailIndex = tailIndex;
    (t)->nWeight = nWeight;


    CurNode->Next = t;


    return t;
}




bool Graph::Init(bool boDirection)
{
    m_boDirection = boDirection;


    cout << "请输入顶点个数" << endl;
    
    cin >> m_VertexNum;


    if (m_VertexNum >= MAXSIZE || m_VertexNum <= 0)
    {
        cout << "顶点个数有误" << endl;
        return false;
    }


    m_AllNode = new GraphNode[m_VertexNum+1];


    for (int i = 1; i <= m_VertexNum; i++)
    {
        m_AllNode[i].first = new BaseNode;
        m_AllNode[i].mIndex = i;
        m_AllNode[i].first->tailIndex = i;
        m_AllNode[i].bVisit = false;
    }


    cout << "请输入边数" << endl;
    cin >> m_EdgeNum;
    if (m_EdgeNum > (m_VertexNum - 1)*m_VertexNum / 2 || m_EdgeNum <= 0 || m_EdgeNum >= MAXSIZE)
    {
        cout << "边数有误" << endl;
        for (int i = 1; i <= m_VertexNum; i++)
        {
            delete m_AllNode[i].first;
            m_AllNode[i].first = NULL;
        }
        delete[] m_AllNode;
        m_AllNode = NULL;
        return false;
    }


    cout << "请输入边的信息" << endl;


    for (int i = 0; i < m_EdgeNum; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (a <= m_VertexNum)
        {
            BaseNode * aLast = m_AllNode[a].first;
            
            while (aLast->Next)
            {
                aLast = aLast->Next;
            }


            InsertNode(aLast, b, c);
            if (!boDirection)
            {
                BaseNode * bLast = m_AllNode[b].first;


                while (bLast->Next)
                {
                    bLast = bLast->Next;
                }
                InsertNode(bLast, a, c);
            }
                
        }
        else
        {
            cout << "a Error" << endl;
        }
            
    }
    return true;
}




void Graph::Print()
{
    for (int i = 1; i <= m_VertexNum; i++)
    {
        cout << "访问" << m_AllNode[i].mIndex<<"开始"<<endl;
        BaseNode * t = m_AllNode[i].first;
        while (t)
        {
            cout << t->tailIndex <<"  "<< t->nWeight << " ";
            t = t->Next;
        }
        cout << "访问" << m_AllNode[i].mIndex << "结束" << endl;
    }
}


//深度优先遍历
void Graph::depth()
{
    cout << "深度优先遍历开始" << endl;
    for (int i = 1; i <= m_VertexNum; i++)
    {
        depthSearch(i);
    }


    for (int i = 0; i <= m_VertexNum; i++)
    {
        m_AllNode[i].bVisit = false;
    }
}


void  Graph::depthSearch(int index)
{
    if (m_AllNode[index].bVisit == true)
    {
        return;
    }
    cout << "访问" << index << endl;


    m_AllNode[index].bVisit = true;


    BaseNode * t = m_AllNode[index].first->Next;
    while (t)
    {
        depthSearch(t->tailIndex);
        t = t->Next;
    }
}


deque<int> tQueue;
void Graph::Breadth()
{
    cout << "广度优先遍历开始" << endl;
    for (int i = 1; i <= m_VertexNum; i++)
    {
        BreadthSearch(i);
    }


    for (int i = 0; i <= m_VertexNum; i++)
    {
        m_AllNode[i].bVisit = false;
    }
}


void Graph::BreadthSearch(int index)
{
    if (m_AllNode[index].bVisit == true)
    {
        return;
    }
    cout << "访问" << index << endl;


    m_AllNode[index].bVisit = true;


    BaseNode * t = m_AllNode[index].first->Next;


    while (t)
    {
        if (!m_AllNode[t->tailIndex].bVisit)
        {
            tQueue.push_back(t->tailIndex);
        }
        t = t->Next;
    }


    while (tQueue.size() > 0)
    {
        int m = tQueue.front();
        tQueue.pop_front();
        BreadthSearch(m);
    }
}


 
bool Graph::MakeMiniSpanTree()
{
    vector<int> noSelectPoint;
    vector<int> selectPoint;
    vector<ArcInfo> selectArc;
    vector<ArcInfo> tempList;


    for (int i = 1; i <= m_VertexNum; i++)
    {
        noSelectPoint.push_back(i);
    }


    for (int i = 1; i <= m_VertexNum; i++)
    {
        SelectPoint(i, selectPoint, noSelectPoint, selectArc,tempList);
    }
    cout << "开始输出最小生成树的弧信息" << endl;
    for (int i = 0; i < selectArc.size(); i++)
    {
        cout << selectArc[i].headIndex << " " << selectArc[i].tailIndex << " " << selectArc[i].nWeight << endl;
    }
    cout << "结束" << endl <<endl<<endl;
    return true;
}


void Graph::SelectPoint(int index, vector<int> &selectPoint, vector<int> &noSelectPoint, vector<ArcInfo> &selectArc,vector<ArcInfo> &tempList)
{
    //没有被选中
    if (find(selectPoint.begin(), selectPoint.end(), index) == selectPoint.end())
    {
        BaseNode * t = m_AllNode[index].first->Next;
        vector<int>::iterator it ;
        while (t)
        {
            it = find(selectPoint.begin(), selectPoint.end(), t->tailIndex);
            if (it == selectPoint.end())
            {
                ArcInfo k;
                k.headIndex = index;
                k.tailIndex = t->tailIndex;
                k.nWeight = t->nWeight;
                bool bofind = false;
                for (int i = 0 ;i<tempList.size();i++)
                {
                    if (tempList[i].tailIndex == k.tailIndex  )
                    {
                        bofind = true;
                        if (tempList[i].nWeight > k.nWeight)
                        {
                            cout<<tempList[i].headIndex<<" "<<tempList[i].tailIndex<<" "<<tempList[i].nWeight<<"->";
                            tempList[i].headIndex = k.headIndex;
                            tempList[i].tailIndex = k.tailIndex;
                            tempList[i].nWeight = k.nWeight;
                            cout<<tempList[i].headIndex<<" "<<tempList[i].tailIndex<<" "<<tempList[i].nWeight<<endl;
                        }
                    }
                }
                if (!bofind)
                {
                    tempList.push_back(k);
                    cout<<"push: "<<k.headIndex<<" "<<k.tailIndex<<" "<<k.nWeight<<endl;
                }
            }
            
            t = t->Next;
        }


        if (tempList.size() > 0)
        {
            ArcInfo min = tempList[0];
            int p = 0;
            for (int i = 1; i < tempList.size(); i++)
            {
                if (min.nWeight > tempList[i].nWeight)
                {
                    min = tempList[i];
                    p = i;
                }
            }
            tempList.erase(tempList.begin()+p);
            selectPoint.push_back(index);
            vector<int>::iterator it = find(noSelectPoint.begin(), noSelectPoint.end(), index);
            if (it != noSelectPoint.end())
            {
                noSelectPoint.erase(it);
            }
            selectArc.push_back(min);


            if (noSelectPoint.size() > 1)
            {
                SelectPoint(min.tailIndex, selectPoint, noSelectPoint, selectArc,tempList);
            }
            else if (noSelectPoint.size() == 1 && noSelectPoint[0] == min.tailIndex)
            {
                selectPoint.push_back(min.tailIndex);
                vector<int>::iterator it = find(noSelectPoint.begin(), noSelectPoint.end(), min.tailIndex);
                if (it != noSelectPoint.end())
                {
                    noSelectPoint.erase(it);
                }
                cout << "最小生成树 生成成功" << endl;
            }
            
        }
    }
}


int main()
{
    while (true)
    {
        Graph a;
        if (a.Init(false))
        {
            a.Print();
            a.depth();
            a.Breadth();
            a.MakeMiniSpanTree();
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/seanbill/article/details/79690364
今日推荐