Is there a minimum spanning tree that does not contain the min/max weighted edge? - algorithm

If we have an (arbitrary) connected undirected graph G, whose edges have distinct weights,
does every MST of G contains the minimum weighted edge?
is there an MST of G that does not contain the maximum weighted edge?
Also, I'm more thankful if someone can give a hint of the key things one must keep in mind when dealing with such MST questions.
This is a homework problem. Thanks.

is there an MST of G that does not contain the maximum weighted edge?
There may be, but there doesn't have to be. Consider a 4-vertex graph as follows:
[A]--{2}--[B]
| |
| |
{1} {3}
| |
| |
[C]-{50}--[D]
The minimum spanning tree consists of the edge set {CA, AB, BD}. The maximum edge weight is 50, along {CD}, but it's not part of the MST. But if G were already equal to its own MST, then obviously it would contain its own maximum edge.
does every MST of G contains the minimum weighted edge?
Yes. MSTs have a cut property. A cut is simply a partition of the vertices of the graph into two disjoint sets. For any cut you can make, if the weight of an edge in that cut is smaller than the weights of the other edges in the cut, then this edge belongs to all MSTs in the graph. Because you guaranteed that the edge weights are distinct, you have also guaranteed that there is an edge which is smaller than all other edges.
Also, I'm more thankful if someone can give a hint of the key things one must keep in mind when dealing with such MST questions.
Your best bet is to reason about things using the properties of MSTs in general, and to try to construct specific counterexamples which you think will prove your case. I gave an instance of each line of reasoning above. Because of the cut and cycle properties, you can always determine exactly which edges are in an MST, so you can systematically test each edge to determine whether or not it's in the MST.

Does every MST of G contains the minimum weighted edge?
Yes. Lets assume we have a MST which does not contain the min weight edge. Now the inclusion of this edge to the MST will result in a cycle. Now there will always be another edge in the cycle which can be removed to remove the cycle and still maintain the graph(MST) connected.
Is there an MST of G that does not contain the maximum weighted edge?
Depends on the graph. If the graph itself is a tree then we need to include all of its n-1 edges in the MST, so the max weight edge cannot be excluded. Also if the max weight edge is a cut-edge so that its exclusion will never result in connectivity, then the max weight edge cannot be excluded. But if the max weight edge is a part of a cycle then it is possible to exclude from the MST.

For your first question the answer is no, and kruskal's algorithm proves it. It will always select the minimum cost edge.
For the second question the answer is yes, and it's trivial to find an example graph:
1 - 2 (cost 10)
2 - 3 (cost 100)
3 - 1 (cost 1000)
The third edge will never be selected as it introduces a cycle. So basically, if the edge with the maximum cost would create a cycle if inserted in the MST, it won't be inserted.

I see you too are studying for CSC263 through the 2009 test? (Same here!)
Another way to see that the minimum is always in the MST is to look simply at this minimum edge (call it e):
e
v1 ---------------- v2
(Assume this has connections to other verticies). Now, for e NOT to be included in the final MST means at one point we have, without loss of generality, v1 in the MST but not v2. However, the only way to add v2 without adding e would be to say that the addition of v1 didn't add e to the queue (because by definition, e would be at the top of the queue because it has lowest priority) but this contradicts the MST construction theorem.
So essentially, it is impossible to have an edge with minimum weight not get to the queue which means that any MST constructed would have it.

Related

Which of the following options are correct with respect to a MST?

