1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
FromTheMoon [43]
4 years ago
9

Given n ropes of different lengths, we need to connect these ropes into one rope. We can connect only 2 ropes at a time. The cos

t required to connect 2 ropes is equal to sum of their lengths. The length of this connected rope is also equal to the sum of their lengths. This process is repeated until n ropes are connected into a single rope.
Find the min possible cost required to connect all ropes.
For example: Suppose you have three ropes with lengths 2, 5, and 8. If you chose first to connect the length 5 and 8 ropes, then connect the length 2 and 13 ropes, the total cost would be (5 + 8) + (13 + 2) = 28. However, if you first chose to connect the length 2 and 5 ropes, then the length 7 and 8 ropes, the total cost would be (2 + 5) + (7 + 8) = 22 (which happens to be optimal).

(a) Specify with pseudo-code a greedy algorithm to connect the ropes with minimum cost.
(b) Prove your algorithm always finds the least cost solution if the lengths of the ropes are distinct.
(c) Analyze your algorithm's complexity.
Computers and Technology
1 answer:
Lelu [443]4 years ago
4 0

Answer:

Given n ropes of different length, the task is to connect all the ropes into one. We need to connect these ropes in minimum cost. The cost of connecting two ropes is the sum of their lengths.

Eg: Given 4 ropes of length { 3, 7, 9, 4 }

We can connect the ropes in the following way:

1) First, connect the ropes of length 3 and 4. Cost of connecting these ropes is 3 + 4 = 7. Now we have ropes of length { 7, 7, 9 }

2) Then, connect the ropes with length 7 and 7. Now the cost of connecting these ropes is 7 + 7 = 14. Now we have ropes of length { 9, 14 }

3) Finally, connect the two last ropes with cost 9 + 14 = 23.

So, Total Cost of connecting these ropes in the above order is 7 + 14 + 23 = 44.

Approach

Given an array rope[] of length n, where rope[i] is the length of ith rope.

First of all, build minHeap from the given array of rope length, because we want to extract ropes with the minimum length of all in every iteration.

In each iteration:

1) Extract two ropes with minimum length from the minHeap.

2) add both the ropes (cost).

3) add the cost to the result and push cost again in the minHeap.

The main reason for selecting ropes with minimum length in each iteration is: The value that is picked first will be included in the final result more than once. Since we want the minimum cost of connecting ropes, we will pick long length ropes at a later stage to minimize its impact on our final cost.

Time Complexity

The complexity of insertion in minHeap is O(logn). Since we are building heap for n ropes, the overall complexity of building minHeap is O(nlogn).

Then in the iteration, we are extracting minimum value from minHeap and pushing the cost of connecting two ropes back to the minHeap. Extract/Insert operation from minHeap costs O(logn).

So, the Overall time complexity of the problem is O(nlogn).

Space Complexity

We are using space for storing minHeap of the ropes which is equal to the number of ropes we have. So Space complexity of the problem is O(n).

Explanation:

Code is:

int main() {

int t;

scanf("%d", &t);

while(t--) {

    int count, first, second;

    int result = 0;

    scanf("%d", &count);

    priority_queue< int, vector<int>, greater<int> > pq;

    for(int i=0; i<count; i++){

        int ropeLen;

        scanf("%d", &ropeLen);

        pq.push(ropeLen);

    }

    while(pq.size() > 1) {

        first= pq.top(); pq.pop();

        second = pq.top(); pq.pop();

        result += (first+second);

        pq.push(first+second);

    }

    cout<<result<<endl;

}

}

You might be interested in
When long labels are required, which of these commands can you use to improve readability of a worksheet? .
kykrilka [37]
Whilst all the options presented can help improve readability of a worksheet, the best answer is B. Sort and Filter.

When working with very long tables, the sort and filter functions allow table data to be better organised to improve readability. You can sort by a criteria or filter out all data except what you are looking for.

Merge & centre and wrap text relate to text formatting in the cells themselves, these are useful features in general but not essential. Conditional formatting is a useful tool to highlight certain types of data, such as to colour the cell based on its value. Again, it is not as powerful as sort/filter.
3 0
3 years ago
Read 2 more answers
Explain the importance of mobile computing in communication​
ludmilkaskok [199]
<h3><em><u>Importance of Mobile computing in communication</u></em></h3>

*It’s portable and easy-to-use.

*It helps for different communication and information exchange.

*It saves user’s time and effort.

*It provides quick services like online business, e-mail, e-fax, etc.

*It increases the social interaction by sharing location services and GPS system.

7 0
3 years ago
Read 2 more answers
While adding information to the employee information database, Neil's computer crashed, and the entire database was erased. Whic
Varvara68 [4.7K]

Answer:

logic bombs

Explanation:

logic bombs -

It consists of certain coding , which is put into the software and will perform certain malicious function at the very correct time, is known as logic bomb ,

they are also known as time bomb.

It is a time of virus , but gets activated as soon as all conditions are met , which is coded in to the software.

Hence , from the given scenario of the question,

The correct term is logic bomb.

6 0
3 years ago
In order to avoid slipping in the shop, your footwear should ___________.
Arisa [49]

Answer:

b

Explanation:

has traction so it can grip

4 0
3 years ago
Logical are functions used when looking for an entry or value in the spreadsheet. what is it?
Mila [183]

Explanation:

Logical functions provide decision-making tools for information in a spreadsheet. They allow you to look at the contents of a cell, or to perform a calculation, and then test that result against a required figure or value.

5 0
3 years ago
Other questions:
  • Jeff is preparing for a presentation on effective communication skills. He has to face an audience of over 50 people. Which stra
    9·2 answers
  • Honestly reporting experimental findings is an example of good what?
    7·2 answers
  • When there is flooding and you see pooled water, what should you do?
    13·1 answer
  • (a) Explain the difference between a web browser and a search engine.​
    15·1 answer
  • Consider two different implementations, M1 and M2, of the same instruction set. There are three classes of instructions (A, B, a
    14·1 answer
  • Linda is training to become a certified network design expert and consultant. While researching about the process of cellular ra
    12·1 answer
  • What is the difference page break and column break ​
    7·2 answers
  • Explain how a monitor can display the letters that you type on a keyboard. (Hint: Three Basic Computer Functions) *
    11·1 answer
  • Which scientific tool would be primarily used by scientists?
    12·2 answers
  • Malware is any malicious software installed on a computer or network without the owner’s knowledge.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!