bfs_beam_edges#
- graph_tool.search.bfs_beam_edges(g, source, value, width=None)[git][source]#
Iterate over the edges of a breadth-first beam search.
- Parameters:
- g
Graph Graph to be used.
- source
Vertexorint Source vertex.
- value
VertexPropertyMapor callable Score of every vertex (higher is better), as a scalar property map or a function of the vertex index.
- width
int(optional, default:None) Number of best neighbors of every vertex that are explored. If not given, all neighbors are (in decreasing order of value).
- g
- Returns:
- edgesiterator over pairs of
int The tree edges \((u, v)\) of the search, in the order in which the vertices \(v\) are discovered.
- edgesiterator over pairs of
See also
bfs_iteratorBreadth-first search iterator
bfs_searchBreadth-first search
astar_iteratorHeuristic best-first search iterator
Notes
Beam search is a breadth-first search in which only the
widthout-neighbors of every vertex with the largest values are explored, and in decreasing order of value [russell-artificial-2010]; it trades completeness for speed in heuristic search. More precisely, the vertices are processed in first-in, first-out order starting fromsource, and when a vertex \(u\) is processed, its out-neighbors are sorted in decreasing order of value (ties keep the order of the adjacency list) and truncated to the firstwidthof them; each vertex \(v\) among these that has not yet been discovered is then discovered, appended to the queue, and yields the tree edge \((u, v)\). Neighbors that were already discovered still occupy one of thewidthslots, so that fewer thanwidthnew vertices may be discovered from \(u\), and only vertices reachable fromsourceare visited. The edges are returned once the search is complete; ifvalueis a function, it is first evaluated for every vertex. The time is \(O(V + E\log \Delta)\), where \(\Delta\) is the largest degree. For the full visitor interface of breadth-first search, seebfs_search().References
[russell-artificial-2010]Stuart Russell and Peter Norvig, “Artificial Intelligence: A Modern Approach”, 3rd edition, Prentice Hall (2010).
Examples
>>> g = gt.collection.data["karate"] >>> k = g.degree_property_map("out") >>> print(list(gt.bfs_beam_edges(g, 0, k, width=2))) [(0, 2), (0, 1), (2, 32), (32, 33), (33, 31)]
The edges of the search can be highlighted in a drawing of the network:
>>> ecolor = g.new_ep("string", val="#999999") >>> ewidth = g.new_ep("double", val=1) >>> for u, v in gt.bfs_beam_edges(g, 0, k, width=2): ... ecolor[g.edge(u, v)] = "#e41a1c" ... ewidth[g.edge(u, v)] = 3 >>> vcolor = g.new_vp("string", val="#cccccc") >>> vcolor[0] = "#9bbedc" >>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor, edge_color=ecolor, ... edge_pen_width=ewidth, output="bfs_beam_edges-karate.pdf") <...>
Tree edges of the beam search with width two in Zachary’s karate club network, in red, with the degrees as values. The source vertex is shown in blue.#