数据结构c++:广度优先搜索连通图

博客围绕信息技术领域展开,涉及C++语言,重点提及图论和广度优先算法,还关联到数据结构相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<bits/stdc++.h>
using namespace std;
#define MVNum 100
#define MAXQSIZE 100
typedef char VerTexType;
typedef int ArcType;
bool visited[MVNum];
typedef struct{
    VerTexType vexs[MVNum] ;
    ArcType arcs[MVNum][MVNum] ;
    int vexnum,arcnum;
    }Graph;

typedef struct{
    ArcType *base;
    int front;
    int rear;
    }sqQueue;

void InitQueue(sqQueue &Q){
    Q.base = new ArcType[MAXQSIZE];
    if(!Q.base)exit(1);
    Q.front = Q.rear = 0;}
void EnQueue(sqQueue &Q, ArcType e){
    if((Q.rear + 1) % MAXQSIZE == Q.front)
        return;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXQSIZE;
}
bool QueueEmpty(sqQueue Q){
    if(Q.rear == Q.front)return true;
return false;
}

void DeQueue(sqQueue &Q,ArcType &u) {
u = Q.base[Q.front];
Q.front = (Q.front + 1) %MAXQSIZE;
}

int LocateVex(Graph G , VerTexType v){
//确定点在G中的位置
    for(int i = 0; i <G.vexnum; ++i)
        if(G.vexs[i]==v)
    return i;
return -1;
}//LocateVex

void CreateUDN(Graph &G){
//采用邻接矩阵表孟法创建无向网G
    int i , j , k;
    cout <<"请输入总顶点数,总边数,以空格隔开:";
    cin >> G.vexnum >> G.arcnum;
    cout << endl;

    cout <<"输入点的名称,如a" << endl;
    for(i = 0; i < G.vexnum; ++i){
        cout <<"请输入第" << (i+1) <<"个点的名称:";
        cin >> G.vexs[i];
        }
    cout << endl;
    for(i = 0; i < G.vexnum; ++i)
        for(j = 0; j < G.vexnum; ++j)
        G.arcs[i][j] = 0;
    cout <<"输入边依附的顶点,如a b" << endl;
    for(k = 0; k < G.arcnum;++k){
            VerTexType v1,v2;
    cout <<"请输入第"<< (k + 1) <<"条边依附的顶点:";
    cin >> v1 >> v2;
    i = LocateVex(G, v1); j = LocateVex(G, v2);
    G.arcs[j][i] = G.arcs[i][j] = 1;}}

int FirstAdjVex(Graph G , int v){
    int i;
    for(i = 0 ; i < G.vexnum ; ++i){
            if(G.arcs[v][i] == 1 && visited[i] == false)
    return i;}
    return -1;
}
int NextAdjVex(Graph G , int v , int w){
int i;for(i = w ; i < G.vexnum ; ++i){
    if(G.arcs[v][i] == 1 && visited[i] == false)return i;}
return -1;
}

void BFS(Graph G, int v){
sqQueue Q;
ArcType u;
ArcType w;
for(int i=0;i<G.vexnum; i++)visited[i] = false;
cout << G.vexs[v] << "      ";
visited[v] = true;
InitQueue(Q);
EnQueue (Q, v) ;
while(!QueueEmpty(Q)){
        DeQueue (Q, u) ;
for(w = FirstAdjVex(G,u); w >= 0; w = NextAdjVex(G,u,w)){
        if(!visited[w]){
            cout << G.vexs[w] <<"       ";visited[w] = true;
EnQueue(Q, w);}}}}

int main(){
    cout<<"*********广度优先搜索遍历连通图***********"<< endl << endl;
Graph G;
CreateUDN(G);
cout << endl;
cout <<"无向连通图G创建完成!"<< endl << endl;
VerTexType c;
while(1){
cout <<"请输入遍历连通图的起始点;";
cin >>c;int i = LocateVex(G,c);
if(i!=-1){
cout <<"广度优先搜索遍历连通图结果:"<< endl;
BFS(G ,i);
cout <<endl;}
else{
cout <<"该顶点不存在:" << endl;}}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值