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 8 years ago.
Improve this question
How can the Floyd-Warshall algorithm be modified to find the shortest path of any negative cost cycle of a directed graph that maintains O(V^3) time complexity?
There is no shortest path in a graph with negative cycle, for every path - one can find a shorter one by traversing the cycle one more time.
If you are referring to Shortest Simple Path (each vertex can be visited at most once) - it cannot be done, unless P=NP, and it most likely isn't.
Assume you have an efficient shortest simple path algorithm A.
Given an instance of the Longest Path Problem and a graph G=(V,E,w), create a new graph G'=(V,E,w') where w'(u,v) = -1*w(u,v). Now invoke A on G', and you got the shortest simple path on G' - which is the longest path on G.
However, since Longest Path is NP-Hard - such a solution is not possible unless P=NP.
tl;dr:
In a graph with negative cycle, there is no such thing as shortest path.
You cannot find a shortest simple path in a graph with negative cycles in O(V^3) time (unless P=NP, and even then it's not sure to be O(V^3)).
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.
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 5 years ago.
Improve this question
Lets assume i have a weighted undirected graph with edges and wanted to find the shortest path as well as all possible paths that i could follow from the startpoint to the endpoint with distances, what would be the best way to implement this? Breadth depth search and k paths algorithm seem to offer reasonable solutions, although im not sure which is best
Sorry, can't post this as comments...
If you need all possible paths, you can't do really better than "tree" traversal (BFS or DFS for instance). Note that you'll need to consider each node as many times as it can be reached from the start (the "tree" is much bigger than the original graph - even infinite if you have cycles in your graph, but let's assume you don't).
To get the smallest path, you could look for it in your list in the end; or preferably, you could use a Dijkstra-like order for your tree traversal, so the shortest path will be the first to come up.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a directed graph, Is it possible to determine whether there exists at least one path between two predefined nodes in constant time? If I use adjacency matrix data structure will it be useful?
Please suggest me what I am missing, what I need to learn. If there is no standard algorithm can you explain some solution for me.
Well, without pre-processing it cannot be done in constant time, you are bounded by the shortest path between these nodes to find the shortest path, and if no such path exists - it might decay to the size of the graph.
If you allow pre-processing, you can construct Strongly Connected Components graph (let it be G'), lexicographically sort it, and add an indication of all pairs (v',u') if there is a path from v' to u' on G'.
On query time you can search for the v' that contains v, and u' that contains u. and check if there is a path from v' to u', the answer will be the same.
If you pre-process the graph with Kruskal's algorithm, then it's subsequently possible to determine whether two nodes are connected in constant time. The algorithm will generate one or more sets of connected nodes. Two nodes that are in the same set are connected by one or more paths. Nodes in disjoint sets are not connected.
If you aren't allowed to pre-process the graph, then the answer is "No".
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 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.