Difference between DIjkstra and BellmanFord algorithm [closed] - algorithm

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.

Related

Floyd-Warshall Algorithm that works with negative cycles [closed]

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)).

Hamiltonian cycle in O(N) [closed]

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've been training with codility.com questions. There is a problem Eta 2011, which is trying to find the number of unique hamiltonian path.
You can read the whole problem here
In summary. we have a graph, where each inner node is connected to exactly 3 other nodes, while outer nodes are connected to 1 inner node. We draw a path that passes through all outer nodes. Now all nodes(inner and outer) are connected to exactly 3 nodes. This is an undirected graph.
He would like to solve the problem in O(N)!!!
The solutions available solves the problem in O(2^N) or higher. There are also heuristic solutions but obviously they are not precise.
Using the knowledge that each node in the graph is connected to exactly three other nodes, is it possible to solve the hamiltonian path in O(N)?
Due to copyright I believe I'm not authorized to copy/paste the whole problem. but a link is provided in the first paragraph.
cheers
Moataz
From wikipedia:
... They remain NP-complete even for undirected planar graphs of
maximum degree three
So, unless you have more information on the graph structure, all planar graphs of in-degree 3 is a subset of the possible input cases for this problem, and thus if you can solve this problem polynomially - you can also solve the problem for all planar graphs with in-degree 3 polynomially, and you can conclude P=NP
The graph is basically a tree with root node having 3 children and all other non-leaf nodes having 2 children. The leaves are connected from left to right.
You can think of each sub-tree as having two endpoint leaf nodes (say start and end).
Now given a subtree rooted at node n. If the hamiltonian route does not involve n and it's parent, then it will involve a path from the start to end and will cover all vertices of the sub-tree (in effect, a hamiltion route in the subtree routed at n).
Now consider the root of the tree. Suppose we take edges to x and y, with x being to left and y to the right.
Now we have to take the path from root to end point of subtree at x, and start point of subtree at y.
(A figure helps).
The rest of the path is completed by connect start to end of the subtrees which need paths to themselves.
This gives a recursive algorithm, and can be computed in O(n) time I believe.
Insane expectation of 30 minutes.
Hamiltonian cycle is polynomial for certain subsets of graphs, e.g. co-comparability graphs.
If your input graph is one of such graphs, you can solve the problem in polynomial time. Note that I am not stating that Hamiltonian cycle is not NP-C. All I am saying that it is polynomial for certain graphs.
Thus, if your input graph is a co-comparability graph, then you have a polynomial solution.
First, consider a tree with the root and all the non-leaf nodes having two children.
The leaves are also connected from left to right, but the first leaf is not connected
to the last. How many paths are there from the leftmost to the rightmost leaf?
The answer is only one, and it's not hard to prove.
Now take the tree from the input. Pick any node and remove one of its edges.
You're left with two trees of the same structure as the one I mentioned at the beginning.
Using those two you can build exactly one Hamiltonian path.
Now the node you picked had three edges you could remove, hence there are three ways to
make a Hamiltonian path in total :).
So the code boils down to checking for consistency and writing 3 in case everything is OK.
30 minutes is not that insane really.

Finding all vertices on negative cycles [closed]

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.

Using DFS on a Graph - Determine if a graph is a clique with specific SCC [closed]

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.

Verify Dijkstras algorithm in O (V + E) [closed]

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.

Resources