
    wg\7                         d Z ddlZddlmZ dgZd Zd Z G d d      Z ed	       ej                  dd
      dd              Z
y)zBGenerates graphs resembling the Internet Autonomous System network    N)py_random_staterandom_internet_as_graphc                     ddl m} || k\  sJ d|z  | z
  }| ||      z
  dz  }t        |j                          ||      | z
  z  | z         }|j                         |k  rd}||z   S d}||z   S )ax  Pick a random integer with uniform probability.

    Returns a random integer uniformly taken from a distribution with
    minimum value 'a' and average value 'm', X~U(a,b), E[X]=m, X in N where
    b = 2*m - a.

    Notes
    -----
    p = (b-floor(b))/2
    X = X1 + X2; X1~U(a,floor(b)), X2~B(p)
    E[X] = E[X1] + E[X2] = (floor(b)+a)/2 + (b-floor(b))/2 = (b+a)/2 = m
    r   )floor      )mathr   roundrandom)amseedr   bpX1X2s           k/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/generators/internet_as_graphs.pyuniform_int_from_avgr   	   s     6M6	A	A	
U1XA	t{{}a1-1	2B{{}q 7N 7N    c                 X   t        |       dk(  ryt        | j                               }|dk(  r(|j                  t	        | j                                     S |j                         |z  }t	        | j                               }d}| ||      }||kD  r|dz  }|| ||      z  }||kD  r||   S )a  Pick a random value, with a probability given by its weight.

    Returns a random choice among degs keys, each of which has a
    probability proportional to the corresponding dictionary value.

    Parameters
    ----------
    degs: dictionary
        It contains the possible values (keys) and the corresponding
        probabilities (values)
    seed: random state

    Returns
    -------
    v: object
        A key of degs or None if degs is empty
    r   Nr   )lensumvalueschoicelistkeysr   )degsr   svnodesiaccs          r   choose_pref_attachr#   $   s    & 4yA~DKKMAAv{{4		,--AE	A
uQx.C
c'	QtE!H~ c' 8Or   c                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)AS_graph_generatorz$Generates random internet AS graphs.c                 (   || _         t        |t        | j                   j                         dz  dz               | _        t        d|z        | _        t        d|z        | _        t        d|| j                  z
  | j
                  z
  | j                  z
        | _        dd|z  dz  z   | _	        dd|z  dz  z   | _
        d	d
|z  dz  z   | _        d	d|z  dz  z   | _        dd|z  dz  z   | _        dd|z  dz  z   | _        d| _        d| _        d| _        y)av  Initializes variables. Immediate numbers are taken from [1].

        Parameters
        ----------
        n: integer
            Number of graph nodes
        seed: random state
            Indicator of random number generation state.
            See :ref:`Randomness<randomness>`.

        Returns
        -------
        GG: AS_graph_generator object

        References
        ----------
        [1] A. Elmokashfi, A. Kvalbein and C. Dovrolis, "On the Scalability of
        BGP: The Role of Topology Growth," in IEEE Journal on Selected Areas
        in Communications, vol. 28, no. 8, pp. 1250-1261, October 2010.
        r      g333333?皙?r   g      @i'  g      ?r      i 皙?g      ?g      ?N)r   minr
   r   n_tn_mn_cpmaxn_cd_md_cpd_cp_m_mp_cp_mp_cp_cpt_mt_cpt_c)selfnr   s      r   __init__zAS_graph_generator.__init__J   s   , 	q%		 0 0 2Q 6 :;<?$(O	q!dhh,1DII=>a5((qE))	A''!a%5(
QUeO+q1u..	r   c                    t        j                         | _        t        | j                        D ]  }| j                  j                  |d       | j                  D ]   }| j                  |   j                  |       " | j                  j                         D ]  }||k7  s	| j                  ||d        t               | j                  |<   t               | j                  |<    | j                  S )zGenerates the core mesh network of tier one nodes of a AS graph.

        Returns
        -------
        G: Networkx Graph
            Core network
        T)typepeer)nxGraphGranger,   add_noderegionsaddr    add_edgeset	customers	providers)r:   r!   rjs       r   t_graphzAS_graph_generator.t_graphr   s     txx 	&AFFOOACO(\\ 'Q##A&'VV\\^ 06MM!Q/0 !$DNN1 #DNN1	& vvr   c                 h    |dk(  rt        |      }nd}| j                  j                  ||||       y )Ntransitnone)r?   customer)strrC   rH   )r:   r!   rM   kindrR   s        r   rH   zAS_graph_generator.add_edge   s0    91vHH14(;r   c                 ~    i }|D ]!  }| j                   j                  |   d   ||<   # t        || j                        S )zPick a node with a probability weighted by its peer degree.

        Pick a node from node_list with preferential attachment
        computed only on their peer degree
        peers)rC   r    r#   r   )r:   	node_listdr;   s       r   choose_peer_pref_attachz*AS_graph_generator.choose_peer_pref_attach   sC      	,A66<<?7+AaD	,!!TYY//r   c                 v    t        | j                  j                  |            }t        || j                        S )zPick a node with a probability weighted by its degree.

        Pick a node from node_list with preferential attachment
        computed on their degree
        )dictrC   degreer#   r   )r:   rW   r   s      r   choose_node_pref_attachz*AS_graph_generator.choose_node_pref_attach   s,     DFFMM),-!$		22r   c                    | j                   |   j                  |       | j                  |   j                  |       | j                  |   D ]>  }| j                   |   j                  |       | j                  |   j                  |       @ y)z=Keep the dictionaries 'customers' and 'providers' consistent.N)rJ   rG   rK   )r:   r!   rM   zs       r   add_customerzAS_graph_generator.add_customer   sx     	qa qa " 	%ANN1!!!$NN1!!!$	%r   c                    d}| j                   j                         |k  rd}t               }| j                  j	                  ||d       t               | j
                  |<   t               | j                  |<   | j                  |   j                  |       | j                   j                  t        | j                        |      D ]>  }|j                  | j                  |         }| j                  |   j                  |       @ t        d|| j                         }	|j                  | j                  d         }
