
    h\7                         S r SSKrSSKJr  S/rS rS r " S S5      r\" S	5      \R                  " SS
S9SS j5       5       r
g)zBGenerates graphs resembling the Internet Autonomous System network    N)py_random_staterandom_internet_as_graphc                     SSK Jn  X:  d   eSU-  U -
  nXC" U5      -
  S-  n[        UR                  5       U" U5      U -
  -  U -   5      nUR                  5       U:  a  SnXg-   $ SnXg-   $ )aT  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           X/var/www/html/env/lib/python3.13/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                 V   [        U 5      S:X  a  g[        U R                  5       5      nUS:X  a(  UR                  [	        U R                  5       5      5      $ UR                  5       U-  n[	        U R                  5       5      nSnXU      nX6:  a  US-  nX`XE      -  nX6:  a  M  XE   $ )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
Qx.C
'	QEH~ ' 8Or   c                   `    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrg)AS_graph_generatorG   z$Generates random internet AS graphs.c                 $   X l         [        U[        U R                   R                  5       S-  S-   5      5      U l        [        SU-  5      U l        [        SU-  5      U l        [        SXR                  -
  U R
                  -
  U R                  -
  5      U l        SSU-  S-  -   U l	        SSU-  S-  -   U l
        S	S
U-  S-  -   U l        S	SU-  S-  -   U l        SSU-  S-  -   U l        SSU-  S-  -   U l        SU l        SU l        SU l        g)a  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__AS_graph_generator.__init__J   s   , 	q%		 0 0 2Q 6 :;<?$(O	q!hh,1DII=>a5((qE))	A''!a%5(
QUeO+q1u..	r   c                    [         R                  " 5       U l        [        U R                  5       H  nU R                  R                  USS9  U R                   H!  nU R                  U   R                  U5        M#     U R                  R                  5        H  nX:w  d  M
  U R                  XS5        M     [        5       U R                  U'   [        5       U R                  U'   M     U R                  $ )zuGenerates 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_graphAS_graph_generator.t_graphr   s     txxAFFOOACO(\\Q##A& "VV\\^6MM!/ $ !$DNN1 #DNN1 ! vvr   c                 `    US:X  a  [        U5      nOSnU R                  R                  XX4S9  g )Ntransitnone)rA   customer)strrE   rJ   )r;   r!   rO   kindrU   s        r   rJ   AS_graph_generator.add_edge   s,    91vHH4;r   c                 ~    0 nU H!  nU R                   R                  U   S   X#'   M#     [        X R                  5      $ )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)rE   r    r#   r   )r;   	node_listdr<   s       r   choose_peer_pref_attach*AS_graph_generator.choose_peer_pref_attach   s:     A66<<?7+AD !!YY//r   c                 t    [        U R                  R                  U5      5      n[        X R                  5      $ )zPick a node with a probability weighted by its degree.

Pick a node from node_list with preferential attachment
computed on their degree
)dictrE   degreer#   r   )r;   r[   r   s      r   choose_node_pref_attach*AS_graph_generator.choose_node_pref_attach   s*     DFFMM),-!$		22r   c                     U R                   U   R                  U5        U R                  U   R                  U5        U R                  U    H?  nU R                   U   R                  U5        U R                  U   R                  U5        MA     g)z=Keep the dictionaries 'customers' and 'providers' consistent.N)rL   rI   rM   )r;   r!   rO   zs       r   add_customerAS_graph_generator.add_customer   ss     	qa qa "ANN1!!!$NN1!!!$ #r   c                    SnU R                   R                  5       U:  a  Sn[        5       nU R                  R	                  XSS9  [        5       U R
                  U'   [        5       U R                  U'   U R                  U   R                  U5        U R                   R                  [        U R                  5      U5       H?  nUR                  U R                  U   5      nU R                  U   R                  U5        MA     [        SX@R                   5      n	UR                  U R                  S   5      n
UR                  U R                  S   5      nX;   a  UR                  U5        SnX:  a  [!        U
5      S:  d  [!        U5      S:  a  [!        U5      S:X  d-  [!        U
5      S:  aA  U R                   R                  5       U:  a#  U R#                  U
5      nU
R                  U5        O"U R#                  U5      nUR                  U5        U R%                  XS5        U R'                  X5        US-  nX:  a"  [!        U
5      S:  a  M  [!        U5      S:  a  M  U$ )a6  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   )rA   rZ   r@   MrS   )r   r   rK   rE   rG   rL   rM   r    rI   sampler   rH   unionr   intersectionremover   rb   rJ   rf   )r;   r!   rW   reg2probavg_degt_edge_probregsnode_optionsrN   edge_num	t_options	m_optionsr\   rO   s                 r   rG   AS_graph_generator.add_node   s   0 99(DuA.EqEq

4Q!!$t||"4d;A'--dll1o>LLLO" < (7II> --djjo>	 --djjo>	>QlI 2c)nq6H9~"I"tyy'7'7'9K'G00;  #00;  #MM!	*a#FA lI 2c)nq6H r   c                 ,   U R                   S   R                  U R                  U   5      nUR                  U R                  U   5      nX;   a  UR	                  U5        U R
                  R                  U5       H  nXC;   d  M
  UR	                  U5        M     [        U5      S:  al  U R                  U5      nU R                  XS5        U R
                  R                   U   S==   S-  ss'   U R
                  R                   U   S==   S-  ss'   gg)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
ri   r   rB   rZ   r   TF)
r    
differencerL   rM   rm   rE   	neighborsr   r]   rJ   )r;   r   to_kindrr   rO   s        r   add_m_peering_link%AS_graph_generator.add_m_peering_link   s    & zz#11$..2CD#..t~~a/@A" !!!$A ##A& % |q ,,\:AMM!'FFLLOG$)$FFLLOG$)$r   c                    [        5       nU R                   H5  nXR                  U   ;   d  M  UR                  U R                  U   5      nM7     U R                  U   R	                  U5      nX;   a  UR                  U5        UR                  U R                  U   5      nU R                  R                  U5       H  nXS;   d  M
  UR                  U5        M     [        U5      S:  a  U R                  R                  [        U5      S5      S   nU R                  XS5        U R                  R                  U   S==   S-  ss'   U R                  R                  U   S==   S-  ss'   gg)a5  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   rB   rZ   TF)rK   rH   rk   r    rl   rm   rx   rM   rE   ry   r   r   rj   r   rJ   )r;   cprz   rr   rN   rO   s         r   add_cp_peering_link&AS_graph_generator.add_cp_peering_link  s6   $ uA\\!_$+11$,,q/B 
 zz'*77E # $..t~~b/AB !!"%A ##A& & |q 		  l!3Q7:AMM"(FFLLW%*%FFLLOG$)$r   c                 |    0 U l         [        U5       H&  n[        5       U R                   S[        U5      -   '   M(     g)zYInitializes AS network regions.

Parameters
----------
rn: integer
    Number of regions
REGN)rH   rF   rK   rV   )r;   rnr!   s      r   graph_regions AS_graph_generator.graph_regions?  s1     rA+.5DLLQ( r   c                 4   SnUS:X  a  U R                   nU R                  nUS:X  a+  U R                  nUS:X  a  U R                  nOU R                  nU R
                  U    H4  n[        SWU R                  5      n[        U5       H  nU" XR5        M     M6     g)z8Utility function to add peering links among node groups.Nri   CPr   )	r{   r5   r   r6   r7   r    r   r   rF   )r;   	from_kindrz   peer_link_methodr   r!   num_s           r   add_peering_links$AS_graph_generator.add_peering_linksL  s    #66

A#77#~KKLLI&A&q!TYY7C3Z ,   'r   c           
      0   U R                  S5        0 U l        0 U l        [        5       [        5       [        5       [        5       S.U l        U R                  5         [        U R                  R	                  5       5      U R                  S'   [        U R                  S   5      n[        U R                  5       HM  nU R                  S   R                  U R                  USSU R                  U R                  5      5        US-  nMO     [        U R                  5       HM  nU R                  S   R                  U R                  USSU R                  U R                   5      5        US-  nMO     [        U R"                  5       HM  nU R                  S	   R                  U R                  US	S
U R$                  U R&                  5      5        US-  nMO     U R)                  SS5        U R)                  SS5        U R)                  SS5        U R                  $ )a  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@   ri   r   Cr@   ri   r+   r   r   r)   r   r   )r   rL   rM   rK   r    rP   rE   r   rF   r.   rI   rG   r2   r8   r/   r3   r9   r1   r4   r:   r   )r;   r!   r   s      r   generate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   )rE   rL   r4   r3   r2   r1   r/   r.   r-   r    r7   r6   r5   rM   rH   r   r:   r9   r8   N)__name__
__module____qualname____firstlineno____doc__r=   rP   rJ   r]   rb   rf   rG   r{   r   r   r   r   __static_attributes__ r   r   r%   r%   G   sE    .&P*<
03%9v&P-^1-$.r   r%   r   T)graphsreturns_graphc                 <    [        X5      nUR                  5       nU$ )a  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   GGrE   s       r   r   r     s    P 
A	$B
AHr   )N)r   networkxrC   networkx.utilsr   __all__r   r#   r%   _dispatchabler   r   r   r   <module>r      s\    H  *%
&6 FE EP
 T2( 3 (r   