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入门问题

    1.给列设置默认时间 在创建表的图形化界面,不能直接用函数sysdate()或者now()来设置默认当前时间,要用sql脚本创建的话就可以 IF NOT EXISTS `tb_1` THENBEGIN ...

  2. reduce()

    Professional.JavaScript.for.Web.Developers.3rd.Edition.Jan.2012 var value = [1,2,3,4,5]; var sum = v ...

  3. ecshop换用redis做缓存

    <?php /** * ecshop SESSION 保存类 * ================================================================ ...

  4. LINQ to SQL系列四 使用inner join,outer join

    先看一个最简单的inner join,在读取Student表时inner join Class表取的对应的Class信息: static void Main(string[] args) { usin ...

  5. appium关于定位元素

    Windows上定位元素我用的是uiautomatorviewer 这个工具在你的SDK-tools目录下,点击uiautomatorviewer.bat启动,注意appium在跑的时候是取不到的 工 ...

  6. JQuery事件的链式写法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. Selenium2学习-030-WebUI自动化实战实例-028-获取元素位置及大小

    自动化测试过程中,有时需要获取元素的位置.大小,以获取元素的位置,通过 Actions 模拟鼠标,进行相对坐标操作.例如,有些元素定位不方便,或者需要对某一元素相对区域范围进行暴力点击测试,此时就需要 ...

  8. windows下安装yaf和git

    不得不说win7下安装yaf比mac下安装yaf简单多了 1. phpinof()看一下你的php版本.我的是php 5.4所以我选择是php_yaf-2.1.9-x86-5.4-zts-nodebu ...

  9. Ubuntu 安装 Brother MFC7470D 驱动

    Ubuntu 安装 Brother MFC7470D 驱动 办公室的打印机是 Brother MFC7470D ,在 Ubuntu 中安装打印机驱动时,发现没有这个型号的驱动.只有 MFC7450 的 ...

  10. nginx的内存管理

    先来看内存池的实现,nginx的内存池实现的非常简单. 这里内存池的一些图表可以看老朱同学的slides : http://blog.zhuzhaoyuan.com/2009/09/nginx-int ...