0

I am trying to find all the child elements of a node in the following Javascript Object.

var graphObj = {

        a : {
            'true' : ['e', 'i'],
            'false' : ['u'],
            'blah' : 'extra key'
        },
        e : {
            'true' : ['o'],
            'false' : ['v'],
            'blah' : 'extra key'
        },
        f : {
            'true' : [],
            'false' : [],
            'blah' : 'extra key'
        },
        i : {
            'true' : [],
            'false' : ['f'],
            'blah' : 'extra key'
        },
        o : {
            'true' : [],
            'false' : [],
            'blah' : 'extra key'
        },
        u: {
            'true': [],
            'false': [],
            'blah' : 'extra key'
        },
        v: {
            'true': [],
            'false': [],
            'blah' : 'extra key'
        },
        z: {
            'true': [],
            'false': [],
            'blah' : 'extra key'
        },

    };

This method will return the children of a given node

var getChilds = function (opId) {


            var r;

            if (graphObj.hasOwnProperty(opId)) {

                var t = graphObj[opId].true.slice();
                var f = graphObj[opId].false.slice();

                r = t.concat(f);


            } else {
                console.log('No node found with the ID');
            }

            return r;

        }

        console.log(getChilds('a'));

current [output] => ['e', 'i', 'u']

But I need a way to accumulate all the child nodes by recursively traversing the graph.

required output => ['e', 'i', 'u', 'o', 'v', 'f']

Can anyone help me?

Note : This is a directed graph. No loops. Also true and false are the only keys on the node storing edge types.

7
  • Could you provide one sample function call based on the graphObj for the function you want? As in, define a unit test case? Commented Jan 10, 2016 at 4:26
  • Do your graphs always look like this? i.e. at the top level you have an object, at the second level several objects and at the third level arrays that contain strings? Commented Jan 10, 2016 at 4:30
  • @SpiderPig yes. It always look like that. Commented Jan 10, 2016 at 4:31
  • Is this the exact order you want? Does order matter at all? Right now, it looks like you at least want it partitioned by traversal minimum distance. Commented Jan 10, 2016 at 4:34
  • @AndrewTempleton you are correct. Order doesn't matter. Commented Jan 10, 2016 at 4:35

1 Answer 1

1

I took your "order does not matter" comment into consideration and also made sure to only include ['true', 'false'] as edge type keys to traverse on.

var graphObj = {

        a : {
            'true' : ['e', 'i'],
            'false' : ['u']
        },
        e : {
            'true' : ['o'],
            'false' : ['v']
        },
        f : {
            'true' : [],
            'false' : []
        },
        i : {
            'true' : [],
            'false' : ['f']
        },
        o : {
            'true' : [],
            'false' : []
        },
        u: {
            'true': [],
            'false': []
        },
        v: {
            'true': [],
            'false': []
        }
    };
console.log(getChildren('a')); // [ 'e', 'o', 'v', 'i', 'f', 'u' ]


function getChildren(entry) {
    var visited = {};
    var children = {};
    traverse(entry);
    return Object.keys(children);
    function traverse (entry) {
        if (visited[entry]) {
            return;
        }
        if (!graphObj[entry]) {
            throw new Error('Node "' + entry + '" does not exist in graph!');
        }
        var edgeTypes = ['true', 'false'];
        // USE THIS LINE FOR ARBITRARY EDGE TYPES
        // var edgeTypes = Object.keys(graphObj[entry]);
        visited[entry] = true;
        edgeTypes.forEach(function(edgeType) {
            var nodeList = graphObj[entry][edgeType] || [];
            nodeList.forEach(function(nodeLetter) {
                children[nodeLetter] = true;
                traverse(nodeLetter);
            });
        });
    }
}

Sign up to request clarification or add additional context in comments.

3 Comments

This solution provides the exact behaviour I wanted. Kudos.
No problem :) Please update your actual question to reflect that (1) the graph is not strictly a tree and (2) true and false are the only keys on the node storing edge types, so that the question and answer make sense for future readers
Sure. I will modify it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.