15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法

算法分析和具体步骤解说直接写在代码注释上了

TvT 没时间了等下还要去洗衣服 就先不赘述了

有不明白的欢迎留言交流!(估计是没人看的了)


 

直接上代码:

  1 #include<stdio.h>
  2 #include<queue>
  3 #include<iostream>
  4 using namespace std;
  5 typedef struct{
  6 int Vex[10];//顶点表
  7 int Edge[10][10];
  8 int vexnum,arcnum;
  9 }MGraph;
 10 bool visited[10];
 11 void printGraph(MGraph &G)
 12 {
 13     cout<<"Show the Graph."<<endl;
 14     cout<<"----------------------------"<<endl;
 15     for(int i=0;i<10;i++){
 16         for(int j=0;j<10;j++)
 17     {
 18         cout<<" "<<G.Edge[i][j]<<" ";
 19     }
 20     cout<<endl;
 21     }
 22     cout<<"----------------------------"<<endl;
 23     cout<<"There are "<<G.arcnum<<" arcs in the Graph!"<<endl;
 24     cout<<"Arcs are listed as follow:"<<endl;
 25     for(int i=0;i<10;i++){
 26         for(int j=0;j<10;j++)
 27     {
 28         if(G.Edge[i][j]==1)
 29             cout<<i<<"-->"<<j<<endl;
 30     }
 31     }
 32 }
 33 int initGraph(MGraph &G)
 34 {
 35     G.vexnum=10;
 36     G.arcnum=0;
 37     for(int i=0;i<10;i++)
 38         for(int j=0;j<10;j++)
 39     {
 40         G.Edge[i][j]=0;
 41     }
 42     cout<<"input the relation of a pair of dots (A,B) to build a graph.(0<A,B<10)"<<endl;
 43     cout<<"End up with data(0,0)"<<endl;
 44     int a,b;
 45     cin>>a>>b;
 46     while(a!=0&&b!=0){
 47     G.arcnum++;
 48     G.Edge[a][b]=1;
 49     cin>>a>>b;
 50     }
 51     cout<<"successful."<<endl;
 52     printGraph(G);
 53 }
 54 bool isNeibour(MGraph &G,int a,int b)
 55 {
 56     if(G.Edge[a][b]==1)
 57         return true;
 58     else return false;
 59 }
 60 int firstNeighbor(MGraph &G,int v)
 61 {
 62     int pos=-1;
 63     for(int i=0;i<G.vexnum;i++)
 64     {
 65         if((G.Edge[v][i]==1))
 66             {pos=i;break;}
 67     }
 68     return pos;
 69 }
 70 int nextNeighbor(MGraph &G,int v,int w)
 71 {
 72     int pos=-1;
 73     for(int i=w+1;i<G.vexnum;i++)
 74     {
 75         if((G.Edge[v][i]==1))
 76         {
 77             pos=i;break;
 78         }
 79     }
 80     return pos;
 81 }
 82 void visit(int v)
 83 {
 84     visited[v]=true;
 85     cout<<v<<" ";
 86 }
 87 queue<int> Q;
 88 void bfs(MGraph G,int v)
 89 {
 90     visit(v);
 91     Q.push(v);
 92     int now;
 93     while(!Q.empty())
 94     {
 95         now=Q.front();
 96         Q.pop();
 97         for(int w=firstNeighbor(G,now);w>=0;w=nextNeighbor(G,now,w))
 98         {
 99             if(!visited[w])
100             {
101                 visit(w);
102                 Q.push(now);
103             }
104         }
105     }
106 }
107 void BFS(MGraph &G)
108 {
109     for(int i=0;i<G.vexnum;i++)
110         visited[i]=false;
111     for(int i=0;i<G.vexnum;i++)
112     {
113         if(!visited[i])
114             bfs(G,i);
115     }
116 }
117 /*DFS 深度优先搜索*/
118 /*类似于树的先序遍历
119 其搜索策略:尽可能【深】地搜索一个图。
120 遍历思想:
121 从一个起始顶点v出发,访问与v邻接但未被访问的任一顶点a→继续访问a顶点的下一未被访问的顶点b→……→无法再向下访问时依次退回到最近被访问的顶点
122 直到所有顶点都被访问为止
123 */
124 void dfs(MGraph &G,int v)
125 {
126     visit(v);
127     int w;
128     for(w=firstNeighbor(G,v);w>=0;w=nextNeighbor(G,v,w))
129     {
130         if(!visited[w])
131         {
132             dfs(G,w);
133         }
134     }
135 }
136 void DFS(MGraph &G)
137 {
138     for(int i=0;i<G.vexnum;i++)
139     {
140         visited[i]=false;
141     }
142     for(int i=0;i<G.vexnum;i++)
143     {
144         if(!visited[i])
145             dfs(G,i);
146     }
147 }
148 int main()
149 {
150     MGraph P;
151     initGraph(P);
152     cout<<"test of BFS:"<<endl;
153     BFS(P);
154     cout<<endl;
155     cout<<"test of DFS:"<<endl;
156     DFS(P);
157     return 0;
158 }

附一张运行截图


 

转载于:https://www.cnblogs.com/AKsnoopy/p/7609161.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值