My problem is as follows. I have an array of objects in the form of source/target from a graph (these are the id’s of the node). It looks like this:
[{"source": 1053, "target": 845, "value": 751}, {"source": 845, "target": 862, "value": 751}, {"source": 1053, "target": 611, "value": 751}, {"source": 1053, "target": 611, "value": 751}, {"source": 1054, "target": 905, "value": 17}, {"source": 1055, "target": 837, "value": 8}, {"source": 1055, "target": 837, "value": 8}, {"source": 1055, "target": 837, "value": 8}, {"source": 1055, "target": 400, "value": 8}, {"source": 1055, "target": 400, "value": 8}, {"source": 1055, "target": 400, "value": 8}]
Now: For a certain node id I would like to know all its children and the subsequent subchildren of these children and so on, all the way down. It works for the first “generation”, but then I would need to iterate over the targets again and again. How do I accomplish this?
nodes = [1053]
function getChildren(nodes, links) {
var children = [];
$.each(links, function(key, link) {
if (nodes.indexOf(link.source) > -1) {
children.push(link.target);
}
});
return children;
}
The recursion would look like:
…which would be a simple depth-first search.
You could also accomplish this with a loop over
result, which may be faster.