Let's motivate our solution with some examples. Suppose n = 5, m = 10 and k = 1. Then any solution that contains the following graph will work (oh, I'm going to number my nodes from 0 to n-1, and then convert 0 to n at the end. It makes life easier.)
Now, suppose instead that n = 5, m = 10 and k = 2. Then any solution that contains the following graph will work:
Those are two extreme cases, and they form the basis for our answer. We'll work in rounds, and instead of dealing with individual nodes, we'll deal with connected components. We are going to add edges to our system in increasing order of weight. When we're done, if we have edges leftover, we'll simply add them randomly to any pair of nodes that don't have edges already.
If our round number is equal to k, then we connect the components as in the first example above. That way, Boruvka's algorithm will complete in that step.
If the round number is less than k, then we try to delay the completion of the algorithm as much as we can. We can do that by reducing the number of components by a factor of two: For each even number i we add an edge from component i to component i+1.
If we end up with one component and k is still greater than the round number, then it is impossible, and we return { -1 }.
Here's an example. Suppose n=18, m=22 and k=3. Here is what the graph looks like after round 1:
And round 2:
We need to finish up in round three, so we connect all of the components with edges in consecutive order:
Finally, we need to add 5 random edges to the system, so we do so, and we rename node 0 to node 18. We're done!
Three comments with respect to implementing this: