• 问题 F: 案例6-1.2:邻接表存储图的广度优先遍历


    题目描述

    一个图有n个节点编号从0至n-1和m条边编号从0至m-1。 输出从点x开始的广度优先遍历顺序。

    输入格式

    第一行为n、m、x。 
    接下来m行每行有一组u,v。表示点u可以到达点v,点v也可以到达点u。

    输出格式

    输出经过点的顺序。(输出字典序最小的答案)

    输入样例

    1. 7 9 5
    2. 0 3
    3. 0 2
    4. 0 4
    5. 3 1
    6. 3 2
    7. 4 5
    8. 1 5
    9. 2 5
    10. 5 6

    输出样例  

    5 1 2 4 6 3 0

    代码展示 

    emmm函数不一定全用上了有些是别的题留下的为了之后方便使用没删

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. #define MAX_SIZE 100
    7. //领接矩阵存储的图
    8. struct Graph{
    9. int vexNumber;
    10. string vexInfo[MAX_SIZE];
    11. int adjMatrix[MAX_SIZE][MAX_SIZE];
    12. };
    13. //弧结点定义
    14. struct ArcNode{
    15. int weight;//弧上的信息部分
    16. int adj;//邻接点的序号
    17. ArcNode *nextarc;
    18. };
    19. //顶点结点定义
    20. struct VexNode{
    21. string Info;
    22. ArcNode *firstarc;
    23. };
    24. //领接表结构的图的定义
    25. struct linkGraph{
    26. VexNode *vexes;
    27. int vexnumber;
    28. };
    29. int preInitGraph(linkGraph &G,const Graph &g){
    30. G.vexes=new VexNode[g.vexNumber];
    31. G.vexnumber=g.vexNumber;
    32. for(int i=0;i
    33. G.vexes[i].firstarc=NULL;
    34. G.vexes[i].Info=g.vexInfo[i];
    35. }
    36. return 0;
    37. }
    38. //将邻接矩阵存储的图转换为领接表存储的图
    39. void InitGraph(linkGraph &G,const Graph &g){
    40. preInitGraph(G,g);
    41. for(int i=0;i
    42. for(int j=0;j
    43. if(g.adjMatrix[i][j]==1){
    44. ArcNode *p=new ArcNode();
    45. p->nextarc=NULL;
    46. p->adj=j;
    47. ArcNode *q=G.vexes[i].firstarc;
    48. if(G.vexes[i].firstarc==NULL)
    49. G.vexes[i].firstarc=p;
    50. else{
    51. while(q->nextarc!=NULL){
    52. q=q->nextarc;
    53. }
    54. q->nextarc=p;
    55. }
    56. }
    57. }
    58. }
    59. }
    60. int DestroyGraph(linkGraph &G){
    61. for(int i=0;i
    62. while(G.vexes[i].firstarc!=NULL){
    63. ArcNode *p=G.vexes[i].firstarc;
    64. G.vexes[i].firstarc=p->nextarc;
    65. delete p;
    66. }
    67. }
    68. delete[]G.vexes;
    69. G.vexes=NULL;
    70. G.vexnumber=0;
    71. return 0;
    72. }
    73. //输出领接表存储的图
    74. void PrintGraph(const linkGraph &G){
    75. for(int i=0;i
    76. cout<
    77. ArcNode *p=G.vexes[i].firstarc;
    78. while(p!=NULL){
    79. cout<<" --> "<adj].Info;
    80. p=p->nextarc;
    81. }
    82. cout<
    83. }
    84. }
    85. //查找v0的未被访问的邻接点
    86. int findAdjVex(const Graph &G,int v0,int visited[]){
    87. }
    88. //以顶点v0为起点,进行一趟DFS
    89. string oneDFS(const linkGraph &G,int v0,int visited[],string &result){
    90. result+=G.vexes[v0].Info;
    91. result+=" ";
    92. stacks;
    93. ArcNode *p=G.vexes[v0].firstarc;
    94. s.push(p);
    95. while(!s.empty()){
    96. ArcNode *vNode=s.top();
    97. for(p=vNode;p!=NULL;p=p->nextarc){
    98. if(visited[p->adj]==0){
    99. result+=G.vexes[p->adj].Info;
    100. result+=" ";
    101. visited[p->adj]=1;
    102. if(p->nextarc!=NULL) s.push(p->nextarc);
    103. break;
    104. }
    105. }
    106. if(p==NULL) s.pop();
    107. }
    108. return result;
    109. }
    110. //对整个图进行DFS
    111. string DFS(const Graph &G){
    112. string result="";
    113. linkGraph LG;
    114. InitGraph(LG,G);
    115. //test
    116. //PrintGraph(LG);
    117. int *visited=new int[G.vexNumber];
    118. for(int i=0;i
    119. visited[i]=0;//初始化visited数组
    120. for(int i=0;i
    121. if(!visited[i]){//以每个未被遍历的顶点为起点,进行DFS
    122. result=oneDFS(LG,i,visited,result);
    123. }
    124. }
    125. delete[]visited;
    126. return result;
    127. }
    128. int oneBFS(Graph &G,int v0,int visited[]){
    129. linkGraph LG;
    130. InitGraph(LG,G);
    131. queue<int>q;
    132. q.push(v0);
    133. visited[v0]=1;
    134. while(!q.empty()){
    135. int v=q.front();
    136. q.pop();
    137. cout<" ";
    138. for(ArcNode *p=LG.vexes[v].firstarc;p!=NULL;p=p->nextarc){
    139. if(!visited[p->adj]){
    140. q.push(p->adj);
    141. visited[p->adj]=1;
    142. }
    143. }
    144. }
    145. return 0;
    146. }
    147. int BFS(Graph &G,int x){
    148. int *visited=new int[G.vexNumber];
    149. for(int i=0;i
    150. visited[i]=0;
    151. for(int i=x;i
    152. if(!visited[i])
    153. oneBFS(G,i,visited);
    154. }
    155. delete[]visited;
    156. return 0;
    157. }
    158. //comment
    159. int main(){
    160. //freopen("/config/workspace/test/test","r",stdin);
    161. int n,m,x;
    162. cin>>n>>m>>x;
    163. if(n==0){
    164. cout<<0;
    165. return 0;
    166. }
    167. Graph G;
    168. G.vexNumber=n;
    169. for(int i=0;i
    170. G.vexInfo[i]=to_string(i);
    171. }
    172. G.adjMatrix[MAX_SIZE][MAX_SIZE]={0};
    173. for(int i=0;i
    174. int a,b;
    175. cin>>a>>b;
    176. G.adjMatrix[a][b]=1;
    177. G.adjMatrix[b][a]=1;
    178. }
    179. BFS(G,x);
    180. return 0;
    181. }

  • 相关阅读:
    python与Electron联合编程记录之九(Electron与Flask联合编程实现)
    PTA题目集(Java语言)
    android 自定义View:仿QQ拖拽效果
    haas506 2.0开发教程-hota(仅支持2.2以上版本)
    Java 入门练习(6 - 10)
    cdh 6.3.2 离线部署
    js-设计模式-状态模式(1)-电灯开关
    CentOS7.9 下修改MariaDB访问端口不能访问
    机器视觉工程师前景如何,计算机视觉工程师薪资
    贪心算法part2 | ● 122.买卖股票的最佳时机II ● 55. 跳跃游戏 ● 45.跳跃游戏II
  • 原文地址:https://blog.csdn.net/weixin_65908362/article/details/127874896