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
Difference between volatile and non volatile memory
slega [8]
<span>Volatile memory requires electricity or some kind of current to store information, and nonvolatile memory does not.</span>
6 0
3 years ago
Read 2 more answers
Decimal numbers are based on __________.
Shtirlitz [24]
Decimals are based on the preceding powers of 10. Thus, as we move from left to right, the place value of digits gets divided by 10, meaning the decimal place value determines the tenths, hundredths and thousandths. A tenth means one tenth or 1/10. In decimal form, it is 0.1.
6 0
3 years ago
Which of the following statements is true of a time management plan? It is work in progress that need to be altered many times?
marusya05 [52]
Cake cake cake cake
3 0
3 years ago
Will technology be the destruction or salvation of human-kind
Delicious77 [7]

MARK ME As BRAINLIEST

Answer is salvation
5 0
3 years ago
Read 2 more answers
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To ac
GREYUIT [131]

Answer:

import java.util.Scanner;

public class LabProgram {

  public static void main(String[] args) {

      Scanner scnr = new Scanner(System.in);

      int inputYear;

      boolean isLeapYear;

      isLeapYear = false;

      inputYear = scnr.nextInt();

      // If a year is divisible by 400,  then it is a leap year

      if (inputYear % 400 == 0)

          isLeapYear = true;

      // If a year is divisible by 100,  then it is not a leap year

      if (inputYear % 100 == 0)

          isLeapYear = false;

      // If a year is divisible by 4,  then it is a leap year

      if (inputYear % 4 == 0)

          isLeapYear = true;

      if(isLeapYear)

          System.out.println(inputYear + " is a leap year.");

      else

          System.out.println(inputYear + " is not a leap year.");

  }

}

Explanation:

  • Take the year as an input from user and store it to inputYear variable.
  • If the year is a century year, check if the year is divisible by 400 ( the year must be evenly divisible by 400 ),  then set the boolean isLeapYear to true. If a year is divisible by 100,  then set the boolean isLeapYear to false. If a year is divisible by 4,  then set the boolean isLeapYear to true.
  • Check if isLeapYear is true, then print that it is a leap year. Otherwise, print that it is not a leap year.

Output:

1712

1712 is a leap year.

4 0
3 years ago
Read 2 more answers
Other questions:
  • Compare and contrast the TwoFish encryption algorithm with the DES and AES algorithms. In your comparison be sure to compare and
    6·1 answer
  • When creating an input control (for example, a button) on a form the _____________ indicates the data that is contained in the n
    13·1 answer
  • Frank is a writer. He needs to work for long hours and type for long periods on the computer. What injury can Frank develop?
    15·2 answers
  • Every time you are asked to work with others, you are being asked to:
    6·2 answers
  • Rate is how fast a screen updates the information being displayed.
    6·2 answers
  • Identify the layout in which you will be able to view and edit the header and footer
    12·1 answer
  • With the ease of the Internet, it is not uncommon for individuals to copy skills from resume templates to make themselves look m
    9·1 answer
  • Select the three reasons that the gaming industry is set to grow.
    12·2 answers
  • New product ideas must fit into a company's mission statement and?
    12·1 answer
  • Which quality should an experiment have to be replicable and valid? O clearly detailed results O a well-cited research paper O s
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!