bfs_beam_edges

Contents

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:
gGraph

Graph to be used.

sourceVertex or int

Source vertex.

valueVertexPropertyMap or callable

Score of every vertex (higher is better), as a scalar property map or a function of the vertex index.

widthint (optional, default: None)

Number of best neighbors of every vertex that are explored. If not given, all neighbors are (in decreasing order of value).

Returns:
edgesiterator over pairs of int

The tree edges \((u, v)\) of the search, in the order in which the vertices \(v\) are discovered.

See also

bfs_iterator

Breadth-first search iterator

bfs_search

Breadth-first search

astar_iterator

Heuristic best-first search iterator

Notes

Beam search is a breadth-first search in which only the width out-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 from source, 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 first width of 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 the width slots, so that fewer than width new vertices may be discovered from \(u\), and only vertices reachable from source are visited. The edges are returned once the search is complete; if value is 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, see bfs_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")
<...>
../_images/bfs_beam_edges-karate.png

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.#