feedback_arc_set

feedback_arc_set#

graph_tool.structure.feedback_arc_set(g, weight=None, fas=None, exact=False)[git][source]#

Return a set of edges whose removal leaves the graph without cycles.

Parameters:
gGraph

Graph to be used.

weightEdgePropertyMap (optional, default: None)

Edge weights. If given, a set with a small total weight is sought.

fasEdgePropertyMap (optional, default: None)

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

exactbool (optional, default: False)

If True, a set of minimum weight is returned for directed graphs, which is feasible only if the strongly connected components are small (see below).

Returns:
fasEdgePropertyMap

Boolean edge property map marking the edges of the feedback arc set.

See also

feedback_vertex_set

Vertex counterpart

is_DAG

Test for directed acyclic graphs

find_cycle

A single cycle of the graph

Notes

For undirected graphs, the edges outside a maximum-weight spanning forest are returned, which is a set of minimum weight.

For directed graphs, finding a minimum feedback arc set is NP-hard, and the heuristic of Eades, Lin, and Smyth [eades-fast-1993] is used instead: the vertices are arranged in a sequence by repeatedly moving the sinks to its end and the sources to its start, and otherwise the vertex with the largest difference between the total weight of its out- and in-edges to its start; the edges that point backwards in the sequence form the set. Self-loops are always part of it. The algorithm runs in time \(O((V + E)\log V)\).

With exact == True, a minimum set is found for directed graphs in every strongly connected component separately, as the backward edges of the ordering of its vertices that minimizes their total weight, by dynamic programming over the subsets of vertices: the smallest cost of a set of vertices placed first is the minimum, over its last vertex \(v\), of the cost of the rest plus the weight of the edges from \(v\) to it. This takes time \(O(2^n n)\) and memory \(O(2^n)\) for a component of \(n\) vertices, and a ValueError is raised if a component has more than 24 vertices.

References

[eades-fast-1993]

Peter Eades, Xuemin Lin and W. F. Smyth, “A fast and effective heuristic for the feedback arc set problem”, Information Processing Letters 47, 319-323 (1993), DOI: 10.1016/0020-0190(93)90079-O [sci-hub, @tor]

Examples

>>> g = gt.collection.data["celegansneural"]
>>> fas = gt.feedback_arc_set(g)
>>> u = gt.GraphView(g, efilt=fas.fa == 0)
>>> print(fas.fa.sum(), gt.is_DAG(u))
499 True

For a small graph, the heuristic can be compared with the minimum set:

>>> g = gt.random_graph(20, lambda: (3, 3))
>>> print(gt.feedback_arc_set(g).fa.sum(),
...       gt.feedback_arc_set(g, exact=True).fa.sum())
20 14

The minimum set can be shown in a drawing of this graph:

>>> fas = gt.feedback_arc_set(g, exact=True)
>>> pos = gt.sfdp_layout(g)
>>> ecolor = g.new_ep("string", vals=np.where(fas.a, "#e41a1c",
...                                           "#999999"))
>>> ewidth = g.new_ep("double", vals=np.where(fas.a, 2.5, 1))
>>> gt.graph_draw(g, pos, vertex_fill_color="#cccccc", edge_color=ecolor,
...               edge_pen_width=ewidth,
...               output="feedback_arc_set-random.pdf")
<...>
../_images/feedback_arc_set-random.png

Minimum feedback arc set of a random directed graph of 20 vertices with in- and out-degrees three, with its edges shown in red.#