§
    bŠtj‚  ã                   óª   — d Z ddlZddlmZ dgZ ed¦  «         ed¦  «         ej        dddœ¬	¦  «        d
ddœd„¦   «         ¦   «         ¦   «         ZdS )zTime dependent algorithms.é    N)Únot_implemented_forÚcd_indexÚ
undirectedÚ
multigraphé   )ÚtimeÚweight)Ú
node_attrsr   c          	      ó�  ‡ ‡‡‡‡	— t          ˆ ˆfd„‰ D ¦   «         ¦  «        st          j        d¦  «        ‚	 ‰ j        ‰         ‰         |z   Š	ˆ ˆ	ˆfd„‰ j        ‰         D ¦   «         }n#  t          j        d¦  «        ‚xY wˆ ˆfd„|D ¦   «         }t           |j        ˆ ˆfd„‰ ‰         D ¦   «         Ž ¦  «        }|dk    rt          j        d¦  «        ‚‰€*t          t          d
„ |D ¦   «         ¦  «        |z  d¦  «        S ˆ ˆfd„|D ¦   «         }t          t          d„ t          ||¦  «        D ¦   «         ¦  «        |z  d¦  «        S )a  Compute the CD index for `node` within the graph `G`.

    Calculates the CD index for the given node of the graph,
    considering only its predecessors who have the `time` attribute
    smaller than or equal to the `time` attribute of the `node`
    plus `time_delta`.

    Parameters
    ----------
    G : graph
       A directed networkx graph whose nodes have `time` attributes and optionally
       `weight` attributes (if a weight is not given, it is considered 1).
    node : node
       The node for which the CD index is calculated.
    time_delta : numeric or timedelta
       Amount of time after the `time` attribute of the `node`. The value of
       `time_delta` must support comparison with the `time` node attribute. For
       example, if the `time` attribute of the nodes are `datetime.datetime`
       objects, then `time_delta` should be a `datetime.timedelta` object.
    time : string (Optional, default is "time")
        The name of the node attribute that will be used for the calculations.
    weight : string (Optional, default is None)
        The name of the node attribute used as weight.

    Returns
    -------
    float
       The CD index calculated for the node `node` within the graph `G`.

    Raises
    ------
    NetworkXError
       If not all nodes have a `time` attribute or
       `time_delta` and `time` attribute types are not compatible or
       `n` equals 0.

    NetworkXNotImplemented
        If `G` is a non-directed graph or a multigraph.

    Examples
    --------
    >>> from datetime import datetime, timedelta
    >>> G = nx.DiGraph()
    >>> nodes = {
    ...     1: {"time": datetime(2015, 1, 1)},
    ...     2: {"time": datetime(2012, 1, 1), "weight": 4},
    ...     3: {"time": datetime(2010, 1, 1)},
    ...     4: {"time": datetime(2008, 1, 1)},
    ...     5: {"time": datetime(2014, 1, 1)},
    ... }
    >>> G.add_nodes_from([(n, nodes[n]) for n in nodes])
    >>> edges = [(1, 3), (1, 4), (2, 3), (3, 4), (3, 5)]
    >>> G.add_edges_from(edges)
    >>> delta = timedelta(days=5 * 365)
    >>> nx.cd_index(G, 3, time_delta=delta, time="time")
    0.5
    >>> nx.cd_index(G, 3, time_delta=delta, time="time", weight="weight")
    0.12

    Integers can also be used for the time values:
    >>> node_times = {1: 2015, 2: 2012, 3: 2010, 4: 2008, 5: 2014}
    >>> nx.set_node_attributes(G, node_times, "new_time")
    >>> nx.cd_index(G, 3, time_delta=4, time="new_time")
    0.5
    >>> nx.cd_index(G, 3, time_delta=4, time="new_time", weight="weight")
    0.12

    Notes
    -----
    This method implements the algorithm for calculating the CD index,
    as described in the paper by Funk and Owen-Smith [1]_. The CD index
    is used in order to check how consolidating or destabilizing a patent
    is, hence the nodes of the graph represent patents and the edges show
    the citations between these patents. The mathematical model is given
    below:

    .. math::
        CD_{t}=\frac{1}{n_{t}}\sum_{i=1}^{n}\frac{-2f_{it}b_{it}+f_{it}}{w_{it}},

    where `f_{it}` equals 1 if `i` cites the focal patent else 0, `b_{it}` equals
    1 if `i` cites any of the focal patents successors else 0, `n_{t}` is the number
    of forward citations in `i` and `w_{it}` is a matrix of weight for patent `i`
    at time `t`.

    The `datetime.timedelta` package can lead to off-by-one issues when converting
    from years to days. In the example above `timedelta(days=5 * 365)` looks like
    5 years, but it isn't because of leap year days. So it gives the same result
    as `timedelta(days=4 * 365)`. But using `timedelta(days=5 * 365 + 1)` gives
    a 5 year delta **for this choice of years** but may not if the 5 year gap has
    more than 1 leap year. To avoid these issues, use integers to represent years,
    or be very careful when you convert units of time.

    References
    ----------
    .. [1] Funk, Russell J., and Jason Owen-Smith.
           "A dynamic network measure of technological change."
           Management science 63, no. 3 (2017): 791-817.
           http://russellfunk.org/cdindex/static/papers/funk_ms_2017.pdf

    c              3   ó6   •K  — | ]}‰‰j         |         v V — Œd S ©N©Únodes)Ú.0ÚnÚGr   s     €€ú`/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/algorithms/time_dependent.pyú	<genexpr>zcd_index.<locals>.<genexpr>q   s.   øè è € Ð-Ð- aˆt�q”w˜q”zÐ!Ð-Ð-Ð-Ð-Ð-Ð-ó    z&Not all nodes have a 'time' attribute.c                 óB   •— h | ]}‰j         |         ‰         ‰k    ¯|’ŒS © r   )r   Úir   Útarget_dater   s     €€€r   ú	<setcomp>zcd_index.<locals>.<setcomp>x   s.   ø€ ÐKÐKÐK�a¨1¬7°1¬:°dÔ+;¸{Ò+JÐ+J�Ð+JÐ+JÐ+Jr   zPAddition and comparison are not supported between 'time_delta' and 'time' types.c                 ó^   •‡— g | ](Št          ˆˆfd „‰‰         D ¦   «         ¦  «        rdnd‘Œ)S )c              3   ó,   •K  — | ]}|‰‰         v V — Œd S r   r   )r   Újr   r   s     €€r   r   z&cd_index.<locals>.<listcomp>.<genexpr>€   s+   øè è € Ð.Ð. �1˜˜!œ�9Ð.Ð.Ð.Ð.Ð.Ð.r   éÿÿÿÿr   )Úany)r   r   r   Únodes    @€€r   ú
<listcomp>zcd_index.<locals>.<listcomp>€   sG   øø€ ÐDÐDÐD¸!�sÐ.Ð.Ð.Ð.Ð. a¨¤gÐ.Ñ.Ô.Ñ.Ô.Ð	5ˆˆ°AÐDÐDÐDr   c              3   ó^   •K  — | ]'}‰j         |                              ¦   «         ‰hz
  V — Œ(d S r   )ÚpredÚkeys)r   Úsr   r    s     €€r   r   zcd_index.<locals>.<genexpr>ƒ   s:   øè è € ÐDÐD°q˜œ œŸšÑ)Ô)¨T¨FÑ2ÐDÐDÐDÐDÐDÐDr   r   zThe cd index cannot be defined.Nc              3   ó   K  — | ]}|V — Œd S r   r   )r   Úbis     r   r   zcd_index.<locals>.<genexpr>‰   s"   è è € Ð(Ð( ˜Ð(Ð(Ð(Ð(Ð(Ð(r   é   c                 óR   •— g | ]#}‰j         |                              ‰d ¦  «        ‘Œ$S )r   )r   Úget)r   r   r   r	   s     €€r   r!   zcd_index.<locals>.<listcomp>�   s-   ø€ Ð;Ð;Ð;°�1”7˜1”:—>’> &¨!Ñ,Ô,Ð;Ð;Ð;r   c              3   ó&   K  — | ]\  }}||z  V — Œd S r   r   )r   r'   Úwts      r   r   zcd_index.<locals>.<genexpr>Ž   s*   è è € Ð?Ð?¡V R¨˜˜b™Ð?Ð?Ð?Ð?Ð?Ð?r   )
ÚallÚnxÚNetworkXErrorr   r#   ÚlenÚunionÚroundÚsumÚzip)
r   r    Ú
time_deltar   r	   r#   Úbr   Úweightsr   s
   `` ``    @r   r   r   	   sœ  øøøøø€ õP Ð-Ð-Ð-Ð-Ð-¨1Ð-Ñ-Ô-Ñ-Ô-ð IÝÔÐGÑHÔHÐHð	
à”g˜d”m DÔ)¨JÑ6ˆàKÐKÐKÐKÐKÐK˜1œ6 $œ<ÐKÑKÔKˆˆøð
ÝÔð ñ
ô 
ð 	
øøøð 	EÐDÐDÐDÐD¸tÐDÑDÔD€Aõ 	ˆJˆDŒJÐDÐDÐDÐDÐD¸A¸d¼GÐDÑDÔDÐEÑFÔF€AØˆA‚v€vÝÔÐ@ÑAÔAÐAð €~Ý•SÐ(Ð( aÐ(Ñ(Ô(Ñ(Ô(¨1Ñ,¨aÑ0Ô0Ð0ð <Ð;Ð;Ð;Ð;°dÐ;Ñ;Ô;ˆÝ•SÐ?Ð?­s°1°g©¬Ð?Ñ?Ô?Ñ?Ô?À!ÑCÀQÑGÔGÐGs   ·1A) Á)A?)Ú__doc__Únetworkxr.   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r   r   r   ú<module>r=      s·   ðØ  Ð  à Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .àˆ,€ð Ð�\Ñ"Ô"ØÐ�\Ñ"Ô"Ø€Ô d°aÐ8Ð8Ð9Ñ9Ô9Ø*0¸ð BHð BHð BHð BHñ :Ô9ñ #Ô"ñ #Ô"ðBHð BHð BHr   