find_negative_cycle

find_negative_cycle#

graph_tool.structure.find_negative_cycle(g, weight, source=None)[git][source]#

Return a cycle of negative total weight, if there is one.

Parameters:
gGraph

Graph to be used. For undirected graphs, every edge can be traversed in both directions.

weightEdgePropertyMap

Edge weights.

sourceVertex or int (optional, default: None)

If given, only the cycles reachable from this vertex are considered.

Returns:
cyclenumpy.ndarray or None

The vertices of a negative cycle, in order, or None if there is none.

See also

find_cycle

A single cycle of the graph

cycle_ratio

Maximum or minimum cycle ratio

graph_tool.search.bellman_ford_search

Shortest paths with negative weights

Notes

The Bellman-Ford algorithm [bellman-routing-1958] is run from a virtual source joined to every vertex (or from source); if some distance is still reduced after \(V\) rounds, the predecessors lead from it to a negative cycle. The time is \(O(VE)\). For undirected graphs, any edge of negative weight forms a negative cycle with itself.

References

[bellman-routing-1958]

Richard Bellman, “On a routing problem”, Quarterly of Applied Mathematics 16, 87-90 (1958), DOI: 10.1090/qam/102435 [sci-hub, @tor]

Examples

>>> g = gt.Graph([(0, 1), (1, 2), (2, 0), (2, 3)])
>>> w = g.new_ep("double", vals=[1, -3, 1, 1])
>>> print(gt.find_negative_cycle(g, w))
[0 1 2]