3

Let $G(V,E)$ be a directed graph, where $V=\{a_1,\ldots,a_n\}$ is a set of vertices and $E$ is a set of ordered pairs of $V$, with $|V|=n$.

Now, let be $G(W,F)$ be a graph where $W$ is a set of vertices, such that $W=\{a_1,\ldots,a_{2n}\}$ with $|W|=2n$, and $F$ is a set of ordered pairs of $W$ defined as follows: $$\forall i, j \in \{1, \ldots, n\} : (a_i,a_{j+n}) \in F \text{ if } (a_i,a_{j}) \in E \\ \forall i \in \{1, \ldots, n\} : (a_{i+n},a_i) \in F$$

Now we define the capacities: $$c(a,b)=1 \text{ if } (a,b) \in F$$

And we define a cost: $$\forall i \in \{1, \ldots, n\} : p(a_{i+n},a_i)=-1 \text{ if } (a_{i+n},a_i) \in F \\ p=0 \text{ else}$$

The source as $a_{n+1}$ and the sink as $a_{n+1}$.

Does the graph $G(W,F,c,p)$ have a minimum cost flow of $-n$ if and only if the graph $G(V,E)$ has a Hamiltonian cycle?


(Comment to an answer in the comments section)

Michael, for your matrix with capacities G(V,E) the matrix G(W,F) is:

cap = [[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]]

cost = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0]]

RobPratt
  • 50,938
yugikaiba
  • 176
  • Got the idea from this post https://stackoverflow.com/a/16770830/14870160 when thinking about finding a path between two points A,B with a third point C contained in that path (path = no repeated edges). – yugikaiba Jan 02 '21 at 17:15
  • The minimum cost flow problem has a polynomial time solution (Ford-Fulkerson or Bellman-Ford algorithm) – yugikaiba Jan 02 '21 at 17:31
  • Do you mean min cost flow of $-(n-1)$? Take a trivial example of a connected graph with only 2 nodes and 2 links, which trivially has a Hamiltonian cycle but the corresponding bipartite graph has a min cost flow of -1, if I am understanding your setup correctly. (Anton’s answer deals with the more important concept of separated cycles, explicit counter-examples there could be two completely separated 2-node graphs, so $n=4$ and a Hamiltonian cycle is impossible, yet we can still get flow weight $-3$ in its corresponding bipartite graph.) – Michael Jan 02 '21 at 19:26
  • two points (n=2) connected to each other (V), then I have 4 for W. https://imgur.com/a/Hdkd5oX from $x_3 \rightarrow x_1 \rightarrow x_4 \rightarrow x_2 \rightarrow x_3$ and the cost is -2. – yugikaiba Jan 02 '21 at 19:41
  • (I edited the post the source and the sink is be the same point) – yugikaiba Jan 02 '21 at 20:20
  • The problem with Ford-Fulkerson is that you can go through one vertex multiple times (in and out), but can't repeat edges. So by changing the problem G(V,E) into G(W,F) and transforming the vertices of V into edges in F then Ford Fulkerson should give a quick answer. – yugikaiba Jan 03 '21 at 19:55
  • The basic Ford-Fulkerson method is a way to solve a max-flow problem (not a min-cost problem). It is not clear what you have in mind, perhaps you are thinking about some variation. The min cost routing problem can be formulated as a linear program, and you can get disjoint cycles as solutions to the linear program because they indeed satisfy all in-flow and out-flow constraints. – Michael Jan 03 '21 at 22:04
  • true Bellman-Ford it is then – yugikaiba Jan 03 '21 at 22:41
  • How does Bellman-Ford avoid repeated links when there are negative cycles? – Michael Jan 03 '21 at 22:46
  • Consider a negative cost cycle, if all flow has to pass through this cycle, the total cost is always reducing for every cycle completed. This would result in an infinite loop in the desire of minimizing the total cost. So, whenever a cost network includes a negative cycle, it implies, the cost can further be minimized (by flowing through the other side of the cycle instead of the side currently considered). A negative cycle once detected is removed by flowing a Bottleneck Capacity through all the edges in the cycle. – yugikaiba Jan 04 '21 at 00:28
  • Are you just giving ad-hoc heuristics here? – Michael Jan 04 '21 at 00:30
  • Is it heuristic? Not ad-hoc. From https://www.geeksforgeeks.org/minimum-cost-maximum-flow-from-a-graph-using-bellman-ford-algorithm/ – yugikaiba Jan 04 '21 at 00:31
  • Acording to the site -Time Complexity: O($V^2$ * $E^2$) where V is the number of vertices and E is the number of edges. - Auxiliary Space: O(V) – yugikaiba Jan 04 '21 at 00:38
  • That looks bogus. The explanation is quite weak and incomplete. No mention of what to do if, when we try removing a single link, the result gives us a negative cycle in other links, and that happens no matter what single link we remove. So then we need to pick all pairs of links, triplets of links, quadruplets of links, and so on, exponentially many different combinations to consider. – Michael Jan 04 '21 at 00:47
  • Try it, input a graph with a Hamiltonian path and run the code, if it takes too long to compute then it doesn't run in Polynomial time. But if it does work then it may be worth taking a closer look. – yugikaiba Jan 04 '21 at 01:10
  • I ran it on a symmetric graph with 7 nodes, all link capacities 1, all costs -1, and a Hamiltonian path from node 2 to node 6. It (surprisingly) found a min cost of -6. Then I reversed the source and sink to node 6 and node 2: It (incorrectly) gave an answer of -5. In other words, the program sometimes works, sometimes does not. It seems to be a heuristic. Correctness is the main issue (not running time). – Michael Jan 04 '21 at 05:16

