graph_tool.search.bfs_iterator#
- graph_tool.search.bfs_iterator(g, source=None, array=False)[source]#
Return an iterator of the edges corresponding to a breath-first traversal of the graph.
- Parameters:
- g
Graph Graph to be used.
- source
Vertex(optional, default:None) Source vertex. If unspecified, all vertices will be traversed, by iterating over starting vertices according to their index in increasing order.
- array
bool(optional, default:False) If
True, anumpy.ndarraywith the edge endpoints be returned instead.
- g
- Returns:
- bfs_iteratorIterator or
numpy.ndarray An iterator over the edges in breath-first order. If
array == True, this will be anumpy.ndarrayinstead, of shape(E,2), containing the edge endpoints.
- bfs_iteratorIterator or
See also
dfs_iteratorDepth-first search
dijkstra_iteratorDijkstra’s search algorithm
astar_iterator\(A^*\) heuristic search algorithm
Notes
See
bfs_search()for an explanation of the algorithm.The time complexity is \(O(1)\) to create the generator and \(O(V + E)\) to traverse it completely.
References
[bfs]Edward Moore, “The shortest path through a maze”, International Symposium on the Theory of Switching, 1959
[bfs-wikipedia]Examples
>>> g = gt.load_graph("search_example.xml") >>> name = g.vp["name"] >>> for e in gt.bfs_iterator(g, g.vertex(0)): ... print(name[e.source()], "->", name[e.target()]) Bob -> Eve Bob -> Chuck Bob -> Carlos Bob -> Isaac Eve -> Imothep Eve -> Carol Carlos -> Alice Alice -> Oscar Alice -> Dave