Skip to content

Commit

Permalink
Add the ability to check bipartiteness of directed and undirected
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Dec 12, 2024
1 parent fa3157c commit eea372a
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions xgi/convert/bipartite_graph.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Methods for converting to and from bipartite graphs."""

import networkx as nx

import xgi
from networkx import bipartite

from ..exception import XGIError
from ..generators import empty_hypergraph, empty_dihypergraph
from ..generators import empty_dihypergraph, empty_hypergraph

__all__ = ["from_bipartite_graph", "to_bipartite_graph"]

Expand Down Expand Up @@ -74,12 +74,12 @@ def from_bipartite_graph(G, create_using=None, dual=False):
else:
raise XGIError("Invalid type specifier")

if not _is_bipartite(G, nodes, edges):
raise XGIError("The network is not bipartite")

if G.is_directed():
H = empty_dihypergraph(create_using)
else:
if not bipartite.is_bipartite_node_set(G, nodes):
raise XGIError("The network is not bipartite")

H = empty_hypergraph(create_using)

H.add_nodes_from(nodes)
Expand All @@ -95,6 +95,16 @@ def from_bipartite_graph(G, create_using=None, dual=False):
return H.dual() if dual else H


def _is_bipartite(G, nodes1, nodes2):
"""Assumption is that nodes1.union(nodes2) == G.nodes"""
for i, j in G.edges:
cond1 = i in nodes1
cond2 = j in nodes2
if not cond1 == cond2: # if not both true or both false
return False
return True


def to_bipartite_graph(H, index=False):
"""Create a NetworkX bipartite network from a hypergraph.
Expand Down

0 comments on commit eea372a

Please sign in to comment.