Question

Suppose you are given n ropes of different lengths, you need to connect these ropes into...

Suppose you are given n ropes of different lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. You want to find the minimum cost way to do this. 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 you algorithm always finds the least cost solution if the lengths of the ropes are distinct.

c)Analyze your algorithms complexity

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A)
Greedy based solution:
Algo to find the minimum cost for connecting n ropes.

Suppose we have n ropes in an array element as length = arr[n]

total_cost = 0
1) Create a min heap and insert all length in min heap.
2) Do following while number of elements in min heap is greater then 1.
   i) min1 = get minimum from min heap // smallest
   ii) min2 = get miniumum from min heap // second smallest
   iii) total_cost += min1 + min2
   iv) insert (min1 + min2) to min heap
3) return total_cost

B)

   main point to be noted here is, the lengths of the ropes which are selected first
   are included multiple times in calculating total cost.
   let r1 len(r1) = l1 and r2 len(r2) = l2 and l1 < l2 (distinct length)
   if we are merging rope r2 before r1, then l2 will be added multiple times let it be K times. so total cost C2 = K.l2 + X
   instead if we are merging rope r1, then total cost C1 = K.l1 + X
   since, l1 < l2
           K.l1 < K.l2
           k.l1 + X < k.l2 + X
           C1 < C2
   so, our approcah is optimal for distinct length

C)
   complexity:
   1) Create a min heap and insert all length in min heap.   O(n) // time taken to create heap
   2) Do following while number of elements in min heap is greater then 1. // n no of times
       i) min1 = get minimum from min heap // smallest O(lon n)
       ii) min2 = get miniumum from min heap // second smallest O(log n)
       iii) total_cost += min1 + min2 O(1)
       iv) insert (min1 + min2) to min heap O(log n)
   3) return total_cost O(1)
  
   total compeexity = nlogn

Add a comment
Know the answer?
Add Answer to:
Suppose you are given n ropes of different lengths, you need to connect these ropes into...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you are given an array of n integers ai, az, ..., an. You need to...

    Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...

  • Suppose you are given an array of n integers a1, a2, ..., an. You need to...

    Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • Q(1) Given a rope of length n meters, cut the rope in different parts of integer...

    Q(1) Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes product of lengths of all parts. You must make at least one cut. Assume that the length of rope is more than 2 meters Examples: (n-4) Input: rope length is 4 Output: 2*2-4(Maximum obtainable product is 2*2) Input: rope length is 5 Output: 2*3-6 (Maximum obtainable product is 2*3) (n-5) Input: rope length is 10 (n- 10) Output:...

  • You will be given an array of N elements sorted small to large. For the X...

    You will be given an array of N elements sorted small to large. For the X and Y values ​​that satisfy the X ≤ Y condition, draw the flow diagram of the algorithm that finds the start and end addresses of the region with the numbers greater than X and less than Y with the divide and manage approach and with O (logN) complexity. Also code the algorithm in c language. (not c++) Example: A[0…8] array 3, 5, 7, 9,...

  • (20 points) You are given an array A of distinct integers of size n. The sequence...

    (20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...

  • Analysis of Algorithms Fall 2013 Do any (4) out of the following (5) problems 1. Assume n-3t is a...

    Analysis of Algorithms Fall 2013 Do any (4) out of the following (5) problems 1. Assume n-3t is a power of 3 fork20. Solve accurately the following recursion. If you cannot find the exact solution, use the big-O notation. Tu) T(n)Tin/3)+2 2. Suppose that you have 2 differeut algorithms to solve a giveu probleen Algorithm A has worst-case time complexity e(n2) and Algorithm B has worst-case time complexity e(nlog n). Which of the following statements are true and which are...

  • 5) (10 pts) Greedy Algorithms The 0-1 Knapsack problem is as follows: you are given a...

    5) (10 pts) Greedy Algorithms The 0-1 Knapsack problem is as follows: you are given a list of items, each item has an integer weight and integer value. The goal of the problem is to choose a subset of the items which have a sum of weights less than or equal to a given W with a maximal sum of values. For example, if we had the following five items (each in the form (weight, value)): 11(6, 13), 2(4, 10),...

  • There are n trading posts numbered 1 to n as you travel downstream. At any trading...

    There are n trading posts numbered 1 to n as you travel downstream. At any trading post i you can rent a canoe to be returned at any of the downstream trading posts j>i. You are given a cost array R(i,j) giving the cost of these rentals for all 1≤i<j≤n. We can assume that R(i,i)=0, and that you can't go upriver (so perhaps R(i,j)= ∞ if i>j). For example, one cost array with n=4 might be the following. The problem...

  • In the bin packing problem, items of different weights (or sizes) must be packed into a...

    In the bin packing problem, items of different weights (or sizes) must be packed into a finite number of bins each with the capacity C in a way that minimizes the number of bins used. The decision version of the bin packing problem (deciding if objects will fit into <= k bins) is NP-complete. There is no known polynomial time algorithm to solve the optimization version of the bin packing problem. In this practice problem you will be examining three...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT