[MIT QUIZ]Heap
[How to index]Given a Array, How to check MAX-Heap
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
[How to index]Given a MAX-Heap, Where are the possible minimum number?
跟上面那個類似概念,floor function的概念
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
[Bottom-up]Build-Heap :
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Build-Heap ORDER cannot be from the root
走到下面時,如果發現下面有比走過的地方還要大的data,那無法被移到上面去,就會產生錯誤。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
[Top-down]Build-Heap-
連續執行 “Insert” 的動作,每一個步驟執行後均維持MaxHeap
Top-Down and Bottom-Up result may be different
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Heapsort is unstable
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
如果只有一個node違反heap性質,只對他做max-heapify可能是不夠的
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
[insertion] 游上去
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Deletion of Heap
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Wrong way to deletion
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Complexity of decrease-key/increase-key :
在max-heap當中
- increase-key: SWAP
- decrease-key: MAX-HEAPIFY
在min-heap當中
- increase-key: MIN-HEAPIFY
- decrease-key: SWAP
為什麼會這樣?
因為在max-heap中,
在某一點increase-key可能會違反到上面的data,所以要swap到上面。
在某一點decrease-key的話,不會影響到上面的data,所以對那一個點max-heapify就好。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Complexity of Find-Min in MAX-Heap :
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
d-ary HEAP
Indexing Of Parent/Children
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Operation Complexity for max-heap
Insert :
Increase-key也是這樣,因為要游上去
- does 1 comparison at each level of the heap
- d上升=>高度下降=>變快
Max-Heapify :
extract-max/ decrease-key 也會是這樣
- does d element comparisons at each level of the heap
- d上升=>每一層要比較的變多=>變慢
Height Under Different <-對不同操作有不同影響
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Complexity under different
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Use Heap to implement queue/stack
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Queue
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Stack
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Treap - BST + Heap
BST invariant比較難maintain,所以先依照BST原則insert,之後有違反heap的property再用rotation的方式修正。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
重點是在兩個Heap(max-heap/ min-heap)
維持size的關係
Running median : 一直有數字進來
Max - Heap A < Min - Heap B
要刻意維持A和B之間的大小關係
2 case
- |A| = |B|+1 => extract-max from A, then insert it to B
- |B| = |A|+2 => extrac-min from B, then insert it to A
Complexity of insertion =


[重要] 找前 個大的所有數字
Max-Heap中前k個大的data並非單純就是Heap中的前面k個!



All elements in heap :
[Improvement]
Way 1 : Use Oder Statistics
- Use order statistic algorithm to find the th largest element. Please see the topic selection in worst-case linear time O(n)
- Use QuickSort Partition algorithm to partition around the kth largest number O(n).
- Sort the k-1 elements (elements greater than the th largest element) O(iLogi). This step is needed only if sorted output is required.
Time complexity: if we don’t need the sorted output, otherwise
https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/
概念上是利用一個輔助的heap,然後原本的heap不會更動到。
Merge Sort 不管 sequence的sorted程度,複雜度都一樣
insertion sort 在 sorted的情況下
Get largest element in heap -
Doesn't need to be sorted


[Advanced] Largest number from max-heap with elements -
看到要有求smallest/largest問題 => size 為 的heap/tree
Similar to problem "Merge k sorted array each with elements" and "Neighbor find in lower degree graph"
This problem is very tricky
盲點 - 懷疑insert到的children可能不會包含next largest
如何釐清盲點:用 高度計 的觀點來看,把中的所有data放進高度計,然後再把要insert進的data也加進去考慮
Part 1 : 以每個sub max-heap root建立一個Max-heap ()-
不會被變動
Part 2 : 在exract-max之後,把extract-max的data的下面child插入進
- 可能插入的child 數目
- 2個:sub max-heap下面的child
- 4個:每個sub max-heap的root
在對做extract-max,就是要求的 next largest
Complexity analysis
因為的size最多會有 個,所以每次的 insertion 和 extract-max 的複雜度都是,然後因為執行次,所以這部分的複雜度是

[Builind Block] Merge k sorted array each with elements

Naive way -
Technique : Same as the Merge Sort

Using Min-Heap -
- Build Heap of elements -
Insert the min of each sorted array into heap
- During each iteration -
extract-min of heap -
insert the next element in the same list -
Total complexity =
This technique is often used
extract-min -> insert
similar to the problem "找前k個大的所有數字"

c-Merge Sort
Similar to the problem "Merge k sorted array each with elements
如果在merge那個step沒有用min-heap的話,複雜度就會變成 =
每次要合併的c個 array 每個都是已經sorted 過的了!


[Special Case]If c is constant - =
Complexity =
因為c is constant =>

複雜度跟c的大小無關
[General Case]If c is function of - =

Nearly Sorted Sequence - fixed distance from final position
Given an array of n elements, where each element is at most k away from its target position, devise an algorithm that sorts in O(nlog k) time
Insertion sort :
Heap : Complexity =
https://www.geeksforgeeks.org/nearly-sorted-algorithm/
Application
Coolest person you want to talk to
用到2個heap
為了coolness:維持一個max-heap (H)
為了leave time:維持一個min-heap(H’)
如果有人arrive => insert進H’/H
時間到了要extract-min in H’,然後在H對應的node也要被delete
- 為了支援這個操作,需要維持一個array
(array的index是H’中的index)
(array的value是H中的index)


[這個不太會]



Similar idea
