Minimum of number of minimum spanning trees(MST) of complete graph - data-structures

What's minimum number of minimum spanning trees(MST) of complete graph with N vertex?

I believe that the answer is 1.
It is possible to construct a complete graph with n nodes that has exactly one MST. To do this, construct a graph with n nodes labeled 1, 2, 3, ..., n. Then, add an edge of cost 0 from 1 to 2, from 2 to 3, from 3 to 4, ..., from n - 1 to n, and add edges connecting every other pair of nodes that has cost 1. Clearly, picking all the zero-cost edges gives one possible spanning tree of this graph, and it's the minimum spanning tree because if any other choice of edges were picked, the cost would be at least 1. Moreover, this is the only MST in the graph that has cost 0, since if another set of edges were picked, that set would have to include at least one edge of cost at least 1, so the total MST would have cost at least 1.
Hope this helps!

Related

An undirected graph having n edges, then find out no. of vertices that graph have?

An undirected graph having 'n' number of edges, then find out number of vertices that graph have?‏‏‎
Since an edge is a connection between to vertices, the amount of vertices is at max 2n.
The amount of vertices is at minimum n+1. (This is pretty logical if you imagine that you have 2 edges - then you will at minimum have 3 vertices, because each edge must connect 2 vertices)
So if e = n, then n+1 <= v <= 2n
There is no exact formula for the number of vertices in terms of number of edges in the general case. However, if you take special cases, you can say more: if the graph is a tree, then the number of vertices is one more than the number of edges.

Give minimum permutation weight for edges such that a given set of edge is the Minimum Spanning Tree

Question:
Given a graph of N nodes and M edges, the edges are indexed from 1 -> M. It is guaranteed that there's a path between any 2 nodes.
You need to assign weights for M edges. The weights are in the range of [1...M], and each number can only occur once.
To be shorted, the answer should be a permutation array of [1...M], in which arr[i] = x means edge[i] has the weight of x.
You are given a set R of n-1 edges. R is guaranteed to be a Spanning Tree of the graph.
Find a way to assign weights so that R is the Minimum Spanning Tree of the graph, if there are multiple answers, print the one with minimum lexicographical order.
Contraints:
N, M <= 10^6
Example:
Edges:
3 4
1 2
2 3
1 3
1 4
R = [2, 4, 5]
Answer: 3 4 5 1 2
Explaination:
If you assign weights for the graph like the above image, the MST would be the set R, and it has the smallest lexicographical order.
My take with O(N^2):
Since it asks for the minimum lexicographical order, I traverse through the list of edges, assigning the weights in an increasing order. Intially, w = 1. There can be 3 situations:
If edge[i] is in R, assign weight[i] = w, increase w by 1
If edge[i] is not in R: say edge[i] connect nodes u and v. assign weight and increase w for each edge in the path from u to v in R (if that edge is not assigned yet). Then assign weight and increase w for edge[i]
If edge[i] is assigned, skip it
Is there any way to improve my solution so that it can work in O(N.logN) or less?
Yes, there's an O(m log m)-time algorithm.
The fundamental cycle of a non-tree edge e is comprised of e and the path in the tree between the endpoints of e. Given weights, the spanning tree is minimum if and only if, for every non-tree edge e, the heaviest edge in the fundamental cycle of e is e itself.
The lexicographic objective lends itself to a greedy algorithm, where we find the least valid assignment for edge 1, then edge 2 given edge 1, then edge 3 given the previous edges, etc. Here's the core idea: if the next unassigned edge is a non-tree edge, assign the next numbers to the unassigned tree edges in its fundamental cycle; then assign the next number.
In the example, edge 3-4 is first, and edges 1-3 and 1-4 complete its fundamental cycle. Therefore we assign 1-3 → 1 and 1-4 → 2 and then 3-4 → 3. Next is 1-2, a tree edge, so 1-2 → 4. Finally, 2-3 → 5 (1-2 and 1-3 are already assigned).
To implement this efficiently, we need two ingredients: a way to enumerate the unassigned edges in a fundamental cycle, and a way to assign numbers. My proposal for the former would be to store the spanning tree with the assigned edges contracted. We don't need anything fancy; start by rooting the spanning tree somewhere and running depth-first search to record parent pointers and depths. The fundamental cycle of e will be given by the paths to the least common ancestor of the endpoints of e. To do the contraction, we add a Boolean field indicating whether the parent edge is contracted, then use the path compression trick from disjoint-set forests. The work will be O(m log m) worst case, but O(m) average case. I think there's a strong possibility that the offline least common ancestor algorithms can be plugged in here to get the worst case down to O(m).
As for number assignment, we can handle this in linear time. For each edge, record the index of the edge that caused it to be assigned. At the end, stably bucket sort by this index, breaking ties by putting tree edges before non-tree. This can be done in O(m) time.

Specific Graph and some claims on shortest path?

