Algorithm to find shortest path to net values across nodes

391 Views Asked by At

I have an undirected graph $G = (V, E)$ with nodes $V$ and edges $E$. Each node $v$ has an associated number $n_v \in \mathbf{Z}$

Let us define for any two nodes $v, w \in V$ connected by an edge $e \in E$, that to net node $v$ with node $w$ means that we set $n_v = 0$ and set $n_w = n_w + n_v$. We also define that the cost of such a netting operation is $|n_v|f(e) \geq 0$ where $f : E \rightarrow[0,\infty)$ is a function describing unit cost of netting between $v, w$.

As an example:

enter image description here

In the above image, the big numbers in each node $v$ represent $n_v$ and the little numbers along each vertex $e$ represent $f(e)$.

The Question

By only netting nodes, I wish to make node $Z$ (green) the only non-zero node. I also want this done at minimal cost.

Does anyone know of an algorithm that does this at minimal cost?

In the above example, we should net node $A$ with $C$ (with a cost of $|-2|\times1$) and $B$ with $C$ (with a cost of $|-3|\times1$). All nodes apart from $Z$ are then $0$ with a total cost of $5$.

Motivation

Using an application for motivation, imagine we are a charity distributing medicine across cities in a country with a viral outbreak. The situation is rapidly evolving as the virus spreads or as people get better, and so some cities may find they have a shortage of medicine while others have surplus medicine.

The situation may look like:

enter image description here

We need to move medicine from where there is a surplus to where there is a shortage. The medicine can be transported from one city to another via interconnecting roads. There is a transportation cost involved in transporting each unit of medicine. Equivalently, we can move people from one city's hospital to another (we assume the same cost of transportation as medicine for simplicity, i.e. the undirected graph case).

We wish to ensure that all cities have neither a surplus nor a shortage of medicine. Any surplus must be moved back to the depot. Any shortage must also be moved back to the depot (this represents the scenario when we need to create and distribute new medicine from the medicine depot).

1

There are 1 best solutions below

0
On BEST ANSWER

Seems to be solved immediately by transforming it into a min cost max flow problem (with multiple sources and sinks). First, calculate the sum of all the nodes' values --- this will be node Z's final value (the final value for all other nodes are by definition 0). For a node, consider the difference between their final value and their initial value -- if the initial value is larger, you need to move some of its content around, otherwise it will receive some things from other nodes. Thus, for the former case, it can be treated as one of the multiple sources, and the latter case is treated as one of the multiple sinks.

Each edge shall have infinite capacity and have a cost equal to $f(e)$. The cost of the min-cost max-flow of this graph is exactly the minimum cost required.

Note that this will work even if there are edges with negative value, but negative cycles will result in the algorithm working incorrectly.

EDIT: whoops, didn't read the comments.