Ë
    D^(hÕA  ã                   óZ  — d Z ddlmZ ddlmZmZ ddlZddlm	Z	m
Z
 g d¢ZdZdZ ee«      D � �ci c]  \  } }| e|d	z
     “Œ c}} Zd
„ Z e	d«      ej"                  dd„«       «       Zej"                  d„ «       Z e	d«      ej"                  d„ «       «       Z e	d«       ej"                  d¬«      d„ «       «       Z e	d«      ej"                  d„ «       «       Z e	d«      ej"                  d„ «       «       Z e	d«       e
d	«       ej"                  dd¬«      dd„«       «       «       Zyc c}} w )z*Functions for analyzing triads of a graph.é    )Údefaultdict)ÚcombinationsÚpermutationsN)Únot_implemented_forÚpy_random_state)Útriadic_censusÚis_triadÚall_tripletsÚ
all_triadsÚtriads_by_typeÚ
triad_typeÚrandom_triad)@é   é   r   é   r   é   é   é   r   r   é   é   r   r   r   é   r   r   r   r   r   é	   r   é   r   é
   r   é   r   r   é   é   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   é   )Ú003Ú012Ú102Ú021DÚ021UÚ021CÚ111DÚ111UÚ030TÚ030CÚ201Ú120DÚ120UÚ120CÚ210Ú300r   c                 ó`   ‡ — ||df||df||df||df||df||dff}t        ˆ fd„|D «       «      S )zñReturns the integer code of the given triad.

    This is some fancy magic that comes from Batagelj and Mrvar's paper. It
    treats each edge joining a pair of `v`, `u`, and `w` as a bit in
    the binary representation of an integer.

    r   r   r   r   r   é    c              3   ó:   •K  — | ]  \  }}}|‰|   v sŒ|–— Œ y ­w©N© )Ú.0ÚuÚvÚxÚGs       €úX/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/triads.pyú	<genexpr>z_tricode.<locals>.<genexpr>€   s!   øè ø€ Ò4‘W�Q˜˜1¨!¨q°©tª)ŒqÑ4ùs   ƒ”)Úsum)r8   r6   r5   ÚwÚcomboss   `    r9   Ú_tricoder>   w   sL   ø€ ð �!�Qˆi˜!˜Q ˜ Q¨¨1 I°°1°a¨y¸1¸aÀ¸*ÀqÈ!ÈRÀjÐQ€FÜÓ4 Ô4Ó4Ð4ó    Ú
undirectedc           	      ó6  ‡‡ ‡!‡"— t        | j                  |«      «      Š!|�"t        |«      t        ‰!«      k7  rt        d«      ‚t        | «      Š‰t        ‰!«      z
  }t	        ‰!«      D ��ci c]  \  }}||“Œ
 }}}|r2| j
                  ‰!z
  }|j                  ˆfd„t	        |«      D «       «       | D �ci c]>  }|| j                  |   j                  «       | j                  |   j                  «       z  “Œ@ }}| D �ci c]>  }|| j                  |   j                  «       | j                  |   j                  «       z  “Œ@ c}Š |r~D �ci c]>  }|| j                  |   j                  «       | j                  |   j                  «       z  “Œ@ c}Š"t        ˆ!ˆ"fd„|D «       «      }|dz  }	t        ˆ ˆ!fd„|D «       «      }
|
dz  }t        D �ci c]  }|d“Œ }}‰!D �]^  }||   }‰ |   }|rdx}x}x}}|D �]  }||   ||   k  rŒ||   }||z  ||hz
  }|D ]O  }||   ||   k  s ||   ||   cxk  r	||   k  sŒ#n Œ&|||   vsŒ.t        | |||«      }|t        |   xx   dz  cc<   ŒQ ||v r|dxx   ‰t        |«      z
  dz
  z  cc<   n|d	xx   ‰t        |«      z
  dz
  z  cc<   |sŒ³|‰!vsŒ¸‰"|   }t        ||‰!z
  z  «      z  }t        ||z
  ‰!z
  «      z  }‰ |   }t        ||‰!z
  z  «      z  }t        ||z
  ‰!z
  «      z  }�Œ |s�Œ3|d	xx   	dz  z   z
  z  cc<   |dxx   dz  z   z
  z  cc<   �Œa ‰‰dz
  z  ‰dz
  z  d
