about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-4a52a71956a8d46fcb7294ac71734504bb09bcc2.tar.gz
two version of R2R are here HEAD master
Diffstat (limited to '.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py')
-rw-r--r--.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py b/.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py
new file mode 100644
index 00000000..cb9388f6
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/spectral.py
@@ -0,0 +1,69 @@
+"""
+Spectral bipartivity measure.
+"""
+
+import networkx as nx
+
+__all__ = ["spectral_bipartivity"]
+
+
+@nx._dispatchable(edge_attrs="weight")
+def spectral_bipartivity(G, nodes=None, weight="weight"):
+    """Returns the spectral bipartivity.
+
+    Parameters
+    ----------
+    G : NetworkX graph
+
+    nodes : list or container  optional(default is all nodes)
+      Nodes to return value of spectral bipartivity contribution.
+
+    weight : string or None  optional (default = 'weight')
+      Edge data key to use for edge weights. If None, weights set to 1.
+
+    Returns
+    -------
+    sb : float or dict
+       A single number if the keyword nodes is not specified, or
+       a dictionary keyed by node with the spectral bipartivity contribution
+       of that node as the value.
+
+    Examples
+    --------
+    >>> from networkx.algorithms import bipartite
+    >>> G = nx.path_graph(4)
+    >>> bipartite.spectral_bipartivity(G)
+    1.0
+
+    Notes
+    -----
+    This implementation uses Numpy (dense) matrices which are not efficient
+    for storing large sparse graphs.
+
+    See Also
+    --------
+    color
+
+    References
+    ----------
+    .. [1] E. Estrada and J. A. Rodríguez-Velázquez, "Spectral measures of
+       bipartivity in complex networks", PhysRev E 72, 046105 (2005)
+    """
+    import scipy as sp
+
+    nodelist = list(G)  # ordering of nodes in matrix
+    A = nx.to_numpy_array(G, nodelist, weight=weight)
+    expA = sp.linalg.expm(A)
+    expmA = sp.linalg.expm(-A)
+    coshA = 0.5 * (expA + expmA)
+    if nodes is None:
+        # return single number for entire graph
+        return float(coshA.diagonal().sum() / expA.diagonal().sum())
+    else:
+        # contribution for individual nodes
+        index = dict(zip(nodelist, range(len(nodelist))))
+        sb = {}
+        for n in nodes:
+            i = index[n]
+            sb[n] = coshA.item(i, i) / expA.item(i, i)
+        return sb