Return True if there exists a negative edge cycle anywhere in G.
- G (NetworkX graph) –
- weight (string, optional (default='weight')) – Edge data key corresponding to the edge weight
negative_cycle – True if a negative edge cycle exists, otherwise False.
>>> import networkx as nx >>> G = nx.cycle_graph(5, create_using = nx.DiGraph()) >>> print(nx.negative_edge_cycle(G)) False >>> G['weight'] = -7 >>> print(nx.negative_edge_cycle(G)) True
Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed.
This algorithm uses bellman_ford() but finds negative cycles on any component by first adding a new node connected to every node, and starting bellman_ford on that node. It then removes that extra node.