
    wg                         d dl Z d dlZddgZ ej                  d      ddd       Z ej                  d      ddd       Zy)	    Nedge_betweenness_partition'edge_current_flow_betweenness_partitionweight)
edge_attrsr   c                6   |dk  rt        j                  d      |dk(  rt        |       gS |t        |       k(  r| D cg c]  }|h c}S |t        |       kD  rt        j                  d      | j	                         }t        t        j                  |            }t        |      |k  rjt        j                  ||      }t        ||j                        } |j                  |  t        t        j                  |            }t        |      |k  rj|S c c}w )an  Partition created by iteratively removing the highest edge betweenness edge.

    This algorithm works by calculating the edge betweenness for all
    edges and removing the edge with the highest value. It is then
    determined whether the graph has been broken into at least
    `number_of_sets` connected components.
    If not the process is repeated.

    Parameters
    ----------
    G : NetworkX Graph, DiGraph or MultiGraph
      Graph to be partitioned

    number_of_sets : int
      Number of sets in the desired partition of the graph

    weight : key, optional, default=None
      The key to use if using weights for edge betweenness calculation

    Returns
    -------
    C : list of sets
      Partition of the nodes of G

    Raises
    ------
    NetworkXError
      If number_of_sets is <= 0 or if number_of_sets > len(G)

    Examples
    --------
    >>> G = nx.karate_club_graph()
    >>> part = nx.community.edge_betweenness_partition(G, 2)
    >>> {0, 1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21} in part
    True
    >>> {
    ...     2,
    ...     8,
    ...     9,
    ...     14,
    ...     15,
    ...     18,
    ...     20,
    ...     22,
    ...     23,
    ...     24,
    ...     25,
    ...     26,
    ...     27,
    ...     28,
    ...     29,
    ...     30,
    ...     31,
    ...     32,
    ...     33,
    ... } in part
    True

    See Also
    --------
    edge_current_flow_betweenness_partition

    Notes
    -----
    This algorithm is fairly slow, as both the calculation of connected
    components and edge betweenness relies on all pairs shortest
    path algorithms. They could potentially be combined to cut down
    on overall computation time.

    References
    ----------
    .. [1] Santo Fortunato 'Community Detection in Graphs' Physical Reports
       Volume 486, Issue 3-5 p. 75-174
       http://arxiv.org/abs/0906.0612
    r   number_of_sets must be >0    number_of_sets must be <= len(G)r   key)nxNetworkXErrorsetlencopylistconnected_componentsedge_betweenness_centralitymaxgetremove_edge)Gnumber_of_setsr   nH	partitionrankingedges           k/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/community/divisive.pyr   r      s    Z :;;AxQAABB	AR,,Q/0I
i.>
)006B7,t0034		 i.>
)
   s   
Dc                (   |dk  rt        j                  d      |dk(  rt        |       gS |t        |       k(  r| D cg c]  }|h c}S |t        |       kD  rt        j                  d      t	        j
                  t         j                  d|      }| j                         }t        t        j                  |            }t        |      dkD  r-|D cg c]!  }|j                  |      j                         # }}n|g}i }	|D ]  }
|	j                   ||
              t        |      |k  rYt        |	|	j                        }t        ||      D ]"  \  }}
|d   |v s |
j                  |  |	|= t        t        j                  |
            }t        |      dkD  rt!        |t              }|
j                  |      j                         } ||      }|j#                         D ]  \  }}||	||	v r|n|ddd	   <    |j%                  |       |j%                  |       |
j'                  |       |j)                  |        ||
      }|j#                         D ]  \  }}||	||	v r|n|ddd	   <     n t        |      |k  rY|S c c}w c c}w )
a%  Partition created by removing the highest edge current flow betweenness edge.

    This algorithm works by calculating the edge current flow
    betweenness for all edges and removing the edge with the
    highest value. It is then determined whether the graph has
    been broken into at least `number_of_sets` connected
    components. If not the process is repeated.

    Parameters
    ----------
    G : NetworkX Graph, DiGraph or MultiGraph
      Graph to be partitioned

    number_of_sets : int
      Number of sets in the desired partition of the graph

    weight : key, optional (default=None)
      The edge attribute key to use as weights for
      edge current flow betweenness calculations

    Returns
    -------
    C : list of sets
      Partition of G

    Raises
    ------
    NetworkXError
      If number_of_sets is <= 0 or number_of_sets > len(G)

    Examples
    --------
    >>> G = nx.karate_club_graph()
    >>> part = nx.community.edge_current_flow_betweenness_partition(G, 2)
    >>> {0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 19, 21} in part
    True
    >>> {8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33} in part
    True


    See Also
    --------
    edge_betweenness_partition

    Notes
    -----
    This algorithm is extremely slow, as the recalculation of the edge
    current flow betweenness is extremely slow.

    References
    ----------
    .. [1] Santo Fortunato 'Community Detection in Graphs' Physical Reports
       Volume 486, Issue 3-5 p. 75-174
       http://arxiv.org/abs/0906.0612
    r   r	   r
   r   F)
normalizedr   r   N)r   r   r   r   	functoolspartial(edge_current_flow_betweenness_centralityr   r   r   subgraphupdater   r   zipr   minitemsappendremove_nodes_fromdifference_update)r   r   r   r   rankr   r   ccHcc_subgraphsr   Hccr   splitcc_listcc_newHcc_newnewranksers                     r    r   r   k   s   r :;;	1	Ax	3q6	!	#a&	 ABB
33fD
 	
AR,,Q/0I
9~9BC2B,,.CCG "tCy!" i.>
)7,9m4 	GBAw"}&DM#B$;$;C$@A|$q( 37F!ll62779G#G}H ( 0 D1BCQ'\q2w?D $$V,!((1 ))&1((09$NN, @DAq>?GgA1TrT7;@1	 i.>
)6 _   Ds   
J
&J)r$   networkxr   __all___dispatchabler   r        r    <module>r>      sg      !- X&<@ \ '\~ X&IM l 'lr=   