dominator_tree

Contents

dominator_tree#

graph_tool.structure.dominator_tree(g, root, dom_map=None)[git][source]#

Return a vertex property map containing the dominator vertices for each vertex.

Parameters:
gGraph

Graph to be used.

rootVertex

The root vertex.

dom_mapVertexPropertyMap (optional, default: None)

If provided, the dominator map will be written in this property map.

Returns:
dom_mapVertexPropertyMap

The dominator map. It contains for each vertex, the index of its immediate dominator vertex.

See also

dominance_frontiers

Dominance frontier of every vertex

lowest_common_ancestors

Lowest common ancestors of pairs of vertices

topological_sort

Topological ordering of a directed acyclic graph

Notes

A vertex u dominates a vertex v, if every path of directed graph from the entry to v must go through u.

The values of the root and of the vertices that are not reachable from it are left unchanged. If dom_map is not provided, they are therefore zero, the same value as for the vertices whose immediate dominator is vertex 0, so the latter can only be identified by also determining which vertices are reachable from the root (e.g. with label_out_component()).

The algorithm of Lengauer and Tarjan is run on the subgraph of the vertices reachable from the root, since the predecessors that are not reachable would otherwise affect the result.

The algorithm runs with \(O((V+E)\log (V+E))\) complexity.

References

Examples

>>> g = gt.random_graph(100, lambda: (2, 2))
>>> tree = gt.minimum_spanning_tree(g)
>>> g.set_edge_filter(tree)
>>> root = [v for v in g.vertices() if v.in_degree() == 0]
>>> dom = gt.dominator_tree(g, root[0])
>>> print(dom.a)
[ 0  0  0  0  0 38  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0]

For a small control-flow graph, the dominator tree can be drawn together with the graph:

>>> g = gt.Graph([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (5, 1),
...               (4, 6)])
>>> dom = gt.dominator_tree(g, 0)
>>> print(dom.a)
[0 0 1 1 1 4 4]
>>> h = g.copy()
>>> h.add_edge_list([(dom[v], v) for v in range(1, 7)])
>>> ecolor = h.new_ep("string", vals=["#999999"] * 8 + ["#e41a1c"] * 6)
>>> pos = h.new_vp("vector<double>", vals=[(0, 0), (0, 1), (-1, 2),
...                                        (1, 2), (0, 3), (-1, 4),
...                                        (1, 4)])
>>> gt.graph_draw(h, pos, vertex_text=h.vertex_index,
...               vertex_fill_color="#cccccc", edge_color=ecolor,
...               vertex_size=25, edge_pen_width=2,
...               output="dominator_tree-cfg.pdf")
<...>
../_images/dominator_tree-cfg.png

Directed graph (gray edges) and its dominator tree from vertex 0 (red edges, from every immediate dominator to the vertices it dominates).#