is_forest#
- is_forest(G)[source]#
Returns True if
G
is a forest.A forest is a graph with no undirected cycles.
For directed graphs,
G
is a forest if the underlying graph is a forest. The underlying graph is obtained by treating each directed edge as a single undirected edge in a multigraph.- Parameters:
- Ggraph
The graph to test.
- Returns:
- bbool
A boolean that is True if
G
is a forest.
- Raises:
- NetworkXPointlessConcept
If
G
is empty.
See also
Notes
In another convention, a directed forest is known as a polyforest and then forest corresponds to a branching.
Examples
>>> G = nx.Graph() >>> G.add_edges_from([(1, 2), (1, 3), (2, 4), (2, 5)]) >>> nx.is_forest(G) True >>> G.add_edge(4, 1) >>> nx.is_forest(G) False