|j                  | j                  d         }||v r|j                  |       d}||	k  rt!        |
      dkD  st!        |      dkD  rt!        |      dk(  s+t!        |
      dkD  r@| j                   j                         |k  r#| j#                  |
      }|
j                  |       n"| j#                  |      }|j                  |       | j%                  ||d       | j'                  ||       |dz  }||	k  rt!        |
      dkD  rt!        |      dkD  r|S )a  Add a node and its customer transit edges to the graph.

        Parameters
        ----------
        i: object
            Identifier of the new node
        kind: string
            Type of the new node. Options are: 'M' for middle node, 'CP' for
            content provider and 'C' for customer.
        reg2prob: float
            Probability the new node can be in two different regions.
        avg_deg: float
            Average number of transit nodes of which node i is customer.
        t_edge_prob: float
            Probability node i establish a customer transit edge with a tier
            one (T) node

        Returns
        -------
        i: object
            Identifier of the new node
        r   r   r   )r?   rV   r>   MrP   )r   r   rI   rC   rE   rJ   rK   r    rG   sampler   rF   unionr   intersectionremover   r]   rH   r`   )r:   r!   rT   reg2probavg_degt_edge_probregsnode_optionsrL   edge_num	t_options	m_optionsrX   rM   s                 r   rE   zAS_graph_generator.add_node   s    0 99(DuA.EqEq

4Q!!$t||"4d; 	#A'--dll1o>LLLO"	# (7DII> --djjo>	 --djjo>		>Q(lI 2c)nq6H9~"I"tyy'7'7'9K'G00;  #00;  #MM!Q	*a#FA (lI 2c)nq6H r   c                 "   | j                   d   j                  | j                  |         }|j                  | j                  |         }||v r|j	                  |       | j
                  j                  |      D ]  }||v s|j	                  |        t        |      dkD  rm| j                  |      }| j                  ||d       | j
                  j                   |   dxx   dz  cc<   | j
                  j                   |   dxx   dz  cc<   yy)a  Add a peering link between two middle tier (M) nodes.

        Target node j is drawn considering a preferential attachment based on
        other M node peering degree.

        Parameters
        ----------
        m: object
            Node identifier
        to_kind: string
            type for target node j (must be always M)

        Returns
        -------
        success: boolean
        rb   r   r@   rV   r   TF)
r    
differencerJ   rK   rf   rC   	neighborsr   rY   rH   )r:   r   to_kindrk   rM   s        r   add_m_peering_linkz%AS_graph_generator.add_m_peering_link   s    & zz#11$..2CD#..t~~a/@A" !!!$ 	'AL ##A&	' |q ,,\:AMM!Q'FFLLOG$)$FFLLOG$)$r   c                    t               }| j                  D ]2  }|| j                  |   v s|j                  | j                  |         }4 | j                  |   j	                  |      }||v r|j                  |       |j                  | j                  |         }| j                  j                  |      D ]  }||v s|j                  |        t        |      dkD  r| j                  j                  t        |      d      d   }| j                  ||d       | j                  j                  |   dxx   dz  cc<   | j                  j                  |   dxx   dz  cc<   yy)a  Add a peering link to a content provider (CP) node.

        Target node j can be CP or M and it is drawn uniformly among the nodes
        belonging to the same region as cp.

        Parameters
        ----------
        cp: object
            Node identifier
        to_kind: string
            type for target node j (must be M or CP)

        Returns
        -------
        success: boolean
        r   r   r@   rV   TF)rI   rF   rd   r    re   rf   rp   rK   rC   rq   r   r   rc   r   rH   )r:   cprr   rk   rL   rM   s         r   add_cp_peering_linkz&AS_graph_generator.add_cp_peering_link  sJ   $ u 	CAT\\!_$+11$,,q/B	C
 zz'*77E # $..t~~b/AB !!"% 	'AL ##A&	' |q 		  l!3Q7:AMM"a(FFLLW%*%FFLLOG$)$r   c                 x    i | _         t        |      D ]%  }t               | j                   dt        |      z   <   ' y)zInitializes AS network regions.

        Parameters
        ----------
        rn: integer
            Number of regions
        REGN)rF   rD   rI   rS   )r:   rnr!   s      r   graph_regionsz AS_graph_generator.graph_regions?  s6     r 	1A+.5DLLQ(	1r   c                 (   d}|dk(  r| j                   }| j                  }|dk(  r*| j                  }|dk(  r| j                  }n| j                  }| j
                  |   D ]2  }t        d| j                        }t        |      D ]  } |||        4 y)z8Utility function to add peering links among node groups.Nrb   CPr   )	rs   r4   rv   r5   r6   r    r   r   rD   )r:   	from_kindrr   peer_link_methodr   r!   num_s           r   add_peering_linksz$AS_graph_generator.add_peering_linksL  s    #66

A#77#~KKLLI& 	-A&q!TYY7C3Z - G,-	-r   c           
      $   | j                  d       i | _        i | _        t               t               t               t               d| _        | j                          t        | j                  j	                               | j                  d<   t        | j                  d         }t        | j                        D ]L  }| j                  d   j                  | j                  |dd| j                  | j                               |dz  }N t        | j                        D ]L  }| j                  d   j                  | j                  |dd| j                  | j                                |dz  }N t        | j"                        D ]L  }| j                  d	   j                  | j                  |d	d
| j$                  | j&                               |dz  }N | j)                  dd       | j)                  dd       | j)                  dd       | j                  S )ai  Generates a random AS network graph as described in [1].

        Returns
        -------
        G: Graph object

        Notes
        -----
        The process steps are the following: first we create the core network
        of tier one nodes, then we add the middle tier (M), the content
        provider (CP) and the customer (C) nodes along with their transit edges
        (link i,j means i is customer of j). Finally we add peering links
        between M nodes, between M and CP nodes and between CP node couples.
        For a detailed description of the algorithm, please refer to [1].

        References
        ----------
        [1] A. Elmokashfi, A. Kvalbein and C. Dovrolis, "On the Scalability of
        BGP: The Role of Topology Growth," in IEEE Journal on Selected Areas
        in Communications, vol. 28, no. 8, pp. 1250-1261, October 2010.
        r)   )r>   rb   r|   Cr>   rb   r*   r   r|   r(   r   r   )rz   rJ   rK   rI   r    rN   rC   r   rD   r-   rG   rE   r1   r7   r.   r2   r8   r0   r3   r9   r   )r:   r!   r   s      r   generatezAS_graph_generator.generate^  s   . 	15suCEF
dfflln-

3

3 txx 	AJJsOac488TXX NOFA	 tyy! 	AJJt  q$dii!STFA	 txx 	AJJsOaa488 LMFA	 	sC(tS)tT*vvr   N)__name__
__module____qualname____doc__r<   rN   rH   rY   r]   r`   rE   rs   rv   rz   r   r    r   r   r%   r%   G   sE    .&P*<
03%9v&P-^1-$.r   r%   r   T)graphsreturns_graphc                 >    t        | |      }|j                         }|S )aJ  Generates a random undirected graph resembling the Internet AS network

    Parameters
    ----------
    n: integer in [1000, 10000]
        Number of graph nodes
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G: Networkx Graph object
        A randomly generated undirected graph

    Notes
    -----
    This algorithm returns an undirected graph resembling the Internet
    Autonomous System (AS) network, it uses the approach by Elmokashfi et al.
    [1]_ and it grants the properties described in the related paper [1]_.

    Each node models an autonomous system, with an attribute 'type' specifying
    its kind; tier-1 (T), mid-level (M), customer (C) or content-provider (CP).
    Each edge models an ADV communication link (hence, bidirectional) with
    attributes:

      - type: transit|peer, the kind of commercial agreement between nodes;
      - customer: <node id>, the identifier of the node acting as customer
        ('none' if type is peer).

    References
    ----------
    .. [1] A. Elmokashfi, A. Kvalbein and C. Dovrolis, "On the Scalability of
       BGP: The Role of Topology Growth," in IEEE Journal on Selected Areas
       in Communications, vol. 28, no. 8, pp. 1250-1261, October 2010.
    )r%   r   )r;   r   GGrC   s       r   r   r     s!    P 
At	$B
AHr   )N)r   networkxrA   networkx.utilsr   __all__r   r#   r%   _dispatchabler   r   r   r   <module>r      s^    H  *%
&6 FE EP
 T2( 3 (r   