<p>If you don't require the cycle to be simple, then break the (directed) graph into its strongly connected components, and for each component containing one of the given vertices $V_i$, check whether the component contains a negative cycle.  If no component does, there is no negative cycle containing any $V_i$.  But if some component does, you can find a (non-simple) negative cycle containing $V_i$ by taking many copies of the negative cycle, and adding to that paths to and from some vertex in the cycle to $V_i$.  (The total time to find an implicit representation of the desired cycle will be the same as the time to find a negative cycle in a directed graph, e.g. $O(nm)$, if I recall.)</p>

<p>If you do require the cycle to be simple, then the problem becomes NP-complete, even if only a single vertex $V_1$ is given.  (You can reduce Hamiltonian Path to the problem: to find a Hamiltonian path from a given source $S$ to a given sink $T$ in a given graph $G$, give the existing edges weight -1, then add an artificial vertex $V_1$ with two edges of cost $N/2-0.01$ each, one from $V_1$ to $S$ and one from $T$ to $V_1$.)</p>

<p>If you allow the cycle to repeat vertices but not edges, I believe it is still NP-complete (by a similar reduction, but splitting each vertex $v$ into a directed edge $(v,v')$ in a standard way).</p>

CC BY-SA 3.0
2012-07-02T16:19:06.503Z
I like this answer much better than mine.
David Eppstein
CC BY-SA 3.0
2012-07-02T05:25:29.363Z
2012-07-02T05:25:29.363Z
Neal Young
<p>I'm going to assume your input is a directed graph; I don't know how to do this for the undirected case.</p>

<p>Make $n$ copies of the vertex set of your graph, where $n$ is the number of vertices in the graph. Replace each edge from $u$ to $v$ in your original graph by edges that go from copy $i$ of $u$ to copy $i+1$ of $v$, for all choices of $i$. Additionally, if $u$ belongs to the specified vertex set but not otherwise, also include an edge that goes from copy $i$ of $u$ to copy $0$ of $v$.</p>

<p>The cycles in the expanded graph all project back down to cycles in the original graph, but every cycle in the expanded graph contains one of the specified vertices (otherwise you can't go backwards through the layers of expansion), so the original graph contains a negative cycle containing a specified vertex iff the expanded graph contains any negative cycle.</p>

CC BY-SA 3.0
2012-07-02T00:27:11.410Z
If the original graph has $n$ vertices and $m$ edges, the newly constructed graph will have $n^2$ vertices and $nm$ edges. Finding negative cycles in it will require $O(n^3m)$ time, which seems pretty large. I am still waiting for better solution, and thanks a lot!
Tianyi Cui
CC BY-SA 3.0
2012-07-02T04:38:47.473Z
Possibly more problematic, the cycles it finds won't necessarily be simple. Do you require simple negative cycles?
David Eppstein
CC BY-SA 3.0
2012-07-01T17:31:30.090Z
2012-07-01T17:31:30.090Z
David Eppstein
<p>Given a graph with weighted edges, how can we find a negative cycle that contains at least one vertex in a given vertex set $\{V_1, V_2, \ldots, V_k\}$? Thanks.</p>

CC BY-SA 3.0
2012-07-01T07:59:34.360Z
This question is pretty unclear.   Weights on what, edges or vertices?  What's $\{V_1, V_2, \dots, V_k\}$, is $V_1$ a vertex or a set of vertices?
Yixin Cao
CC BY-SA 3.0
2012-07-01T10:14:42.813Z
@YixinCao Thanks for noting, edited: weight on edges, $V_1$ is a vertex.
Tianyi Cui
<ds.algorithms><graph-theory>
Find negative cycle with vertex constraints
Tianyi Cui
# Find negative cycle with vertex constraints

(Asked by: username_0 on 2012-07-01)

Given a graph with weighted edges, how can we find a negative cycle that contains at least one vertex in a given vertex set $\{V\_1, V\_2, \ldots, V\_k\}$? Thanks.

**Tags:** ds.algorithms, graph-theory

> **Comments:**
> **username_1**: This question is pretty unclear.   Weights on what, edges or vertices?  What's $\{V_1, V_2, \dots, V_k\}$, is $V_1$ a vertex or a set of vertices?  
> **username_0**: @YixinCao Thanks for noting, edited: weight on edges, $V_1$ is a vertex.  

---

## 2 answers

### Answer by username_2 on 2012-07-02. Score: 8

If you don't require the cycle to be simple, then break the (directed) graph into its strongly connected components, and for each component containing one of the given vertices $V\_i$, check whether the component contains a negative cycle. If no component does, there is no negative cycle containing any $V\_i$. But if some component does, you can find a (non-simple) negative cycle containing $V\_i$ by taking many copies of the negative cycle, and adding to that paths to and from some vertex in the cycle to $V\_i$. (The total time to find an implicit representation of the desired cycle will be the same as the time to find a negative cycle in a directed graph, e.g. $O(nm)$, if I recall.)

If you do require the cycle to be simple, then the problem becomes NP-complete, even if only a single vertex $V\_1$ is given. (You can reduce Hamiltonian Path to the problem: to find a Hamiltonian path from a given source $S$ to a given sink $T$ in a given graph $G$, give the existing edges weight -1, then add an artificial vertex $V\_1$ with two edges of cost $N/2-0.01$ each, one from $V\_1$ to $S$ and one from $T$ to $V\_1$.)

If you allow the cycle to repeat vertices but not edges, I believe it is still NP-complete (by a similar reduction, but splitting each vertex $v$ into a directed edge $(v,v')$ in a standard way).

> **Comments:**
> **username_3**: I like this answer much better than mine.  

---

### Answer by username_3 on 2012-07-01. Score: 6

I'm going to assume your input is a directed graph; I don't know how to do this for the undirected case.

Make $n$ copies of the vertex set of your graph, where $n$ is the number of vertices in the graph. Replace each edge from $u$ to $v$ in your original graph by edges that go from copy $i$ of $u$ to copy $i+1$ of $v$, for all choices of $i$. Additionally, if $u$ belongs to the specified vertex set but not otherwise, also include an edge that goes from copy $i$ of $u$ to copy $0$ of $v$.

The cycles in the expanded graph all project back down to cycles in the original graph, but every cycle in the expanded graph contains one of the specified vertices (otherwise you can't go backwards through the layers of expansion), so the original graph contains a negative cycle containing a specified vertex iff the expanded graph contains any negative cycle.

> **Comments:**
> **username_0**: If the original graph has $n$ vertices and $m$ edges, the newly constructed graph will have $n^2$ vertices and $nm$ edges. Finding negative cycles in it will require $O(n^3m)$ time, which seems pretty large. I am still waiting for better solution, and thanks a lot!  
> **username_3**: Possibly more problematic, the cycles it finds won't necessarily be simple. Do you require simple negative cycles?  

---

