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

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) {
// write your code here
HashSet<DirectedGraphNode> visited = new HashSet<DirectedGraphNode>();
       visited.add(s);
return dfs(s, t, visited);
} public boolean dfs(DirectedGraphNode s, DirectedGraphNode t, HashSet<DirectedGraphNode> visited) {
if (s == t) return true;
for (DirectedGraphNode neighbor : s.neighbors) {
if (!visited.contains(neighbor)) {
visited.add(s);
if (dfs(neighbor, t, visited))
return true;
}
}
return false;
}
}

BFS:

 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) {
// write your code here
HashSet<DirectedGraphNode> visited = new HashSet<DirectedGraphNode>();
LinkedList<DirectedGraphNode> queue = new LinkedList<DirectedGraphNode>();
if (s == t) return true;
queue.offer(s);
visited.add(s);
while (!queue.isEmpty()) {
DirectedGraphNode cur = queue.poll();
for (DirectedGraphNode neighbor : cur.neighbors) {
if (neighbor == t) return true;
if (visited.contains(neighbor)) continue;
visited.add(neighbor);
queue.offer(neighbor);
}
}
return false;
}
}

Lintcode: Route Between Two Nodes in Graph的更多相关文章

  1. 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. mysql从只有一个备份文件(多个数据库的备份)中恢复数据到指定数据库

    mysql -uroot -p 要恢复的数据库的名字 --one-database<备份文件

  2. Linux下使用inode删除文件

    Linux 下有时候某些文件无法使用 rm 直接删除, 比如该文件的文件名含有终端不能正确显示的字符.# ls -litotal 0441511 -rw-r--r-- 1 root root 0 Ap ...

  3. laravel authorize(授权)

    1.方法一  直接在AuthServiceProvider 中定义闭包,比较灵活 namespace App\Providers; ... class AuthServiceProvider exte ...

  4. textView截取字符串-医生工作台1期

    textfield截取字符串 ios7 会崩溃 解: 之前的写法是这样的 正确的写法:   先判断markedTextRange是否为nil,   markedTextRange这个属性是啥意思呢 表 ...

  5. HelloWorld之jetty运行

    jetty是一个轻便的嵌入式servlet容器.其启动运行非常简单.eclipse下运行jetty容器有如下几步, 一.建一个普通的java工程 二.把jetty需要的包导入工程分别是jetty-6. ...

  6. JBPM3.2 TABLE

    http://m.blog.csdn.net/blog/longjie_happy/9343349

  7. Jquery元素选取、常用方法;js只能获取内联样式,jquery内联内嵌都可以获取到;字符串.trim();去字符串前后空格

    一:常用的选择器: 基本选择器 $("#myDiv") //匹配唯一的具有此id值的元素 $("div") //匹配指定名称的所有元素 $(".myC ...

  8. Windows 一键安装OpenSSL

    原理:OpenSSL在github上有开源项目,我们只需要把代码克隆到本地,在本地编译一下就好了 注意事项: 1->在github上获取源码,必须要安装git for windows,网址 ht ...

  9. IOS测试程序运行耗时

    iOS设备相对于电脑,内存和处理能力有限,所以一段代码或者程序运行的时间需要时刻注意,这里提供两种获取精确时间的方法. 方法一:使用系统时间 NSDate* tmpStartData = [[NSDa ...

  10. Top Five Communication Skills for Project Managers

    Research among project managers globally identifies top communication skills for leading teams. Lead ...