Route between, Nodes in Graph (Java)

Source: Internet
Author: User

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

Example

Given Graph:

A----->B----->C \     |  \    |   \   |    \  v     ->D----->E

s = B t = E for and, returntrue

s = D t = C for and, returnfalse

/*** Definition for Directed graph. * Class Directedgraphnode {* int label; * ARRAYLIST<DIRECTEDGRAPHNODE> ; Neighbors;     * Directedgraphnode (int x) {* label = x; * Neighbors = new arraylist<directedgraphnode> (); * } * }; */ Public classSolution {/**     * @paramgraph:a List of Directed graph node *@params:the starting Directed graph node *@paramt:the Terminal Directed Graph node *@return: A Boolean value*/     Public BooleanHasroute (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(intI=0;i<s.neighbors.size (); i++)            { if(Hasroute (Graph,s.neighbors.get (i), T))                return true; }                    }        return false; }}

Route between, Nodes in Graph (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.