Graph Loop and Path
Graph Loop and Path
//1, 1
    //
    //1, 4 
    //<1, 2->3>
    //<2, 4>
    //<3, 4>
    //<2, 1>
    //              1 
    //         2        3     
    //              4
    static boolean isAPath(Node sNode, Node dNode, Map<Node, Node> map, Set<Node> set){
        if(sNode != null && dNode != null){
            if(set.contains(sNode)){
                return false;
            }else{
                set.add(sNode);
            }
            if(sNode.data == dNode.data)
                return true;
            else{
                Node curr = map.get(sNode);
                while(curr != null){
                    if(curr.data == dNode.data){
                        return true;
                    }else{
                        return isAPath(curr.next, dNode, map, set);
                    }
                }
            }
        }
        return false;
    }

    static boolean isLoop(Node curr, Map<Node, Node> map, Set<Node> set){
        if(curr != null){
            if(set.contains(curr)){
                return true;
            }else{
                set.add(curr);
                Node child = map.get(curr);
                while(child != null){
                    if(isLoop(child, map, set))
                        return true; 
                    child = child.next;
                }
                set.remove(curr);
            }
        }
        return false;
    }

    static boolean isPathMatrix(int s, int d, int[][] arr, int height, int width){
        if(arr != null && s < height){
            if(s == d)
                return true;
            else{
                for(int i=0; i< width; i++){
                    if(arr[s][i] == 1){
                        return isPathMatrix(i, d, arr, height, width); 
                    }
                }
            }
        }
        return false;
    }