z  }||dz
  z  |dz
  z  d
z  }||z
  }|t        |j                  «       «      z
  |d<   |S c c}}w c c}w c c}w c c}w c c}w )am  Determines the triadic census of a directed graph.

    The triadic census is a count of how many of the 16 possible types of
    triads are present in a directed graph. If a list of nodes is passed, then
    only those triads are taken into account which have elements of nodelist in them.

    Parameters
    ----------
    G : digraph
       A NetworkX DiGraph
    nodelist : list
        List of nodes for which you want to calculate triadic census

    Returns
    -------
    census : dict
       Dictionary with triad type as keys and number of occurrences as values.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 1), (3, 4), (4, 1), (4, 2)])
    >>> triadic_census = nx.triadic_census(G)
    >>> for key, value in triadic_census.items():
    ...     print(f"{key}: {value}")
    003: 0
    012: 0
    102: 0
    021D: 0
    021U: 0
    021C: 0
    111D: 0
    111U: 0
    030T: 2
    030C: 2
    201: 0
    120D: 0
    120U: 0
    120C: 0
    210: 0
    300: 0

    Notes
    -----
    This algorithm has complexity $O(m)$ where $m$ is the number of edges in
    the graph.

    For undirected graphs, the triadic census can be computed by first converting
    the graph into a directed graph using the ``G.to_directed()`` method.
    After this conversion, only the triad types 003, 102, 201 and 300 will be
    present in the undirected scenario.

    Raises
    ------
    ValueError
        If `nodelist` contains duplicate nodes or nodes not in `G`.
        If you want to ignore this you can preprocess with `set(nodelist) & G.nodes`

    See also
    --------
    triad_graph

    References
    ----------
    .. [1] Vladimir Batagelj and Andrej Mrvar, A subquadratic triad census
        algorithm for large sparse networks with small maximum degree,
        University of Ljubljana,
        http://vlado.fmf.uni-lj.si/pub/networks/doc/triads/triads.pdf

    z3nodelist includes duplicate nodes or nodes not in Gc              3   ó2   •K  — | ]  \  }}||‰z   f–— Œ y ­wr2   r3   )r4   ÚiÚnÚNs      €r9   r:   z!triadic_census.<locals>.<genexpr>×   s   øè ø€ Ò?¡  1�!�Q˜‘U”Ñ?ùs   ƒc              3   ó@   •K  — | ]  }‰|   D ]  }|‰vsŒd –— Œ Œ y­w©r   Nr3   )r4   rD   ÚnbrÚnodesetÚsgl_nbrss      €€r9   r:   z!triadic_census.<locals>.<genexpr>â   ó(   øè ø€ ÒV˜°H¸Q±KÒV¨SÀ3ÈgÒCU”!ÐV�!ÑVùó   ƒ•	r   c              3   ó@   •K  — | ]  }‰|   D ]  }|‰vsŒd –— Œ Œ y­wrG   r3   )r4   rD   rH   Údbl_nbrsrI   s      €€r9   r:   z!triadic_census.<locals>.<genexpr>ä   rK   rL   r   r   r!   r    r   r   )ÚsetÚnbunch_iterÚlenÚ
ValueErrorÚ	enumerateÚnodesÚupdateÚpredÚkeysÚsuccr;   ÚTRIAD_NAMESr>   ÚTRICODE_TO_NAMEÚvalues)#r8   ÚnodelistÚNnotrC   rD   ÚmÚnot_nodesetÚnbrsÚsglÚsgl_edges_outsideÚdblÚdbl_edges_outsideÚnameÚcensusr6   ÚvnbrsÚ	dbl_vnbrsÚsgl_unbrs_bdyÚsgl_unbrs_outÚdbl_unbrs_bdyÚdbl_unbrs_outr5   ÚunbrsÚ	neighborsr<   ÚcodeÚ	sgl_unbrsÚ	dbl_unbrsÚtotal_trianglesÚtriangles_without_nodesetÚtotal_censusrE   rN   rI   rJ   s#                                  @@@@r9   r   r   ƒ   sø  û€ ôP �!—-‘- Ó)Ó*€GØÐ¤ H£´°W³Ò =ÜÐNÓOÐOäˆA‹€AØŒs�7‹|Ñ€Dô $ GÓ,×-‘$�!�QˆˆA‰Ð-€AÑ-Ùà—g‘g Ñ'ˆØ	�‰Ó?¬	°+Ó(>Ô?Ô?ð
 =>Ö>°qˆAˆq�v‰v�a‰y�~‰~Ó !§&¡&¨¡)§.¡.Ó"2Ñ2Ñ2Ð>€DÐ>Ø@AÖB¸1��1—6‘6˜!‘9—>‘>Ó# a§f¡f¨Q¡i§n¡nÓ&6Ñ6Ñ6ÒB€HáØDOÖP¸q�A�q—v‘v˜a‘y—~‘~Ó'¨!¯&©&°©)¯.©.Ó*:Ñ:Ñ:ÒPˆäÔV˜[ÔVÓVˆØ 1™HÐÜÔV˜[ÔVÓVˆØ 1™HÐô #.Ö.˜$ˆd�A‰gÐ.€FÐ.àó %VˆØ�Q‘ˆØ˜Q‘Kˆ	ÙàLMÐMˆMÐM˜MÐM¨M¸MØó 	BˆAØ�‰t�q˜‘tŠ|ØØ˜‘GˆEØ ™¨1¨a¨&Ñ0ˆIàò 7�Ø�Q‘4˜!˜A™$’; 1 Q¡4¨!¨A©$Ô#5°°1±Ö#5¸!À4ÈÁ7Ò:JÜ# A q¨!¨QÓ/�DØœ?¨4Ñ0Ó1°QÑ6Ô1ð7ð �I‰~Ø�u“ ¤S¨£^Ñ!3°aÑ!7Ñ7”à�u“ ¤S¨£^Ñ!3°aÑ!7Ñ7“ò
 ˜ Ò(Ø$ Q™K�	Ø¤ Y°¸±Ñ%@Ó!AÑA�Ø¤ Y°Ñ%6¸Ñ%@Ó!AÑA�Ø$ Q™K�	Ø¤ Y°¸±Ñ%@Ó!AÑA�Ø¤ Y°Ñ%6¸Ñ%@Ó!AÑA’ð5	Bó8 à�5‹MÐ.°-À-ÐSTÑBTÑ2TÑUÑU‹MØ�5‹MÐ.°-À-ÐSTÑBTÑ2TÑUÑU�MðK%VðR ˜A ™E‘{ a¨!¡eÑ,°Ñ2€OØ!%¨°©Ñ!2°d¸Q±hÑ!?ÀAÑ EÐØ"Ð%>Ñ>€LØ ¤3 v§}¡}£Ó#7Ñ7€Fˆ5�Mà€MùóK 	.ùò ?ùÚBùò Qùò /s!   Á*NÂ2ANÃ;ANÅANÇ	
Nc                 óÐ   ‡ — t        ‰ t        j                  «      rK‰ j                  «       dk(  r8t        j                  ‰ «      r#t        ˆ fd„‰ j                  «       D «       «      syy)at  Returns True if the graph G is a triad, else False.

    Parameters
    ----------
    G : graph
       A NetworkX Graph

    Returns
    -------
    istriad : boolean
       Whether G is a valid triad

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
    >>> nx.is_triad(G)
    True
    >>> G.add_edge(0, 1)
    >>> nx.is_triad(G)
    False
    r   c              3   óF   •K  — | ]  }||f‰j                  «       v –— Œ y ­wr2   )Úedges)r4   rD   r8   s     €r9   r:   zis_triad.<locals>.<genexpr>4  s    øè ø€ Ò>¨q˜˜1�v §¡£Ô*Ñ>ùs   ƒ!TF)Ú
isinstanceÚnxÚGraphÚorderÚis_directedÚanyrT   )r8   s   `r9   r	   r	     sE   ø€ ô. �!”R—X‘XÔØ�7‰7‹9˜Š>œbŸn™n¨QÔ/ÜÓ>°A·G±G³IÔ>Ô>ØØr?   c                 ór   — ddl }|j                  dt        d¬«       t        | j	                  «       d«      }|S )a`  Returns a generator of all possible sets of 3 nodes in a DiGraph.

    .. deprecated:: 3.3

       all_triplets is deprecated and will be removed in NetworkX version 3.5.
       Use `itertools.combinations` instead::

          all_triplets = itertools.combinations(G, 3)

    Parameters
    ----------
    G : digraph
       A NetworkX DiGraph

    Returns
    -------
    triplets : generator of 3-tuples
       Generator of tuples of 3 nodes

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
    >>> list(nx.all_triplets(G))
    [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]

    r   Nze

all_triplets is deprecated and will be removed in v3.5.
Use `itertools.combinations(G, 3)` instead.r   ©ÚcategoryÚ
stacklevelr   )ÚwarningsÚwarnÚDeprecationWarningr   rT   )r8   r‚   Útripletss      r9   r
   r
   9  s>   € ó: à‡M�Mð:ô $Øð ô ô ˜AŸG™G›I qÓ)€HØ€Or?   T)Úreturns_graphc              #   ó�   K  — t        | j                  «       d«      }|D ]#  }| j                  |«      j                  «       –— Œ% y­w)a  A generator of all possible triads in G.

    Parameters
    ----------
    G : digraph
       A NetworkX DiGraph

    Returns
    -------
    all_triads : generator of DiGraphs
       Generator of triads (order-3 DiGraphs)

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 1), (3, 4), (4, 1), (4, 2)])
    >>> for triad in nx.all_triads(G):
    ...     print(triad.edges)
    [(1, 2), (2, 3), (3, 1)]
    [(1, 2), (4, 1), (4, 2)]
    [(3, 1), (3, 4), (4, 1)]
    [(2, 3), (3, 4), (4, 2)]

    r   N)r   rT   ÚsubgraphÚcopy)r8   r…   Útriplets      r9   r   r   d  sA   è ø€ ô4 ˜AŸG™G›I qÓ)€HØò )ˆØ�j‰j˜Ó!×&Ñ&Ó(Ó(ñ)ùs   ‚AAc                 ó†   — t        | «      }t        t        «      }|D ]!  }t        |«      }||   j	                  |«       Œ# |S )aþ  Returns a list of all triads for each triad type in a directed graph.
    There are exactly 16 different types of triads possible. Suppose 1, 2, 3 are three
    nodes, they will be classified as a particular triad type if their connections
    are as follows:

    - 003: 1, 2, 3
    - 012: 1 -> 2, 3
    - 102: 1 <-> 2, 3
    - 021D: 1 <- 2 -> 3
    - 021U: 1 -> 2 <- 3
    - 021C: 1 -> 2 -> 3
    - 111D: 1 <-> 2 <- 3
    - 111U: 1 <-> 2 -> 3
    - 030T: 1 -> 2 -> 3, 1 -> 3
    - 030C: 1 <- 2 <- 3, 1 -> 3
    - 201: 1 <-> 2 <-> 3
    - 120D: 1 <- 2 -> 3, 1 <-> 3
    - 120U: 1 -> 2 <- 3, 1 <-> 3
    - 120C: 1 -> 2 -> 3, 1 <-> 3
    - 210: 1 -> 2 <-> 3, 1 <-> 3
    - 300: 1 <-> 2 <-> 3, 1 <-> 3

    Refer to the :doc:`example gallery </auto_examples/graph/plot_triad_types>`
    for visual examples of the triad types.

    Parameters
    ----------
    G : digraph
       A NetworkX DiGraph

    Returns
    -------
    tri_by_type : dict
       Dictionary with triad types as keys and lists of triads as values.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 3), (3, 1), (5, 6), (5, 4), (6, 7)])
    >>> dict = nx.triads_by_type(G)
    >>> dict["120C"][0].edges()
    OutEdgeView([(1, 2), (1, 3), (2, 3), (3, 1)])
    >>> dict["012"][0].edges()
    OutEdgeView([(1, 2)])

    References
    ----------
    .. [1] Snijders, T. (2012). "Transitivity and triads." University of
        Oxford.
        https://web.archive.org/web/20170830032057/http://www.stats.ox.ac.uk/~snijders/Trans_Triads_ha.pdf
    )r   r   Úlistr   Úappend)r8   Úall_triÚtri_by_typeÚtriadre   s        r9   r   r   ƒ  sL   € ôn ˜‹m€GÜœdÓ#€KØò (ˆÜ˜%Ó ˆØ�DÑ× Ñ  Õ'ð(ð Ðr?   c                 óˆ  — t        | «      st        j                  d«      ‚t        | j	                  «       «      }|dk(  ry|dk(  ry|dk(  r[| j	                  «       \  }}t        |«      t        |«      k(  ry|d   |d   k(  ry|d   |d   k(  ry	|d   |d   k(  s|d   |d   k(  ry
