Route Between Two Nodes in Graph (java)

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

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

/**
 * 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
        if(s==t) return true;
        if(s.neighbors.size()==0) return false;
        if(s.neighbors!=null)
        {
           for(int i=0;i<s.neighbors.size();i++)
            { if(hasRoute(graph,s.neighbors.get(i),t))
                return true;
            }

        }
        return false;
    }
}
时间: 2024-10-12 19:08:34

Route Between Two Nodes in Graph (java)的相关文章

Route Between Two Nodes in Graph

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

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

[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 nodes. LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制,Course Schedule 课程清单 和 Course Schedule II 课程清单之二.目前看来CareerCup中有关图的题在第四章中仅此一道,这是

leetcode 133. Clone Graph ----- java

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

24. Swap Nodes in Pairs Java solutions

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

BFS vs DFS

1 Clone Graph   1  copy ervery nodes by bfs  2  add neighbors public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return node; } List<UndirectedGraphNode> nodes = new ArrayList<>(); Map<UndirectedGraphNode,

HDU 4240 Route Redundancy

Route Redundancy Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 424064-bit integer IO format: %I64d      Java class name: Main A city is made up exclusively of one-way steets.each street in the city has a ca

hdu 2680 Choose the best route 大年三十的首A 赤裸裸的Dijkstra 做这题需要一个小技巧

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8063    Accepted Submission(s): 2655 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2801    Accepted Submission(s): 1