Print all children k distance from given node in C++
1. How we can translate a graph from two dimentional array or vice versa.
The idea is simple:
The Graph is formed by nodes and edges
Each edge is decided by two nodes
It means we can use $[x, y]$ to define an edge
Edge is just a weight
Graph can be translated to two dimentional array from about idea.
TODO: there is error on this code when k = 0
    void kdistGraph(int** arr, int k, int h, int w){
        if(k == 0){
            std::cout<<"["< 0){
            for(int i=0; i< w; i++){
                if(h != i && arr[h][i]){
                    if(k - 1 == 0)
                        std::cout<< "[" << i << "]" << std::endl;
                    else if(k > 0)
                        kdistGraph(arr, k-1, i, w);
                }
            }
        }
        
    }
    

Print all children K distance from a Node in a Graph

Source Code

Correct version in Java it handles when k is zero
    When k = 0, it handles it separately if printPath()
    
     // /Users/cat/myfile/bitbucket/java/MinimumPath.java
    // The method handles k = 0
    public static void printPath(int[][] arr2d, int h, int w, int k){
        if(arr2d[h][w] > 0){
            if(k == 0)
                Print.pl("[" + h + "][" + w + "]=" + arr2d[h][w]);
            else {
                allPaths(arr2d, w, k - 1);
            }
        }
    }
    // find all the paths that have length k
    // if k == 0, then print the root node, if node has loop 
    // if node has loop, it is trick => you can loop forever 
    // (0) - 1 > (1) => [1][2] = 1
    public static void allPaths(int[][] arr, int h, int k){
        if(arr != null){
            int height = arr.length;
            int width  = arr[0].length;
            for(int w = 0; w < width; w++){
                if(h != w && arr[h][w] > 0){
                    if(k == 0){
                        Print.pl("[" + h + "][" + w + "]=" + arr[h][w]);
                    }else{
                        allPaths(arr, w, k - 1); 
                    }
                }
            }
        }
    }
    
Find the maximum weight path in k distances from a graph
    The problem is similar to previous problem:
    We need to find the maximum weight of all children when the distance is k

    Algorithm:
    If the distance is equal to zero, 
        then return maximum weight between current max weight and the weight of current node
    else if the distance is less than k, 
        recure each child for the current node with k - 1

    Skeleton code:
    maxPath(int[][] arr, int h, int k)
        int currMax = 0;
        for(int r = 0; r < len; r++)
            if(h != r && arr[h][r] > 0)
                if( k == 0)
                    return max(currMax, arr[h][r])
                else
                    return maxPath(arr, r, k - 1) 
                
            
        return currMax;