Can anyone explain why the tree resulting from Kruskal is different from Dijkstra ?
I know for the fact that kruskal works on nondescending order of edges but Dijkstra takes advantage of priority queue but still cannot understand why the resulted tree from them are different?
The basic difference, I would say, is that given a set of nodes, Dijkstra's algorithm finds the shortest path between 2 nodes. Which does not necessarily cover all the nodes in the graph.
However on Kruskal's case, the algorithm tries to cover all the nodes while keeping the edge cost minimum.
Consider this graph:
E
3 / |
B | 3
5 /3\ |
/ D
A | 2
\ F
1 \ / 1
C
2 \
G
Dijkstra's algorithm will return the path A-C-F-D-E for source and destination nodes A and E, at a total cost of 7. However Kruskal's algorithm should cover all the nodes, therefore it will consider edges [AC], [CG], [CF], [FD], [DB] and [DE] with a total cost of 12.
In Dijkstra, the irrelevant nodes (ones that are not in the path from source the destination) are ignored, e.g., G and B in this case. The resulting path, of course, is a tree but does not cover all the nodes. There might be millions of nodes connected to G (assuming they are not connected to other nodes) which would not be in the path of the Dijkstra's result. On the other hand, Kruskal had to add those nodes to the resulting tree.
The minimum spanning tree is not unique.
The Kruskal's algorithm select a minimum length edge of all possible edges which connect two different disjoint MST components, found so far.
The Dijkstra/Prim/Jarnik's algorithm selects minimum length edge of all the edges, which extend the single MST component found so far.
At each step, in the general case the algorithms select a minimum edge from distinct sets of possiblities.
PS. Well, if the OP refers to the shortest path tree of the Dijkstra's shortest path algorithm the answer is that the shortest path tree is not necessarily a minimum spanning tree, the algorithms compute different things.
They solve different problems. It might be possible that they produce the same trees in some cases (e.g. the graph is already a tree), but in general the trees are optimized for different things: one finds the shortest paths from a single source, another minimizes the total weight of the tree.
For example, consider we built MST with Kruskall. Let's say all the edges in MST weight 1 and it looks more or less linear. Assume that to get from A to Z it takes 5 edges, so the path from A to Z in MST will cost 5. However, it might as well be possible that original graph had an edge from A to Z with cost 3 (< 5), which MST didn't include, yet Dijkstra will probably have the edge in its resulting tree.
Related
I have a question that asks me to make a graph such that the BFS and DFS trees from the graph is not a minimum spanning tree and the order of the adjacency list does not matter, I know the properties of BFS DFS and MST but I'm a confused by the question. How should I be approaching the problem? (not looking for the solution)
Imagine a complete graph on k vertices. For k > 3, a DFS tree will always look different from a BFS tree. For k > 4, you can have a MST that is different from both the BFS and DFS trees. You can choose the shape of the MST to be different from the DFS tree by ensuring one vertex needs three edges coming out of it. You can choose the shape of the MST to be different from the BFS tree by ensuring no vertex has more than three edges coming out of it. You choose the shape of the MST by assigning weights to make the edges you choose, and only those edges, part of the MST.
DFS Tree
1-----2----3
|
|
4-----5
BFS Tree
1
____|____
/ / \ \
2 3 4 5
MST
2
|
1---3---5
|
4
The complete graph on five vertices has 5 * 4 / 2 = 10 edges, of which only four are needed in any tree.
My suggestion would be to look at an algorithm for finding the MST from a graph, and think about why it is not just a BFS or DFS search.
For any algorithm, finding an edge case that actually tests the tricky parts is an important part of implementation. You could start with a simple example, and try adding new edges or altering the input in a way that would make a simple BFS/DFS search fail.
The condition that it should be independent of the adjacency list order is to ensure that your test graph has a truly hard structure, not just that you have artificially fed the edges to the algorithm in a way that makes it fail.
I currently studying about graphs and their algorithms, and i noticed a question which i don't know to to exactly prove:
If we have a connected, undirected graph G=(V,E), and every edge is with weight=1,is it true to say that every spanning tree that built from the shortest paths from the root, is a minimum spanning tree?
I ran some examples in http://visualgo.net/sssp.html and is seems for me that the answer for this question is true, but someone can show me how can i prove this?
and another question that crossed my mind, does the other direction is also true?
Every tree has exactly n - 1 edges. Since all weights are equal to 1, every spanning tree of G has a total weight of n - 1. It is also true for the minimal spanning tree. So the answer is yes.
TLDR: Not Necessarily
Consider the triangle graph with unit weights - it has three vertices x,y,z, and all three edges {x,y},{x,z},{y,z} have unit weight. The shortest path between any two vertices is the direct path. Agree?However, to satisfy the condition of a MST in the graph, you have to put together a set of 2 edges connecting the 3 vertices. Say {x,y}, {y,z} for example. This does not represent the shortest path between any pair of vertices.
Hence, your proposition is false :)
Prim's and Kruskal's algorithms are used to find the minimum spanning tree of a graph that is connected and undirected. Why can't they be used on a graph that is directed?
It's a minor miracle that these algorithms work in the first place -- most greedy algorithms just crash and burn on some instances. Assuming that you want to use them to find a minimum spanning arborescence (directed paths from one vertex to all others), then one problematic graph for Kruskal looks like this.
5
--> a
/ / ^
s 1| |2
\ v /
--> b
3
We'll take the a->b arc of cost 1, then get stuck because we really wanted s->b of cost 3 and b->a of cost 2.
For Prim, this graph is problematic.
5
--> a
/ /
s 1|
\ v
--> b
3
We'll take s->b of cost 3, but we really wanted s->a of cost 5 and a->b of cost 1.
Prim's and Kruskal's algorithms output a minimum spanning tree for connected and "undirected" graphs. If the graph is not connected, we can tweak the algorithms to output minimum spanning forests.
In Prim's algorithm, we divide the graph in two sets of vertices. One set of the explored vertices which have already formed a MST (Set 1) and another set of unexplored vertices which will eventually join the first set to complete "spanning" (Set 2). At each instant, we select a minimum weighted edge in the cut joining the two disjoint sets. If there is no directed edge from explored nodes of the MST to the remaining unexplored nodes, the algorithm gets stuck even though there are edges from unexplored nodes to explored nodes in the MST.
In Kruskal's algorithm, the idea is to sort the edges in ascending order by their weight and pick them up in order and include them in the MST of explored nodes/edges if they do not already form a cycle with any explored nodes. This is done using the Union-Find data structure. But detection of cycles for directed graphs fails with this method. For example, a graph containing edges [1->2] [2->3] [1->3] will be reported to contain a cycle with the Union-Find method.
So Prim's fails because it assumes every node is reachable from every node which, though valid for undirected graphs, may not be true for digraphs. Kruskal's fails because of failure to detect cycles and sometimes it is essential to add edges making cycles to satisfy the "minimum" weighted property of MST.
Also, in case of digraphs, MST doesn't make complete sense. Its equivalent for digraphs is "minimum spanning arborescence" which will produce a tree where every vertex can be reached from a single vertex.
I'm trying to find an efficient method of detecting whether a given graph G has two different minimal spanning trees. I'm also trying to find a method to check whether it has 3 different minimal spanning trees. The naive solution that I've though about is running Kruskal's algorithm once and finding the total weight of the minimal spanning tree. Later , removing an edge from the graph and running Kruskal's algorithm again and checking if the weight of the new tree is the weight of the original minimal spanning tree , and so for each edge in the graph. The runtime is O(|V||E|log|V|) which is not good at all, and I think there's a better way to do it.
Any suggestion would be helpful,
thanks in advance
You can modify Kruskal's algorithm to do this.
First, sort the edges by weight. Then, for each weight in ascending order, filter out all irrelevant edges. The relevant edges form a graph on the connected components of the minimum-spanning-forest-so-far. You can count the number of spanning trees in this graph. Take the product over all weights and you've counted the total number of minimum spanning trees in the graph.
You recover the same running time as Kruskal's algorithm if you only care about the one-tree, two-trees, and three-or-more-trees cases. I think you wind up doing a determinant calculation or something to enumerate spanning trees in general, so you likely wind up with an O(MM(n)) worst-case in general.
Suppose you have a MST T0 of a graph. Now, if we can get another MST T1, it must have at least one edge E different from the original MST. Throw away E from T1, now the graph is separated into two components. However, in T0, these two components must be connected, so there will be another edge across this two components that has exactly the same weight as E (or we could substitute the one with more weight with the other one and get a smaller ST). This means substitute this other edge with E will give you another MST.
What this implies is if there are more than one MSTs, we can always change just a single edge from a MST and get another MST. So if you are checking for each edge, try to substitute the edge with the ones with the same weight and if you get another ST it is a MST, you will get a faster algorithm.
Suppose G is a graph with n vertices and m edges; that the weight of any edge e is W(e); and that P is a minimal-weight spanning tree on G, weighing Cost(W,P).
Let δ = minimal positive difference between any two edge weights. (If all the edge weights are the same, then δ is indeterminate; but in this case, any ST is an MST so it doesn't matter.) Take ε such that δ > n·ε > 0.
Create a new weight function U() with U(e)=W(e)+ε when e is in P, else U(e)=W(e). Compute Q, an MST of G under U. If Cost(U,Q) < Cost(U,P) then Q≠P. But Cost(W,Q) = Cost(W,P) by construction of δ and ε. Hence P and Q are distinct MSTs of G under W. If Cost(U,Q) ≥ Cost(U,P) then Q=P and distinct MSTs of G under W do not exist.
The method above determines if there are at least two distinct MSTs, in time O(h(n,m)) if O(h(n,m)) bounds the time to find an MST of G.
I don't know if a similar method can treat whether three (or more) distinct MSTs exist; simple extensions of it fall to simple counterexamples.
One can use Prim's algorithm or Kruskal's algorithm to find the minimum spanning tree/graph of a collection of vertices/nodes and edges/links. What I want though, is an algorithm that finds the minimum spanning graph of this collection, but the resulting graph needs to include only arbitrarily chosen nodes, instead of all nodes. It's okay if the resulting graph includes more nodes than just those needed.
Does such an algorithm exist? Perhaps one could just use Prim's (or Kruskal's) algorithm after modifying the graph to include only the needed nodes? But, I'm not sure how to modify the graph to do so while maintaining its connectedness.
For example, say we have a diamond shaped starting graph (with costs of links in brackets):
A
(2)/ \(1)
B C
(2)\ /(5)
D
Now, we arbitrarily decide that only nodes A and D are needed. If we started at A, we'd still want it to take the left path, because ((2 + 2) < (1 + 5)).
Say we modify the graph slightly:
A
(2)/ \(1) (2)
B C ------E
(2)\ /(5)
D
If we decide that only nodes A, D, and E are needed, we realize that the path with the minimum cost is not necessarily the one with the fewest links. Taking A--B--D and A--C--E costs 7, but A--C--D and C--E costs 8.
What you want to find is a discrete Steiner tree. When not all vertices in the graph are mandatory but the tree is allowed to split at the optional vertices, the problem is NP-hard.
Wikipedia says (linked above) of this problem: it is believed that arbitrarily good approximation ratios cannot in general be achieved in polynomial time. There is a polynomial-time algorithm that finds a factor 1.39 approximation of a minimum Steiner tree.