y|dk(  r¶t        | j	                  «       d«      D ]˜  \  }}}t        |«      t        |«      k(  r|d   |v r y yt        |«      j                  t        |«      «      t        |«      k(  sŒY|d   |d   |d   h|d   |d   |d   hcxk(  rt        | j                  «       «      k(  r y  y y y|dk(  rÑt        | j	                  «       d«      D ]³  \  }}}}t        |«      t        |«      k(  sŒ t        |«      t        |«      k(  r y|d   h|d   hcxk(  r't        |«      j                  t        |«      «      k(  r y |d   h|d   hcxk(  r't        |«      j                  t        |«      «      k(  r y |d   |d   k(  sŒ³ y y|dk(  ry|dk(  ryy)aø  Returns the sociological triad type for a triad.

    Parameters
    ----------
    G : digraph
       A NetworkX DiGraph with 3 nodes

    Returns
    -------
    triad_type : str
       A string identifying the triad type

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
    >>> nx.triad_type(G)
    '030C'
    >>> G.add_edge(1, 3)
    >>> nx.triad_type(G)
    '120C'

    Notes
    -----
    There can be 6 unique edges in a triad (order-3 DiGraph) (so 2^^6=64 unique
    triads given 3 nodes). These 64 triads each display exactly 1 of 16
    topologies of triads (topologies can be permuted). These topologies are
    identified by the following notation:

    {m}{a}{n}{type} (for example: 111D, 210, 102)

    Here:

    {m}     = number of mutual ties (takes 0, 1, 2, 3); a mutual tie is (0,1)
              AND (1,0)
    {a}     = number of asymmetric ties (takes 0, 1, 2, 3); an asymmetric tie
              is (0,1) BUT NOT (1,0) or vice versa
    {n}     = number of null ties (takes 0, 1, 2, 3); a null tie is NEITHER
              (0,1) NOR (1,0)
    {type}  = a letter (takes U, D, C, T) corresponding to up, down, cyclical
              and transitive. This is only used for topologies that can have
              more than one form (eg: 021D and 021U).

    References
    ----------
    .. [1] Snijders, T. (2012). "Transitivity and triads." University of
        Oxford.
        https://web.archive.org/web/20170830032057/http://www.stats.ox.ac.uk/~snijders/Trans_Triads_ha.pdf
    z"G is not a triad (order-3 DiGraph)r   r   r   r    r   r!   r"   r#   r$   r   r&   r%   r(   r'   r   r)   r*   r+   r,   r   r-   r   r.   N)
r	   ry   ÚNetworkXAlgorithmErrorrQ   rw   rO   r   Úsymmetric_differencerT   Úintersection)r8   Ú	num_edgesÚe1Úe2Úe3Úe4s         r9   r   r   Â  sN  € ôf �AŒ;Ü×'Ñ'Ð(LÓMÐMÜ�A—G‘G“I“€IØ�A‚~ØØ	�aŠØØ	�aŠØ—‘“‰ˆˆBÜˆr‹7”c˜"“gÒØØ�‰U�b˜‘eŠ^ØØ�‰U�b˜‘eŠ^ØØ�‰U�b˜‘eŠ^˜r !™u¨¨1©š~Øð  .à	�aŠÜ& q§w¡w£y°!Ó4ò 
	‰JˆB��BÜ�2‹wœ#˜b›'Ò!Ø�a‘5˜B‘;Ù!áÜ�R“×-Ñ-¬c°"«gÓ6¼#¸b»'ÓAØ�q‘E˜2˜a™5 " Q¡%Ð(¨R°©U°B°q±E¸2¸a¹5Ð,AÔSÄSÈÏÉËÃ^ÒSÙ!ð Tó ñ
	ð 
�aŠÜ*¨1¯7©7«9°aÓ8ò 
	"‰NˆB��B˜Ü�2‹wœ#˜b›'Ó!ä�r“7œc "›gÒ%Ù Ø�q‘E�7˜r !™u˜gÔF¬¨R«×)=Ñ)=¼cÀ"»gÓ)FÒFÙ!ð Gà�q‘E�7˜r !™u˜gÔF¬¨R«×)=Ñ)=¼cÀ"»gÓ)FÒFÙ!ð Gà�a‘5˜B˜q™E“>Ù!ñ
	"ð 
�aŠØØ	�aŠØð 
r?   )Úpreserve_all_attrsr†   c                 ó  — ddl }|j                  dt        d¬«       t        | «      dk  r"t	        j
                  dt        | «      › d�«      ‚|j                  t        | j                  «       «      d«      }| j                  |«      }|S )	aV  Returns a random triad from a directed graph.

    .. deprecated:: 3.3

       random_triad is deprecated and will be removed in version 3.5.
       Use random sampling directly instead::

          G.subgraph(random.sample(list(G), 3))

    Parameters
    ----------
    G : digraph
       A NetworkX DiGraph
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G2 : subgraph
       A randomly selected triad (order-3 NetworkX DiGraph)

    Raises
    ------
    NetworkXError
        If the input Graph has less than 3 nodes.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 3), (3, 1), (5, 6), (5, 4), (6, 7)])
    >>> triad = nx.random_triad(G, seed=1)
    >>> triad.edges
    OutEdgeView([(1, 2)])

    r   Nz�

