I would like to drop incoming nodes in a directed graph.
tl; dr (go to section 3)
I have a graph in which I run BFS to remove all unrelated nodes (relative to the candidate edge).
1. Example chart data
Let it be the graph data structure:
Where: - id1- source, id2- target
var links = [
{id1: 1, id2: 2},
{id1: 2, id2: 3},
{id1: 9, id2: 3},
{id1: 6, id2: 8},
{id1: 3, id2: 4}
]
Visualization above:

2. I successfully remove unbound nodes / edges
I run BFS (function below) to drop all unrelated nodes relatively edge=1that give this:
var links = [
{id1: 1, id2: 2},
{id1: 2, id2: 3},
{id1: 9, id2: 3},
{id1: 3, id2: 4}
]
Visualization for the above:

3. Now I want to delete incoming nodes
, "" node (- ), edge=1
:
var links = [
{id1: 1, id2: 2},
{id1: 2, id2: 3},
{id1: 9, id2: 3},
{id1: 3, id2: 4}
]

?
/:
var filterUnrelated = function(data, candidateId) {
var toTest = [candidateId];
var connected = [];
function addToTest(node) {
if (connected.indexOf(node) < 0 && toTest.indexOf(node) < 0) {
toTest.push(node);
}
}
function findAllConnectedNode(node) {
connected.push(node);
data.filter(function(d) {
return (d.id1 === node) || (d.id2 === node);
}).map(function(d) {
if (d.id1 === node) {
addToTest(d.id2);
}
else {
addToTest(d.id1);
}
});
}
while (toTest.length > 0) {
findAllConnectedNode(toTest.shift());
}
return data.filter(function(d) {
return (connected.indexOf(d.id1) >= 0 ||
connected.indexOf(d.id2) >= 0);
})
}
var links = [
{id1: 1, id2: 2},
{id1: 2, id2: 3},
{id1: 9, id2: 3},
{id1: 6, id2: 8},
{id1: 3, id2: 4}
]
console.log(filterUnrelated(links, 1));
Hide result