Breadth-first search
given a graph G and a distinguished source vertex s, breadth-first
search systematically explores the edges of G to “discover” every vertex that is
reachable from s.
To keep track of progress, breadth-first search colors each vertex white, gray, or
black. All vertices start out white and may later become gray and then black. A
vertex is discovered the first time it is encountered during the search, at which time
it becomes nonwhite. Gray and black vertices, therefore, have been discovered, but
breadth-first search distinguishes between them to ensure that the search proceeds
in a breadth-first manner
package element_graph; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import edu.princeton.cs.algs4.Queue; public class breadth_first_search {
private static class vertex{
private LinkedList<vertex> link;
private String name;
private String color;
private vertex p;
private int d;
public vertex(String na,LinkedList<vertex> lin){
name = na;
link = lin;
color = "white";
p = null;
d = 9999;
}
}
public static void BFS(vertex s){
s.color = "grey";
s.d = 0;
Queue<vertex> q = new Queue<vertex>();
q.enqueue(s);
while(!q.isEmpty()){
vertex u = q.dequeue();
for (vertex v : u.link) { //every adj
if(v.color == "white"){
v.color = "gray";
v.d = v.d + 1;
v.p = u;
q.enqueue(v);
}
}
u.color = "black";
} }
public static void printpath(vertex s,vertex v){
if(v == s){
System.out.println(s.name);
}
else if(v.p == null){ //will not get s
System.out.println("no way");
}
else{
printpath(s,v.p);
System.out.println(v.name);
}
} public static void main(String[] args) {
LinkedList<vertex> sl = new LinkedList<vertex>(); LinkedList<vertex> rl = new LinkedList<vertex>(); LinkedList<vertex> vl = new LinkedList<vertex>(); LinkedList<vertex> wl = new LinkedList<vertex>(); LinkedList<vertex> tl = new LinkedList<vertex>(); LinkedList<vertex> xl = new LinkedList<vertex>(); LinkedList<vertex> ul = new LinkedList<vertex>(); LinkedList<vertex> yl = new LinkedList<vertex>(); vertex sv = new vertex("s",sl);
vertex rv = new vertex("r",rl);
vertex vv = new vertex("v",vl);
vertex wv = new vertex("w",wl);
vertex tv = new vertex("t",tl);
vertex xv = new vertex("x",xl);
vertex uv = new vertex("u",ul);
vertex yv = new vertex("y",yl);
sl.add(rv);
sl.add(wv);
rl.add(sv);
rl.add(vv);
vl.add(rv);
wl.add(xv);
wl.add(tv);
wl.add(sv);
tl.add(xv);
tl.add(uv);
tl.add(wv);
xl.add(tv);
xl.add(uv);
xl.add(yv);
xl.add(wv);
xl.add(tv);
xl.add(xv);
xl.add(yv);
xl.add(uv);
xl.add(xv);
BFS(sv);
printpath(sv,tv); }
}
Breadth-first search的更多相关文章
- 广度优先搜索(Breadth First Search, BFS)
广度优先搜索(Breadth First Search, BFS) BFS算法实现的一般思路为: // BFS void BFS(int s){ queue<int> q; // 定义一个 ...
- 广度优先搜索(Breadth First Search)
Date:2019-07-03 14:29:02 走完一层的所有房间,再走下一层,用队列实现 算法实现 /*--------------------------模版------------------ ...
- [Algorithm] Breadth First JavaScript Search Algorithm for Graphs
Breadth first search is a graph search algorithm that starts at one node and visits neighboring node ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
Depth first search is a graph search algorithm that starts at one node and uses recursion to travel ...
- C#算法知识点记录
针对算法的知识点进行记录 简易桶排序 首先看一个简易桶排序,有一串数字,进行从大到小排列.数字间隔不大,使用一维数组来当作桶,进行插入排序. static void Main(string[] arg ...
- 译:Boost Property Maps
传送门:Boost Graph Library 快速入门 原文:Boost Property Map 图的抽象数学性质与它们被用来解决具体问题之间的主要联系就是被附加在图的顶点和边上的属性(prope ...
- 分支界定法 branch-and-bound 分析与实现)(转载)
1. 介绍分支界定法之前需要了解一下广度优先搜索breadth-First-search(BFS) 1.从图中某个顶点V0出发,并访问此顶点:以层为顺序,一层一层往下遍历 2.从V0出发,访问V0的各 ...
- I am Nexus Master!(虽然只是个模拟题。。。但仍想了很久!)
I am Nexus Master! The 13th Zhejiang University Programming Contest 参见:http://www.bnuoj.com/bnuoj/p ...
- 用python语言讲解数据结构与算法
写在前面的话:关于数据结构与算法讲解的书籍很多,但是用python语言去实现的不是很多,最近有幸看到一本这样的书籍,由Brad Miller and David Ranum编写的<Problem ...
随机推荐
- Django web框架-----url path name详解
说明:mytestsite是django框架下的项目,quicktool是mytestsite项目中的应用 quicktool/view.py文件修改视图函数index(),渲染一个home.html ...
- MTLAB: 稀疏矩阵的表示-sparse
我们在学习数值算法的过程中,发现像有限差分,谱方法和有限元方法的微分矩阵($\tt Differentiation~Matrices$)往往是稀疏的(即非零元素个数为$O(N)$,其中$N$为矩阵的维 ...
- 【Java】【14】从后往前每隔n位加逗号(用于货币)
1,String类型的数据 /** * @param strValue 待处理的数 * @param num 隔的位数 */ public static String separateStr(Stri ...
- python 报错 TabError: inconsistent use of tabs and spaces in indentation
写python的时候如果出现如题的错误 TabError: inconsistent use of tabs and spaces in indentation 意为:制表符错误:缩进中制表符和空格使 ...
- ubuntu 安装nginx, 出现 Unable to locate package
今天在初始化一台新的ubuntu 服务器时,敲上了 sudo apt-get install nginx 来安装nginx, 却发现提示: Reading package lists... Done ...
- index.html jquery
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 【IDEA填坑】springboot整合ssm框架
遇到俩问题:一个是mybatis生疏 在EmpMapper.xml中定义resultMap <resultMap id="EmpWithDept" type="c ...
- Angular7.1.4+Typescript3.1框架学习(三)
接着第二部分,这部分介绍常见angular指令使用 1. 在父页面文件中添加组件(通过ng命令生成的component) <app-messages></app-messages&g ...
- 使用 dva 如何配置异步加载路由组件
来源:https://www.jianshu.com/p/69694013e36b----------------------------------------------------- 普通方式 ...
- js生成二维码 qrcode
js生成二维码 QRcode npm 地址 1.安装qrcode //在项目文件夹中执行: npm install --save qrcode //或者,将其全局安装以使用qrcode命令行来保存qr ...