
    wgd                     P    d Z ddlmZ ddlZdZdZdgZej                  dd       Z	y)	z
=============================
Breadth First Search on Edges
=============================

Algorithms for a breadth-first traversal of edges in a graph.

    )dequeNforwardreverseedge_bfsc           	   #      K   t         j                  |            }|sy j                         }ddi j                         du rdd<    fd}n;|rdk(  r fd}n-d	k(  r fd
}n!dk(  r fd}nt	        j
                  d      |r j                  }fd}n j                  }d }|xr dv }t        |      }	t               }
t        |D cg c]  }| ||      f c}      }|r|j                         \  }}|D ]k  }|r|d   t        k(  r|d   }n|d   }||	vr*|	j                  |       |j                  | ||      f        ||      }||
vsW|
j                  |       | m |ryyc c}w w)a;  A directed, breadth-first-search of edges in `G`, beginning at `source`.

    Yield the edges of G in a breadth-first-search order continuing until
    all edges are generated.

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

    source : node, list of nodes
        The node from which the traversal begins. If None, then a source
        is chosen arbitrarily and repeatedly until all edges from each node in
        the graph are searched.

    orientation : None | 'original' | 'reverse' | 'ignore' (default: None)
        For directed graphs and directed multigraphs, edge traversals need not
        respect the original orientation of the edges.
        When set to 'reverse' every edge is traversed in the reverse direction.
        When set to 'ignore', every edge is treated as undirected.
        When set to 'original', every edge is treated as directed.
        In all three cases, the yielded edge tuples add a last entry to
        indicate the direction in which that edge was traversed.
        If orientation is None, the yielded edge has no direction indicated.
        The direction is respected, but not reported.

    Yields
    ------
    edge : directed edge
        A directed edge indicating the path taken by the breadth-first-search.
        For graphs, `edge` is of the form `(u, v)` where `u` and `v`
        are the tail and head of the edge as determined by the traversal.
        For multigraphs, `edge` is of the form `(u, v, key)`, where `key` is
        the key of the edge. When the graph is directed, then `u` and `v`
        are always in the order of the actual directed edge.
        If orientation is not None then the edge tuple is extended to include
        the direction of traversal ('forward' or 'reverse') on that edge.

    Examples
    --------
    >>> nodes = [0, 1, 2, 3]
    >>> edges = [(0, 1), (1, 0), (1, 0), (2, 0), (2, 1), (3, 1)]

    >>> list(nx.edge_bfs(nx.Graph(edges), nodes))
    [(0, 1), (0, 2), (1, 2), (1, 3)]

    >>> list(nx.edge_bfs(nx.DiGraph(edges), nodes))
    [(0, 1), (1, 0), (2, 0), (2, 1), (3, 1)]

    >>> list(nx.edge_bfs(nx.MultiGraph(edges), nodes))
    [(0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (1, 2, 0), (1, 3, 0)]

    >>> list(nx.edge_bfs(nx.MultiDiGraph(edges), nodes))
    [(0, 1, 0), (1, 0, 0), (1, 0, 1), (2, 0, 0), (2, 1, 0), (3, 1, 0)]

    >>> list(nx.edge_bfs(nx.DiGraph(edges), nodes, orientation="ignore"))
    [(0, 1, 'forward'), (1, 0, 'reverse'), (2, 0, 'reverse'), (2, 1, 'reverse'), (3, 1, 'reverse')]

    >>> list(nx.edge_bfs(nx.MultiDiGraph(edges), nodes, orientation="ignore"))
    [(0, 1, 0, 'forward'), (1, 0, 0, 'reverse'), (1, 0, 1, 'reverse'), (2, 0, 0, 'reverse'), (2, 1, 0, 'reverse'), (3, 1, 0, 'reverse')]

    Notes
    -----
    The goal of this function is to visit edges. It differs from the more
    familiar breadth-first-search of nodes, as provided by
    :func:`networkx.algorithms.traversal.breadth_first_search.bfs_edges`, in
    that it does not stop once every node has been visited. In a directed graph
    with edges [(0, 1), (1, 2), (2, 1)], the edge (2, 1) would not be visited
    if not for the functionality provided by this function.

    The naming of this function is very similar to bfs_edges. The difference
    is that 'edge_bfs' yields edges even if they extend back to an already
    explored node while 'bfs_edges' yields the edges of the tree that results
    from a breadth-first-search (BFS) so no edges are reported if they extend
    to already explored nodes. That means 'edge_bfs' reports all edges while
    'bfs_edges' only report those traversed by a node-based BFS. Yet another
    description is that 'bfs_edges' reports the edges traversed during BFS
    while 'edge_bfs' reports all edges in the order they are explored.

    See Also
    --------
    bfs_edges
    bfs_tree
    edge_dfs

    NdataFTkeysc                 <    t         j                  | fi       S N)iteredges)nodeGkwdss    j/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/traversal/edgebfs.py
edges_fromzedge_bfs.<locals>.edges_fromx   s    --..    originalc              3   V   K    j                   | fi D ]  }|t        fz     y wr   )r   FORWARDr   er   r   s     r   r   zedge_bfs.<locals>.edges_from}   s2     QWWT*T* %7*n$%   &)r   c              3   V   K    j                   | fi D ]  }|t        fz     y wr   )in_edgesREVERSEr   s     r   r   zedge_bfs.<locals>.edges_from   s2     QZZ-- %7*n$%r   ignorec              3      K    j                   | fi D ]  }|t        fz      j                  | fi D ]  }|t        fz     y wr   )r   r   r   r   r   s     r   r   zedge_bfs.<locals>.edges_from   s]     QWWT*T* %7*n$%QZZ-- %7*n$%s   A
Azinvalid orientation argument.c                     | d d S | S )N )edgeorientations    r   edge_idzedge_bfs.<locals>.edge_id   s     + 749ATAr   c                 ,    t        | d d       f| dd  z   S )N   )	frozenset)r"   s    r   r$   zedge_bfs.<locals>.edge_id   s!    d2Ah')DH44r   )r   r   r    r      )listnbunch_iteris_directedis_multigraphnxNetworkXError
successors	neighborssetr   popleftr   addappend)r   sourcer#   nodesdirectedr   r0   r$   check_reversevisited_nodesvisited_edgesnqueueparentchildren_edgesr"   childedgeidr   s   ` `               @r   r   r      s    p v&'E}}HE?DD V 	/ 
2	% 
		!	% 
	 	% >??LL		B
 KK		5 E0E!EM JMEMu5!Az!}%56E
!&" 	DbW!4QQM)!!%(eZ%678T]F]*!!&)
	  6s   CFE=,A4F!F;F)NN)
__doc__collectionsr   networkxr-   r   r   __all___dispatchabler   r!   r   r   <module>rF      s?     

, ] ]r   