I have the following problem on my homework:
Give an O(n+m) algorithm to find that whether an edge e would be a part of the MST of a graph
(We are allowed to get help from others on this assignment, so this isn't cheating.)
I think that I could do a BFS and find if this edge is a edge between two layers and if so whether this edge was the smallest across those layers. But what could I say when this edge is not a tree edge of the BFS tree?
As a hint, if an edge is not the heaviest edge in any cycle that contains it, there is some MST that contains that edge. To see this, consider any MST. If the MST already contains the edge, great! We're done. If not, then add the edge into the MST. This creates a cycle in the graph. Now, find the heaviest edge in that cycle and remove it from the graph. Everything is now still connected (because if two nodes used to be connected by a path that went across that edge, now they can be connected by just going around the cycle the other way). Moreover, since the cost of the edge was deleted wasn't any smaller than the cost of the edge in question (because the edge isn't the heaviest edge in the cycle), the cost of this tree can't be any greater than before. Since we started with an MST, we must therefore end with an MST.
Using this property, see if you can find whether the edge is the heaviest edge on any cycle in linear time.
We will solve this using MST cycle property, which says that, "For any cycle C in the graph, if the weight of an edge e of C is larger than the weights of all other edges of C, then this edge cannot belong to an MST."
Now, run the following O(E+V) algorithm to test if the edge E connecting vertices u and v will be a part of some MST or not.
Step 1
Run dfs from one of the end-points(either u or v) of the edge E considering only those edges that have weight less than that of E.
Step 2
Case 1
If at the end of this dfs, the vertices u and v get connected, then edge E cannot be a part of some MST. This is because in this case there definitely exists a cycle in the graph with the edge E having the maximum weight and it cannot be a part of the MST(from the cycle property).
Case 2
But if at the end of the dfs u and v stay disconnected, then edge E must be the part of some MST as in this case E is never the maximum weight edge of the cycles that it is a part of.
Find if there are any paths that are cheaper than the current one (u,v) that creates a cycle to u and v. If yes, then (u,v) is not on the mst. Otherwise it is. This can be proved by the cut property and the cycle property.
Related
I'm trying to struct an efficient algorithm gets undirected graph, and edge e(u,v), and decides if the edge belongs to some cycle in the graph ,but not all of the cycles!
My approach is to take out the edge (u,v) from the graph, and run BFS to see if v is still reachable from u. If yes then the original graph has a cycle containing e, otherwise there isn't.
But i'm not so sure how to tweak the algorithm that it will decide if the edge doesn't belong to all the cycles of the graph.
An undirected graph can contain an edge which belongs to all of its cycles graph only if this graph has a single cycle.
Let's look at an example. Edge (2,3) belongs to two cycles, but you always can find a third cycle to which such an edge doesn't belong.
After you have checked that the edge belongs to some cycle, you can check if this is the only cycle in the graph by removing this edge and checking if the reduced graph has any cycles at all. Thanks to #nomanpouigt for pointing that out.
So I have an exercise that I should prove or disprove that:
1) if e is a minimum weight edge in the connected graph G such that not all edges are necessarily distinct, then every minimum spanning tree of G contains e
2) Same as 1) but now all edge weights are distinct.
Ok so intuitively, I understand that for 1) since not all edge weights are distinct, then it's possible that a vertex has the path with edge e but also another edge e_1 such that if weight(e) = weight (e_1) then there is a spanning tree which does not contain the edge e since the graph is connected. Otherwise if both e_1 and e are in the minimum spanning tree, then there is a cycle
and for 2) since all edge weights are distinct, then of course the minimum spanning tree will contain the edge e since any algorithm will always choose the smaller path.
Any suggestions on how to prove these two though? induction? Not sure how to approach.
Actually in your first proof when you say that if both e and e_1 are in G, then there's a cycle, that's not true, because they're minimal edges, so there doesn't have to be a cycle, and you do need to include them both into the MST, because if |E| > 1 and |V| > 2 then they both have to be there.
Anyways, a counter example for the first one is a complete graph with all edges of the same weight as e, the MST will contain only |V|-1 edges, but you didn't include all the other edges of that same weight, hence you have a contradiction.
As for the second one, if all edges are distinct, then if you remove the minimum edge and want to reconstruct the MST, the only way to go about this is to add a an edge connecting the 2 disjoint sets that were broken up by that minimum-weight edge.
Now suppose that you didn't remove that minimum-weight edge, and added that other edge, now you've created a cycle, and since all edges are distinct the cycle-creating edge will be greater than all of them, hence if you remove any former MST edge from that cycle, it will only increase the size of the MST. Which means that pretty much all former MST edges are critical when all edges have distinct weights.
Let's say there's Graph G such that it all its edges have weights that correspond to distinct integers. So no two edge has the same weight.
Let E be all the edges of G. Let emax be an edge in E with the maximum weight.
Another property of Graph G is that every edge e belongs to some cycle in G.
I have to prove that no minimum spanning tree of G contains the edge emax.
I can see why this is true, since all edges are distinct and every edge belongs to a cycle, the minimum spanning tree algorithm can simply choose the edge with lower weight in the cycle that contains emax.
But I'm not sure how to concretely prove it.
This is related to the Cycle Property of the Minimum Spanning Tree, which is basically saying that given a cycle in a graph the edge with the greatest weight does not belong in the MST (easily proven by contradiction in the link above). Thus since the edge emax belongs to a cycle it must not be in the MST.
Proof by contradiction works here. Suppose you have a minimum spanning tree including the maximum edge. If you remove that edge you have two components no longer connected from each other. Every vertex is in one component or the other. There is a cycle including the maximum edge. Start on a vertex at one side of the maximum edge and move along the cycle. Because you will eventually cycle round to the other side of the maximum edge in the other component you will find - before then - an edge which has one vertex in one of the disconnected components and one vertex in another of the disconnected components. Since the components are disconnected that edge is not in the minimum spanning tree. By adding it to the tree you reconnect the components and create a minimum spanning tree with smaller weight than you started with - so you original minimum spanning tree was not minimum.
Does a MST contain the maximum weight edge?
Sometimes, Yes.
It depends on the type of graph. If the edge with maximum weight is the only bridge that connects the components of a graph, then that edge must also be present in the MST.
CLRS - Chapter 22
Theorem 22.10
In a depth-first search of an undirected graph G, every edge of G is
either a tree edge or a back edge.
Proof Let (u,v) be an arbitrary edge of G, and suppose without loss of
generality that u.d < v.d. Then the search must discover and finish v
before it finishes u (while u is gray), since v is on u’s adjacency
list. If the first time that the search explores edge (u,v), it is in
the direction from u to v, then v is undiscovered (white) until that
time, for otherwise the search would have explored this edge already
in the direction from v to u. Thus, (u.v) becomes a tree edge. If the
search explores (u,v) first in the direction from v to u, then (u,v)
is a back edge, since u is still gray at the time the edge is first
explored.
I most certainly understand the proof; but not quite convinced with the idea of forward edges.
In the above image, there is a forward edge from the first vertex to the third vertex (first row). The first vertex is the source.
As I understand DFS(S) would include a forward vertex 1 -> 3. (I am obviously wrong, but I need somebody to set me straight!)
It looks like you didn't include the definition of "forward edge," so I'll start with the definition I learned.
Assuming u.d < v.d, DFS labels the edge (u,v) a forward edge if
when crossing the edge from u to v, v has already been marked as visited.
Because of that though, I claim that you cannot have forward edges in an undirected graph.
Assume for the sake of contradiction that it was possible. Therefore, the destination node is already marked as visited. Thus, DFS has already gone there and crossed all of the adjacent edges. In particular, you had to have already crossed that edge in the opposite direction. Thus, the edge has already been marked as a certain type of edge and thus won't be marked as a "forward edge".
Because of this, forward edges can only occur in directed graphs.
Now, just in case you mixed up "forward edges" and "tree edges", the edge you describe still isn't necessarily a tree edge. It is only a tree edge if when crossing, that was the first time you've visited the destination node. The easy way to think about it in undirected graphs is that when you traverse an edge, it is a back edge if the destination node has been reached already, and a tree edge otherwise.
I believe that what you are missing is some assumption about the order in which the algorithm would visit the different vertices.
Let's assume the algorithm visits the vertices in a lexicographic order. let's name the vertices this way:
-------
| |
S - A - B
| | |
C - D - E
In this case, the forward edges will be S->A, A->B, B->E, E->D, D->C. the rest of the edges are back edges.
Now let's rename the graph:
-------
| |
S - B - A
| | |
C - D - E
In this case, the forward edges will be S->A, A->B, B->D, D->C, D->E (note that S->A and S->B are not the same edge as in the previous example).
As you can see, the output depends on the order in which the algorithm selects the vertices. when the graph is anonymous, any output may be correct.
In the DFS tree of a general graph, there are TREE, FORWARD, BACK and CROSS edges.
In the DFS tree of an undirected graph, the would-be FORWARD edges are labeled as BACK edges.
The would-be CROSS edges are labeled as TREE edges.
In both cases, the reason is that the edges can be traversed in both directions, but you first encounter them as BACK and TREE and second time as FORWARD and maybe CROSS and they are already labeled.
In a sense, an edge is both FORWARD and BACK and can be both CROSS and TREE, but is first found as BACK and TREE, repectively.
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