Heaps in 6 minutes - Methods

  Рет қаралды 90,665

Michael Sambol

Michael Sambol

Күн бұрын

Пікірлер: 51
@MichaelSambol
@MichaelSambol 2 жыл бұрын
The "height of binary tree" slide should read "height of nearly complete binary tree." Regular binary trees may become unbalanced and thus not have a height of O(logn). Because heaps are nearly complete binary trees (which are balanced), we know that the height is O(logn).
@turkialotibi7168
@turkialotibi7168 2 жыл бұрын
If you are reading this comment, I would like to say thank you for your educational content. You helped me pass Algorithms with a grade of B.
@MichaelSambol
@MichaelSambol 2 жыл бұрын
Awesome! 👊🏼
@DancingHippo
@DancingHippo 7 ай бұрын
Micheal is singlehandedly helping every single computer science student pass this exam
@MichaelSambol
@MichaelSambol 7 ай бұрын
God bless
@prpshrt
@prpshrt 2 ай бұрын
and every software engineer leetcoding out there
@Fuego958
@Fuego958 Жыл бұрын
Love your videos. Short, clear, and no frills.
@narenbabu629
@narenbabu629 2 жыл бұрын
typo at 5:01 last line is max_heapify(a, 5, i) instead it should be max_heapify(a, 10, i)
@MichaelSambol
@MichaelSambol 2 жыл бұрын
You're right! Sorry about that, @Naren Babu. Copy/paste error on my part. Thanks for pointing it out.
@BABEENGINEER
@BABEENGINEER 9 ай бұрын
Absolutely appreciate your visual explanations!!!
@MichaelSambol
@MichaelSambol 9 ай бұрын
thank you!
@n.h.son1902
@n.h.son1902 6 ай бұрын
I've got a dumb question which is why is the running time of build_max_heap O(n), n = len(a)? (refer to 5:29). In my opinion, the for loop is O(n) and inside each for loop is O(logn) to call max_heapify so in total, the time complexity would be O(nlogn), right?
@Zicrus
@Zicrus 3 ай бұрын
This is not explained in the video, but here is my explanation: max_heapify is actually log(subtree size), and not log(n), as stated in the video. This is very important since almost all calls to max_heapify happen at the lower levels, which have small subtrees. In total, I believe there are about 4*n calls to max_heapify, also counting recursive calls. This was calculated with a recursive formula in excel up to n=2^1000.
@josecalles7634
@josecalles7634 10 ай бұрын
Bro, really thanks to give so clean a very good explained things, you're literally save my life
@dannggg
@dannggg 2 жыл бұрын
You always makes things clear
@govindrai93
@govindrai93 Жыл бұрын
Hey Michael! What a great video! This six minute video actually took me a few hours to really gain understanding. :D QQ: You state at 5:29 that the run time is O(n) but in each iteration of the for loop you call max_heapify which has a run time of log(n). Being that, shouldn't the runtime be 0(n * log(n))? Thank you?
@DannyJoseph
@DannyJoseph Жыл бұрын
It doesn't run n times it runs n-1 times so it should be n-1*logn
@smoulibabca
@smoulibabca 11 ай бұрын
@@DannyJoseph n-1 = n in terms of algorithm complexity
@劉梓堂
@劉梓堂 3 ай бұрын
Same though, I think that it's 0(n * log(n))
@Zicrus
@Zicrus 3 ай бұрын
​@@劉梓堂 I believe it is still actually O(n), because the vast majority of the calls to max_heapify from build_max_heap happens right above the leaves, which makes it constant in those cases (since it doesn't have to recurse). I actually calculated the number of calls to max_heapify in total, with recursion, for up to ~2^1000 and it seems very linear. For ~2^1000 it is about 4*n calls, which is also the case already at 2^8.
@viper33m
@viper33m 2 ай бұрын
@@Zicrus "O(n), because the vast majority of the calls" Big-O is worst case, Theta is average case. So it's Theta(n), but O(nlogn)
@balive053
@balive053 3 ай бұрын
Very clear explanation. Thanks very much!
@Xxnightwalk1
@Xxnightwalk1 2 жыл бұрын
I really enjoy the bite sized nature of your videos It really helps me understand the concept, and I then know about it for future research if need be Thanks a lot for your content
@dannyelsupremo6127
@dannyelsupremo6127 Жыл бұрын
Amazing! Very straight to the point
@govindrai93
@govindrai93 Жыл бұрын
It would be great if you can also show methods for adding elements into the heap and removing them and the complexities involved there. Thanks!
@NickWinters
@NickWinters 7 ай бұрын
Thanks! Very useful video, it clarified some things for me
@DemosthenesKar
@DemosthenesKar 6 ай бұрын
I think there is a mistake at 3:24, for 9 to float down we need to keep using i in the recursive max_heapify, not the largest which is now 18
@Zicrus
@Zicrus 3 ай бұрын
"largest" is the index of the largest value (18) prior to the swap. So at the point where it recurses, "largest" is indeed the index of 9 (because 9 and 18 swapped places), which is correct.
@01eksii
@01eksii 7 ай бұрын
Oh yeah, another addition to collection of golden clasics
@WailingDarkness
@WailingDarkness Жыл бұрын
whats the purpose of l
@user-jt4hk1rl8r
@user-jt4hk1rl8r Жыл бұрын
I don’t understand why build max heap is not nlogn in run time since u run max heapify on n nodes
@teodorneagoe
@teodorneagoe 2 ай бұрын
Build heap is O(n) because although heapify is O(log n) for a single node, most nodes are near the leaves and require much less work, leading to a total cost that sums to O(n)
@Singh1405-l5u
@Singh1405-l5u 6 ай бұрын
just a little doubt, in maxheapify shouldnt it be if la[largest]
@mfahmy6733
@mfahmy6733 3 ай бұрын
In max_heapify, shouldn't it be if la[largest], since you're setting largest to i anyways? I guess it works as-is but just for consistency?
@moriartyjames4856
@moriartyjames4856 2 ай бұрын
Very helpful video. However, I don’t understand why the time complexity of max heapify is O(log n). Could you explain or provide any reference materials? Thank you for reading this comment.
@aymanhww
@aymanhww 7 ай бұрын
shouldnt it be -1 rather than 0 in the second parameter of the for loop ?
@fedormiron1922
@fedormiron1922 7 ай бұрын
Argument that build_max_heap() is O(n) because it has one loop is wrong, as it calls max_heapify which is O(logn) => total is O(nlogn).However it is true that it is possible to implement it in O(n), for proof please check Lecture 4: Heaps and Heap Sort from MIT.
@Zicrus
@Zicrus 3 ай бұрын
With the implementation shown in the video, it is actually still O(n), because max_heapify is log(subtree size), not log(n), and the vast majority of the calls to max_heapify are on the bottom few layers.
@kuldeepchouhan8467
@kuldeepchouhan8467 Жыл бұрын
It was very helpful. thanks!!
@-_art0m_-632
@-_art0m_-632 6 ай бұрын
I dont get the part where we get left and right indexes in heapify. Why arent they l = 2*i + 1 and r = 2*1 + 2?
@nastrimarcello
@nastrimarcello 2 ай бұрын
Because he's indexing from 1 instead of from zero.
@DavidZapata-q4n
@DavidZapata-q4n 8 ай бұрын
Why does the examples uses null as the first index in the array? is it necessary on all heap algorithms?
@MichaelSambol
@MichaelSambol 8 ай бұрын
check my notes here: github.com/msambol/dsa/blob/master/data_structures/heap.py#L39
@DavidZapata-q4n
@DavidZapata-q4n 8 ай бұрын
@@MichaelSambol Thanks, and great videos!
@julianduhnen942
@julianduhnen942 9 ай бұрын
Damn, this guy sounds like the Lord
@colinburgess4316
@colinburgess4316 2 жыл бұрын
What do you do with the intersect of maxheap, and minheap, where there is a fuzziness between these two properties?
@qiphoenix3043
@qiphoenix3043 6 ай бұрын
ur video is awesome!
@Explainer400
@Explainer400 2 жыл бұрын
Why you start the array with 1
@Eren-dm5xy
@Eren-dm5xy Жыл бұрын
u have to add other conditions as for left children of tree lying on node 0 will also fall on the same index 0 so we have to add condition to place it on index one and for index two it would be 1 so again problem
@Twst3628
@Twst3628 Жыл бұрын
If your array starts with 0 left = 2i + 1 right = 2i + 2
Heap sort in 4 minutes
4:13
Michael Sambol
Рет қаралды 1 МЛН
Heaps, heapsort, and priority queues - Inside code
19:01
Inside code
Рет қаралды 104 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
B-trees in 6 minutes - Deletions
6:00
Michael Sambol
Рет қаралды 67 М.
Hash Tables and Hash Functions
13:56
Computer Science Lessons
Рет қаралды 1,6 МЛН
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 1,1 МЛН
Fibonacci heaps in 6 minutes - Intro
6:37
Michael Sambol
Рет қаралды 27 М.
Heaps in 3 minutes - Intro
3:29
Michael Sambol
Рет қаралды 204 М.
Generative AI in a Nutshell - how to survive and thrive in the age of AI
17:57
Big-O notation in 5 minutes
5:13
Michael Sambol
Рет қаралды 1,2 МЛН
What is mathematical thinking actually like?
9:44
Benjamin Keep, PhD, JD
Рет қаралды 25 М.
Fibonacci heaps in 8 minutes - Extract Min
8:05
Michael Sambol
Рет қаралды 9 М.