random_triad is deprecated and will be removed in NetworkX v3.5.
Use random.sample instead, e.g.::

	G.subgraph(random.sample(list(G), 3))
r   r   r   z2G needs at least 3 nodes to form a triad; (it has z nodes))
r‚   rƒ   r„   rQ   ry   ÚNetworkXErrorÚsamplerŒ   rT   rˆ   )r8   Úseedr‚   rT   ÚG2s        r9   r   r   $  s…   € óN à‡M�Mð8ô $Øð ô ô ˆ1ƒv�‚zÜ×ÑØ@ÄÀQÃÀÈÐPó
ð 	
ð �K‰Kœ˜QŸW™W›Y›¨Ó+€EØ	
�‰�EÓ	€BØ€Ir?   r2   )Ú__doc__Úcollectionsr   Ú	itertoolsr   r   Únetworkxry   Únetworkx.utilsr   r   Ú__all__ÚTRICODESrY   rS   rZ   r>   Ú_dispatchabler   r	   r
   r   r   r   r   )rC   ro   s   00r9   ú<module>r¨      sŒ  ðñ
 1å #ß 0ã ß ?ò€ðA€ðJ€ñ* <EÀXÓ;N×O±°°4�1�k $¨¡(Ñ+Ñ+ÓO€ò	5ñ �\Ó"Ø×ÑòSó ó #ðSðl ×Ññó ðñ: �\Ó"Ø×Ññ&ó ó #ð&ñR �\Ó"Ø€×Ñ Ô%ñ)ó &ó #ð)ñ: �\Ó"Ø×Ññ:ó ó #ð:ñz �\Ó"Ø×Ññ]ó ó #ð]ñ@ �\Ó"Ù�ÓØ€×Ñ T¸Ô>ò5ó ?ó ó #ñ5ùóg Ps   °D'