The first thing the tool does is gather a list of all nodes in the current graph view. This will be stored in a list, ordered top to bottom in terms of hierarchy. (This can prevent cases where Houdini wouldn't know what to reconnect nodes to after a child gets deleted) Afterwards it will loop through the created list and delete any node which does not have a connection to any other node, and will also make sure that node is not required by any other node in the network. This could be seen as a pre-cleanup step, which reduces the amount of data that needs to be scanned.
After the pre-cleanup in step 1, we now need to index which nodes we want to keep. The tool will first gather all "Output" nodes, and then do a recursive scan upwards in the connected network. We then know exactly which nodes to keep. However, some of the nodes in the graph are dependent on data from nodes not directly connected to the output network. This means we will need to exclude those from being deleted aswell.
In step 1 and 2 we indexed which nodes should be not be cleaned up, but we dont know yet if any of those indexed nodes rely on information from nodes not contained in the network. In this step we will scan all nodes marked for keeping for references and dependencies. The tool will do something called iterative scanning, where it will look for nodes that have certain dependencies, and nodes which are connected above it. This process will keep going until it has found all possible dependencies, updating potential candidates while scanning. Therefore called iterative rather than recursive.
Step 4 is the last step of the tool. In this tool we delete all nodes not marked for keeping. An additional option we could offer the user is to pack all "to be deleted nodes" in a subnet rather than actually deleting them. This could be integrated into the popup window asking for a confirmation. It is important to show this popup, since it is a very deconstructive action.
The Graph Cleanup Shelf Tool is really easy to install. Simply copy the entire script below (Make sure you dont miss any parts!!), and then in Houdini rightclick your shelf tab. This opens up a popup window where you can press "New Tool". After pressing "New Tool", paste the script you copied in the "Script" tab, and press "Apply". Congratulations! You have now successfully installed Graph Cleanup. You are now able to use it.