3 Answers3

3

No, it's possible to have a flow of cost -n without having a Hamiltonian cycle. Consider for example a graph consisting of two disjoint cycles. To make the example less trivial, you could add some edges between them without introducing a Hamiltonian cycle. The two disjoint cycles still give a flow of cost -n.

  • No if 2 points $a_i$, $a_j$ are disjoint in G(V,E) then either $f(a_{i+n},a_i)=0$ or $f(a_{j+n},a_j)=0$. – yugikaiba Jan 02 '21 at 17:52
  • https://en.wikipedia.org/wiki/Minimum-cost_flow_problem – yugikaiba Jan 02 '21 at 17:53
  • (in the sense that there is no path neither from $a_i$ to $a_j$ nor from $a_j$ to $a_i$) – yugikaiba Jan 02 '21 at 17:56
  • $\sum_{(u,v) \in F} p(u,v) f(u,v) = \sum_{i\in {1,\ldots,n}} -f(a_{i+n},a_i) \ge \sum_{i\in {1,\ldots,j-1,j+1,\ldots,n}} -f(a_{i+n},a_i) = -n+1 $ – yugikaiba Jan 02 '21 at 18:05
  • Perhaps I'm not understanding your construction. It looks to me that if you start with a disjoint union of two directed graphs $G(V_1, E_1)\sqcup G(V_2, E_2)$, then the new directed graph you construct will be the disjoint union $G(W_1, F_1, c_1, p_1) \sqcup G(W_2, F_2, c_2, p_2)$. – Anton Geraschenko Jan 02 '21 at 18:22
  • The minimum cost flow on a disjoint union is just given by the minimum cost flows on the connected components, and the cost of that minimum flow is just the sum of the costs of the component flows. If the two original components have Hamiltonian cycles, then this minimum cost will be $-|V_1|-|V_2|$, achieving the minimum you wanted, but a disconnected graph obviously doesn't have a Hamiltonian cycle. – Anton Geraschenko Jan 02 '21 at 18:22
  • I start with a graph G(V, E) (any graph) and then I construct the graph G(W,F) from it. Get any example of a graph G(V,E) and construct G(W,F) from it (following the instructions above) to understand how to build it. – yugikaiba Jan 02 '21 at 18:25
  • If you can give a counterexample of a graph G(V,E) that doesn't have a Hamiltonian cycle in it such that G(W,F,c,p) have a minimum cost flow of -n that would proof the unsoundness of the statement. On the other hand if the statement is true then P=NP and the proof of that statement will proof P=NP. – yugikaiba Jan 02 '21 at 18:52
  • 1
    I sense you're not making the effort to understand what I write, which is frustrating. My answer produces just such a counterexample. To make it very specific, let G(V, E) be the disjoint union of two cycles, each of length 2. This obviously does not have a Hamiltonian cycle, as it is not connected. Then G(W, F, c, p) is also a disjoint union of two cycles, each of length 4, with all edges having a capacity of 1 and half the edges having a cost of -1 (the remaining having a cost of 0). The flow which gives weight 1 to all edges achieves a cost of -4, and this is clearly minimal cost. – Anton Geraschenko Jan 02 '21 at 20:58
  • https://imgur.com/a/mgVacLR – yugikaiba Jan 02 '21 at 21:02
  • The problem is that since they are disconected by Ford-Fulkerson or Bellman-Ford you can't go from the source to some disconnected point back to the sink. – yugikaiba Jan 02 '21 at 21:06
  • I don't know if you agree there is no minimum flow in that case with cost = -n, it has to be connected even if there is a Hamiltonian path (but not a Hamiltonian cycle) it still won't work. – yugikaiba Jan 02 '21 at 21:09
  • I will leave it as unanswered for now since Ford-Fulkerson and Bellman-Ford depend on a source and a sink and of being connected and someone might still come up with something. I will wait until I am either completely convinced or see some irrefutable proof/counterexample. – yugikaiba Jan 02 '21 at 21:51
