I have a graph as shown below:
S1
/. \
ss1 ss2
/ \. /. \
ss3 ss4 ss5
I want to start from the root node and then check the count of incoming and outgoing relation, if incoming = 0 and outgoing != 0 , then delete the node and relation (in the above example delete ss1 and ss2 and relation) if incoming = 0 and outgoing = 0 (means its a leaf node) - delete only the node. Once S1 and its relations are deleted then take ss1 (in the example above) and perform the same check and keep deleting. The above graph is just an example: the graph will have different nodes each time.
Also, if there are shared nodes (example: parents are 2 (one parent from this graph and one from another graph), then the given node and its children should not be deleted. only the relation to this node from the given graph should be deleted.