I have a connected, directed, circular graph. The task is to detect each node in the graph without falling into an infinite loop, as a normal tree traversal algorithm does.
It can be assumed that I already know why to start a node in order to reach all the points in a directed graph, and that for each node I have a function that will return the nodes to which it directs. Is there a known algorithm for finding all nodes?
The main problem is to avoid loops, and I would love it if there was a way to do this without tracking every node and comparing it with a list of nodes already passed.
If you need more information, the urgent task is to get a list of each named function in JavaScript, including functions that are properties of other objects. So I tried something like the following, since I thought that the JS object references to each other made a tree (but of course it is not):
function __findFunctions(obj){
for (var f in obj){
if (obj === obj[f]){
continue
}
if (typeof obj[f] === 'function' &&
obj.hasOwnProperty(f) &&
!(/\[(native\scode|object\sfunction)\]/i).test(obj[f].toString()) &&
!(/^\s*function\s*__/).test(obj[f].toString())
){
document.write(f + "<br/>" + obj[f].toString() + "<hr/>");
}
__findFunctions(obj[f]);
}
}
__findFunctions(window);
The problem with this code is that it gets stuck in loops.
source
share