3

Here is an example where the program you gave fails. This gives details from my comment above. You can plug in the following data into the Python program you linked here: https://www.geeksforgeeks.org/minimum-cost-maximum-flow-from-a-graph-using-bellman-ford-algorithm/


s = 6
t = 2

cap = [ [ 0, 1, 1, 1, 0, 0, 0], [1, 0, 1, 1, 1, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 1, 0]]

cost = [ [ 0, -1, -1, -1, 0, 0, 0], [-1, 0, -1, -1, -1, 0, 0], [-1, -1, 0, 0, 0, 0, 0], [-1, -1, 0, 0, -1, -1, 0], [0, -1, 0, -1, 0, -1, 0], [0, 0, 0, -1, -1, 0, -1], [0, 0, 0, 0, 0, -1, 0]]


This is a graph with 7 nodes labeled {0, 1, ..., 6}. The graph is symmetric, so for every link (i,j) there is a link (j,i). All link capacities are 1 and all costs are -1. Node 6 is only connected to node 5 and so the max flow to or from node 6 is 1 unit.

I used source s=6 and destination t=2. The program gives an output of 1, -5. This means a flow of 1 unit and a cost of -5 (5 hops). This is incorrect because there is a 6-hop Hamiltonian path that the program does not find:

$$ 6\rightarrow 5\rightarrow 4 \rightarrow 3 \rightarrow 1 \rightarrow 0 \rightarrow 2 $$

On the other hand, if we reverse the path and use s=2, t=6, the program gives a correct output of 1, -6.

Unfortunately the program only outputs the cost of the path, not the actual path, so I don't know what paths it is proposing in either case.

Michael
  • 26,378
  • You need to do the transformation G(V,E) to G(W,F) first, try the one I wrote in the post (Too big for the comments) – yugikaiba Jan 04 '21 at 07:19
  • if you want to go from node i to node j in G(V,E) you will have to go from node i+n to node j+n in G(W,F) or (likewise) from node i to node j in G(W,F). – yugikaiba Jan 04 '21 at 07:58
  • @yugikaiba Um, no. There is no need to do the bipartite graph to find Hamiltonian paths. The graph I constructed via the cap and cost matrices shows the Python program does not work for doing what it stated (finding a max-flow with min cost). Further, I constructed the example graph so that, if the Python program did work, it would find a Hamiltonian path. If you want to do other types of transformations or examples then you are free to do it. – Michael Jan 04 '21 at 08:53
  • There is a need to do a bipartite graph and define the cost function as I did because with Bellman-Ford you can have repeated vertices (but not repeated edges). So G(W,F) by Bellman-Ford should only have a minimum cost of -n for G(W,F) if and only if G(V,E) has a hamiltonian path. – yugikaiba Jan 04 '21 at 10:17
  • It's true I forgot Bellman-Ford doesn't work when there is a negative cycle. Can't see anything that makes me think otherwise. – yugikaiba Jan 04 '21 at 17:32
0

Let $f$ be the minimum-cost flow. That here, there [probably] are negative cost cycles does put a twist into what $f$ may look like. In particular, here is no reason why the arcs $e$ for which $f(e)$ is positive have to form a connected graph; indeed $f$ could consist of a path $P$ between $a_1$ and $a_{n+1}$ and then vertex-disjoint cycles [disjoint from $P$ as well] where each vertex participates in a cycle.

In fact you also need to require that the capacity of each vertex is also 1 and that the net flow into a vertex besides $a_1$, $a_{n+1}$ is 0; without this even edge-disjointness of the cycles suffices, and the minimum-cost flow could be less than $-n$.

Mike
  • 23,484
  • Give me a counter example, write down a graph G such that (don't need the picture, just the graph) G doesn't have a Hamiltonian path such that (W,F) has a minimum cost flow strictly higher than -n. – yugikaiba Jan 02 '21 at 19:52
  • I have no idea and my answer never promised such a graph--assuming each vertex has capacity 1 that is. However, $G$ can be a bunch of vertex-disjoint cycles and the flow will have value $-n$; it just won't form a connected subgraph. – Mike Jan 02 '21 at 19:58
  • if you a graph G(V,E) with just two disconnected points with no edges $x_1$ and $x_2$ then you just can't find a minimum cost flow from $x_3 $ to $x_3$ for G(W,F) with a cost of -2 (indeed the cost will be as low as the largest subcycle in G(V,E) that goes through x_1, in this example https://imgur.com/a/KfANro6 zero) – yugikaiba Jan 02 '21 at 20:08