
    h`                     "   S r SSKrSSKJr  / SQr\R                  " SS9S 5       rSS jr\" S5      \R                  " SS	S
9SS j5       5       r	\R                  " SS9S 5       r
S rS r\" S5      \R                  " SS	S
9SS j5       5       rg)zCGenerate graphs with a given joint degree and directed joint degree    N)py_random_state)is_valid_joint_degreeis_valid_directed_joint_degreejoint_degree_graphdirected_joint_degree_graph)graphsc                    0 nU  HD  nUS:  d  M  [        X   R                  5       5      U-  nUR                  5       (       d    gX1U'   MF     U  H~  nX    Hs  n[        X   U   5      R                  5       (       d      gX$:w  a  X   U   X   X   -  :  a      gX$:X  d  MJ  X   U   X   X   S-
  -  :  a      gX   U   S-  S:w  d  Mr      g   M     g)a  Checks whether the given joint degree dictionary is realizable.

A *joint degree dictionary* is a dictionary of dictionaries, in
which entry ``joint_degrees[k][l]`` is an integer representing the
number of edges joining nodes of degree *k* with nodes of degree
*l*. Such a dictionary is realizable as a simple graph if and only
if the following conditions are satisfied.

- each entry must be an integer,
- the total number of nodes of degree *k*, computed by
  ``sum(joint_degrees[k].values()) / k``, must be an integer,
- the total number of edges joining nodes of degree *k* with
  nodes of degree *l* cannot exceed the total number of possible edges,
- each diagonal entry ``joint_degrees[k][k]`` must be even (this is
  a convention assumed by the :func:`joint_degree_graph` function).


Parameters
----------
joint_degrees :  dictionary of dictionary of integers
    A joint degree dictionary in which entry ``joint_degrees[k][l]``
    is the number of edges joining nodes of degree *k* with nodes of
    degree *l*.

Returns
-------
bool
    Whether the given joint degree dictionary is realizable as a
    simple graph.

References
----------
.. [1] M. Gjoka, M. Kurant, A. Markopoulou, "2.5K Graphs: from Sampling
   to Generation", IEEE Infocom, 2013.
.. [2] I. Stanton, A. Pinar, "Constructing and sampling graphs with a
   prescribed joint degree distribution", Journal of Experimental
   Algorithmics, 2012.
r   F      T)sumvalues
is_integerfloat)joint_degreesdegree_countkk_sizels        V/var/www/html/env/lib/python3.13/site-packages/networkx/generators/joint_degree_seq.pyr   r      s    R Lq5)0023a7F$$&&$O  !A)!,-88::]-a0<?\_3TT #A&LOa<O)PP  #A&*a/  "      c                 j   Ub  X4   S:  a  [        [        U5      5      nO[        U5      n [        U5      nXT:w  a  OM  X   nX    H  nX;  d  M
  X:w  d  M  Un	  O   U R                  UW	5        U R                  XY5        X1==   S-  ss'   X5==   S-  ss'   X5   S:X  a  UR	                  U5        gg)a  Releases one free stub for ``w``, while preserving joint degree in G.

Parameters
----------
G : NetworkX graph
    Graph in which the neighbor switch will take place.
w : integer
    Node id for which we will execute this neighbor switch.
unsat : set of integers
    Set of unsaturated node ids that have the same degree as w.
h_node_residual: dictionary of integers
    Keeps track of the remaining stubs  for a given node.
avoid_node_id: integer
    Node id to avoid when selecting w_prime.

Notes
-----
First, it selects *w_prime*, an  unsaturated node that has the same degree
as ``w``. Second, it selects *switch_node*, a neighbor node of ``w`` that
is not  connected to *w_prime*. Then it executes an edge swap i.e. removes
(``w``,*switch_node*) and adds (*w_prime*,*switch_node*). Gjoka et. al. [1]
prove that such an edge swap is always possible.

References
----------
.. [1] M. Gjoka, B. Tillman, A. Markopoulou, "Construction of Simple
   Graphs with a Target Joint Degree Matrix and Beyond", IEEE Infocom, '15
Nr
   r   )nextiterremove_edgeadd_edgeremove)
Gwunsath_node_residualavoid_node_idw_primeiter_varw_prime_neighbsvswitch_nodes
             r   _neighbor_switchr'   Q   s    < 	?#AA#EtE{# ;8nG'  jOT$1<K  MM![!JJw$!!1$W %r   r
   T)r   returns_graphc           
         [        U 5      (       d  Sn[        R                  " U5      eU R                  5        VVs0 s H*  u  p4US:  d  M  U[	        UR                  5       5      U-  _M,     nnn[	        UR                  5       5      n[        R                  " U5      n0 n0 n	Sn
UR                  5        H2  u  p[        XU-   5      X'   X    H  nXU'   M	     U
[        U5      -  n
M4     U  GHx  nX    GHk  nX   U   nUS:  d  M  X4:  d  M  XS   nXT   nX   nX   nU Vs1 s H  oU   S:  d  M  UiM     nnX4:w  a  U Vs1 s H  nU	U   S:  d  M  UiM     nnOUnX   U   S-  nUS:  d  M}  UUR                  U5         nUUR                  U5         nUR                  UU5      (       d  UU:w  a  X   S:X  a  [        X}UU	5        U	U   S:X  a   X4:w  a  [        UUUU	5        O[        UUUXS9  UR                  UU5        X==   S-  ss'   U	U==   S-  ss'   US-  nX   S:X  a  UR                  U5        U	U   S:X  a  UR                  U5        US:  a  M  GMn     GM{     U$ s  snnf s  snf s  snf )a  Generates a random simple graph with the given joint degree dictionary.

Parameters
----------
joint_degrees :  dictionary of dictionary of integers
    A joint degree dictionary in which entry ``joint_degrees[k][l]`` is the
    number of edges joining nodes of degree *k* with nodes of degree *l*.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : Graph
    A graph with the specified joint degree dictionary.

Raises
------
NetworkXError
    If *joint_degrees* dictionary is not realizable.

Notes
-----
In each iteration of the "while loop" the algorithm picks two disconnected
nodes *v* and *w*, of degree *k* and *l* correspondingly,  for which
``joint_degrees[k][l]`` has not reached its target yet. It then adds
edge (*v*, *w*) and increases the number of edges in graph G by one.

The intelligence of the algorithm lies in the fact that  it is always
possible to add an edge between such disconnected nodes *v* and *w*,
even if one or both nodes do not have free stubs. That is made possible by
executing a "neighbor switch", an edge rewiring move that releases
a free stub while keeping the joint degree of G the same.

The algorithm continues for E (number of edges) iterations of
the "while loop", at the which point all entries of the given
``joint_degrees[k][l]`` have reached their target values and the
construction is complete.

References
----------
..  [1] M. Gjoka, B. Tillman, A. Markopoulou, "Construction of Simple
    Graphs with a Target Joint Degree Matrix and Beyond", IEEE Infocom, '15

Examples
--------
>>> joint_degrees = {
...     1: {4: 1},
...     2: {2: 2, 3: 2, 4: 2},
...     3: {2: 2, 4: 1},
...     4: {1: 1, 2: 2, 3: 1},
... }
>>> G = nx.joint_degree_graph(joint_degrees)
>>>
z8Input joint degree dict not realizable as a simple graphr   r   )r!   r
   )r   nxNetworkXErroritemsr   r   empty_graphrangeint	randrangehas_edger'   r   discard)r   seedmsgr   r   r   Nr   h_degree_nodelistr    nodeiddegree	num_nodesr%   n_edges_addr   l_sizek_nodesl_nodesk_unsatr   l_unsats                         r   r   r      s   v !//Hs## 9F8K8K8MW8MQRUVQV+As188:!++8MLW 	L!"A
qA  O F)//1$)&92D$E!"*A!'A +#i. 	 2 !A (*1-Kaaf%% ,.+. '.Hg1Ca1G1gH6*1L'Q_Q5G!5Kq'GLG%G"/"21"5":K!Aov 67Av 67A ::a++a*-2,Q7OL +1-2 v 0Aw P 0$%q'?!"
 

1a('*a/*'*a/*#q(*-2#OOA.*1-2#OOA.; "Aoo1 " n H[ XN I Ms#   I,!I,#I23I2I7I7c                   ^
^ 0 m0 n[        U 5      [        U5      :w  a  g[        [        U 5      5       H]  nX   nX   nTR                  US4S5      S-   TUS4'   TR                  US4S5      S-   TUS4'   UR                  Xe4S5      S-   X6U4'   M_     0 m
U H  nX'    H  nX'   U   n	[        U	5      R	                  5       (       d      gU	S:  d  M3  T
R                  US4S5      U	-   T
US4'   T
R                  US4S5      U	-   T
US4'   XR                  Xx4S5      -   TUS4   TUS4   -  :  d  M      g   M     [        U
U4S jT
 5       5      $ )a  Checks whether the given directed joint degree input is realizable

Parameters
----------
in_degrees :  list of integers
    in degree sequence contains the in degrees of nodes.
out_degrees : list of integers
    out degree sequence contains the out degrees of nodes.
nkk  :  dictionary of dictionary of integers
    directed joint degree dictionary. for nodes of out degree k (first
    level of dict) and nodes of in degree l (second level of dict)
    describes the number of edges.

Returns
-------
boolean
    returns true if given input is realizable, else returns false.

Notes
-----
Here is the list of conditions that the inputs (in/out degree sequences,
nkk) need to satisfy for simple directed graph realizability:

- Condition 0: in_degrees and out_degrees have the same length
- Condition 1: nkk[k][l]  is integer for all k,l
- Condition 2: sum(nkk[k])/k = number of nodes with partition id k, is an
               integer and matching degree sequence
- Condition 3: number of edges and non-chords between k and l cannot exceed
               maximum possible number of edges


References
----------
[1] B. Tillman, A. Markopoulou, C. T. Butts & M. Gjoka,
    "Construction of Directed 2K Graphs". In Proc. of KDD 2017.
Fr   r
   c              3   F   >#    U  H  nTU   US    -  TU   :H  v   M     g7f)r   N ).0sSVs     r   	<genexpr>1is_valid_directed_joint_degree.<locals>.<genexpr>a  s&     .Aqqtad{ad"As   !)lenr.   getr   r   all)
in_degreesout_degreesnkk	forbiddenidxior   r   valrE   rF   s             @@r   r   r      sp   L 	AI
:#k**S_%OEE1a&!$q(1a&	EE1a&!$q(1a&	%MM1&!4q8	a& & 	AA&)C:((**QwEE1a&!,s21a&	EE1a&!,s21a&	vq11Aq!fI1a&	4II    .A...r   c                    UR                  5       nUR                  U5        [        U R                  U5      5      n[        U R                  U5      5      n	U H  n
X;  d  M
  Xz:w  d  M  U R	                  X5        U R                  Xz5        XZ   U:X  a$  UR                  X45        UR                  Xz45        X1==   S-  ss'   X7==   S-  ss'   X7   S:X  a  UR                  U5          g   U$ )a  Releases one free stub for node w, while preserving joint degree in G.

Parameters
----------
G : networkx directed graph
    graph within which the edge swap will take place.
w : integer
    node id for which we need to perform a neighbor switch.
unsat: set of integers
    set of node ids that have the same degree as w and are unsaturated.
h_node_residual_out: dict of integers
    for a given node, keeps track of the remaining stubs to be added.
chords: set of tuples
    keeps track of available positions to add edges.
h_partition_in: dict of integers
    for a given node, keeps track of its partition id (in degree).
partition: integer
    partition id to check if chords have to be updated.

Notes
-----
First, it selects node w_prime that (1) has the same degree as w and
(2) is unsaturated. Then, it selects node v, a neighbor of w, that is
not connected to w_prime and does an edge swap i.e. removes (w,v) and
adds (w_prime,v). If neighbor switch is not possible for w using
w_prime and v, then return w_prime; in [1] it's proven that
such unsaturated nodes can be used.

References
----------
[1] B. Tillman, A. Markopoulou, C. T. Butts & M. Gjoka,
    "Construction of Directed 2K Graphs". In Proc. of KDD 2017.
r
   r   N)popaddlist
successorsr   r   r2   r   )r   r   r   h_node_residual_outchordsh_partition_in	partitionr"   	w_neighbsr$   r%   s              r   _directed_neighbor_switchr^   d  s    H iikG	IIgQ\\!_%I1<<01O$',MM!JJw" I-

A6"|,"a'"(A-("+q0W% " Nr   c                    UR                  5       nUR                  U5        [        U R                  U5      5      n[        U R                  U5      5      n	U H  n
X;  d  M
  Xz:w  d  M  U R	                  X5        U R                  X5        XZ   U:X  a$  UR                  X45        UR                  X45        X1==   S-  ss'   X7==   S-  ss'   X7   S:X  a  UR                  U5          g   U$ )a!  The reverse of directed_neighbor_switch.

Parameters
----------
G : networkx directed graph
    graph within which the edge swap will take place.
w : integer
    node id for which we need to perform a neighbor switch.
unsat: set of integers
    set of node ids that have the same degree as w and are unsaturated.
h_node_residual_in: dict of integers
    for a given node, keeps track of the remaining stubs to be added.
chords: set of tuples
    keeps track of available positions to add edges.
h_partition_out: dict of integers
    for a given node, keeps track of its partition id (out degree).
partition: integer
    partition id to check if chords have to be updated.

Notes
-----
Same operation as directed_neighbor_switch except it handles this operation
for incoming edges instead of outgoing.
r
   r   N)rU   rV   rW   predecessorsr   r   r2   r   )r   r   r   h_node_residual_inrZ   h_partition_outr\   r"   r]   r$   r%   s              r   _directed_neighbor_switch_revrc     s    6 iikG	IIgQ^^A&'I1>>'23O$',MM!JJq"!Y.

A6"|,!Q&!'1,'!*a/W%   Nr      c                    [        XU5      (       d  Sn[        R                  " U5      e[        R                  " 5       n0 n0 n0 n0 n	0 n
0 n0 n0 n0 n[	        U 5       Hu  u  nn[        U5      nUS:  d  M  UR                  U/ 5        UR                  U[        5       5        UU   R                  U5        UU   R                  U5        UX'   UX'   Mw     [	        U5       H  u  nnX   nUR                  UX   4S5      S-   UUX   4'   [        U5      nUS:  a\  UR                  U/ 5        U	R                  U[        5       5        UU   R                  U5        U	U   R                  U5        UX'   UX'   UR                  U5        M     0 n0 nU H  n[        UU   5      UU'   M     U H  n[        UU   5      UU'   M     U GH  nUU    GH  nUU   U   nUS:  d  M  [        5       nUU   nUU   nUR                  [        UU-  5      UUR                  UU4S5      -   5      nSn[        U5      U:  aK  UU   UU   U-     nUU   UU   U-     nUS-  nUU:w  a  UR                  UU45        [        U5      U:  a  MK  U	U   nUU   nUS:  d  M  UR                  5       u  n n!UR                  U U!45        U
U    S:X  a  [!        UU UU
UUU5      n"U"b  U"n UU!   S:X  a  [#        UU!UUUUU5      n#U#b  U#n!UR%                  U U!5        U
U ==   S-  ss'   UU!==   S-  ss'   US-  nUR'                  U U!45        U
U    S:X  a  UR'                  U 5        UU!   S:X  a  UR'                  U!5        US:  a  M  GM     GM     U$ )a  Generates a random simple directed graph with the joint degree.

Parameters
----------
degree_seq :  list of tuples (of size 3)
    degree sequence contains tuples of nodes with node id, in degree and
    out degree.
nkk  :  dictionary of dictionary of integers
    directed joint degree dictionary, for nodes of out degree k (first
    level of dict) and nodes of in degree l (second level of dict)
    describes the number of edges.
seed : hashable object, optional
    Seed for random number generator.

Returns
-------
G : Graph
    A directed graph with the specified inputs.

Raises
------
NetworkXError
    If degree_seq and nkk are not realizable as a simple directed graph.


Notes
-----
Similarly to the undirected version:
In each iteration of the "while loop" the algorithm picks two disconnected
nodes v and w, of degree k and l correspondingly,  for which nkk[k][l] has
not reached its target yet i.e. (for given k,l): n_edges_add < nkk[k][l].
It then adds edge (v,w) and always increases the number of edges in graph G
by one.

The intelligence of the algorithm lies in the fact that  it is always
possible to add an edge between disconnected nodes v and w, for which
nkk[degree(v)][degree(w)] has not reached its target, even if one or both
nodes do not have free stubs. If either node v or w does not have a free
stub, we perform a "neighbor switch", an edge rewiring move that releases a
free stub while keeping nkk the same.

The difference for the directed version lies in the fact that neighbor
switches might not be able to rewire, but in these cases unsaturated nodes
can be reassigned to use instead, see [1] for detailed description and
proofs.

The algorithm continues for E (number of edges in the graph) iterations of
the "while loop", at which point all entries of the given nkk[k][l] have
reached their target values and the construction is complete.

References
----------
[1] B. Tillman, A. Markopoulou, C. T. Butts & M. Gjoka,
    "Construction of Directed 2K Graphs". In Proc. of KDD 2017.

Examples
--------
>>> in_degrees = [0, 1, 1, 2]
>>> out_degrees = [1, 1, 1, 1]
>>> nkk = {1: {1: 2, 2: 2}}
>>> G = nx.directed_joint_degree_graph(in_degrees, out_degrees, nkk)
>>>
z)Input is not realizable as a simple graphr   r
   )r   r*   r+   DiGraph	enumerater/   
setdefaultsetappendrV   rJ   add_noderI   sampler.   rU   r^   rc   r   r2   )$rL   rM   rN   r3   r4   r   h_degree_nodelist_inh_degree_nodelist_outh_degree_nodelist_in_unsath_degree_nodelist_out_unsatrY   ra   rb   r[   
non_chordsrP   rQ   rR   nk_innk_outpr   r   r:   rZ   k_lenl_lenchords_samplenumjr>   r?   r%   r   _v_ws$                                       r   r   r     s   D **3GG9s## 	

A !#"$ONJ J'Q#hq5 ++Ar2&11!SU; #**3/&q)--c2&'#"#N ( K(Q+5>>1jo:NPQ+RUV+V
Az'(#hq5!,,Q3'221ce<!!$++C0'*..s3'($#$O 	

3 ) EF!+A./a ""-a01q	 #
 QAa&)KQq	a $%%-(+
1vq8Q*Q! &kK/-a0s1Ce1KLA,Q/c0Be0KLA1HCAv

Aq6* &kK/ 6a84Q7!Ao!::<DAqJJ1v& +1-26#/"* > "A *!,1:#."+ > "A JJq!$'*a/*&q)Q.)1$KNNAq6**1-2*)!,1*U "Aoo7  N Hr   )N)__doc__networkxr*   networkx.utilsr   __all___dispatchabler   r'   r   r   r^   rc   r   rB   r   r   <module>r      s    I  * ? ?D<~ T2K 3 K\ @/ @/F<~1h T2 3 r   