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:
- g
Graph Graph to be used. For undirected graphs, every edge can be traversed in both directions.
- weight
EdgePropertyMap Edge weights.
- source
Vertexorint(optional, default:None) If given, only the cycles reachable from this vertex are considered.
- g
- Returns:
- cycle
numpy.ndarrayorNone The vertices of a negative cycle, in order, or
Noneif there is none.
- cycle
See also
find_cycleA single cycle of the graph
cycle_ratioMaximum or minimum cycle ratio
graph_tool.search.bellman_ford_searchShortest 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]