
    wg                         d Z ddlZddlmZ ddgZ ej                  dd      d
d       Z ej                  dd      d
d	       Zy)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                 |   | dk  rt        d      || dz
  k  rt        d      || | dz
  z  dz  kD  rt        d      t        j                  | |      }d|z  | z  }| dz  dk(  s|dz  dk(  r|dz  }t        |       D ]F  }t        d|dz         D ]2  }|j	                  |||z
  | z         |j	                  |||z   | z         4 H |dz  r*| dz  }t        |      D ]  }|j	                  |||z           d|z  | z  }	|	dkD  r+t        |	dz        D ]  }|j	                  |||z   dz           |S |dz
  dz  }t        |       D ]F  }t        d|dz         D ]2  }|j	                  |||z
  | z         |j	                  |||z   | z         4 H | dz  }t        || |z  z
        D ]  }|j	                  |||z   | z          |S )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
             e/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/generators/harary_graph.pyr   r      s   Z 	1u?@@1q5yDEE1A;!EFF 	q,'A	A
A 	
A
A
aq 	+A1fqj) +

1q1uk*

1q1uk*+	+ q56D4[ (

1a$h'( EAIq516] .

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

1q1uk*

1q1uk*+	+ Avq1v:~& 	*AJJq1t8q.)	* H    c                    | dk  rt        d      || dz   k  rt        d      | dk(  rt        j                  ||      }|S t        j                  ||      }| dz  dk(  s|dz  dk(  r| dz  }t	        |      D ]F  }t	        d|dz         D ]2  }|j                  |||z
  |z         |j                  |||z   |z         4 H | dz  r*|dz  }t	        |      D ]  }|j                  |||z           |S | dz
  dz  }t	        |      D ]F  }t	        d|dz         D ]2  }|j                  |||z
  |z         |j                  |||z   |z         4 H |dz  }t	        |dz         D ]  }|j                  |||z   |z          |S )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1q5yBCC 	AvMM!\* 	q,'A 	
A
A
aq 	+A1fqj) +

1q1uk*

1q1uk*+	+ q56D4[ (

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

1q1uk*

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