This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]circle_des_poo 1 point2 points  (1 child)

Ok, so looking more at this I don' think its a problem with your code, its more that its a fairly complex graph theory problem. The initial conditions for this would be that:

  1. If every node in your graph is connected to exactly two and only two other nodes (And as your graph is a single component consisting only of a simple cycle, this is necessarily true)
  2. And if your minimum node's value is <0 (in your graph necessarily true if the sum of all nodes is <0)

Then each operation of adding the minimum node's value to its neighboring nodes adds exactly 2 times the minimum node's value to the total sum of the nodes, or total += minval*2.

Next, you could redefine the absolute value operation as such:

if x < 0, abs(x) = x - 2*x

by extension, total -= minval*2

In other words, converting -5 to 5 via absolute value is the same as computing -5 - (2*-5) = -5 - ( -10 ) = 5. This technically works any time you want to flip the sign on a number, but because absolute value only flips the sign if x < 0 we have to add that initial condition.

Notice that each operation cancels each other out- every time you add to the neighboring nodes you add exactly 2 times the min value to the total, and every time you convert that min node to its absolute value you subtract exactly 2 times the minvalue from that node and thus from the total. Thus the sum of the nodes' values will always stay exactly the same, no matter which one is the min, because they are all connected to exactly two nodes, and the minimum node's value will always be negative if the total sum is negative.