Skip to content

Commit 323a3f0

Browse files
authored
Update DSA Heaps 1 Introduction.md
1 parent 8da3aa5 commit 323a3f0

1 file changed

Lines changed: 61 additions & 121 deletions

File tree

Academy DSA Typed Notes/Advanced/DSA Heaps 1 Introduction.md

Lines changed: 61 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Advanced DSA: Heaps 1: Introduction
22

3-
---
4-
## Problem 1 Connecting the ropes
53

4+
## Problem 1 Connecting the ropes
5+
6+
### Problem Description
67
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.
78

89
**`To illustrate:`**
910

10-
**Example 1**:
11+
### Example 1:
1112

1213
int A[] = {2, 5, 3, 2, 6}
1314

1415
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/154/original/Screenshot_2024-01-09_at_11.02.13_AM.png?1704778393" width=500 />
1516

16-
**Example 2**:
17+
### Example 2:
1718

1819
**`Initial Ropes: [2, 5, 6, 3]`**
1920

@@ -34,7 +35,7 @@ Final Rope: [16]
3435
This is one of the options for finding the cost of connecting all ropes, but we need to find the minimum cost of connecting all the ropes.
3536

3637

37-
#### Observation
38+
### Observation
3839
Say, we have 3 ropes, **`x < y < z`**
3940
Which 2 ropes should we connect first ?
4041

@@ -50,7 +51,7 @@ Comparing case 2 and 3, x and y are different, now since x < y, we can say cost
5051

5152
**`Conclusion:`** Connecting smaller length ropes gives us lesser cost.
5253

53-
#### Process:
54+
### Process:
5455

5556
**`Initial Setup:`** Start with an array of rope lengths, e.g., [2, 2, 3, 5, 6]. First, sort the array.
5657

@@ -71,23 +72,21 @@ We are basically applying **`insertion sort`**.
7172
- Combine ropes 7 and 11 (cost = 18). Final rope: 18. Total cost: 40.
7273

7374

74-
#### Complexity
75+
### Complexity
7576
**Time Complexity:** O(N^2^)
7677
**Space Complexity:** O(1)
7778

7879
---
7980
### Question
8081
What is the minimum cost of connecting all the ropes for the array [1, 2, 3, 4]?
8182

82-
**Choices**
83+
### Choices
8384
- [x] 19
8485
- [ ] 20
8586
- [ ] 10
8687
- [ ] 0
8788

88-
89-
90-
**Explanation**:
89+
### Explanation:
9190

9291
**Always pick two of the smallest ropes and combine them.**
9392

@@ -100,16 +99,10 @@ After combining the two smallest ropes at every step, we need to sort an array a
10099
Final Length: 10
101100
Total Cost = (3 + 6 + 10) = 19
102101

103-
104102
---
105103

106-
:::warning
107-
Please take some time to think about the solution approach on your own before reading further.....
108-
:::
109-
110104
### Connecting the ropes optimisation
111105

112-
113106
Heaps efficiently perform the necessary operations:
114107

115108
- Insertion of elements in **`O(log n)`** time.
@@ -123,22 +116,21 @@ Say, for above problem, we use a min heap. At every step, it will give us the 2
123116

124117
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/155/original/Screenshot_2024-01-09_at_11.22.15_AM.png?1704779546" width=700 />
125118

