I have a function that determines if a node is a leaf.
I define a leaf as being a node that is not part of a cycle in the graph.
An example:
Leaf nodes are marked with red arrows, they do not belong to a cycle.
Before I find all the cycles in the graph, I first want to eliminate the need to check these leaf nodes to optimise my algorithm.
My current method does not traverse to find if a node is a leaf node as I am not sure how to go about it, I have a basic check that looks like this:
private static bool IsLeafNode(Node node)
{
int TotalLeafs(Node node)
{
int j = 0;
for (int i = 0; i < node.Nodes.Count; i++){
Node n = node.Nodes[i];
j += n.Nodes.Count == 1 ? 1 : 0; //if leaf add 1
}
return j;
}
//has 1 connection OR all its connections lead to nodes that have 1 connection
return node.ConnectionCount == 1 || TotalLeafs(node) == node.Nodes.Count;
}
The problem here is it does not consider the two leaf nodes that have 2 connections (but it is obvious still that they are leaf nodes).
How might I go about eliminating all the nodes that are leaf nodes?