|
| 1 | +/* |
| 2 | +Consider n-ropes with different length. Find algo to tie up all the rope into a single rope with min cost |
| 3 | +
|
| 4 | +Cost in this case is l1+l2 (if lengths of two ropes to be combined is l1 and l2 resp) |
| 5 | +
|
| 6 | +METHOD: |
| 7 | +This approach is similar to huffman coding. The max lies closer to root and min lies far away from root so |
| 8 | +that the cost can be minimized. So we repeat min more than max. Therefore we make a min heap of the given |
| 9 | +inputs and each time we extract min twice. Combine those and insert again to the min heap |
| 10 | +
|
| 11 | +Time complexity: O(3*nlogn)+O(n) //extract min twice + insert + create min heap |
| 12 | +Space complexity: O(1) //everything done inplace in the given input array |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <limits.h> |
| 18 | +#define MAX 100 |
| 19 | + |
| 20 | +void swap(int *a, int *b){ |
| 21 | + int temp = *a; |
| 22 | + *a = *b; |
| 23 | + *b = temp; |
| 24 | +} |
| 25 | + |
| 26 | +void minHeapify(int *arr, int i, int size){ |
| 27 | + int left = 2*i+1; |
| 28 | + int right = 2*i + 2; |
| 29 | + int smallest; |
| 30 | + int heapSize = size; |
| 31 | + if(left < heapSize && arr[left]<arr[i]){ |
| 32 | + smallest = left; |
| 33 | + }else{ |
| 34 | + smallest = i; |
| 35 | + } |
| 36 | + if(right < heapSize && arr[right] < arr[smallest]){ |
| 37 | + smallest = right; |
| 38 | + } |
| 39 | + if(smallest <heapSize && smallest != i){ |
| 40 | + swap(&arr[smallest],&arr[i]); |
| 41 | + minHeapify(arr,smallest,size); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void buildMinHeap(int *arr, int size){ |
| 46 | + int index = (size/2)-1; |
| 47 | + int i; |
| 48 | + for(i=index;i>=0;i--){ |
| 49 | + minHeapify(arr,i,size); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +int extractMin(int *arr, int *size){ |
| 54 | + if(*size == 0){ |
| 55 | + return 0; |
| 56 | + } |
| 57 | + int temp = arr[0]; |
| 58 | + arr[0] = arr[*size-1]; |
| 59 | + *size = *size - 1; |
| 60 | + minHeapify(arr,0,*size); |
| 61 | + return temp; |
| 62 | +} |
| 63 | + |
| 64 | +void decreaseKey(int *arr, int *size, int key, int data){ |
| 65 | + int i=key, temp; |
| 66 | + arr[i] = data; |
| 67 | + minHeapify(arr,i,*size); |
| 68 | +} |
| 69 | + |
| 70 | +void printHeap(int *arr, int size){ |
| 71 | + printf("heap now is,.,......\n"); |
| 72 | + for(int i=0; i<size;i++){ |
| 73 | + printf("%d\n", arr[i]); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +int findMinCost(int *arr, int size){ |
| 78 | + buildMinHeap(arr,size); |
| 79 | + printHeap(arr,size); |
| 80 | + int cost = 0; |
| 81 | + while(size > 1){ |
| 82 | + int value = extractMin(arr,&size)+extractMin(arr,&size); |
| 83 | + cost += value; |
| 84 | + arr[size] = INT_MAX; |
| 85 | + size++; |
| 86 | + decreaseKey(arr,&size,size-1, value); |
| 87 | + } |
| 88 | + return cost; |
| 89 | +} |
| 90 | + |
| 91 | +int main(){ |
| 92 | + int size, i; |
| 93 | + printf("enter the size of the array\n"); |
| 94 | + scanf("%d",&size); |
| 95 | + int arr[MAX]; |
| 96 | + for(i=0;i<size;i++){ |
| 97 | + printf("enter the %d element\n", i); |
| 98 | + scanf("%d",&arr[i]); |
| 99 | + } |
| 100 | + |
| 101 | + int cost = findMinCost(arr,size); |
| 102 | + printf("min cost is %d", cost); |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
0 commit comments