Route Between Two Nodes in Graph
Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
Given graph:
A----->B----->C
\ |
\ |
\ |
\ v
->D----->E
for s = B and t = E, return true
for s = D and t = C, return false
Analysis:
Set + DFS
/**
* Definition for Directed graph.
* class DirectedGraphNode {
* int label;
* ArrayList<DirectedGraphNode> neighbors;
* DirectedGraphNode(int x) {
* label = x;
* neighbors = new ArrayList<DirectedGraphNode>();
* }
* };
*/
public class Solution {
/**
* @param graph: A list of Directed graph node
* @param s: the starting Directed graph node
* @param t: the terminal Directed graph node
* @return: a boolean value
*/
public boolean hasRoute(ArrayList<DirectedGraphNode> graph,
DirectedGraphNode s, DirectedGraphNode t) {
Set<DirectedGraphNode> visited = new HashSet<DirectedGraphNode>();
return dfs(graph, s, t, visited);
} public boolean dfs(ArrayList<DirectedGraphNode> graph,
DirectedGraphNode s, DirectedGraphNode t,
Set<DirectedGraphNode> visited) {
// corner cases
if (s == null || t == null) return false;
if (s == t) {
return true;
} else {
// flag visited node, avoid cylic
visited.add(s);
// compare unvisited neighbor nodes recursively
if (s.neighbors.size() > ) {
for (DirectedGraphNode node : s.neighbors) {
if (visited.contains(node)) continue;
if (dfs(graph, node, t, visited)) return true;
}
}
} return false;
}
}
Route Between Two Nodes in Graph的更多相关文章
- Lintcode: Route Between Two Nodes in Graph
Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...
- [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nod ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- BFS vs DFS
1 Clone Graph 1 copy ervery nodes by bfs 2 add neighbors public UndirectedGraphNode cloneGraph( ...
- HDU 4240 Route Redundancy
Route Redundancy Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...
- HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)
主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...
- 深度学习在graph上的使用
原文地址:https://zhuanlan.zhihu.com/p/27216346 本文要介绍的这一篇paper是ICML2016上一篇关于 CNN 在图(graph)上的应用.ICML 是机器学习 ...
- Hdoj 2454.Degree Sequence of Graph G 题解
Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and bro ...
- Echarts中graph类型的运用求教
以下是百度Echarts官网上关系图的源码,但是这个关系图的node节点和edge都是静态文件里规定好的,我现在想动态实现,点击其中一个节点A然后新产生一个新节点B,并且有A和B之间的edge,就类似 ...
随机推荐
- 冲刺Two之站立会议7
今天我们把软件的基本功能完成之后,又对所有的界面进行了统一规范化并进行了相应的优化.
- Four-Operations
开发环境:Eclipse 结对小伙伴:201306114416 陈键 (http://www.cnblogs.com/be-the-one/) 201306114452 吴舒婷 (http://www ...
- (第二周)读《我是一只IT小小鸟》有感
读了蒋宇东学长的这篇描述他成长经历和生活感悟的博文,我真的收获了很多,有一种“相见恨晚”的感觉.同为航院的学子,我们有太多太多相同的生活学习经历. 我已经是一名大三的学生了,不知不觉中我大学生活的大部 ...
- Objective-C语言--self和super关键字解析
看代码: @implementation Son : Father - (id)init{ self = [super init]; if (self){ } return self; } self是 ...
- Java的4种保留4位小数的方法(转)
上网查到的4种方法 其实刚学java……谁知道java里面的这么多方法啊……java里面重要的包本来就不少啊 ……跟我学粤语的小徒弟问我的东东…… 写两种方式……直接在main函数里面写的.还有就是利 ...
- Jenkins发送邮件中文乱码问题解决
在环境变量中添加: JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8 配置好后,重启Jenkins即可
- AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到
AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到
- suse11/12关闭防火墙
suse11关闭操作为:service SuSEfirewall2_setup stopservice SuSEfirewall2_init stop 取消开机启动防火墙:chkconfig SuS ...
- Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别以及ClassPathXmlApplicationContext 的具体路径
一.ClassPathXmlApplicationContext 的具体路径 String s[] = System.getProperty("java.class.path"). ...
- MT【187】余弦的线性组合
已知$\alpha+\beta+\gamma=\pi,(\alpha,\beta,\gamma\ge0)$ 求:$3\cos\alpha+4\cos\beta+5\cos\gamma$的最大值____ ...