无向图,邻接链表存储,prim算法实现最小生成树,深度优先搜索,广度优先搜索。

仅实现算法过程,对输入未监测

//#include
#include
#include
//using namespace std;
const int MaxVectices = 100;
typedef int VerT;
typedef int DisT;
//-------------------声明定义结构体和类
struct Edge
{
    int dest;
    DisT weight;
    Edge *next;
};
struct item
{
    VerT data;
    Edge *adj;
};
struct MinSpanTree
{
    int begin, end;
    float length;
};
class AdjTWGraph
{
private:
    item Vertices[MaxVectices];
    int numV;
    int numE;
public:
    AdjTWGraph();
    ~AdjTWGraph();
    void CreatG(int n, int e);
    void PrintOut();
    void DepthFirst();
    void BroadFirst();
    void Prim(int v);
private:
    void DepthFirst(int v, int visited[]);
    void BroadFirst(int v, int visited[]);
};

//-------------------声明函数

//-------------------定义函数
AdjTWGraph::AdjTWGraph()
{
    for(int i=0; inext;
            delete p;
            p = q;
        }
        Vertices[i].adj = nullptr;
    }
    cout<<"\n调用析构函数";
}

void AdjTWGraph::CreatG(int n, int e)
{
    int vi,vj;
    DisT w;
    numE = e;                                       //e为边的数目,n为顶点的数目
    numV = n;
    cout<<"\n输入顶点的信息";
    for(int i=0; i>Vertices[i].data;
    }
    for(int i=0; i>vi>>vj>>w;
        if(vi<1||vj<1||vi>numV||vj>numV)
            cout<<"\n顶点号越界";
        //------------------------------第i条链表插入vi--vj
        Edge *q = new Edge;
        q->dest = vj;
        q->weight = w;
        q->next = nullptr;
        if(Vertices[vi-1].adj == nullptr)                                //插入第一条边
            Vertices[vi-1].adj = q;
        else
        {
            Edge *curr = Vertices[vi-1].adj, *pre = nullptr;
            while(curr != nullptr && (curr->dest < q->dest))              //向右搜索直到标记点到q后侧
            {
                pre = curr;
                curr = curr->next;
            }
            if(pre==nullptr)                                            //在已有链表的第一个边节点前插入
            {
                q->next = Vertices[vi-1].adj;
                Vertices[vi-1].adj = q;
            }
            else
            {
                q->next = pre->next;
                pre->next = q;
            }
        }
        //------------------------------第j条链表插入vj--vi
        Edge *p = new Edge;
        p->dest = vi;
        p->weight = w;
        p->next = nullptr;
        if(Vertices[vj-1].adj==nullptr)
            Vertices[vj-1].adj = p;
        else
        {
            Edge *curr = Vertices[vj-1].adj;
            Edge *pre = nullptr;
            while(curr!=nullptr && curr->destdest)
            {
                pre = curr;
                curr = curr->next;
            }
            if(pre==nullptr)
            {
                p->next = Vertices[vj-1].adj;
                Vertices[vj-1].adj = p;
            }
            else
            {
                p->next = pre->next;
                pre->next = p;
            }
        }
    }
}

void AdjTWGraph::PrintOut()
{
    Edge *curr, *pre;
    cout<<"\n 输出顶点信息编号,邻接点的编号和边的权重:";
    for(int i=0; idest-1].data<<"\t权重 "<weight;
            pre = curr;
            curr = curr->next;
        }
        cout<<"\n";
    }
}

void AdjTWGraph::DepthFirst(int v, int visited[])
{
    v--;
    int vj;
    Edge *p;
    cout<dest;
        if(visited[vj-1]==0)
            DepthFirst(vj, visited);
        p = p->next;
    }
}

void AdjTWGraph::DepthFirst()
{
    int v0;
    int visited[MaxVectices];
    for(int i=0; i>v0;
    cout<<"\n深度优先遍历";
    DepthFirst(v0, visited);
    cout<<"\n遍历结束";
}

void AdjTWGraph::BroadFirst(int v, int visited[])
{
    v--;
    int vj;
    Edge *p;
    SqQueueQ;
    cout<dest;
            if(visited[vj-1]==0)
            {
                cout<next;
        }
    }
    cout<<"\n end";
}

void AdjTWGraph::BroadFirst()
{
    int v0, visited[MaxVectices];
    for(int i=0; i>v0;
    BroadFirst(v0, visited);
    cout<<"\n广度优先遍历结束";
}

//-----------------------------定义程序的函数,实现最小生成树算法
void AdjTWGraph::Prim(int v)                             //从顶点v开始的prim算法
{
    int n = numV;
    MinSpanTree e, mintree[20];
    Edge *p = Vertices[v-1].adj;
    for(int i=0; idest))
        {
            mintree[i].length = p->weight;
            p = p->next;
        }
        else 
            mintree[i].length = INT_MAX;
    }
    for(int i=v-1; idest))
        {
            mintree[i].length = p->weight;
            p = p->next;
        }
        else 
            mintree[i].length = INT_MAX;
    }

    for(int j=0; jdest)
                    break;
            }
            if(p->weight < mintree[k].length && kweight;
                mintree[k].begin = vv;
            }
            p = p->next;
        }
    }
    cout<<"\nPrim算法,邻接链表存储下\n";
    for(int j=0; j>k;
    while(k>0 && k<5)
    {
        switch(k)
        {
            case 1:
                cout<<"\n输入图的总顶点数和总边数(n,e=?)";
                cin>>n>>e;
                G.CreatG(n, e);
                G.PrintOut();
                break;
            case 2:
                G.DepthFirst();
//                G.PrintOut();
                break;
            case 3:
                G.BroadFirst();
//                G.PrintOut();
                break;
            case 4:
                cout<<"\n输入起始顶点v";
                cin>>v;
                G.Prim(v);
                break;
            default:
                break;
        }
        cout<<"\n 请输入你的选择(1,2,3,4)";
        cin>>k;
    }    
    cout<<"\n 程序结束";
    return 0;
}

模板类 SqQueue.h

#include
using namespace std;
templateclass SqQueue
{
public:
    SqQueue(int sz=30);
    ~SqQueue(){delete[] elem;}
    void EnQueue(T item);
    T DeQueue();
    T GetFront();
    void MakeEmpty(){ front=rear=0;}
    bool IsEmpty(){ return front==rear;}
    bool IsFull(){ return (rear+1)&Maxsize==front;}
    int length(){ return (rear-front+Maxsize)%Maxsize;}
    void PrintOut();
private:
    int front, rear;
    T *elem;
    int Maxsize;
};

templateSqQueue::SqQueue(int sz)
{
    front = 0;
    rear = 0;
    Maxsize = sz;
    elem = new T[Maxsize];
    for(int i=0; ivoid SqQueue::EnQueue(T item)
{
    if(!IsFull())
    {
        rear = (rear+1)%Maxsize;
        elem[rear] = item;
    }
}

templateT SqQueue::DeQueue()
{
    if(!IsEmpty())
    {
        front = (front+1)%Maxsize;
        return elem[front];
    }
}

templateT SqQueue::GetFront()
{
    if(!IsEmpty())
    {
        return elem[(front+1)%Maxsize];
    }
}

templatevoid SqQueue::PrintOut()
{
    int i;
    if(IsEmpty())
        cout<<"\n队列为空";
    else
    {
        i = rear;
        while(i!=front)
        {
            cout<

 

你可能感兴趣的:(数据结构与算法)