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. 博弈论初步(SG函数)

    讲解见此博客https://blog.csdn.net/strangedbly/article/details/51137432 理解Nim博弈,基于Nim博弈理解SG函数的含义和作用. 学习求解SG ...

  2. 超详细的PDF Expert的注释功能介绍

    今天,要给大家很是详细地介绍一下PDF Expert(一款专门在mac上使用的PDF阅读编辑器)的注释功能,让有点健忘的各位小伙伴们通过积极地与文本交互,从而记住更多的专业书内容. 具体使用方法请看以 ...

  3. Vue2全家桶之一:vue-cli(vue脚手架)超详细教程

    本文转载于:https://www.jianshu.com/p/32beaca25c0d   都说Vue2简单上手容易,的确,看了官方文档确实觉得上手很快,除了ES6语法和webpack的配置让你感到 ...

  4. 个人信息——头像更换(拍照或相册上传)~ 微信小程序

    微信小程序中 在用户信息中关于用户头像更换(拍照或相册上传)功能实现. 图像点击触发事件: <image src='{{personImage}}' bindtap='changeAvatar' ...

  5. PHP的openssl_encrypt方法的Java实现

    <?php class OpenSSL3DES { /*密钥,22个字符*/ const KEY='09bd821d3e764f44899a9dc6'; /*向量,8个或10个字符*/ cons ...

  6. ACM 第十一届 河南省省赛A题 计划日

    一.题目描述如下: 二.思路分析 其实这个如果是一个填空题,可以直接用Excel快速计算出来,反而用代码比较麻烦 说一下我的代码的思路: 1.如果N大于本月剩下的天数,就先从N天里减去本月剩下的天数, ...

  7. Oracle单机Rman笔记[3]---RMAN脱机备份及命令基础介绍

    A.NOARCHIVELOG模式下的物理备份 1.完全关闭数据库 2.备份所有的数据库文件.控制文件.联机重做日志 3.重新启动数据库 B.RMAN的体系结构概述 重新构建控制文件: 将控制文件备份为 ...

  8. Oracle通用维、父子维相互转换

    所谓通用维即维度层级1.2.3均作为字段展示为列,父子维即维度id+父级维度+维度层级字段 通用维 lvl_id1 lvl_name1 lvl_id2 lvl_name2 lvl_id3 lvl_na ...

  9. input radio单选框绑定change事件

    html页面: <input type="radio" name="sex" value="female">female < ...

  10. kafka已生产消息查看

    在测试过程中有用到kafka,由于开发说我往kafka里面生产了消息,通知了对方要消费....看到这块一头雾水 kafka主要2个功能生产和消费 ##查询topic列表 ./kafka-topics. ...