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)