I'm not aware of any "known" efficient algorithm to compute this, but the networkx package for python does have an is_forest(G)
method that returns true
if G
is a forest.
for node in nx.connected_components(graph):
subgraph = graph.subgraph(node)
if nx.is_forest(subgraph):
forest_count += 1