Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/traversal/edgebfs.py: 12%
56 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
1"""
2=============================
3Breadth First Search on Edges
4=============================
6Algorithms for a breadth-first traversal of edges in a graph.
8"""
9from collections import deque
11import networkx as nx
13FORWARD = "forward"
14REVERSE = "reverse"
16__all__ = ["edge_bfs"]
19@nx._dispatch
20def edge_bfs(G, source=None, orientation=None):
21 """A directed, breadth-first-search of edges in `G`, beginning at `source`.
23 Yield the edges of G in a breadth-first-search order continuing until
24 all edges are generated.
26 Parameters
27 ----------
28 G : graph
29 A directed/undirected graph/multigraph.
31 source : node, list of nodes
32 The node from which the traversal begins. If None, then a source
33 is chosen arbitrarily and repeatedly until all edges from each node in
34 the graph are searched.
36 orientation : None | 'original' | 'reverse' | 'ignore' (default: None)
37 For directed graphs and directed multigraphs, edge traversals need not
38 respect the original orientation of the edges.
39 When set to 'reverse' every edge is traversed in the reverse direction.
40 When set to 'ignore', every edge is treated as undirected.
41 When set to 'original', every edge is treated as directed.
42 In all three cases, the yielded edge tuples add a last entry to
43 indicate the direction in which that edge was traversed.
44 If orientation is None, the yielded edge has no direction indicated.
45 The direction is respected, but not reported.
47 Yields
48 ------
49 edge : directed edge
50 A directed edge indicating the path taken by the breadth-first-search.
51 For graphs, `edge` is of the form `(u, v)` where `u` and `v`
52 are the tail and head of the edge as determined by the traversal.
53 For multigraphs, `edge` is of the form `(u, v, key)`, where `key` is
54 the key of the edge. When the graph is directed, then `u` and `v`
55 are always in the order of the actual directed edge.
56 If orientation is not None then the edge tuple is extended to include
57 the direction of traversal ('forward' or 'reverse') on that edge.
59 Examples
60 --------
61 >>> nodes = [0, 1, 2, 3]
62 >>> edges = [(0, 1), (1, 0), (1, 0), (2, 0), (2, 1), (3, 1)]
64 >>> list(nx.edge_bfs(nx.Graph(edges), nodes))
65 [(0, 1), (0, 2), (1, 2), (1, 3)]
67 >>> list(nx.edge_bfs(nx.DiGraph(edges), nodes))
68 [(0, 1), (1, 0), (2, 0), (2, 1), (3, 1)]
70 >>> list(nx.edge_bfs(nx.MultiGraph(edges), nodes))
71 [(0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (1, 2, 0), (1, 3, 0)]
73 >>> list(nx.edge_bfs(nx.MultiDiGraph(edges), nodes))
74 [(0, 1, 0), (1, 0, 0), (1, 0, 1), (2, 0, 0), (2, 1, 0), (3, 1, 0)]
76 >>> list(nx.edge_bfs(nx.DiGraph(edges), nodes, orientation="ignore"))
77 [(0, 1, 'forward'), (1, 0, 'reverse'), (2, 0, 'reverse'), (2, 1, 'reverse'), (3, 1, 'reverse')]
79 >>> list(nx.edge_bfs(nx.MultiDiGraph(edges), nodes, orientation="ignore"))
80 [(0, 1, 0, 'forward'), (1, 0, 0, 'reverse'), (1, 0, 1, 'reverse'), (2, 0, 0, 'reverse'), (2, 1, 0, 'reverse'), (3, 1, 0, 'reverse')]
82 Notes
83 -----
84 The goal of this function is to visit edges. It differs from the more
85 familiar breadth-first-search of nodes, as provided by
86 :func:`networkx.algorithms.traversal.breadth_first_search.bfs_edges`, in
87 that it does not stop once every node has been visited. In a directed graph
88 with edges [(0, 1), (1, 2), (2, 1)], the edge (2, 1) would not be visited
89 if not for the functionality provided by this function.
91 The naming of this function is very similar to bfs_edges. The difference
92 is that 'edge_bfs' yields edges even if they extend back to an already
93 explored node while 'bfs_edges' yields the edges of the tree that results
94 from a breadth-first-search (BFS) so no edges are reported if they extend
95 to already explored nodes. That means 'edge_bfs' reports all edges while
96 'bfs_edges' only report those traversed by a node-based BFS. Yet another
97 description is that 'bfs_edges' reports the edges traversed during BFS
98 while 'edge_bfs' reports all edges in the order they are explored.
100 See Also
101 --------
102 bfs_edges
103 bfs_tree
104 edge_dfs
106 """
107 nodes = list(G.nbunch_iter(source))
108 if not nodes:
109 return
111 directed = G.is_directed()
112 kwds = {"data": False}
113 if G.is_multigraph() is True:
114 kwds["keys"] = True
116 # set up edge lookup
117 if orientation is None:
119 def edges_from(node):
120 return iter(G.edges(node, **kwds))
122 elif not directed or orientation == "original":
124 def edges_from(node):
125 for e in G.edges(node, **kwds):
126 yield e + (FORWARD,)
128 elif orientation == "reverse":
130 def edges_from(node):
131 for e in G.in_edges(node, **kwds):
132 yield e + (REVERSE,)
134 elif orientation == "ignore":
136 def edges_from(node):
137 for e in G.edges(node, **kwds):
138 yield e + (FORWARD,)
139 for e in G.in_edges(node, **kwds):
140 yield e + (REVERSE,)
142 else:
143 raise nx.NetworkXError("invalid orientation argument.")
145 if directed:
146 neighbors = G.successors
148 def edge_id(edge):
149 # remove direction indicator
150 return edge[:-1] if orientation is not None else edge
152 else:
153 neighbors = G.neighbors
155 def edge_id(edge):
156 return (frozenset(edge[:2]),) + edge[2:]
158 check_reverse = directed and orientation in ("reverse", "ignore")
160 # start BFS
161 visited_nodes = set(nodes)
162 visited_edges = set()
163 queue = deque([(n, edges_from(n)) for n in nodes])
164 while queue:
165 parent, children_edges = queue.popleft()
166 for edge in children_edges:
167 if check_reverse and edge[-1] == REVERSE:
168 child = edge[0]
169 else:
170 child = edge[1]
171 if child not in visited_nodes:
172 visited_nodes.add(child)
173 queue.append((child, edges_from(child)))
174 edgeid = edge_id(edge)
175 if edgeid not in visited_edges:
176 visited_edges.add(edgeid)
177 yield edge