
    h                         S r SSKrSSKJr  SS/r\R                  " SSS9S
S j5       r\R                  " SSS9S
S	 j5       rg)aE  Generators for Harary graphs

This module gives two generators for the Harary graph, which was
introduced by the famous mathematician Frank Harary in his 1962 work [H]_.
The first generator gives the Harary graph that maximizes the node
connectivity with given number of nodes and given number of edges.
The second generator gives the Harary graph that minimizes
the number of edges in the graph with given node connectivity and
number of nodes.

References
----------
.. [H] Harary, F. "The Maximum Connectivity of a Graph."
       Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.

    N)NetworkXErrorhnm_harary_graphhkn_harary_graphT)graphsreturns_graphc                    U S:  a  [        S5      eXS-
  :  a  [        S5      eXU S-
  -  S-  :  a  [        S5      e[        R                  " X5      nSU-  U -  nU S-  S:X  d	  US-  S:X  a  US-  n[        U 5       HG  n[        SUS-   5       H1  nUR	                  XfU-
  U -  5        UR	                  XfU-   U -  5        M3     MI     US-  (       a+  U S-  n[        U5       H  nUR	                  XfU-   5        M     SU-  U -  n	U	S:  a,  [        U	S-  5       H  nUR	                  XfU-   S-   5        M     U$ US-
  S-  n[        U 5       HG  n[        SUS-   5       H1  nUR	                  XfU-
  U -  5        UR	                  XfU-   U -  5        M3     MI     U S-  n[        XU-  -
  5       H  nUR	                  XfU-   U -  5        M     U$ )a
  Returns the Harary graph with given numbers of nodes and edges.

The Harary graph $H_{n,m}$ is the graph that maximizes node connectivity
with $n$ nodes and $m$ edges.

This maximum node connectivity is known to be floor($2m/n$). [1]_

Parameters
----------
n: integer
   The number of nodes the generated graph is to contain

m: integer
   The number of edges the generated graph is to contain

create_using : NetworkX graph constructor, optional Graph type
 to create (default=nx.Graph). If graph instance, then cleared
 before populated.

Returns
-------
NetworkX graph
    The Harary graph $H_{n,m}$.

See Also
--------
hkn_harary_graph

Notes
-----
This algorithm runs in $O(m)$ time.
It is implemented by following the Reference [2]_.

References
----------
.. [1] F. T. Boesch, A. Satyanarayana, and C. L. Suffel,
   "A Survey of Some Network Reliability Analysis and Synthesis Results,"
   Networks, pp. 99-107, 2009.

.. [2] Harary, F. "The Maximum Connectivity of a Graph."
   Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.
   z!The number of nodes must be >= 1!z&The number of edges must be >= n - 1 !   z'The number of edges must be <= n(n-1)/2r   )r   nxempty_graphrangeadd_edge)
nmcreate_usingHdoffsetijhalfrs
             R/var/www/html/env/lib/python3.13/site-packages/networkx/generators/harary_graph.pyr   r      s   Z 	1u?@@q5yDEEA;!EFF 	q'A	A
A 	
A
A
aqA1fqj)

11uk*

11uk* *  q56D4[

1$h' ! EAIq516]

1&j1n- # H a%AqA1fqj)

11uk*

11uk* *  Avqv:~&AJJqt8q.) ' H    c                    U S:  a  [        S5      eXS-   :  a  [        S5      eU S:X  a  [        R                  " X5      nU$ [        R                  " X5      nU S-  S:X  d	  US-  S:X  a  U S-  n[	        U5       HG  n[	        SUS-   5       H1  nUR                  XUU-
  U-  5        UR                  XUU-   U-  5        M3     MI     U S-  (       a+  US-  n[	        U5       H  nUR                  XUU-   5        M     U$ U S-
  S-  n[	        U5       HG  n[	        SUS-   5       H1  nUR                  XUU-
  U-  5        UR                  XUU-   U-  5        M3     MI     US-  n[	        US-   5       H  nUR                  XUU-   U-  5        M     U$ )a  Returns the Harary graph with given node connectivity and node number.

The Harary graph $H_{k,n}$ is the graph that minimizes the number of
edges needed with given node connectivity $k$ and node number $n$.

This smallest number of edges is known to be ceil($kn/2$) [1]_.

Parameters
----------
k: integer
   The node connectivity of the generated graph

n: integer
   The number of nodes the generated graph is to contain

create_using : NetworkX graph constructor, optional Graph type
 to create (default=nx.Graph). If graph instance, then cleared
 before populated.

Returns
-------
NetworkX graph
    The Harary graph $H_{k,n}$.

See Also
--------
hnm_harary_graph

Notes
-----
This algorithm runs in $O(kn)$ time.
It is implemented by following the Reference [2]_.

References
----------
.. [1] Weisstein, Eric W. "Harary Graph." From MathWorld--A Wolfram Web
 Resource. http://mathworld.wolfram.com/HararyGraph.html.

.. [2] Harary, F. "The Maximum Connectivity of a Graph."
  Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.
r	   z#The node connectivity must be >= 1!z$The number of nodes must be >= k+1 !r
   r   )r   r   
path_graphr   r   r   )kr   r   r   r   r   r   r   s           r   r   r   t   s   X 	1uABBq5yBCC 	AvMM!* 	q'A 	
A
A
aqA1fqj)

11uk*

11uk* *  q56D4[

1$h' ! H a%AqA1fqj)

11uk*

11uk* *  AvtaxAJJqt8q.) ! Hr   )N)	__doc__networkxr   networkx.exceptionr   __all___dispatchabler   r    r   r   <module>r$      sd   "  ,1
2 T2X 3Xv T2R 3Rr   