I'm taking the Algorithms: Design and Analysis II course, and one of the questions is as follows:
Consider a connected undirected graph with distinct edge costs. Which
of the following are true? [Check all that apply.]
Suppose the edge 𝑒 is not the cheapest edge that crosses the cut (𝐴,𝐵). Then 𝑒 does not belong to any minimum spanning tree.
Suppose the edge 𝑒 is the most expensive edge contained in the cycle 𝐶. Then 𝑒 does not belong to any minimum spanning tree.
The minimum spanning tree is unique.
Suppose the edge 𝑒 is the cheapest edge that crosses the cut (𝐴,𝐵). Then 𝑒 belongs to every minimum spanning tree.
To my knowledge, all four options are correct. Options 1, 2 and 4 follow from the Cut property; option 3 is correct because edge weights are distinct. However, including option 1 is turning out to be wrong. Why?
No
Yes
Yes
Yes
The main part here is to answer #3. For a graph with all distinct edge costs that is true. Answers for all other questions you can derive using answer to the third one.
For #1:
A1 --- B1
|
A2 --- B2
Suppose w(A1,B1) > w(A2,B2), but you still need to include both of them into MST.
First of all lets look at the mst definition.MST is a subset of a connected undirected graph with distinct edge costs that connects all the vertices together, without any cycles and with the minimum possible total edge weight.
1.If the edge e is the only way to traverse A to B without causing a cycle it may belong to a mst.
2.If there is a cycle C then we can't talk about mst it will be a closed path.That is the definition of the cycle.
3.If each edge has a distinct cost as you mentioned then there will be only one, unique minimum spanning tree.
4.It may not because it can cause a loop like cycle or circuit then we don't use that edge to traverse A to B

Finding MST such that a specific vertex has a minimum degree

Given undirected, connected graph G={V,E}, a vertex in V(G), label him v, and a weight function f:E->R+(Positive real numbers), I need to find a MST such that v's degree is minimal. I've already noticed that if all the edges has unique weight, the MST is unique, so I believe it has something to do with repetitive weights on edges. I though about running Kruskal's algorithm, but when sorting the edges, I'll always consider edges that occur on v last. For example, if (a,b),(c,d),(v,e) are the only edges of weight k, so the possible permutations of these edges in the sorted edges array are: {(a,b),(c,d),(v,e)} or {(c,d),(a,b),(v,e)}. I've ran this variation over several graphs and it seems to work, but I couldn't prove it. Does anyone know how to prove the algorithm's correct (Meaning proving v's degree is minimal), or give a contrary example of the algorithm failing?
First note that Kruskal's algorithm can be applied to any weighted graph, whether or not it is connected. In general it results in a minimum-weight spanning forest (MSF), with one MST for each connected component. To prove that your modification of Kruskal's algorithm succeeds in finding the MST for which v has minimal degree, it helps to prove the slightly stronger result that if you apply your algorithm to a possibly disconnected graph then it succeeds in finding the MSF where the degree of v is minimized.
The proof is by induction on the number, k, of distinct weights.
Basis Case (k = 1). In this case weights can be ignored and we are trying to find a spanning forest in which the degree of v is minimized. In this case, your algorithm can be described as follows: pick edges for as long as possible according to the following two rules:
1) No selected edge forms a cycle with previously selected edges
2) An edge involving v isn't selected unless any edge which doesn't
involve v violates rule 1.
Let G' denote the graph from which v and all incident edges have been removed from G. It is easy to see that the algorithm in this special case works as follows. It starts by creating a spanning forest for G'. Then it takes those trees in the forest that are contained in v's connected component in the original graph G and connects each component to v by a single edge. Since the components connected to v in the second stage can be connected to each other in no other way (since if any connecting edge not involving v exists it would have been selected by rule 2) it is easy to see that the degree of v is minimal.
Inductive Case: Suppose that the result is true for k and G is a weighted graph with k+1 distinct weights and v is a specified vertex in G. Sort the distinct weights in increasing order (so that weight k+1 is the longest of the distinct weights -- say w_{k+1}). Let G' be the sub-graph of G with the same vertex set but with all edges of weight w_{k+1} removed. Since the edges are sorted in the order of increasing weight, note that the modified Kruskal's algorithm in effect starts by applying itself to G'. Thus -- by the induction hypothesis prior to considering edges of weight w_{k+1}, the algorithm has succeeded in constructing an MSF F' of G' for which the degree, d' of v in G' is minimized.
As a final step, modified Kruskal's applied to the overall graph G will merge certain of the trees in F' together by adding edges of weight w_{k+1}. One way to conceptualize the final step is the think of F' as a graph where two trees are connected exactly when there is an edge of weight w_{k+1} from some node in the first tree to some node in the second tree. We have (almost) the basis case with F'. Modified Kruskal's will add edged of weight w_{k+1} until it can't do so anymore -- and won't add an edge connecting to v unless there is no other way to connect to trees in F' that need to be connected to get a spanning forest for the original graph G.
The final degree of v in the resulting MSF is d = d'+d" where d" is the number of edges of weight w_{k+1} added at the final step. Neither d' nor d" can be made any smaller, hence it follows that d can't be made any smaller (since the degree of v in any spanning forest can be written as the sum of the number of edges whose weight is less than w_{k+1} coming into v and the number off edges of weight w_{k+1} coming into v).
QED.
There is still an element of hand-waving in this, especially with the final step -- but Stack Overflow isn't a peer-reviewed journal. Anyway, the overall logic should be clear enough.
One final remark -- it seems fairly clear that Prim's algorithm can be similarly modified for this problem. Have you looked into that?

