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]
3 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]3 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
Software (often on firmware) designed to make physical products and devices "smarter" by doing things like sharing usage informa
kolezko [41]

Answer:

embedded system

Explanation:

Based on the scenario being described within the question it can be said that the type of software that is being described is known as an embedded system. This is actually a combination of both hardware and software that focuses on a specific function which is later implemented into a much larger system to allow it to become "smarter", by performing more complex tasks, which would otherwise not be possible.

5 0
3 years ago
What is the function of a header when writing HTML
marusya05 [52]
The Function of a header is that it makes your TEXT LOOK BIGGER

5 0
3 years ago
The size of the board is one of the differences between Elevens and Thirteens. Why is size not an abstract method?
Levart [38]

Answer:

The program keeps track of the size of the board in cards.size(). The sub class sets this by passing it into the constructor. After that, the subclass never cares about the size of the board, so it's not necessary to make it accessible with an abstract method. Any need for it is covered by cardIndexes method.

Explanation:

The differences between Elevens and Thirteens

The program keeps track of the size of the board in cards.size(). The sub class sets this by passing it into the constructor. After that, the subclass never cares about the size of the board, so it's not necessary to make it accessible with an abstract method. Any need for it is covered by cardIndexes method.

5 0
3 years ago
Hi Lesiana, After your presentation last week, the manager thinks an in-house solution is the way to go. Although our programmer
zlopas [31]

The Hierarchical drawing of the In- House solutions includes Four categories such as product, service, training, support and about.

<h3>What is Hierarchical drawing?</h3>

Hierarchical drawing is also known as Layered Graph Drawing which includes the drawing in the vertices and are made on the Horizontal rows and layers.

The complete solution is attached below.

The In-House solutions' hierarchical diagram covers four categories, including product, service, training, support, and about.

Learn more about Hierarchical drawing here:

brainly.com/question/26031625

#SPJ1

 

3 0
1 year ago
PLATO
storchak [24]

Answer: here is the answer ☀️keep on shining☀️

Explanation:

5 0
3 years ago
Other questions:
  • The ____ is a new feature in versions of microsoft office, starting with office 2007; it consists of tabs, which contain groups
    5·1 answer
  • What is one course of action available in every problem solving process?
    9·2 answers
  • In order to use an object in a program, its class must be defined.
    9·1 answer
  • Subscribe to epic beast brothers thank you
    7·1 answer
  • In which of the following situations should you expect to provide your Social Security number?
    13·1 answer
  • R6. Suppose N people want to communicate with each of N - 1 other peo- ple using symmetric key encryption. All communication bet
    13·1 answer
  • Discuss, in detail, the usefulness of graphics and images in program development. Explain the importance of the interface of a c
    7·1 answer
  • ABC IF U HAVE A LEGENDARY PET ON ADOPT ME OR ANYTHING COOL!! &lt;3
    5·2 answers
  • What two windows security updates do most organizations always patch?
    7·1 answer
  • Im drinking coffee. and working on school and watching a show. Whos with me?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!