I have a connected graph g
with n
vertices and m
edges.
Each edge can be traversed from both directions, while traversing them in one direction their weight is positive, traverse them in the other direction and their weight is negative.
So for every edge u
-> v
with weight w
there exists an edge v
-> u
with weight -w
.
My goal:
For a given vertex v
, check if there exists a path back to v
(a cycle) so that the sum of the edge weights of that path is not equal to 0
. If such a path exists then output the minimal number of edges of such a path, otherwise output "all cycles are fine"
.
Examples:
An example where all paths from v
to v
sum up to 0
. The output is "all cycles are fine"
:
An example where there exist paths from v
to v
whose edges sum up to something which isn't equal to 0
. The minimal number of edges of such a path is 4 in this example:
My current approach:
Find all paths/cycles from vertex v
to itself and check if all of them sum up to 0
. I have problems finding all paths/cycles in an efficient manner, is there a more elegant solution or should I try to find the most efficient algorithm for finding all paths?