I have a symmetrical graph and created a tree with all shortest path from a random vertex to any other vertex. Can I use the tree to construct a Minimum Spanning Tree(MST)? My algorithm is similar to depth-first algorithm.
In the worst case, a shortest path tree does not help in finding a minimum spanning tree. Consider a graph where we want to find the MST. Add a source vertex with edges of an identical large length to each other vertex. The shortest path tree from that source consists of the very long edges, which we knew a priori, hence the shortest path tree is not useful in this case.
Related
Will Dijkstra's shortest path algorithm return correct results on a directed tree with negative weight edges?
On a general graph with negative weights, the algorithm will fail, but since it’s a directed tree it feels like the algorithm will succeed.
From other answers, you know that there is no good reason to run Dijkstra's algorithm if you know that the graph is a tree.
If you do run it, though, it will work even if the tree has negative edge weights.
The reason that Dijkstra's algorithm doesn't work for graphs with negative weights, is that negative weights allow a 2nd, shorter, path to be found to a vertex after its distance has already been decided. In a tree there are no 2nd paths.
In a tree there is only one path between any two given nodes, so searching for the "shortest" path in a tree makes little sense: when you find a path it is the shortest, and this search does not need to take weights into account, so there is no need to use Dijkstra's algorithm. A simple depth-first search will do.
If the graph is not a tree, but a directed acyclic graph (DAG) with negative edges, then Dijkstra's algorithm cannot be used to find a shortest path. Take this counter example:
If we have to look for the shortest path from A to C, Dijkstra's algorithm will proceed to visit B and C, and as it hits the target, it will stop looking further, never considering the edge from B to C.
Other attempts to apply Dijkstra
Another (now deleted) answer proposed to make all edge weights positive by adding an absolute value to all weights, but this does not yield correct results: what is the shortest path in the original graph, is not guaranteed to be still the shortest path in the derived graph.
Counter example:
Where in the original graph the shortest path from A to C runs via B, in the adjusted graph, the shortest path is A-C.
I was told that BFS can give you the shortest path from the source vertex to the destination vertex, which makes sense since you traverse the adjacent nodes. However, I do not see how that is guaranteed to happen always. Nowhere in the BFS pseudocode logic do I see to pick the correct adjacent node to guarantee to have the shortest path. BFS could pick any random adjacent node and end up with the longer path from the source vertex to the destination vertex. Then how does BFS give the shortest path from the source vertex to the destination vertex?
In the case of unweighted graphs, it is guaranteed that eventually, we will get the shortest path using BFS.
But in the case of a weighted graph, we can still run a variant of BFS, i.e., using a priority queue for neighbors nodes (based on weights of edges). This variant is called Dijkstra’s algorithm.
You are correct. Using BFS alone does not guarantee to find the shortest path.
It will only fetch you the shortest path when all the edges have an equal edge weight in the case of a weighted graph. For unweighted graphs, you can assume equal edge weights.
Other algorithms (for example, Dijkstra’s algorithm) adapt BFS to find the shortest paths.
Thanks to your question, which pushed me to come up with an interesting case, where BFS can actually fetch you the shortest path in case of weighted graphs:
When we have a linear weighted undirected graph (with no cycles), i.e., a graph with 2 adjacent nodes for middle nodes and 1 adjacent node for nodes are the extreme ends (like a double linked list), BFS could actually fetch us the shortest path.
PS: BFS does not pick up any adjacent node randomly. The adjacent node's selection depends on the adjacent nodes' order in the particular node's adjacency list. So, it's dependent on your code implementation and not on some random factor.
Claim: If all the edges weights in a graph are distinct, then there is a unique shortest path tree. Either give a convincing argument that the claim is true or give a counterexample.
If you have MST then there is a unique path from every two vertices which makes the shortest path tree senseless. I assume you meant the result is a MST. However, this is not true. Shortest path trees are different from minimum spanning trees for the same graph and even for the same root. Shortest Path tree rooted on vertex v is usually the result of applying Dijkstra's algorithm over v.
In general, uniqueness for trees over graphs is hard to be believed in unless strict requirements were given (like the new weights equal old ones +1). #rici gave a counterexample with a polytree structure. Here is another counterexample for undirected graphs. Both trees are shortest path trees rooted at A. Note that:
While both are shortest path trees, their total cost differ.
Both are spanning trees but neither one of them is a minimum.
If I understand the question, correctly:
Just need to make sure about this:
When I run Dijkstra's algorithm on a graph, at the end I will have a spanning tree right ? (Not necessarily Minimum spanning tree)
So the difference between Dijkstra and PRIM/Kruskal that the later two algorithms will return a minimum spanning tree?
Thanks
You are correct, with one condition - the graph should have a spanning tree from the source (i.e. - every vertex v in the graph has a path from the given source).
Also, as commented by #Henry - you should continue the algorithm until you found path to all vertices, and do not 'stop' once a target is reached.
Also note that Dijkstra's algorithm (and in general - shortest path solver) is defined for directed graphs, and MST is usually for undirected graphs.
(Note that it easy to define every undirected graph as a directed graph - by simply adding (u,v) and (v,u) for each edge {u,v} in the undirected graph)
I have to create a solution for a weighted undirected graph, passing through all the nodes, with a total minimum cost. Several paths, with no defined starting nodes, should end up and meet at one intersecting node. The number of the paths, and the number of the nodes included in a path are not pre-determined. The nodes can be passed more than once.
What kind of problem am I dealing with, possible algorithms as solution?
I suppose it should be a variation of a Minimum spanning tree (meaning using the intersection node as a starting point for the paths in stead of ending point)
It's called Minimum Cost Hamiltonian Circuit problem.
Here you can read more about it.
It is a tree you are looking for and the problem is Minimum Spanning Tree-- MST: building a tree that spans all the nodes in graph and the cost of edges on the tree is minimum possible. It is a polynomial problem. Prim and Kruskal each have well-known algorithms for the solution.
See http://en.wikipedia.org/wiki/Kruskal's_algorithm for Kruskal's algorithm.
Note: the problem is NP-complete when the tree is supposed to span a given proper subset of nodes instead of all nodes in the graph. This time it is known as the Steiner Minimal Tree problem.