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的更多相关文章

  1. 广度优先搜索(Breadth First Search, BFS)

    广度优先搜索(Breadth First Search, BFS) BFS算法实现的一般思路为: // BFS void BFS(int s){ queue<int> q; // 定义一个 ...

  2. 广度优先搜索(Breadth First Search)

    Date:2019-07-03 14:29:02 走完一层的所有房间,再走下一层,用队列实现 算法实现 /*--------------------------模版------------------ ...

  3. [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 ...

  4. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  5. [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 ...

  6. C#算法知识点记录

    针对算法的知识点进行记录 简易桶排序 首先看一个简易桶排序,有一串数字,进行从大到小排列.数字间隔不大,使用一维数组来当作桶,进行插入排序. static void Main(string[] arg ...

  7. 译:Boost Property Maps

    传送门:Boost Graph Library 快速入门 原文:Boost Property Map 图的抽象数学性质与它们被用来解决具体问题之间的主要联系就是被附加在图的顶点和边上的属性(prope ...

  8. 分支界定法 branch-and-bound 分析与实现)(转载)

    1. 介绍分支界定法之前需要了解一下广度优先搜索breadth-First-search(BFS) 1.从图中某个顶点V0出发,并访问此顶点:以层为顺序,一层一层往下遍历 2.从V0出发,访问V0的各 ...

  9. I am Nexus Master!(虽然只是个模拟题。。。但仍想了很久!)

    I am Nexus Master!  The 13th Zhejiang University Programming Contest 参见:http://www.bnuoj.com/bnuoj/p ...

  10. 用python语言讲解数据结构与算法

    写在前面的话:关于数据结构与算法讲解的书籍很多,但是用python语言去实现的不是很多,最近有幸看到一本这样的书籍,由Brad Miller and David Ranum编写的<Problem ...

随机推荐

  1. laravel框架安装Curl扩展

    然后进入根目录执行命令     composer update 完毕.

  2. PHP 框架实现原理

    一.MVC模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式. 控制器(Controller).负责转发请求,对请求进行处理. 视图(View).界面设计人员 ...

  3. 常用User-Agent大全

    浏览器User-Agent的详细信息 PC端: safari 5.1 – MAC User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8 ...

  4. Scala映射与元组篇

    *Scala有十分易用的语法来创建.查询和便利映射 *你需要从可变的和不可变的映射中做出选择 *默认情况下,你得到的是一个哈希映射,不过你也可以指明要树形映射 *你可以很容易地在Scala映射和Jav ...

  5. jqGrid选择列控件向右拖拽超出边界处理

    jqGrid选择列控件向右拖拽超出边界处理 $("#tb_DeviceInfo").jqGrid('navButtonAdd', '#jqGridPager', {         ...

  6. [cf contest246] E - Blood Cousins Return

    [cf contest246] E - Blood Cousins Return time limit per test 3 seconds memory limit per test 256 meg ...

  7. js中for(var key in o ){};用法小记

    o不只可以是对象,key也不只可以是对象中的键. o也可以是一个数组,这时候的key就是数组的下标,从"0"开始,注意下标“0”是个字符串类型. 但是这种循环在 IE8浏览器下 对 ...

  8. 磁盘当前目录下存在文件 c1.txt,其中存放了一段英文文字。请编程实现将c1.txt中英文文字全部转换为答谢字母,并保存到c2.txt中。要求:c2.txt文件前面保存的是c1.txt文案中的原始文字,后面紧跟着的是转换后的文字

    #include"stdio.h"#include"string.h" void main(){ FILE *fp1,*fp2; char ch[1000]=& ...

  9. python之路-----前端之js(一)

    一.JS发展历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Netscape ...

  10. Python-接口自动化(三)

    python基础知识(三) (三)函数 1.函数 函数的语法: def 函数名(): 函数体 a.函数的关键字是def,函数体就是你希望这个函数帮你实现什么功能,函数名命名需要遵循的原则是以小写字母分 ...