I stuck in one challenging question, I read on my notes.
an Undirected, Weighted and Connected Graph G, (without negative weight and all weights are distinct) is given, we know in this graph the shortest path between any two vertexes is on Minimum Spanning Tree (MST). (for any pair of vertices and for any shortest path between them, it lies on MST). Which of The following Is True?
1) Graph G is a Tree.
2) weight of each {u,v} edge, at least is equal (same) to heaviest edge in shortest path from u to v.
3) shortest path between any two vertex u, v is unique.
4) suppose start from vertex s, Prime (for calculating MST) and Dijkstra (for calculating shortest path), process and add the
vertexes into their Trees, with the same order. (two algorithm works with same order in processing and adding node)
How can verify me these options? This is a challenging question.
No. For example: V = {1, 2, 3}, E = {(1, 2, 1), (2, 3, 2), (1, 3, 4)}(each edge is encoded as a tuple (one vertex, another vertex, weight)). It is not a tree, but all shortest path are on the minimum spanning tree.
Yes. If the weight of this edge is less than the weight of the heaviest edge which is in the shortest path, this edge is shorter than the shortest path(because there are no edges with negative weight). Thus, the shortest path is not the shortest. It is a contradiction.
No*. Let's assume that we have a graph with two vertices {1, 2} and one edge between them with zero weight. There are infinitely many shortest paths between the first and the second vertex([1, 2], [1, 2, 1, 2], ...)
*However, there is a unique simple shortest path between any two vertices because there is only one simple path between any two vertices in a tree, any path which does not fully lie in a minimum spanning tree is longer due to the problem statement and there is only one minimum spanning tree in a graph with distinct edges weights.
No. Consider this tree: V = {1, 2, 3, 4}, E = {(1, 2, 3), (2, 3, 2), (1, 4, 4)}. Let's assume that the start vertex is 1. Prim's algorithm will take the first vertex, than the second one, than the third one and only after this the fourth. But Dijkstra's algorithm will take the fourth vertex before the third one. It happens because the third vertex is located closer to the tree after processing the first two vertices, but it the total distance to it from the start node is larger.
I do not want to give the complete answer, but here is how you approach it:
Can you add edge[s] to the tree such that it is not a tree anymore and still the tree contains all the shortest paths?
What happens if there is an edge that is shorter than the heaviest edge?
it is confusing because the problem says "the shortest path between any two vertexes is on MST", but doesn't address the fact that there might be multiple shortest paths. So you can probably assume that "at least one shortest path lies on the tree". In which case just connect two vertices with an edge whose weight equals to cost through the MST, and you get the answer.
Again, you should start with what happens if the vertices are not added in the same order

Graph Algorithms

What is the maximum and minimum number of edges to be considered in krushkal's algorithm with an example for both cases.
What I thought was since the Krushkal's algorithm is for finding minimum spanning tree the maximum number of edges is (V-1) where V is the number of vertices. Adding one more edge would result in a cycle in the graph. How can we obtain at a minimum value ?
Kruskal's algorithm stops when you've added V - 1 edges to your MST, so this is the minimum that have to be considered. This happens when the lowest value V - 1 edges of your graph do not form a cycle, and they will be added one after the other by the algorithm, after which it will stop.
For example, consider a complete graph with edges with cost 1, which is minimum in the graph, between node 1 and every other node. Make all the other edges have cost 2.
The worst case is when you must inspect every edge (of which there are O(V^2)) until you finally select V - 1. This means that you have to force a lot of cycles to be created before the last edge is added.
Consider a complete graph again. Have the V - 2 edges between node 1 and V - 2 nodes have cost 1, which is minimum in the graph. These will be selected first. Now let node k be the one that is not part of a selected edge, so that is left out of the graph. Have the edge between node k and node 1 have the largest cost. This will cause it to be inspected and added to the MST last, forcing the algorithm to inspect all O(V^2) edges before building the MST.
Remember the Kruskal's algorithm processes edges in increasing order of their cost, rejecting edges that would form a cycle if added to the MST we are building.
A tree of N vertices always has N-1 edges. Consequently you have to consider at least N-1 edges during Kruskal's algorithm. An example may be a graph which is a tree.

Modified breadth-first search on a graph with edge weights of 2, 3 or 5

Suppose that we are given a directed graph H = (V, E). For each edge e, the weight of the edge, w(e) is either 2, 3 or 5. Modify the BFS so that it will compute the length of the shortest path from a single source vertex s. Explain why your algorithm is correct and determine its worst-case running time (You may assume that H is represented via an adjacency list).
How would you go about this? What makes the specific weight edges different from just any?
You can consider imaginary nodes between the edges. So if between 2 nodes there is an edge of length 2. You make an intermediary node and add edges of length 1 between them. Then use the normal breadth first search. (You also need to do this for nodes of length 3 and 5, adding 2 and 4 nodes). Since you only add a O(E) nodes it's the same complexity.

Resources