You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are given an array that represents the size of different ropes. In a single operation, you can connect two ropes. Cost of connecting two ropes is sum of the length of ropes you are connecting. Find the minimum cost of connecting all the ropes.
For every operation, we are removing 2 elements and inserting one back, hence it is 3 * log N. For N operations, **`the time complexity will be O(N * log N)`**
128
121
129
122
**`Space Complexity is O(N)`**
130
123
131
-
---
132
-
## Heap Data Structure
133
124
125
+
## Heap Data Structure
134
126
The heap data structure is a binary tree with two special properties.
135
127
- First property is based on structure.
136
128
-**Complete Binary Tree:** All levels are completely filled. The last level can be the exception but is should also be filled from left to right.
137
129
- Second property is based on the order of elements.
138
130
-**Heap Order Property:** In the case of max heap, the value of the parent is greater than the value of the children. And in the case of min heap, the value of the parent is less than the value of the children.
139
131
140
132
141
-
**Examples**
133
+
### Examples
142
134
143
135
**`Example 1:`**
144
136
@@ -157,9 +149,7 @@ The heap data structure is a binary tree with two special properties.
157
149
- Heap Order Property is also valid at every point in the tree, as 58 is greater than 39 and 26, 39 is greater than 34 and 12, 26 is greater than 3 and 9, 34 is greater than 16 and 1.
158
150
- Hence, it is a **max-heap.**
159
151
160
-
---
161
-
### Array Implementation of Trees(Complete Binary Tree)
But the tree is not satisfying the heap-order property. To regain this heap-order property, first check 12, 4 and 5, the minimum is 4, so swap 12 with 4.
@@ -340,44 +333,46 @@ But the tree is not satisfying the heap-order property. To regain this heap-orde
**`NOTE to Instructor: Perform extract-min again for more clarity on above tree.`**
344
336
345
337
346
-
#### Pseudocode
338
+
339
+
### Pseudocode
347
340
```cpp
348
-
swap(heap, 0, heap - size() - 1)
349
-
heap.remove(heap.size() - 1)
341
+
swap(heap, 0, heap-size()-1)
342
+
heap.remove(heap.size()-1)
350
343
heapify(heap[], 0);
351
344
352
-
void heapify(heap[], i) {
353
-
while (2 i + 1 < N) { //need to handle the edge case when left child is there but not the right child
354
-
x = min(heap[i], heap[2 i + 1], heap[2 i + 2])
355
-
345
+
function heapify(heap[], i) {
346
+
while (2i+1 < N) { //need to handle the edge case when left child is there but not the right child
347
+
x = min(heap[i], heap[2i+1], heap[2i+2])
348
+
356
349
if (x == heap[i]) {
357
350
break
358
-
} else if (x == heap[2 i + 1]) {
359
-
swap(heap, i, 2 i + 1)
360
-
i = 2 i + 1
361
-
} else {
362
-
swap(heap, i, 2 i + 2)
363
-
i = 2 i + 2
351
+
}
352
+
else if (x == heap[2i+1]){
353
+
swap (heap, i, 2i+1)
354
+
i = 2i+1
355
+
}
356
+
else{
357
+
swap (heap, i, 2i+2)
358
+
i = 2i+2
364
359
}
365
360
}
366
361
}
367
362
```
368
363
369
364
370
-
#### Complexity
365
+
### Complexity
371
366
**Time Complexity:** O(log N)
372
367
373
368
374
-
---
369
+
375
370
### Build a heap
376
371
377
372
We have an array of values, we want to make a heap of it.
378
373
**`[5, 13, -2, 11, 27, 31, 0, 19]`**
379
374
380
-
#### Idea 1
375
+
### Idea 1
381
376
Sort the array.
382
377
[-2, 0, 5, 11, 13, 19, 27, 31]
383
378
@@ -389,7 +384,7 @@ Looking at the tree below, we can see this is a heap.
389
384
**`Time Complexity: O(N * logN)`**
390
385
391
386
392
-
#### Idea 2
387
+
### Idea 2
393
388
Call insert(arr[i]) for every element of an array.
394
389
395
390
**Explanation:**
@@ -399,10 +394,9 @@ It will take N * logN, as for each element, we will take O(log N) as heapify sha
399
394
400
395
**`Time Complexity:O(N * logN)`**
401
396
402
-
--
403
-
### Build a heap Idea 3
397
+
## Build a heap Idea 3 linear time
404
398
405
-
#### Idea to build in linear time
399
+
### Idea to build in linear time
406
400
407
401
We have an array
408
402
**`[7, 3, 5, 1, 6, 8, 10, 2, 13, 14, -2]`**
@@ -416,8 +410,8 @@ We can represent this array in the form of a tree.
416
410
- The first non-leaf is nothing but the parent of the last leaf node of the tree and the index of the last node is $n-1$, so the index of the first non-leaf is $((n-1-1)/2)=((n-2)/2)=(n/2)-1$.
417
411
- We will call heapify() starting from for $(n/2)-1$ index to index 0.
418
412
419
-
```cpp
420
-
for (int i = (n / 2) - 1; i >= 0; i--) {
413
+
```cpp=
414
+
for(i -> (n/2)-1 down to 0){
421
415
heapify(heap[], i);
422
416
}
423
417
```
@@ -451,7 +445,7 @@ for (int i = (n / 2) - 1; i >= 0; i--) {
451
445
- Now all the nodes has valid heap-order property.
Here both will cancel out with each other and so our overall time complexity for building a heap is **O(N)**
491
485
492
486
---
487
+
493
488
### Question
494
489
What is the time complexity for building a heap with N nodes?
495
490
496
-
**Choices**
491
+
### Choices
497
492
-[ ] O(1)
498
-
-[ ] O(N$^2$)
493
+
-[ ] O(N<sup>2</sup>)
499
494
-[x] O(N)
500
495
-[ ] O(logN)
501
496
502
-
503
-
504
497
---
505
-
506
-
507
-
### Merge N-sorted arrays
508
-
a - [2, 3, 11, 15, 20]
509
-
b - [1, 5, 7, 9]
510
-
c - [0, 2, 4]
511
-
d - [3, 4, 5, 6, 7, 8]
512
-
e - [-2, 5, 10, 20]
513
-
514
-
We have to merge these sorted arrays.
515
-
516
-
#### Idea
517
-
- If we want to merge two sorted arrays then we need two pointers.
518
-
- If we want to merge three sorted arrays then we need three pointers.
519
-
- If we want to merge N sorted arrays then we need N pointers, in which complexity becomes very high and we need to keep track of N pointers.
520
-
521
-
---
522
-
### Question
523
-
For merging N sorted arrays, which data structure would be the most efficient for this task ?
524
-
525
-
**Choices**
526
-
-[ ] Linked List
527
-
-[ ] Array
528
-
-[x] Min-Heap
529
-
-[ ] Hash Table
530
-
531
-
**Explanation:**
532
-
533
-
A Min-Heap is an efficient data structure choice. The Min-Heap ensures that the smallest element among all the elements in the arrays is always at the front. This allows for constant-time access to the minimum element, making it efficient to extract and merge elements in sorted order.
534
-
535
-
:::warning
536
-
Please take some time to think about the optimised approach on your own before reading further.....
537
-
:::
538
-
539
-
#### Optimized Solution
540
-
- First, we need to compare the 0th index element of every array.
541
-
- Now we use heap here.
542
-
- We will add an index 0 element of every array in the heap, in the form of element value, array number and Index of the element in particular.
Now take the minimum element and insert it in the resultant array,
547
-
548
-
- Now insert the next element of the list for which the minimum element is selected, like first, we have taken the fourth list element, so now insert the next element of the fourth list.
0 commit comments