'networkx find_negative_cycle parameters

What am I supposed to pass as the source parameter to the find_negative_cycle() method of the python networkx module? In the documentation it says to pass a list, but when I try to do so, I get the error:

TypeError: unhashable type: 'list'



Solution 1:[1]

There was an error in the documentation. It should be fixed in version 2.8.1 https://github.com/networkx/networkx/issues/5610#event-6575071112

Solution 2:[2]

you could also try to modify the function itself

https://github.com/networkx/networkx/blob/main/networkx/algorithms/shortest_paths/weighted.py#L2191

in find_negative_cycle line 2191

pred = {source: []}
v = _inner_bellman_ford(G, [source], weight, pred=pred)

with

pred = {v: [] for v in sources}
v = _inner_bellman_ford(G, source, weight, pred=pred)

if source is a list. I think this issue should be reported

Solution 3:[3]

Can you try this:

def get_sequence_upto(x):
    for i in range(x):
        yield i
        
source = get_sequence_upto([1,2,3])

find_negative_cycle(G, source )

where [1,2,3] should be your list of source

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bumblebee18
Solution 2 ymmx
Solution 3 ymmx