transitive_reduction

transitive_reduction#

graph_tool.structure.transitive_reduction(g, tr_map=None)[git][source]#

Return the transitive reduction of a directed acyclic graph.

Parameters:
gGraph

Directed acyclic graph to be used.

tr_mapEdgePropertyMap (optional, default: None)

If provided, the edges of the reduction will be marked in this property map.

Returns:
tr_mapEdgePropertyMap

Boolean edge property map marking the edges of the transitive reduction.

See also

transitive_closure

Inverse operation

topological_sort

Topological ordering of a directed acyclic graph

graph_tool.generation.condensation_graph

Graph of the strong components

Notes

The transitive reduction [aho-transitive-1972] is the graph with the fewest edges that has the same reachability as the original one. For a directed acyclic graph it is unique, and consists of the edges \((u, v)\) such that \(v\) cannot be reached from \(u\) by a path of two or more edges. Of a set of parallel edges, only one is kept.

A search is made from every vertex, in parallel, with a total time of \(O(VE)\) in the worst case and \(O(V)\) memory per thread.

@parallel@

References

[aho-transitive-1972]

Alfred V. Aho, Michael R. Garey and Jeffrey D. Ullman, “The transitive reduction of a directed graph”, SIAM Journal on Computing 1, 131-137 (1972), DOI: 10.1137/0201008 [sci-hub, @tor]

Examples

>>> g = gt.Graph([(0, 1), (1, 2), (0, 2), (2, 3), (0, 3)])
>>> tr = gt.transitive_reduction(g)
>>> print(gt.GraphView(g, efilt=tr).get_edges())
[[0 1]
 [1 2]
 [2 3]]

The edges kept and removed can be shown in a drawing:

>>> pos = g.new_vp("vector<double>", vals=[(0, 0), (1, 1), (2, 0),
...                                        (3, 1)])
>>> ecolor = g.new_ep("string", vals=np.where(tr.a, "#e41a1c",
...                                           "#999999"))
>>> gt.graph_draw(g, pos, vertex_text=g.vertex_index,
...               vertex_fill_color="#cccccc", edge_color=ecolor,
...               vertex_size=25, edge_pen_width=2,
...               output="transitive_reduction-dag.pdf")
<...>
../_images/transitive_reduction-dag.png

Transitive reduction of a directed acyclic graph of four vertices. The edges of the reduction are shown in red, and the edges removed in gray.#