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,就类似 ...
随机推荐
- 淘宝店铺模板开发SDK2.0下载安装图文教程
使用TortoiseSVN Checkout TAE SDK2.0 废话少说,切入主题: 1.在http://tortoisesvn.net/downloads.html上下载TortoiseSVN ...
- 清华大学OS操作系统实验lab1练习知识点汇总
lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...
- Helm 安装 nginx-ingress 的方法
还是没学会 今天继续研究. Using Helm NGINX Ingress controller can be installed via Helm using the chart stable/n ...
- windows无法安装到这个磁盘 gpt分区形式
利用U盘装系统的步骤 工具: Rufus 下载地址:http://rufus.akeo.ie/?locale=zh_CN -------------------------------------- ...
- 深入理解ajax系列第六篇——头部信息
前面的话 每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用.XHR对象提供了操作头部信息的方法.本文将详细介绍HTTP的头部信息 默认信息 默认情况下,在发送XHR请求的同时,还会 ...
- 题解 P1130 【红牌】
逆推dp经典题目:数字三角形的折叠版 为什么这么说? 因为我们会发现:除了每一次都特判一下是否转换行号以外,剩下的思想没什么不同. 没看题目的看这里 先定义: n,m是步骤数目,小组数目 work[i ...
- Django_终端打印原生SQL语句
打印所有的sql语句 在Django项目的settings.py文件中,在最后复制粘贴如下代码: LOGGING = { 'version': 1, 'disable_existing_loggers ...
- 第一天进入博客这个神奇的领域 在此%%%erosun
第一条博客水一水 等会要找一下调博客模板 独立动手,丰衣足食
- NOIP2018备考——DP专题练习
P4095 [HEOI2013]Eden 的新背包问题 P2657 [SCOI2009]windy数 P3413 SAC#1 - 萌数 P3205 [HNOI2010]合唱队 P476 ...
- 洛谷P4774 BZOJ5418 LOJ2721 [NOI2018]屠龙勇士(扩展中国剩余定理)
题目链接: 洛谷 BZOJ LOJ 题目大意:这么长的题面,就饶了我吧emmm 这题第一眼看上去没法列出同余方程组.为什么?好像不知道用哪把剑杀哪条龙…… 仔细一看,要按顺序杀龙,所以获得的剑出现的顺 ...