Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I know that the problem of checking whether given edge of a weighted digraph belongs to a negative cycle is NP-complete (Finding the minimal subgraph that contains all negative cycles) and Bellman-Ford allows to check a vertex for the same thing in O(|V|*|E|) time. But what if I want to find all vertices belonging to negative cycles? I wonder if it could be done faster than Floyd-Warshall's O(|V|^3).
I don't think Floyd-Warshall does the job of finding these vertices. Using a similar approach as taken in the post you're referring to, it can be shown that finding the set of all vertices that lie on a negative cycle is NP-complete as well.
The related post shows that one may use the algorithm to find the set of all edges that lie on a negative cycle to solve the hamiltonian cycle problem, which means that the former problem is NP-complete.
If we can reduce the problem of finding all edges that lie on a negative cycle to the problem of finding the set of all vertices that lie on a negative cycle, we've shown NP-completeness of the latter problem.
For each edge (u,w) in your weighted digraph, introduce a new auxiliary vertex v, and split (u, w) in two edges (u, v) and (v, w). The weight of (u, w) can be assigned to either (u, v) or (v, w).
Now apply the magic polynomial-time algorithm to find all the vertices that lie on a negative cycle, and take the subset that consists of the auxiliary vertices. Since each auxiliary vertex is associated with an edge, we've solved the problem of finding the minimal subgraph that contains all negative cycles, and we can thus also solve the hamiltonian cycle problem in polynomial time, which implies P = NP. Assuming P != NP, finding all vertices that lie on a negative cycle is NP-complete.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have a weighted graph with n vertices and the starting point is given. The shortest path is defined as the path with the least sum of weights.
How can I find out the shortest path that passes through m different vertices ? (each vertex can be visited once or more than once. That is, there are exactly m vertices in the set of the vertices that have been visited, but each vertex may have been visited multiple times.)
Note that the number m is given but the specific m vertices are not. (These m vertices are selected by algorithm)
Is it an NP-Hard problem?
We can reduce the Hamiltonian Path Problem (HPP) to your problem: a Hamiltonian Path is a path in a directed unweighted graph that visits each vertex exactly once. To solve an instance of HPP, convert the graph into a weighted graph with weight 1 on every edge, set m to be |V| and solve your problem. This is a polynomial-time reduction, so your problem is NP-hard, since HPP is NP-complete.
It is also NP-complete since it is clearly in NP. So there is also a polynomial-time reduction from your problem to any other NP-complete problem. Algorithms for solving the Travelling Salesman Problem are probably most suited for you: see here for details and examples.
This question already has answers here:
Why doesn't Dijkstra's algorithm work for negative weight edges?
(12 answers)
Closed 3 years ago.
my current understanding is that dijkstra's algorithm is more efficient then bellman-ford, only it cannot handle negative edges. However say we have an edge weighted graph where there are negative-weight edges, there are no negative-weight cycles in the graph, can we still use dijkstra's algorithm?
This question has already been answered well here.
https://stackoverflow.com/a/13159425/12449779
To summarize, Dijkstra's algorithm in each iteration marks the vertices at minimum distances from "marked" vertices. Initially, the source vertex is the only marked vertex with distance 0. Suppose two marked vertices exists A and B who are at a certain finite distance from source and their paths do not overlap. Currently, the paths to A and B would be considered as the shortest possible paths from A to B. Since the paths do not overlap, any path from Source to A to B would be of length source to A + length of Path from A to B which is greater than path from source to B as paths from source to A and B do not overlap. But if negative weighted edges are allowed, this does NOT hold. As the length of path from A to B may be negative. Thus Dijkstra's Algorithm does not work. There is no simple way to convert a general graph with negative edges to one with non-negative edges either. Bellman Ford works for any graph which does not contain negative weighted cycles.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a simple question on DFS and I'm trying to understand how to use it and not how to solve the whole problem. I'm really looking for an explanation and not a solution to my homework.
I'll write down the question first.
"Suppose you have an undirected graph G=(V,E) and let three of its
vertices to be called v1, v2 and v3. Find an algorithm which
determines if these three vertices are part of a clique
(complete graph) (k>=3)"
Now I suppose to use DFS in order to solve it. As far as I understand DFS will let me know if v1, v2 and v3 are in the same strongly connected component. If I'm correct I should also determine if G is also a clique(complete graph).
I read in the internet and I found out that asserting if a graph is clique or not is NP and cannot be solved easily. Am I correct? Am I missing anything? Is there any propery I can use to determine immediately if a graph is comeplete ?
To clarify the confusion about the NP-completeness: checking whether a graph is a clique is not NP-complete; just count the edges and see whether there are n(n-1)/2. What is NP-complete is to find a maximum clique (meaning the subgraph that has the biggest number of vertices and is a clique) or a clique of k vertices in a graph of n vertices (if k is part of the input instead of a fixed number); the latter case is called the clique decision problem.
EDIT: I just realized you asked something regarding strongly connected components as well; that term only applies to directed graphs (i.e. the edges have a direction, which means for two vertices v and w, the edge v->w is not the same as the edge w->v). Cliques are commonly defined on undirected graphs, for which there are only connected components.
If I understood it properly, all you have to check whether these three vertices are connected, i.e., the edges v1-v2, v2-v3 and v3-v1 exists. If they exist, they form a clique of K=3. If at least one of them does not, these three vertices together can not be in a clique of size k>=3.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am working on solving this problem:
Professor Gaedel has written a program that he claims implements Dijkstra’s algorithm.
The program produces v.d and v.π for each vertex v in V. Give an
O.(V + E)-time algorithm to check the output of the professor’s program. It should
determine whether the d and π attributes match those of some shortest-paths tree.
You may assume that all edge weights are nonnegative.
v.d is the shortest distance from starting node to v.
v.π is v's predecessor in the shortest path from starting node to v
My idea is:
For every vertex (i), compare i.d with (i.π).d. If i's predecessor has a larger d value then we cannot have a shortest-path tree.
I believe this can check if the professor's output is not a shortest-path tree, but I don't think it can confirm that the output is a shortest-path tree. I cannot think of a way to do this without more information.
Am I on the right track?
I think this would work
Do a DFS, but instead of following the regular graph edges, follow only the π value for each vertex. You're doing this to produce a topological ordering, so that the first vertex to finish will be the first vertex in the topological ordering. Note that the first vertex in the topo sort you produce will be the "source" vertex that was given to Gaedel's algorithm.
Now that you have a topo ordering, you can relax edges in the most efficient order, just like how you would do it on a DAG.
for each v in topoSortedVerts
if v.d_verify != v.d_Gaedel
//fail
for each u in v.adjacencies
relax(v, u)
if v.d_verify != v.d_Gaedel
//fail
I think you may also need to make sure all V verts are considered, and that the source vertex matches. Maybe. Also, I guess Gaedel's predecessor subgraph induced by the π values could be real jacked up and have all kinds of crazy things wrong with it, but I assume it doesn't.
It is O(V + E) because the outer loop runs V times, and the inner loop runs E times using aggregate analysis.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am wring thesis about shortest path algorithms.
And i don't understand one thing...
I have made visualisation of dijkstras algorithm.
1) Is it correct ? Or am i doing something wrong?
2) How would look Bellman-Ford algorithm? As fas as i have looked for difference, i found "Bellman-ford: the basic idea is very similar to Dijkstra's, but instead of selecting the shortest distance neighbour edges, it select all the neighbour edges." But also dijkstra checks all vertexes and all edges, isnt it?
dijkstra assumes that the cost of paths is montonically increasing. that plus the ordered search (using the priority queue) mans that when you first reach a node, you have arrived via the shortest path.
this is not true with negative weights. if you use dijkstra with negative weights then you may find a later path is better than an earlier one (because a negative weight improved the path on a later step).
so in bellman-ford, when you arrive at a node you test to see if the new path is shorter. in contrast, with dijkstra, you can cull nodes
in some (most) cases dijkstra will not explore all complete paths. for example, if G linked only back to C then any path through G would be higher cost that any through C. bellman-ford would still consider all paths through G to F (dijkstra would never look at those because they are of higher cost that going through C). if it does not do this it can't guarantee finding negative loops.
here's an example: the above never calculates the path AGEF. E has already been marked as visited by the time you arrive from G.
I am also thinking the same
Dijkstra's algorithm solves the single-source shortest-path problem when all edges have non-negative weights. It is a greedy algorithm and similar to Prim's algorithm. Algorithm starts at the source vertex, s, it grows a tree, T, that ultimately spans all vertices reachable from S. Vertices are added to T in order of distance i.e., first S, then the vertex closest to S, then the next closest, and so on.