背景

(以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool)
DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路。记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个盒子里面搜索。等把这一套盒子都打开完,再打开第二套的。Wikipedia上的讲解是:“Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.”通常来说简便的DFS写法是用递归,如果非递归的话就是栈套迭代,思想是一样的。递归写法的DFS伪代码如下:Input: A graph G and a root v of G
1   procedure DFS(G,v):
2       label v as discovered
3       for all edges from v to w in G.adjacentEdges(v) do
4           if vertex w is not labeled as discovered then
5               recursively call DFS(G,w)
非递归写法的DFS伪代码如下:Input: A graph G and a root v of G
1   procedure DFS-iterative(G,v):
2       let S be a stack
3       S.push(v)
4       while S is not empty
5             v ← S.pop() 
6             if v is not labeled as discovered:
7                 label v as discovered
8                 for all edges from v to w in G.adjacentEdges(v) do
9                     S.push(w)


BFS(Breadth-first Search)这个就是相对于BFS的另外一种对图的遍历方法,对于一个节点来说先把所有neighbors都检查一遍,再从第一个neighbor开始,循环往复。由于BFS的这个特质,BFS可以帮助寻找最短路径。Wikipedia上面对BFS的定义是:“In graph theory, breadth-first search (BFS) is a strategy for searching in a graphwhen search is limited to essentially two operations: (a) visit and
inspect a node of a graph; (b) gain access to visit the nodes that
neighbor the currently visited node. The BFS begins at a root node and
inspects all the neighboring nodes. Then for each of those neighbor
nodes in turn, it inspects their neighbor nodes which were unvisited,
and so on. Compare BFS with the equivalent, but more memory-efficient Iterative deepening depth-first search and contrast with depth-first search.”

题目

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.


OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1/ \/   \0 --- 2/ \\_/

总之就是遍历搜有的路径,然后再一个一个节点添加进去。

代码

/*** Definition for undirected graph.* class UndirectedGraphNode {*     int label;*     List<UndirectedGraphNode> neighbors;*     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }* };*/
public class Solution {public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {if (node==null) return null;HashMap<UndirectedGraphNode,UndirectedGraphNode> hm=new HashMap<UndirectedGraphNode,UndirectedGraphNode>();LinkedList<UndirectedGraphNode> queue=new LinkedList<UndirectedGraphNode>();UndirectedGraphNode head = new UndirectedGraphNode(node.label);hm.put(node, head);queue.add(node);while(!queue.isEmpty()){UndirectedGraphNode current=queue.poll();for(UndirectedGraphNode neighbor:current.neighbors){if(!hm.containsKey(neighbor)){queue.add(neighbor);UndirectedGraphNode temp=new UndirectedGraphNode(neighbor.label);hm.put(neighbor,temp);}hm.get(current).neighbors.add(hm.get(neighbor));}}return head;}
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)相关推荐

  1. LeetCode-笔记-199. 二叉树的右视图——BFS广度优先搜索

    LeetCode-笔记-199. 二叉树的右视图 199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,n ...

  2. PTA:7-102 喊山 (30分)---解析(bfs广度优先搜索,vector)

    7-102 喊山 (30分) 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出"喂-喂喂-喂喂喂--"的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的& ...

  3. 利用BFS广度优先搜索还原二阶魔方

    利用BFS广度优先搜索还原二阶魔方 采用BFS深度优先搜索算法进行了对于魔方求解问题的建模,并且利用C++代码进行了算法实现,能够实现输入魔方状态,自动输出解法的效果. BFS是图论中一种基本的搜索算 ...

  4. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

  5. [ACM_NYOJ_21]三个水杯(BFS广度优先搜索)

    三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只 ...

  6. 哈理工OJ 1490 咒语(BFS广度优先搜索)

    咒语 Time Limit: 1000 MS Memory Limit: 65535 K Total Submit: 162(37 users) Total Accepted: 53(35 users ...

  7. 农夫过河游戏的几种处理方法(DFS深度优先搜索,BFS广度优先搜索)

    农夫过河游戏规则:在左岸有农夫.狼.羊.菜,农夫需要想办法将狼.羊.菜最终都带到右岸,条件就是农夫不在的时候,狼会吃羊,羊会吃菜,而且每次只能带一样,或者不带. 这里会用到堆栈.队列.列表这样的数据结 ...

  8. LeetCode 752. 打开转盘锁 (C#实现)——队列,广度优先搜索

    一.代码实现 思路:使用先进先出的队列,广度优先搜索,使用字典可大幅减少遍历时间 问题:https://leetcode-cn.com/problems/open-the-lock/submissio ...

  9. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

最新文章

  1. Android学习系列(10)--App列表之拖拽ListView(上)
  2. iOS 设置app语言中文,比如 copy中文,拍照按钮cancel 中文
  3. IAR J-Link下载程序出现错误提示:Failed to get CPU status after 4 retries Retry?
  4. macos mysql 阿帕奇_Mac配置apache,mysql
  5. 20非常有用的Java程序片段(3)
  6. 分支结构程序案例c语言,C语言学习之三——分支结构程序
  7. 面试官系统精讲Java源码及大厂真题 - 39 经验总结:不同场景,如何使用线程池
  8. 日志,错误日志,成功日志,日志是个好东西。
  9. nginx限制并发连接数和连接请求数
  10. java发送邮件的两种实现方式(包括如何伪造发件人及其原理)
  11. 各类木材强度_常用木材防腐剂有哪些
  12. 研发Owner的职责
  13. 华清远见-重庆中心-JAVA基础阶段技术总结/知识点梳理/个人总结/关于JAVA技术的解析(看法)/面试题解析
  14. OpenGL-绘制旋转立方体
  15. Java项目:springboot客户关系管理系统
  16. 使用sass预处理器的优劣
  17. AEDA: An Easier Data Augmentation Technique for Text Classification
  18. android 获取文件大小
  19. 怎么让游戏强制窗口化_游戏防上瘾,趁父母午睡时拍照片让你玩不成!
  20. IDL是什么呢???

热门文章

  1. PAT甲级1054 The Dominant Color:[C++题解]哈希表、水题
  2. 基坑监测日报模板_长沙一工地基坑坍塌致2人死亡!基坑坍塌的瞬间,只有无能为力和惊心动魄!...
  3. C++中的Lambda表达式详解
  4. oracle 备份 无客户端,已解决: 备份linux oracle 突然不能备份了 - Dell Community
  5. html5毕业作品开场白,毕业典礼主持人开场白
  6. dbartisan mysql_Sybase数据库安全
  7. Eclipse中的常用快捷键
  8. 计数信号量的原理与创建
  9. 不属于计算机常用软件日常应用的是,综合技能实践+计算机常用应用软件的安装和使用指导 (1)...
  10. matlab vdp1000,第四章 MATLAB的数学运算.ppt