126-
#### Time & Space Complexity
119+
### Time & Space Complexity
127120
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)`**
128121

129122
**`Space Complexity is O(N)`**
130123

131-
---
132-
## Heap Data Structure
133124

125+
## Heap Data Structure
134126
The heap data structure is a binary tree with two special properties.
135127
- First property is based on structure.
136128
- **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.
137129
- Second property is based on the order of elements.
138130
- **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.
139131

140132

141-
**Examples**
133+
### Examples
142134

143135
**`Example 1:`**
144136

@@ -157,9 +149,7 @@ The heap data structure is a binary tree with two special properties.
157149
- 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.
158150
- Hence, it is a **max-heap.**
159151

160-
---
161-
### Array Implementation of Trees(Complete Binary Tree)
162-
152+
### Binary Heap Implementation via Array
163153

164154
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/170/original/3.png?1704790911" width=300/>
165155

@@ -179,9 +169,8 @@ The heap data structure is a binary tree with two special properties.
179169

180170
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/172/original/5.png?1704790938" width=300/>
181171

182-
---
183-
### Insertion in min heap
184172

173+
### Insertion in min heap
185174

186175
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/173/original/6.png?1704790952" width=400/>
187176

@@ -190,7 +179,7 @@ The heap data structure is a binary tree with two special properties.
190179
|5|12|20|25|13|24|22|35|
191180

192181

193-
**Example 1**: Insert 10
182+
### Example 1: Insert 10
194183

195184
In an array, if we will insert 10 at index 8, then our array becomes,
196185

@@ -252,7 +241,7 @@ Now this tree satisfies the min-heap order property.
252241

253242
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/178/original/2.png?1704791329" width=350/>
254243

255-
**Example 2**: Insert 3
244+
### Example 2: Insert 3
256245

257246
First insert 3 at index 9.
258247

@@ -276,11 +265,12 @@ First insert 3 at index 9.
276265
**`NOTE: The maximum swap we can perform for any element to be inserted is equal to the height of the tree.`**
277266

278267
---
268+
279269
### Question
280270

281271
Time Complexity of inserting an element in a heap having n nodes?
282272

283-
**Choices**
273+
### Choices
284274

285275
- [ ] O(1)
286276
- [x] O(log n)
@@ -289,31 +279,32 @@ Time Complexity of inserting an element in a heap having n nodes?
289279

290280

291281
---
292-
### Inserting in min heap pseudocode
293-
#### Pseudocode
282+
283+
### Pseudocode
284+
294285
```cpp
295286
heap[];
296287
heap.insert(val); // inserting at last
297288
i = heap.size - 1;
298-
while (i > 0) {
299-
pi = (i - 1) / 2;
300-
if (heap[pi] > heap[i]) {
301-
swap(heap, pi, i);
302-
i = pi;
303-
} else {
289+
while(i>0){
290+
pi = (i-1)/2;
291+
if(heap[pi] > heap[i]){
292+
swap(heap,pi,i);
293+
i=pi;
294+
}
295+
else
296+
{
304297
break;
305298
}
306299
}
307300
```
308301

309-
#### Complexity
302+
### Complexity
310303
**Time Complexity:** O(height of tree) = O(logN)
311304

312305

313-
---
314306
### Extract Min
315307

316-
317308
**`Min Heap -`**
318309
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/205/original/1.png?1704813588" width=250/>
319310

@@ -326,6 +317,8 @@ In this tree, we have a minimum element at the root. First we swap the first and
326317

327318
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/209/original/a.png?1704813720" width=300/>
328319

320+
</br>
321+
329322
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/210/original/b.png?1704813732" width=300/>
330323

331324
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
340333

341334
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/208/original/4.png?1704813681" width=300/>
342335

343-
**`NOTE to Instructor: Perform extract-min again for more clarity on above tree.`**
344336

345337

346-
#### Pseudocode
338+
339+
### Pseudocode
347340
```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)
350343
heapify(heap[], 0);
351344

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+
356349
if (x == heap[i]) {
357350
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
364359
}
365360
}
366361
}
367362
```
368363
369364
370-
#### Complexity
365+
### Complexity
371366
**Time Complexity:** O(log N)
372367
373368
374-
---
369+
375370
### Build a heap
376371
377372
We have an array of values, we want to make a heap of it.
378373
**`[5, 13, -2, 11, 27, 31, 0, 19]`**
379374
380-
#### Idea 1
375+
### Idea 1
381376
Sort the array.
382377
[-2, 0, 5, 11, 13, 19, 27, 31]
383378
@@ -389,7 +384,7 @@ Looking at the tree below, we can see this is a heap.
389384
**`Time Complexity: O(N * logN)`**
390385
391386
392-
#### Idea 2
387+
### Idea 2
393388
Call insert(arr[i]) for every element of an array.
394389
395390
**Explanation:**
@@ -399,10 +394,9 @@ It will take N * logN, as for each element, we will take O(log N) as heapify sha
399394
400395
**`Time Complexity:O(N * logN)`**
401396
402-
--
403-
### Build a heap Idea 3
397+
## Build a heap Idea 3 linear time
404398
405-
#### Idea to build in linear time
399+
### Idea to build in linear time
406400
407401
We have an array
408402
**`[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.
416410
- 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$.
417411
- We will call heapify() starting from for $(n/2)-1$ index to index 0.
418412
419-
```cpp
420-
for (int i = (n / 2) - 1; i >= 0; i--) {
413+
```cpp=
414+
for(i -> (n/2)-1 down to 0){
421415
heapify(heap[], i);
422416
}
423417
```
@@ -451,7 +445,7 @@ for (int i = (n / 2) - 1; i >= 0; i--) {
451445
- Now all the nodes has valid heap-order property.
452446

453447

454-
#### Time Complexity
448+
### Time Complexity
455449

456450
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/229/original/e.png?1704815439" width=300/>
457451

@@ -490,68 +484,14 @@ Then
490484
Here both will cancel out with each other and so our overall time complexity for building a heap is **O(N)**
491485

492486
---
487+
493488
### Question
494489
What is the time complexity for building a heap with N nodes?
495490

496-
**Choices**
491+
### Choices
497492
- [ ] O(1)
498-
- [ ] O(N$^2$)
493+
- [ ] O(N<sup>2</sup>)
499494
- [x] O(N)
500495
- [ ] O(logN)
501496

502-
503-
504497
---
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.
543-
544-
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/239/original/11.png?1704815927" width=250/>
545-
546-
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.
549-
550-
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/061/240/original/22.png?1704815937" width=250/>
551-
552-
- Now again extract-min() from the heap and insert the next element of that list to which the minimum element belongs.
553-
- And keep repeating this until we have done with all the elements.
554-
555-
#### Time Complexity
556-
**Time Complexity:** (XlogN)
557-
Here X is a total number of elements of all arrays.

0 commit comments

Comments
 (0)