C++邻接表与无向图

C++邻接表与无向图

参考《算法》,采用邻接表实现图结构,实现DFS以及BFS等算法。具体原理参考书本,此处给出C++版本实现。

#include<iostream>
#include<queue>
#include<algorithm>
#include<map>

using namespace std;

/*
 顶点       中间节点
 VNode     ENode
 0 | A --> 2(C) 1(B)
 1 | B --> 4(E) 3(D) 0(A)
 2 | C --> 6(G) 5(F) 0(A)
 3 | D --> 7(H) 1(B)
 4 | E --> 7(H) 1(B)
 5 | F --> 6(G) 2(C)
 6 | G --> 5(F) 2(C)
 7 | H --> 4(E) 3(D)
 */

const int MAX = 20;

struct ENode      //邻接表的中间节点
{
    int adjvex;   //对应索引
    ENode* next;
};

typedef struct VNode //邻接表顶点
{
    char vertex;     //值
    ENode* firstarc; //指向第一个中间节点
}AdjList[MAX];

class ALGraph         //图
{
private:
    AdjList adjList;          //邻接表数组
    int vexNum;              //节点数量
    int arcNum;              //连边数量
    bool visited[MAX];        //标记被访问
    map<int,int> unionfield;  //标记节点属于哪个连通域

public:
    void CreateGraph();       //创建图
    void PrintGraph();        //打印图
    void DFS(int v,int field); //深度优先搜索
    void BFS();               //广度优先搜索
    int DFS_findUnion();      //深度优先搜索寻找连通域
    bool isConnect(int m,int n); //判断两个节点是否连通
};

void ALGraph::CreateGraph()
{
    cout << "请输入图的顶点数:" << endl;
    cin >> this->vexNum;
    cout << "请输入图的弧数:" << endl;
    cin >> this->arcNum;
    cout << "请输入顶点信息:" << endl;
    for (int i = 0; i<this->vexNum; i++)  //构建顶点数组
    {
        cin >> this->adjList[i].vertex;
        this->adjList[i].firstarc = nullptr;
    }
    cout << "请输入" << this->arcNum << "个弧的信息:" << endl;
    for (int i = 0; i<this->arcNum; i++)  //构建每条邻接表
    {
        int h1, h2;
        cin >> h1 >> h2;
        ENode* temp = new ENode();
        temp->adjvex = h2;
        temp->next = this->adjList[h1].firstarc;
        this->adjList[h1].firstarc = temp;

        temp = new ENode();
        temp->adjvex = h1;
        temp->next = this->adjList[h2].firstarc;
        this->adjList[h2].firstarc = temp;
    }
}
void ALGraph::PrintGraph()
{
    for (int i = 0; i<this->vexNum; i++)
    {
        cout << this->adjList[i].vertex << "--------->";
        ENode* p = this->adjList[i].firstarc;
        while (p)
        {
            cout << this->adjList[p->adjvex].vertex << " ";
            p = p->next;
        }
        cout << endl;
    }
}
void ALGraph::BFS()
{
    for(int i=0;i<this->vexNum;i++)
    {
        visited[i] = false;
    }
    queue<int> q;
    for(int i=0;i<this->vexNum;i++)
    {
        if(!visited[i])
        {
            visited[i] = true;
            q.push(i);
            cout<<this->adjList[i].vertex<<" ";
            while(!q.empty())
            {
                int x = q.front();
                q.pop();
                ENode* p = this->adjList[x].firstarc;
                while(p)
                {
                    if(!visited[p->adjvex])
                    {
                        visited[p->adjvex] = true;
                        cout<<this->adjList[p->adjvex].vertex<<" ";
                        q.push(p->adjvex);
                    }
                    p = p->next;
                }
            }
        }
    }
}
void ALGraph::DFS(int v,int field)
{
    visited[v] = true;
    this->unionfield.insert(make_pair(v, field));
    cout << this->adjList[v].vertex << " ";

    ENode* p = this->adjList[v].firstarc;
    while (p)
    {
        if (!visited[p->adjvex])
            DFS(p->adjvex,field);
        p = p->next;
    }
}

int ALGraph::DFS_findUnion()
{
    this->unionfield.clear();
    int count = 0;                //连通域个数
    for(int i=0;i<this->vexNum;i++)
    {
        visited[i] = false;
    }
    cout<<"各个连通域如下所示:"<<endl;
    for(int i=0;i<this->vexNum;i++)
    {
        if(!visited[i])
        {
            DFS(i,++count);
            cout<<endl;
        }

    }
    cout<<endl;
    return count;
}
bool ALGraph::isConnect(int m,int n)
{
    return (this->unionfield[m]==this->unionfield[n])?true:false;
}


int main()
{
    ALGraph* graph = new ALGraph();
    graph->CreateGraph();
    cout<<graph->DFS_findUnion()<<"个连通域"<<endl;
    cout<<"连通情况为:"<<graph->isConnect(6, 7)<<endl;
    return 0;
}
发布了47 篇原创文章 · 获赞 32 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章