is_semiconnected#
- is_semiconnected(G)[source]#
Returns True if the graph is semiconnected, False otherwise.
A graph is semiconnected if and only if for any pair of nodes, either one is reachable from the other, or they are mutually reachable.
This function uses a theorem that states that a DAG is semiconnected if for any topological sort, for node
in that sort, there is an edge . That allows us to check if a non-DAGG
is semiconnected by condensing the graph: i.e. constructing a new graphH
with nodes being the strongly connected components ofG
, and edges (scc_1, scc_2) if there is a edge inG
for some and . That results in a DAG, so we compute the topological sort ofH
and check if for every there is an edge .- Parameters:
- GNetworkX graph
A directed graph.
- Returns:
- semiconnectedbool
True if the graph is semiconnected, False otherwise.
- Raises:
- NetworkXNotImplemented
If the input graph is undirected.
- NetworkXPointlessConcept
If the graph is empty.
Examples
>>> G = nx.path_graph(4, create_using=nx.DiGraph()) >>> print(nx.is_semiconnected(G)) True >>> G = nx.DiGraph([(1, 2), (3, 2)]) >>> print(nx.is_semiconnected(G)) False