I am pretty new in Python (it is my first code) and I am wondering how can I find all cycles in a WEIGHTED directed graph.
I saw How to detect a cycle in a directed graph with Python? (for graphs without weights), but I did not understand, why there is an edge between 'C' and 'D' mentioned twice and why there is g.addEdge('A', 'B') (this edge is mentioned above).
I tried to write code without weights on my own (I have edges between
'1' and '2', with weight 2
'1'and '3', with weight 7
'2' and '3', with weight -2
'2' and '4', with weight -4
'3'and '1', with weight 7
'3'and '4', with weight 3
'4' and '3', with weight 3)
but I do not understand, why all my cycles starts with vertex "3" and how to write a code with weights.
My code for directed graph without weights is:
edges = [('1', '2'),('1', '3'),('2', '3'),('2', '4'),('3', '1'),('3', '4'),('4', '3')]
G = nx.DiGraph(edges)
for cycle in nx.simple_cycles(G):
print(cycle)
and output is:
['3', '4']
['3', '1']
['3', '1', '2', '4']
['3', '1', '2']
And I want something like this:
['3', '4'] weight 6
['3', '1'] weight 11
['3', '1', '2', '4'] weight 5
['3', '1', '2'] weight 4
Thanks a lot!