经典算法之图的广度优先搜索遍历

版权声明:本文为博主原创文章,转载请注明此文链接,谢谢了。个人技术博客:https://wardseptember.github.io/ https://blog.csdn.net/wardseptember/article/details/78801398
/************************
author's email:wardseptember@gmail.com
data:2017.12.14
图的广度优先搜索遍历
************************/
/*
图的广度优先搜索遍历(BFS)类似于树的程序遍历。它的基本思想是:首先访问起始顶点v,然后选取
与v邻接的全部顶点w1...wn进行访问,再依次访问与w1,....,wn邻接的全部顶点(已经访问过的
除外),以此类推,直到所有顶点都被访问完。
*/
#include<iostream>
#define maxSize 8
using namespace std;
typedef struct Node {
    int vertex;
    struct Node *pNext;
}Node;
typedef struct head {
    char data;
    Node *first;
}head, *Graph;
int visit[maxSize];       //定义一个全局变量,用来判断某一结点是否被访问过
Graph create_graph();    //创建一个邻接表
void BFS(Graph graph, int v,int visit[maxSize]); //广度遍历连通图
void bfs(Graph graph);       //广度遍历非连通图
void BFSTrave(Graph graph, int i, int j);//判断顶点i和顶点j(i!=j)之间是否有路径
int main() {
    Graph graph = create_graph();
    cout << "广度遍历连通图结果为:";
    BFS(graph, 7,visit);    //Bfs(graph);广度遍历非连通图
    cout << endl;

    int i, j;
    cout << "请输入要判断的两个顶点(0-7):";
    cin >> i >> j;
    BFSTrave(graph, i, j);
    return 0;
}
Graph create_graph()
{
    //为保存顶点相关信息的数组分配空间,并对数据域赋值
    Graph graph = (Graph)malloc(maxSize * sizeof(head));
    int i;
    //顶点的序号按照输入顺序从0依次向后
    for (i = 0; i < maxSize; i++)
        graph[i].data = 'A' + i;

    //为每个节点对应的的单链表中的节点分配空间
    Node *p00 = (Node *)malloc(sizeof(Node));
    Node *p01 = (Node *)malloc(sizeof(Node));
    Node *p10 = (Node *)malloc(sizeof(Node));
    Node *p11 = (Node *)malloc(sizeof(Node));
    Node *p12 = (Node *)malloc(sizeof(Node));
    Node *p20 = (Node *)malloc(sizeof(Node));
    Node *p21 = (Node *)malloc(sizeof(Node));
    Node *p22 = (Node *)malloc(sizeof(Node));
    Node *p30 = (Node *)malloc(sizeof(Node));
    Node *p31 = (Node *)malloc(sizeof(Node));
    Node *p40 = (Node *)malloc(sizeof(Node));
    Node *p41 = (Node *)malloc(sizeof(Node));
    Node *p50 = (Node *)malloc(sizeof(Node));
    Node *p51 = (Node *)malloc(sizeof(Node));
    Node *p60 = (Node *)malloc(sizeof(Node));
    Node *p61 = (Node *)malloc(sizeof(Node));
    Node *p70 = (Node *)malloc(sizeof(Node));
    Node *p71 = (Node *)malloc(sizeof(Node));

    //为各单链表中的节点的相关属性赋值
    p00->vertex = 1;
    p00->pNext = p01;
    p01->vertex = 2;
    p01->pNext = NULL;
    p10->vertex = 0;
    p10->pNext = p11;
    p11->vertex = 3;
    p11->pNext = p12;
    p12->vertex = 4;
    p12->pNext = NULL;
    p20->vertex = 0;
    p20->pNext = p21;
    p21->vertex = 5;
    p21->pNext = p22;
    p22->vertex = 6;
    p22->pNext = NULL;
    p30->vertex = 1;
    p30->pNext = p31;
    p31->vertex = 7;
    p31->pNext = NULL;
    p40->vertex = 1;
    p40->pNext = p41;
    p41->vertex = 7;
    p41->pNext = NULL;
    p50->vertex = 2;
    p50->pNext = p51;
    p51->vertex = 6;
    p51->pNext = NULL;
    p60->vertex = 2;
    p60->pNext = p61;
    p61->vertex = 5;
    p61->pNext = NULL;
    p70->vertex = 3;
    p70->pNext = p71;
    p71->vertex = 4;
    p71->pNext = NULL;

    //将顶点与每个单链表连接起来
    graph[0].first = p00;
    graph[1].first = p10;
    graph[2].first = p20;
    graph[3].first = p30;
    graph[4].first = p40;
    graph[5].first = p50;
    graph[6].first = p60;
    graph[7].first = p70;

    return graph;
}
void BFS(Graph graph, int v, int visit[maxSize]) {//广度优先遍历连通图
    Node *p;
    int que[maxSize], front = 0, rear = 0;//定义一个顺序队,并初始化
    int j;
    cout << graph[v].data<<' ';
    visit[v] = 1;
    rear = (rear + 1) % maxSize;    //v入队
    que[rear] = v;
    while (front != rear) {   //对空说明遍历完成
        front = (front + 1) % maxSize;   //顶点出队
        j = que[front];
        p = graph[j].first;      //p指向出队顶点j的第一条边
        while (p != NULL) {     //将p的所有邻接点中未被访问的入队
            if (visit[p->vertex] == 0) {//当前邻接顶点未被访问,则进队
                cout << graph[p->vertex].data<<' ';
                visit[p->vertex] = 1;
                rear = (rear + 1) % maxSize;//该顶点进队
                que[rear] = p->vertex;
            }
            p = p->pNext;       //p指向j的下一条边
        }
    }
}
void bfs(Graph graph) {   //广度优先遍历非连通图
    int i;
    for (i = 0; i < maxSize; ++i)
        if (visit[i] == 0)
            BFS(graph, i,visit);
}
void BFSTrave(Graph graph, int i, int j) {//判断顶点i和顶点j(i!=j)之间是否有路径
    int k;
    for (k = 0; k < maxSize; ++k)
        visit[k] = 0;
    BFS(graph, i, visit);
    cout << endl;
    if (visit[j] == 1)//visit[j]=1则证明访问过程遇到了j
        cout << "两顶点间有路径" << endl;
    else
        cout << "两顶点间无路径" << endl;
}

以上如有错误,请指出,大家共同学习进步。

阅读更多
换一批

没有更多推荐了,返回首页