I'm stuck at the following problem: Given a weighted digraph G, I'd like to construct the minimal subgraph of G that contains all negative (simple) cycles of G.
I do know how to find a negative cycle using Bellman-Ford, and I know that the number of simple cycles in a directed graph is exponential.
One naive way to approach the problem would be to simply iterate all simple cycles and pick those that are negative, but I have the feeling that there might be a polynomial-time algorithm. Most articles that I found through Google were about finding a (rather than all) negative cycle.
I'm hoping to find some experts here on stackoverflow that may give some hints towards a polynomial-time solution, or hints towards proving that it can't be solved in polynomial time.
Many thanks in advance!
Cheers, Robert
For anyone interested in or stuck at a similar problem: it's NP-complete. Thanks to wich for pointing me to the thread in cstheory.
To see why it's NP-complete, first of all observe that the problem may be stated as follows: given a weighted directed graph G with N verices and an edge E on G, find out whether E lies on a (simple) negative cycle. If it does, E should be in the subgraph H. If it does not, it should not be in H.
Now, let edge E be E = (u, v) with weight w. We'd like to know whether there's a path from v to u with total weight W such that W + w < 0. If we could do this in polynomial time, we could also solve the Hamiltonian Cycle problem in polynomial time:
Assign to edge E a weight of N - 1.00001. Assign to all other edges in the graph a weight of -1. Now the graph's only negative cycle on which E lies, is the cycle that contains all vertices (that cycle has weight -0.00001) and is thus a Hamiltonian Cycle.
Many thanks for thinking along!
Related
I have a question which I was asked in some past exams at my school and I can't find an answer to it.
Is it possible knowing the final matrix after running the Johnson Algorithm on a graph, to know if it previously had negative cycles or not? Why?
Johnson Algorithm
Johnson's Algorithm is a technique that is able to compute shortest paths on graphs. Which is able to handle negative weights on edges, as long as there does not exist a cycle with negative weight.
The algorithm consists of (from Wikipedia):
First, a new node q is added to the graph, connected by zero-weight edges to each of the other nodes.
Second, the Bellman–Ford algorithm is used, starting from the new vertex q, to find for each vertex v the minimum weight h(v) of a path from q to v. If this step detects a negative cycle, the algorithm is terminated.
Next the edges of the original graph are reweighted using the values computed by the Bellman–Ford algorithm: an edge from u to v, having length w(u, v), is given the new length w(u,v) + h(u) − h(v).
Finally, q is removed, and Dijkstra's algorithm is used to find the shortest paths from each node s to every other vertex in the reweighted graph.
If I understood your question correctly, which should have been as follows:
Is it possible knowing the final pair-wise distances matrix after running the Johnson Algorithm on a graph, to know if it originally had any negative-weight edges or not? Why?
As others commented here, we must first assume the graph has no negative weight cycles, since otherwise the Johnson algorithm halts and returns False (due to the internal Bellman-Form detection of negative weight cycles).
The answer then is that if any negative weight edge e = (u, v) exists in the graph, then the shortest weighted distance between u --> v cannot be > 0 (since at the worst case you can travel the negative edge e between those vertices).
Therefore, at least one of the edges had negative weight in the original graph iff any value in the final pair-wise distances is < 0
If the question is supposed to be interpreted as:
Is it possible, knowing the updated non-negative edge weights after running the Johnson Algorithm on a graph, to know if it originally had any negative-weight edges or not? Why?
Then no, you can't tell.
Running the Johnson algorithm on a graph that has only non-negative edge weights will leave the weights unchanged. This is because all of the shortest distances q -> v will be 0. Therefore, given the edge weights after running Johnson, the initial weights could have been exactly the same.
Hi I am struggling with this question. It is the following:
Devise an algorithm to find the lowest-weight cycle(i.e. of all cycles in the graph, the one with the smallest sum of edge weights), in a weighted, directed graph G = (V,E). Briefly justify the runtime and space complexity. Assume all edges are non-negative. It should run in O(|V||E|log|V|) time.
Hint: Use multiple calls to Dijkstra's algorithm.
I have seen solutions that use Floyd-Warshall but I was wondering how we would do this using Dijkstra's and how to do it within the time constraint given.
I have a few points of confusion:
Firstly, how do we even know how many cycles are in the graph and how
to check those?
Also, why is it |E||V|log|V|? By my understanding you should traverse
through all the vertices therefore making it |V|log|V|.
This is for my personal learning so if anyone has an example they could use, it would greatly help me! I am not really looking for pseudo-code - just a general algorithm to understand how using the shortest path from one node to all nodes is going to help us solve this problem.
Thank you!
Call Dijkstra's algorithm from each vertex to find the shortest path to itself, if one exists. The shortest path from any vertex to itself is the smallest cycle.
Dijkstra's algorithm takes O(|E| log |V|), so total time is O(|V||E| log |V|).
Note that this time can be worse than Floyd-Warshall, because there can be O(|V|^2) edges in the graph.
Call Dijkstra's algorithm |V| times, using each vertex in V as the start vertex. Store the results in a matrix, where dist(u,v) is the shortest path length from u to v.
For each pair of vertices (u,v), dist(u,v) is the shortest parth from u to v and dist(v,u) is the shortest path from v to u. Therefore, dist(u,v) + dist(v, u) is the length of the lowest weight cycle containing u and v. For each pair of vertices, computer this value and take the minimum. This is the lowest weigtht cycle.
I am trying to prove the computer complexity of this optimization problem:
Given a connected graph G = (V, E) and a set S ⊊ V. Find a connected subgraph G'= (V', E ') that:
Min f(G')
Min |V'|
subjet to:
S ⊊ V’
V’ ⊆ V
It looks like a generalization of the minimum spanning tree problem when not all vertexes have to be included in the tree.
Is there a known problem that can be used to proof the complexity of this problem by reduction?
Your problem formulation is not saying what you're optimizing on-- f(G') first and within that Min|V'|, or the other way round, or the two combined in some way.
if you optimize on the cost edges, it is the Steiner minimal tree (SMT) problem as is and NP-complete. if you optimize on |V'|, you can reduce SMT to it in polynomial time with the following:
Let edge (u,v) between nodes u and v have cost k. Replace this edge by the following path:
(u, i_1), (i_1, i_2), ..., (i_k, v)
so that the cost of each edge on this path is 1. You replaced the edge of cost (u, v) with a path with k-1 intermediary nodes on it and every edge has cost 1.
Do this for every edge on graph. It reduces SMT to your problem and proves that yours optimizing on |V'| is NP-complete. Your reduction takes
O(C*|V|^2)
time where C is an upper bound on the cost of edges in graph.
Just saw the problem. Hope it helps.
I believe that the Hamiltonian cycle problem can be summed up as the following:
Given an undirected graph G = (V, E), a
Hamiltonian circuit is a tour in G passing through
every vertex of G once and only once.
Now, what I would like to do is reduce my problem to this. My problem is:
Given a weighted undirected graph G, integer k, and vertices u, v
both in G, is there a simple path in G from u to v
with total weight of AT LEAST k?
So knowing that the Hamiltonian cycle problem is NP-complete, by reducing this problem to the Hamiltonian, this problem is also proved NP-complete. My issue is the function reducing it to Hamiltonian.
The big issue is that the Hamiltonian problem does not deal with edge weights, so I must convert my graph to one that doesn't have any weights.
On top of that, this problem has a designated start and finish (u and v), whereas the Hamiltonian finds a cycle, so any start is the same as the finish.
For (1), I am thinking along the lines of passing a graph with all simple paths of total weight LESS THAN k taken out.
For (2), I am thinking that this is not really an issue, because if there is a Hamiltonian cycle, then the simple path from u to v can be sliced out of it.
So, my real questions are:
Is my solution going to give me the right answer?
If yes, then how can I take out the edges that will produce simple paths of total weight less than k WITHOUT affecting the possibility that one of those edges may be required for the actual solution? Because if an edge e is taken out because it produces a simple path of weight < k for a subset of E, it can still be used in a simple path with a different combination of edges to produce a path of weight >= k.
Thanks!
Your reduction is in the wrong direction. To show that your problem is NP-complete, you need to reduce Hamilton Circuit to it, and that is very easy. I.e. show that every Hamilton Circuit problem can be expressed in terms of your problem variant.
More of a hint than an answer:
A unweighted graph can be interpreted as a weighted graph where each edge has weight 1. What would the cost of a Hamiltonian cycle be in a graph like that?
The part about the mismatch between cycles and paths is correct and is a problem you need to solve. Basically you need to prove that the Hamiltonian Path problem is also NP complete (a relatively straightfoward reduction to the Cycle problem)
As for the other part, you are doing things in the wrong direction. You need to show that your more complicated problem can solve the Hamiltonian Path problem (and is thus NP-hard). To do this you just basically have to use the special case of unit-cost edges.
For fun I'm learning about graph theory and I came across this problem. Given a set of vertices V, a set of edges E, and a weight for each edge in E, how can I efficiently construct a graph G such that:
G is connected (all vertices are connected via some path)
the sum of the weights of the edges is minimized
The edges in E are directed, when all edges in E are present there can be cycles.
See Minimum Spanning Tree algorithms.
ok... can i know what MrDatabase is after? SSSP algorithms (dijkstra, Bellman-Ford) are variation of MST, which ars just mentioned. Dijkstra does not solve negative weight cycle issue while Bellman-Ford does.
To add to ars's answer, if your graph contains edges with negative weight, then the problem becomes more difficult (and there may be no solution if you have a negative-weight cycle).
Read Bellman-Ford Algorithm. It supports negative weight cycles. Dijkstra's algorithm is more efficient but it doesn't support negative weight cycles.