c++ 广度优先搜索
#include <iostream>
using namespace std; const int verMax=;
const int queMax=; // >=9皆可 struct node//声明图形顶点结构
{
int vertex;
struct node *next;
}; typedef struct node *graph;
struct node head[verMax];//声明顶点结构数组
int que[queMax];
int front=-;
int rear=-;
int visit[verMax];//查找记录//队列的存入 int enque(int v)
{
if(rear>=queMax)//队列已满
return -;
else
{
rear++;//对了尾端指针后移
que[rear]=v;//将值存入队列中
return ;
}
}
//队列的取出
int deque()
{
if(front==rear)//队列已空
return -;
else
{
front++;
return que[front];
}
}
//广度优先搜索法
void BFS(int v)
{
graph point;
enque(v);//存入队列
visit[v]=;//已查找
cout<<"["<<v<<"]==>";
while(front!=rear)
{
v=deque();
point=head[v].next;
while(point!=NULL)//读入邻接列表所有顶点
{
if(visit[point->vertex]==)
{
enque(point->vertex);//存入队列
visit[point->vertex]=;//已查找过的顶点
cout<<"["<<point->vertex<<"]==>";
}
point=point->next;
}
}
}
//建立邻接顶点至邻接列表内
void create_graph(int v1,int v2)
{
graph point,New;
New=(graph)malloc(sizeof(struct node));
if(New!=NULL)
{
New->vertex=v2;//邻近顶点
New->next=NULL;
point=&(head[v1]);
while(point->next!=NULL)
point=point->next;
point->next=New;//串连在列表尾端
}
}
//输出邻接列表内数据
void print_graph(struct node *head)
{
graph point;
point=head->next;//设置point为首节点
while(point!=NULL)
{
cout<<"["<<point->vertex<<"] ";
point=point->next;
}
cout<<endl;
}
//主程序
void main()
{
int node[][]={
{,},{,},{,},{,},{,},{,},{,},{,},{,},{,},
{,},{,},{,},{,},{,},{,},{,},{,},{,},{,}};
int i;
for(i=;i<verMax;i++)
{
head[i].vertex=i;
head[i].next=NULL;
}
for(i=;i<verMax;i++)
visit[i]=; for(i=;i<;i++)
create_graph(node[i][],node[i][]); cout<<"======Graph======"<<endl;
for(i=;i<verMax;i++)
{
cout<<"Vertex["<<i<<"]: ";
print_graph(&head[i]);
}
//广度优先搜索
cout<<"Bradth-First-Search:"<<endl<<"&BEGIN==>";
BFS();
cout<<"END&"<<endl;
}
c++ 广度优先搜索的更多相关文章
- 图的广度优先搜索(BFS)
把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...
- 广度优先搜索(BFS)
定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...
- 总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较
广度优先搜索(BFS) 1.将头结点放入队列Q中 2.while Q!=空 u出队 遍历u的邻接表中的每个节点v 将v插入队列中 当使用无向图的邻接表时,复杂度为O(V^2) 当使用有向图的邻接表时, ...
- ACM题目————图的广度优先搜索
题目描述 图的广度优先搜索类似于树的按层次遍历,即从某个结点开始,先访问该结点,然后访问该结点的所有邻接点,再依次访问各邻接 点的邻接点.如此进行下去,直到所有的结点都访问为止.在该题中,假定所有的结 ...
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...
- 广度优先搜索BFS
广度优先搜索可以形成一个广度优先搜索树 算法时间为O(V+E),两重循环 输入:图g,起点start(int) 需要的数据结构:队列Q.color数组(存放每个顶点的颜色) 算法过程: 1. 预处理: ...
随机推荐
- Day6 - H - Balanced Lineup POJ - 3264
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- JAVA实现--基础算法FOR选择排序
首先 实现简单的选择排序. 简单排序的思路很简单,就是通过遍历(数组的length次)的数组,每次遍历找出最小的放到数组的第一个位置,下次遍历时就不用考虑第0位置的数从第1的位置开始找1到length ...
- 树莓派3b安装Windows10 Arm
感谢老外的这个项目:https://github.com/WOA-Project/WOA-Deployer-Rpi 还有这个:https://uupdump.ml/ 首先从https://uupdum ...
- 7 —— node —— 响应图片
const http = require('http'); const fs = require('fs'); const server = http.createServer(); server ...
- CentOS 6.8 32位 安装mysql8
1.清理掉之前安装过的mysql rpm -qa | grep mysql mysql-libs-5.1.52-1.el6_0.1.x86_64 yum remove mysql-libs-5.1.5 ...
- Oracle SQL触发器
一.触发器 触发器是一个数据库对象,是一个特殊的过程,当特定的时间发生时隐式地执行.比如在一个表中发生插入.更新或删除的时间,或者 CREATE.ALTER 这样的数据定义语句执行时,触发器会隐式执行 ...
- 1-Java类结构和main函数
目录 Java类 main函数 1.Java类 - 类是java中最基础的逻辑单位 java中所有的内容都要放在类的范围中 - 类的构成 成员变量/属性 成员方法/函数 - java文件必须以.jav ...
- 134-PHP子类重写父类方法,并调用父类方法
<?php class father{ //定义father类 public function method(){ //定义方法 echo '<br />father method' ...
- benchmark与gem5-gpu交互
gem5-gpu作为一个异构多核系统的模拟器,当我们使用异构融合多核处理器架构(特别是支持HSA的处理器架构)运行GPU与CPU的benchmark时,研究自己设计的算法或添加的硬件对GPU与CPU存 ...
- Spring 控制反转容器(Inversion of Control – IOC)
系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...