[Cracking the Coding Interview] 4.1 Route Between Nodes 节点间的路径
Given a directed graph, design an algorithm to find out whether there is a route between nodes.
这道题让我们判断给定的一个图的任意两个节点间是否有路径。首先我们要知道如何定义一个图, 一般最常用的两种表示图的方法是: Adjacency Matrix 和 Adjacency List。
对选择用哪种方法来表示图取决于你要做什么样的操作和使用的难易程度。下面稍微总结了一下这两种方法的优缺点:
Adjacency Matrix
优点:邻接矩阵容易表示并且是用的过程可以清楚的看到节点之间的关系。删除一条边需要O(1)时间复杂度,判断两个节点间是否有路径非常快,只需要O(1)时间复杂度。
缺点:空间复杂度比较大O(V^2). 即使是稀疏矩阵也需要同样的空间。如果增加一个节点需要O(V^2)的时间复杂度。
Adjacency List
class Node
attr_accessor :name, :neighbors def initialize(name)
@name = name
@neighbors = []
end
end class Graph
attr_accessor :nodes def initialize
@nodes = []
end
end
优点:节省空间O(V+E). 增加一个节点很容易O(1)。
缺点:判断两个节点是否右边不是很快,需要O(V)的时间复杂度。
所以这道题在使用不同的图的表示方法的时候,判断两点间的路径方法是不一样的。我们假设使用Adjacency List的方法来表示图。
如何判断节点间是否有路径呢?这道题很简单,我们只需要使用DFS或者BFS来遍历整个图就可以了。
参见如下代码:
BFS Version
class Node
attr_accessor :name, :neighbors, :visited def initialize(name)
@name = name
@neighbors = []
@visited = false
end
end def has_route?(node1, node2)
return false if node1.nil? || node2.nil? q = Queue.new
node1.visited = true
q << node1 while !q.empty?
cur = q.pop return true if cur.name == node2.name cur.neighbors.each do |n|
if !n.visited
n.visited = true
q << n
end
end
end
false
end
DFS Version
class Node
attr_accessor :name, :neighbors, :visited def initialize(name)
@name = name
@neighbors = []
@visited = false
end
end def has_route?(node1, node2)
return false if node1.nil? || node2.nil?
return true if node1.name == node2.name node1.visited = true
node1.neighbors.each do |n|
next if n.visited
if has_route?(n, node2)
return true
end
end
false
end
[Cracking the Coding Interview] 4.1 Route Between Nodes 节点间的路径的更多相关文章
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- SqlParameter.Value = NULL 引发的数据库异常
摘自:http://www.cnblogs.com/ccweb/p/3403492.html using (SqlCommand cmd = new SqlCommand()) { cmd.Conne ...
- 阿里云免费ssl,https证书的申请和校验
其实写这个之前一直在考虑要不要写出来 ,真的官方文档实在太强大了,连视频都给你录好了,配不好的,是不是可以考虑不用写程序了, 忽然想到第一次使用微信测试号,进行域名认证的时候,因为后台返回“echar ...
- 总结:从Node爬取数据到前端图表展示
最近寒假在家学习Node.js开发,光看书或者跟着敲代码还不够,得找一点有趣的事情来玩一玩,于是我决定写一个Node爬虫,爬取一些有意思或者说是有用的数据.这个决定只与我的兴趣有关,与Python或者 ...
- WAS部署 -- SRVE0255E: 尚未定义要处理 /snoop 的 Web 组/虚拟主机
问题描述: URL:http://localhost:9080/pay: (中文)SRVE0255E: 尚未定义要处理 /snoop 的 Web 组/虚拟主机 访问出现以上错误. 找了很多资料,觉得说 ...
- chromedriver链接
http://npm.taobao.org/mirrors/chromedriver/
- 显卡 GPU 关系
https://zhidao.baidu.com/question/1238935513507031339.htmlGraphic Processing Unit,意思就是图形处理器啊,显卡的由GPU ...
- python 闭包@装饰器
1.装饰器 装饰器(Decorator)相对简单,咱们先介绍它:“装饰器的功能是将被装饰的函数当作参数传递给与装饰器对应的函数(名称相同的函数),并返回包装后的被装饰的函数”,听起来有点绕,没关系,直 ...
- Collections.synchronizedList线程安全性陷阱
摘要: 详细的解析:Collections.synchronizedList 1 :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedLi ...
- iview中table里嵌套i-switch、input、select等
iview中table内嵌套 input render:(h,params) => { return h('Input',{ props: { value:'', size:'small', } ...
- phpstudy 出现You don't have permission to access / on this server.
You don't have permission to access / on this server. 去掉vhost文件中的Options FollowSymLinks ExecCGI 第一次发 ...