Graph Has Two / Three Different Minimal Spanning Trees ?

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.

MST with modification

Can anyone think of a way to modify Kruskal's algorithm for a minimum spanning tree so that it must include a certain edge (u,v)?
I might be confusing, but as far as I remember, kruskal can handle negative weights,so you can give this edge -infinity weight.
Of course it won't actually be -infinity, but a number low enough
to be significant enough that it cannot be ignored, something like -1 * sigma(|weight(e)|) for each e in E.
If you can modify the graph structure, you could remove vertices u and v, and replace them with a new vertex w that has the edges u and v used to have. In the case of duplicate edges, pick the one with the smallest weight.
Provided that we know the edge weight of (u,v), we can also simply add it to the front of our sorted list of edge weights (as Kruskal sorts the edge weights in increasing order). In this case, the edge (u,v) will be the first edge included in our tree and Kruskal will run normally, finding the spanning tree of minimum weight with (u,v).

Second min cost spanning tree

I'm writing an algorithm for finding the second min cost spanning tree. my idea was as follows:
Use kruskals to find lowest MST.
Delete the lowest cost edge of the MST.
Run kruskals again on the entire graph.
return the new MST.
My question is: Will this work? Is there a better way perhaps to do this?
You can do it in O(V2). First compute the MST using Prim's algorithm (can be done in O(V2)).
Compute max[u, v] = the cost of the maximum cost edge on the (unique) path from u to v in the MST. Can be done in O(V2).
Find an edge (u, v) that's NOT part of the MST that minimizes abs(max[u, v] - weight(u, v)). Can be done in O(E) == O(V2).
Return MST' = MST - {the edge that has max[u, v] weight} + {(u, v)}, which will give you the second best MST.
Here's a link to pseudocode and more detailed explanations.
Consider this case:
------100----
| |
A--1--B--3--C
| |
| 3
| |
2-----D
The MST consists of A-B-D-C (cost 6). The second min cost is A-B-C-D (cost 7). If you delete the lowest cost edge, you will get A-C-B-D (cost 105) instead.
So your idea will not work. I have no better idea though...
You can do this -- try removing the edges of the MST, one at a time from the graph, and run the MST, taking the min from it. So this is similar to yours, except for iterative:
Use Kruskals to find MST.
For each edge in MST:
Remove edge from graph
Calculate MST' on MST
Keep track of smallest MST
Add edge back to graph
Return the smallest MST.
This is similar to Larry's answer.
After finding MST,
For each new_edge =not a edge in MST
Add new_edge to MST.
Find the cycle that is formed.
Find the edge with maximum weight in
cycle that is not the non-MST edge
you added.
Record the weight increase as W_Inc
= w(new_edge) - w(max_weight_edge_in_cycle).
If W_Inc < Min_W_Inc_Seen_So_Far Then
Min_W_Inc_Seen_So_Far = W_Inc
edge_to_add = new_edge
edge_to_remove = max_weight_edge_in_cycle
Solution from following link.
http://web.mit.edu/6.263/www/quiz1-f05-sol.pdf
slight edit to your algo.
Use kruskals to find lowest MST.
for all edges i of MST
Delete edge i of the MST.
Run kruskals again on the entire graph.
loss=cost new edge introduced - cost of edge i
return MST for which loss is minimum
Here is an algorithm which compute the 2nd minimum spanning tree in O(n^2)
First find out the mimimum spanning tree (T). It will take O(n^2) without using heap.
Repeat for every edge e in T. =O(n^2)
Lets say current tree edge is e. This tree edge will divide the tree into two trees, lets say T1 and T-T1. e=(u,v) where u is in T1 and v is in T-T1. =O(n^2)
Repeat for every vertex v in T-T1. =O(n^2)
Select edge e'=(u,v) for all v in T-T1 and e' is in G (original graph) and it is minimum
Calculate the weight of newly formed tree. Lets say W=weight(T)-weight(e)+weight(e')
Select the one T1 which has a minimum weight
Your approach will not work, as it might be the case that min. weight edge in the MST is a bridge (only one edge connecting 2 parts of graph) so deleting this edge from the set will result in 2 new MST as compared to one MST.
based on #IVlad's answer
Detailed explanation of the O(V² log V) algorithm
Find the minimum spanning tree (MST) using Kruskal's (or Prim's) algorithm, save its total weight, and for every node in the MST store its tree neighbors (i.e. the parent and all children) -> O(V² log V)
Compute the maximum edge weight between any two vertices in the minimum spanning tree. Starting from every vertex in the MST, traverse the entire tree with a depth- or breadth-first search by using the tree node neighbor lists computed earlier and store the maximum edge weight encountered so far at every new vertex visited. -> O(V²)
Find the second minimum spanning tree and its total weight. For every edge not belonging to the original MST, try disconnecting the two vertices it connects by removing the tree edge with the maximum weight in between the two vertices, and then reconnecting them with the currently considered vertex (note: the MST should be restored to its original state after every iteration). The total weight can be calculated by subtracting the weight of the removed edge and adding that of the added one. Store the minimum of the total weights obtained.
To practice you could try the competitive programming problem UVa 10600 - ACM Contest and Blackout, which involves finding the second minimum spanning tree in a weighted graph, as asked by the OP. My implementation (in modern C++) can be found here.
MST is a tree which has the minimum weight total of all edges of the graph. Thus, 2nd minimum mst will have the 2nd minimum total weight of all edges in the graph.
let T -> BEST_MST ( sort the edges in the graph , then find MST using kruskal algorithm)
T ' -> 2nd best MST
let's say T has 7 edges , now to find T ' we will one by one remove one of those 7 edges and find a replacement for that edge ( cost of that edge definitely will be greater than the edge we just removed from T ).
let's say original graph has 15 edges
our best MST ( T ) has 7 edges
and 2nd best MST ( T ' ) will also going to have 7 edges only
How to find T '
there are 7 edges in T , now for all those 7 edges remove them one by one and find replacement for those edges .
let's say edges in MST ( T ) --> { a,b,c,d,e,f,g }
let's say our answer will be 2nd_BEST_MST and initially it has infinte value ( i know it doesn't sounds good , let's just assume it for now ).
for all edges in BEST_MST :
current_edge = i
find replacement for that edge, replacement for that edge will definitely going to have have weight more than the ith edge ( one of 7 edges )
how we will going to find the replacement for that edge , using Kruskul algorithm ( we are finding the MST again , so we will use kruskal algorithm only , but this we don't have to sort edges again , because we did it when we were finding the BEST_MST ( T ).
NEW_MST will be generated
2nd_best_MST = min( NEW_MST , 2nd_best_MST )
return 2nd_best_MST
ALGORITHM
let' say orignal graph has 10 edges
find the BEST_MST ( using kruskal algo) and assume BEST_MST has only 6 edges
bow there are 4 edges remaining which is not in the BEST_MST ( because their weight value is large and one of those edges will give us our 2nd_Best_MST
for each edge 'X' not present in the BEST_MST ( i.e. 4 edges left ) add that edge in out BEST_MST which will create the cycle
find the edge 'K' with the maximum weight in the cycle ( other than newly_added_edge 'X' )
remove edge 'K' temporarily which will form a new spanning tree
calculate the difference in weight and map the weight_difference with the edge 'X' .
repeat step 4 for all those 4 edges and return the spanning tree with the smallest weight difference to the BEST_MST.

Resources