
    wgH!                         d Z ddlZddlmZmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ g dZddZe
e
d	d
Z ed      d        Zy)a  View of Graphs as SubGraph, Reverse, Directed, Undirected.

In some algorithms it is convenient to temporarily morph
a graph to exclude some nodes or edges. It should be better
to do that via a view than to remove and then re-add.
In other algorithms it is convenient to temporarily morph
a graph to reverse directed edges, or treat a directed graph
as undirected, etc. This module provides those graph views.

The resulting views are essentially read-only graphs that
report data from the original graph object. We provide an
attribute G._graph which points to the underlying graph object.

Note: Since graphviews look like graphs, one can end up with
view-of-view-of-view chains. Be careful with chains because
they become very slow with about 15 nested views.
For the common simple case of node induced subgraphs created
from the graph class, we short-cut the chain by returning a
subgraph of the original graph directly rather than a subgraph
of a subgraph. We are careful not to disrupt any edge filter in
the middle subgraph. In general, determining how to short-cut
the chain is tricky and much harder with restricted_views than
with induced subgraphs.
Often it is easiest to use .copy() to avoid chains.
    N)FilterAdjacencyFilterAtlasFilterMultiAdjacencyUnionAdjacencyUnionMultiAdjacency)	no_filter)NetworkXError)not_implemented_for)generic_graph_viewsubgraph_viewreverse_viewc                    || j                         }nt        j                  d|      }| j                         |j                         k7  rt	        d      t        j
                  |      }| |_        | j                  |_        | j                  |_        |j                         rX| j                         r$| j                  |_
        | j                  |_        |S | j                  |_
        | j                  |_        |S | j                         r^| j                         r't        | j                  | j                        |_        |S t        | j                  | j                        |_        |S | j                  |_        |S )as  Returns a read-only view of `G`.

    The graph `G` and its attributes are not copied but viewed through the new graph object
    of the same class as `G` (or of the class specified in `create_using`).

    Parameters
    ----------
    G : graph
        A directed/undirected graph/multigraph.

    create_using : NetworkX graph constructor, optional (default=None)
       Graph type to create. If graph instance, then cleared before populated.
       If `None`, then the appropriate Graph type is inferred from `G`.

    Returns
    -------
    newG : graph
        A view of the input graph `G` and its attributes as viewed through
        the `create_using` class.

    Raises
    ------
    NetworkXError
        If `G` is a multigraph (or multidigraph) but `create_using` is not, or vice versa.

    Notes
    -----
    The returned graph view is read-only (cannot modify the graph).
    Yet the view reflects any changes in `G`. The intent is to mimic dict views.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_edge(1, 2, weight=0.3)
    >>> G.add_edge(2, 3, weight=0.5)
    >>> G.edges(data=True)
    EdgeDataView([(1, 2, {'weight': 0.3}), (2, 3, {'weight': 0.5})])

    The view exposes the attributes from the original graph.

    >>> viewG = nx.graphviews.generic_graph_view(G)
    >>> viewG.edges(data=True)
    EdgeDataView([(1, 2, {'weight': 0.3}), (2, 3, {'weight': 0.5})])

    Changes to `G` are reflected in `viewG`.

    >>> G.remove_edge(2, 3)
    >>> G.edges(data=True)
    EdgeDataView([(1, 2, {'weight': 0.3})])

    >>> viewG.edges(data=True)
    EdgeDataView([(1, 2, {'weight': 0.3})])

    We can change the graph type with the `create_using` parameter.

    >>> type(G)
    <class 'networkx.classes.graph.Graph'>
    >>> viewDG = nx.graphviews.generic_graph_view(G, create_using=nx.DiGraph)
    >>> type(viewDG)
    <class 'networkx.classes.digraph.DiGraph'>
    r   z-Multigraph for G must agree with create_using)	__class__nxempty_graphis_multigraphr	   freeze_graphgraph_nodeis_directed_succ_pred_adjr   r   )Gcreate_usingnewGs      `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/classes/graphviews.pyr   r   *   s*   | {{}~~a.D..00KLL99T?D DKDJDJ==?DJDJ K DJDJ K 
??+AGGQWW=DI
 K 'qww8DI K FF	K    )filter_nodefilter_edgec                   t        j                  | j                               }||_        |_        | |_        | j                  |_        t        | j                  |      |_        | j                         rt        }dfd	}nt        }dfd	}| j                         r4 || j                  |      |_         || j                  ||      |_        |S  || j                  |      |_        |S )as	  View of `G` applying a filter on nodes and edges.

    `subgraph_view` provides a read-only view of the input graph that excludes
    nodes and edges based on the outcome of two filter functions `filter_node`
    and `filter_edge`.

    The `filter_node` function takes one argument --- the node --- and returns
    `True` if the node should be included in the subgraph, and `False` if it
    should not be included.

    The `filter_edge` function takes two (or three arguments if `G` is a
    multi-graph) --- the nodes describing an edge, plus the edge-key if
    parallel edges are possible --- and returns `True` if the edge should be
    included in the subgraph, and `False` if it should not be included.

    Both node and edge filter functions are called on graph elements as they
    are queried, meaning there is no up-front cost to creating the view.

    Parameters
    ----------
    G : networkx.Graph
        A directed/undirected graph/multigraph

    filter_node : callable, optional
        A function taking a node as input, which returns `True` if the node
        should appear in the view.

    filter_edge : callable, optional
        A function taking as input the two nodes describing an edge (plus the
        edge-key if `G` is a multi-graph), which returns `True` if the edge
        should appear in the view.

    Returns
    -------
    graph : networkx.Graph
        A read-only graph view of the input graph.

    Examples
    --------
    >>> G = nx.path_graph(6)

    Filter functions operate on the node, and return `True` if the node should
    appear in the view:

    >>> def filter_node(n1):
    ...     return n1 != 5
    >>> view = nx.subgraph_view(G, filter_node=filter_node)
    >>> view.nodes()
    NodeView((0, 1, 2, 3, 4))

    We can use a closure pattern to filter graph elements based on additional
    data --- for example, filtering on edge data attached to the graph:

    >>> G[3][4]["cross_me"] = False
    >>> def filter_edge(n1, n2):
    ...     return G[n1][n2].get("cross_me", True)
    >>> view = nx.subgraph_view(G, filter_edge=filter_edge)
    >>> view.edges()
    EdgeView([(0, 1), (1, 2), (2, 3), (4, 5)])

    >>> view = nx.subgraph_view(
    ...     G,
    ...     filter_node=filter_node,
    ...     filter_edge=filter_edge,
    ... )
    >>> view.nodes()
    NodeView((0, 1, 2, 3, 4))
    >>> view.edges()
    EdgeView([(0, 1), (1, 2), (2, 3)])
    c                      || |      S N uvkr!   s      r   reverse_edgez#subgraph_view.<locals>.reverse_edge   s    q!Q''r   c                      ||       S r$   r%   r&   s      r   r*   z#subgraph_view.<locals>.reverse_edge   s    q!$$r   r$   )r   r   r   _NODE_OK_EDGE_OKr   r   r   r   r   r   r   r   r   r   r   )r   r    r!   r   Adjr*   s     `   r   r   r      s    N 99Q[[]#DDMDM DKDJQWWk2DJ"	( 	% 	}}+{;
+|<
 K [9	Kr   
undirectedc                 b    t        |       }| j                  | j                  c|_        |_        |S )a&  View of `G` with edge directions reversed

    `reverse_view` returns a read-only view of the input graph where
    edge directions are reversed.

    Identical to digraph.reverse(copy=False)

    Parameters
    ----------
    G : networkx.DiGraph

    Returns
    -------
    graph : networkx.DiGraph

    Examples
    --------
    >>> G = nx.DiGraph()
    >>> G.add_edge(1, 2)
    >>> G.add_edge(2, 3)
    >>> G.edges()
    OutEdgeView([(1, 2), (2, 3)])

    >>> view = nx.reverse_view(G)
    >>> view.edges()
    OutEdgeView([(2, 1), (3, 2)])
    )r   r   r   )r   r   s     r   r   r      s+    : a DWWaggDJ
Kr   r$   )__doc__networkxr   networkx.classes.coreviewsr   r   r   r   r   networkx.classes.filtersr   networkx.exceptionr	   networkx.utilsr
   __all__r   r   r   r%   r   r   <module>r8      sW   4   / , .
A[| %.9 bJ \" #r   