图的遍历之广度优先搜索和深度优先搜索

广度优先搜索:从一个顶点开始,搜索所有可到达顶点的方法。利用队列的方法实现
《图的遍历之广度优先搜索和深度优先搜索》
深度优先搜索:从一个顶点出发,向下搜索,到达没有邻接点就向上返回。
《图的遍历之广度优先搜索和深度优先搜索》
下面给出代码:

#include<iostream>
#include<stack>
#include<queue>
#define MAX_VERTS 20
using namespace std;
class Vertex//顶点
{
public:
    //char Label;
    Vertex(char lab)
    { 
        Label = lab; 
        wasVisited = false;
    }
public:
    bool wasVisited;
    char Label;//顶点字符
};
class Graph//图
{
public:
    Graph();
    ~Graph();
    void addVertex(char lab);//增加一个顶点
    void addEdge(int start, int end);//增加一条边
    void printMatrix();//打印矩阵
    void showVertex(int v);
    void DFS();//深度优先搜索
    void BFS();

private:
    Vertex* vertexList[MAX_VERTS];//指针数组
    int nVerts;//实际中顶点的个数
    int adjMat[MAX_VERTS][MAX_VERTS];//做邻接矩阵的数组
    int getAdjUnvisitedVertex(int v);//邻接的未访问的顶点
};
void Graph::DFS()
{
    stack<int>gStack;//保存的堆栈的下标
    vertexList[0]->wasVisited = true;//从第一个顶点开始
    showVertex(0);
    gStack.push(0);//把访问过的压入堆栈
    int v;
    while (gStack.size() > 0)//当堆栈为空是结束
    {
        v = getAdjUnvisitedVertex(gStack.top());//v是顶点下标
        if (v == -1)
            gStack.pop();//向回反
        else
        {
            vertexList[v]->wasVisited = true;
            showVertex(v);
            gStack.push(v);
        }
    }
    cout << endl;
    for (int j = 0; j < nVerts; j++)
        vertexList[j]->wasVisited = false;
}
void Graph::BFS()
{
    queue<int >gQueue;
    vertexList[0]->wasVisited = true;//从第一个下标开始
    showVertex(0);
    gQueue.push(0);//访问过的每一个顶点都要放入队列
    int vert1, vert2;
    while (gQueue.size()>0)//如果队列中有顶点
    {
        vert1 = gQueue.front();//把A拿出来
        gQueue.pop();//把A删除掉
        vert2 = getAdjUnvisitedVertex(vert1);//找与A相邻接的
        while (vert2 != -1)//找出所有与A相邻接的
        {
            vertexList[vert2]->wasVisited = true;//标记为访问过
            showVertex(vert2);//显示
            gQueue.push(vert2);//放入队列
            vert2 = getAdjUnvisitedVertex(vert1);

        }

    }
    cout << endl;
    for (int j = 0; j < nVerts;j++)
        vertexList[j]->wasVisited = false;
}
int Graph::getAdjUnvisitedVertex(int v)
{
    for (int j = 0; j < nVerts; j++)
    {
        if ((adjMat[v][j] == 1) && (vertexList[j]->wasVisited == false))
            return j;
    }
    return -1;
}
void Graph::showVertex(int v)
{
    cout << vertexList[v]->Label << " ";

}
Graph::Graph()
{
    nVerts = 0;
    for (int i = 0; i < MAX_VERTS; i++)
    for (int j = 0; j < MAX_VERTS; j++)
        adjMat[i][j] = 0;
}
void Graph::addVertex(char lab)
{
    vertexList[nVerts++] = new Vertex(lab);
}
void Graph::addEdge(int start, int end)
{
    adjMat[start][end] = 1;
    adjMat[end][start] = 1;
}
void Graph::printMatrix()
{
    for (int i = 0; i < nVerts; i++)
    {
        for (int j = 0; j < nVerts; j++)
            cout << adjMat[i][j] << " ";
        cout << endl;
    }
}
Graph::~Graph()
{
    for (int i = 0; i < nVerts; i++)
        delete vertexList[i];
}
int main()
{

    Graph g;
    g.addVertex('A');//0
    g.addVertex('B');//1
    g.addVertex('C');//2
    g.addVertex('D');//3
    g.addVertex('E');//4

    g.addEdge(0, 1);//A_B
    g.addEdge(0, 3);//A-D
    g.addEdge(1, 0);//B-A
    g.addEdge(1, 4);//B-E
    g.addEdge(2, 4);//C-E
    g.addEdge(3, 0);//C-A
    g.addEdge(3, 4);//C-D
    g.addEdge(4, 1);//E-B
    g.addEdge(4, 2);//E-C
    g.addEdge(4, 3);//E-D
    g.printMatrix();
    cout << "深度优先搜索的结果:" << endl;
    g.DFS();
    cout << "广度优先搜索的结果:" << endl;
    g.BFS();



    system("pause");
    return 0;
}
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/Vincentlmeng/article/details/64188249
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