Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

Have you met this question in a real interview?

Yes
Example

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的更多相关文章

  1. 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. ...

  2. [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 ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. BFS vs DFS

    1 Clone Graph   1  copy ervery nodes by bfs  2  add neighbors public UndirectedGraphNode cloneGraph( ...

  5. HDU 4240 Route Redundancy

    Route Redundancy Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...

  6. HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)

    主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...

  7. 深度学习在graph上的使用

    原文地址:https://zhuanlan.zhihu.com/p/27216346 本文要介绍的这一篇paper是ICML2016上一篇关于 CNN 在图(graph)上的应用.ICML 是机器学习 ...

  8. Hdoj 2454.Degree Sequence of Graph G 题解

    Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and bro ...

  9. Echarts中graph类型的运用求教

    以下是百度Echarts官网上关系图的源码,但是这个关系图的node节点和edge都是静态文件里规定好的,我现在想动态实现,点击其中一个节点A然后新产生一个新节点B,并且有A和B之间的edge,就类似 ...

随机推荐

  1. Android的环境搭建

    尽管以前并没有接触过软件开发.但是,现在网络资源实在是太丰富了.所以网搜了一下,认为Android的环境搭建可分为以下五个步骤来完成.第一步:安装JDK:第二步:配置Windows上JDK的变量环境: ...

  2. Map Wiki -- proposed by Shuo Ren

    Map Wiki —— 基于Bing地图的生活百科 在旅游.逛街或是闲逛的时,很多时候,我们往往想要对于身边的美食.医院.旅馆.购物.学习.景点等信息有进一步认识.在这时,我们大多会再打开百度或者大众 ...

  3. node.js处理url常用方法

    处理非阻塞I/O /* *回调函数的方法 异步 */ /* function f(cb){ fs.readFile('./4',(err,data)=>{ cb(data.toString()) ...

  4. 三星a9上测试egret与pixi.js的渲染性能

    for (let i = 0; i < 500; i++) { let shape = new egret.Shape(); shape.graphics.beginFill(0xff0000) ...

  5. UOJ#424 【集训队作业2018】count

    题意 我们定义长度为\(n\),每个数为\(1\sim m\)之间的整数且\(1\sim m\)都至少出现一次的序列为合法序列.再定义\(pos(l,r)\)表示这个序列的区间\([l,r]\)之间的 ...

  6. logstash获取日志,时间戳相差8小时

    背景: logstash版本:2.4.9     由于logstash在获取时区的时候,默认获取的是UTC默认时间,同时elasticsearch在创建索引的时候,统一使用UTC时间,因此实际上在创建 ...

  7. 【刷题】BZOJ 2095 [Poi2010]Bridges

    Description YYD为了减肥,他来到了瘦海,这是一个巨大的海,海中有n个小岛,小岛之间有m座桥连接,两个小岛之间不会有两座桥,并且从一个小岛可以到另外任意一个小岛.现在YYD想骑单车从小岛1 ...

  8. C++实用整数快速输入输出模板(C++)

    随便写一点放在这里,以后想蛇皮卡常就很方便啦 蒟蒻太懒了,也就暂时不搞什么封namespace之类的操作了 程序结束时记得flush一下. #include<cstdio> #define ...

  9. 【AI科技大本营】

    从AutoML.机器学习新算法.底层计算.对抗性攻击.模型应用与底层理解,到开源数据集.Tensorflow和TPU,Google Brain 负责人Jeff Dean发长文来总结他们2017年所做的 ...

  10. NOIP2018 No regrets youth

    NOIP2018在即,20181009总结一些易错的知识点和解题方法 ——by ljc20020730 HGOI NOIP2018 No regrets youth ! NOIP2018 No reg ...