Here is an exercise:
Either prove the following or give a counterexample:
(a) Is the path between a pair of vertices in a minimum spanning tree
of an undirected graph necessarily the shortest (minimum weight) path?
(b) Suppose that the minimum spanning tree of the graph is unique. Is
the path between a pair of vertices in a minimum spanning tree of an
undirected graph necessarily the shortest (minimum weight) path?
My Answer is
(a)
No, for example, for graph 0, 1, 2, 0-1 is 4, 1-2 is 2, 2-0 is 5, then 0-2’s true shortest path is 5, but the mst is 0-1-2, in mst, 0-2 is 6
(b)
My problem comes into this (b).
I don't understand how whether the MST is unique can affect the shortest path.
First, my understanding is that when the weights of edges are not distinct, multiple MST may exist at the same time, right?
Second, even if MST is unique, the answer of (a) above still applies for (b), right?
So lets take a look at a very simple graph:
(A)---2----(B)----2---(C)
\ /
---------3----------
The minimum spanning tree for this graph consists of the two edges A-B and B-C. No other set of edges form a minimum spanning tree.
But of course, the shortest path from A to C is A-C, which does not exist in the MST.
EDIT
So to answer part (b) the answer is no, because there is a shorter path that exists that is not in the MST.
Regarding (a), I agree.
Regarding (b), for some graphs, there may be more minimal spanning trees with the same weight. Consider a circle C3 with vertices a,b,c; weights a->b=1, b->c=2, a->c=2. This graph has two MSTs, {a-b-c} and {c-a-b}.
Nevertheless, your counterexample still holds, because the MST is unique there.
AD a)
a very simple visualization would be:
MSP in a graph:
Shortest Path between A and C:
AD b)
I guess that the uniqueness of MSP means that there is only 1 MSP MSP in a graph. So:
First) Yes, if the weights of edges are not distinct, multiple MST may exist at the same time. In case of our graph, another possible MSP would include arc D-C instead of A-B (as an example).
Second) The uniqueness of MST does not influence the answer for a). As an example:
Isn't the MST related to the start node?!
Then he is trying to get the shortest path from the MST start node. Therefore, yes, the shortest path is given by the MST starting from A!
Related
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 :)
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.
This is not a homework. I am trying to do exercises from a textbook to understand MST (minimum spanning tree).
Suppose I have a cycle C in a weighted undirected graph G. As I understand, the following is correct:
The heaviest edge in C belongs to no MST of G. That is, there is no MST of G, which contains that edge.
The lightest edge in C belongs to some MST of G. That is, there is an MST of G, which contains that edge.
Now I wonder if the followings claims are correct too.
The lightest edge in C belongs to all MST of G. That is, there is no MST of G, which does not contain that edge.
Any edge in C except the heaviest one belongs to some MST. That is, for each edge in C except the heaviest one there is an MST, which contains that edge.
Could you prove the last claim?
Even for the first claim if there are multiple edges which are lightest, all need not be included in the MST.
The first one of your claims is always true. The lightest edge is on the MST for any graph.
The second one is not always true. It is always true if the entire graph is a
cycle and thus every node has 2 edges incident to it. However, in the general case,
an edge (u,v) of weight k is never on MST whenever there is a path between the nodes u and v
connecting them at a total weight less than k.
I don't think your claims are valid. The problem is that you are only considering a cycle in a larger graph.
Consider for example a graph G consisting of 6 nodes in a cycle (with random weights >1). Your claims might hold for that graph but now add 1 node in the center of the graph and connect it with 6 links of cost 1. The MST of your entire graph now will consist of only those 6 edges (which form a star).
If you now look at your claims, you'll see:
The lightest edge in your cycle does not belong to the MST (=star)
None of the edges in the cycle are in the MST
Suppose I have a weighted non-directed graph G = (V,E). Each vertex has a list of elements.
We start in a vertex root and start looking for all occurances of elements with a value x. We wish to travel the least amount of distance (in terms of edge weight) to uncover all occurances of elements with value x.
The way I think of it, a MST will contain all vertices (and hence all vertices that satisfy our condition). Therefore the algorithm to uncover all occurances can just be done by finding the shortest path from root to all other vertices (this will be done on the MST of course).
Edit :
As Louis pointed out, the MST will not work in all cases if the root is chosen arbitrarily. However, to make things clear, the root is part of the input and therefore there will be one and only one MST possible (given that the edges have distinct weights). This spanning tree will indeed have all minimum-cost paths to all other vertices in the graph starting from the root.
I don't think this will work. Consider the following example:
x
|
3
|
y--3--root
| /
5 3
| /
| /
x
The minimum spanning tree contains all three edges with weight 3, but this is clearly not the optimum solution.
If I understand the problem correctly, you want to find the minimum-weight tree in the graph which includes all vertices labeled x. (That is, the correct answer would have total weight 8, and would be the two edges drawn vertically in this drawing.) But this does not include your arbitrarily selected root at all.
I am pretty confident that the following variation on Prim's algorithm would work. Not sure if it's optimal, though.
Let's say the label we are looking for is called L.
Use an all-pairs shortest path algorithm to compute d(v, w) for all v, w.
Pick some node labeled L; call this the root. (We can be sure that this will be in the result tree, since we are including all nodes labeled L.)
Initialize a priority queue with the root initialized to 0. (The priority queue will consist of vertices labeled L, and their minimum distance from any node in the tree, including vertices not labeled L.)
While the priority queue is nonempty, do the following:
Pick out the top vertex in the queue; call it v, and its distance from the tree d.
For each vertex w on the path from v to the tree, v inclusive, find the nearest L-labeled node x to w, and add x to the priority queue, or update its priority. Add w to the tree.
The answer is no, if I'm understanding correctly. Finding the minimum spanning tree will contain all vertices V, but you only want to find the vertices with value x. Thus, your MST result may have unneeded vertices adding extra path length and therefore be sub-optimal.
An example has been given where the MST M1 from Root differs from an MST M2 containing all x nodes but not containing Root.
Here's an example where Root is in both MST's: Let graph G contain nodes R,S,T,U,V (R=Root), and a clockwise path R-S-T-U-V-R, with edge weights 1,1,3,2,2 going clockwise, and x at R, S, T, U. The first MST, M1, will have subtrees S-T and V-U below R, with cost 6 = 2+4, and cost-3 edge T-U not included in M1. But M2 has subtree S-T-U (only) below R, at cost 5.
Negative. If the idea is to find for every node that contains 'x' a separate path from root to it, and minimize the total cost of the paths, then you can just use simple shortest-path calculation separately for every node starting from the root, and put the paths together.
Some of those shortest paths will not be in the minimum spanning tree, so if this is your goal, the MST solution does not work. MST optimizes the cost of the tree, not the sum of costs of paths from root to the nodes.
If your idea is to find one path that starts from root and traverses through all nodes that contain 'x', then this is the traveling salesman problem and it is an NP-complete optimization problem, i.e. very hard.
Is there a graph algorithm that given a start(v) and an end(u) will find a shortest path through the given set of edges, but if u is a disconnected vertex, it will also determine the shortest path to add missing edges until u is no longer disconnected?
I have a pixel matrix where lines are made of 255's(black) and 0's(white). lines(255) can have breaks or spurs and I must get rid of both. I could have a pixel matrix forest with say 7 or so trees of black pixels. I need to find the true end points of each tree, find the single shortest path of each tree, then union all skew trees together to form 1 single line(ie, a single shortest path from the furthest 2 end points in the original matrix). all edge weights could be considered 1.
Thanks
How about running Dijkstra's algorithm and if disconnected, connect v and u? What's your criteria for "best place to add a missing edge?" Do edges have weights (like distance)?
Edit:
For one idea of "the best place," you could try the path that has minimal sum of shortest paths between all connected pairs. Floyd–Warshall algorithm can be used to find shortest paths between all pairs. So, run Floyd-Warshall for each node in v's tree and u.
Your problem isn't well defined for disconnected graphs. I can always add and edge between v and u.
If you meant that given an acyclic undirected disconnected graph, actually known as a forest, and given a subset of edges as a subgraph, can you find the shortest path between vertices, than this problem is trivial since if there is a path in the full graph, there is one path only.
If this is a general graph G, and you are talking about a forest subgraph G', than we need more info. Is this weighted? Is it only positive weights? If it is unweighted, do some variant of Dijksta. Define the diameter of a tree to be the length of the longest path between two leaves (this is well defined in a tree, since ther is only one such path). Let S be the sum of the diameters of all the trees in G', then set the weight all edges out of G' to 2S, then Dijkstra's algorithm will automatically prefer to step through G', stepping outside G' only when there is no choice, since walking through G' would always be cheaper.
Here you have following scenarios:
case:1. (all edges>0)
Negate all edges
find the longest path in graph using this:
https://www.geeksforgeeks.org/longest-path-undirected-tree/
negate the final result
case2: edges might be or might not be negative
Read Floyd–Warshall algorithm for this