JSFiddle - React, Tailwind, and code Playground
by jrab227
JavaScript
let graph = {
'0': [1, 2],
'1': [3],
'2': [4],
'3': [5],
'4': [5],
'5': []
};
function findPaths(path, index, target, seen) {
seen[index] = true;
if (index === target) {
return [path + ',' + index];
}
let paths = [];
for(var end of graph[index]) {
paths.push( findPaths(path + ',' + index, end, target, seen));
}
return [].concat.apply([], paths);
}
console.log(findPaths('', 0, 5, {}));