COMP3600/6466 –Algorithms...week6-Heaps-aftClass Author Hanna Kurniawati Created Date 9/3/2020...

Post on 29-Sep-2020

0 views 0 download

Transcript of COMP3600/6466 –Algorithms...week6-Heaps-aftClass Author Hanna Kurniawati Created Date 9/3/2020...

COMP3600/6466 – Algorithms Abstract Data Structures Cont.:

Heaps + AVL Tree[CLRS 6.3-6.5], [Lev sec. 6.3]

Hanna Kurniawati

https://cs.anu.edu.au/courses/comp3600/Comp_3600_6466@anu.edu.au

A1• Marks out last Wednesday • Sent to your ANU email around noon time• Very good results• #submissions: 293• Average: 74.28 ; Median: 80 ; Max: 99.5• #HD: 147 ; #D: 53 ; #Cr: 34 ; #P: 27• Feedback sessions will be arranged for each

marking group in MS Teams• Marking group is stated in the email about your mark• Marking group ≠ Tutorial group• Watch out in piazza, we’ll release the schedule tonight• Solution example is in the class website

A2• All concept/theory needed to answer A2 have

been covered in lectures prior to week 5• C/C++ programming necessary has been

covered in tutorial in week 5• Programming component• Template for I/O of the program is in the class website• A few test cases are in the class website• Note: We will test your program with test cases we did

not release. Therefore, do NOT extrapolate specifications from the test cases. Specifications are as defined in the problem description

Due dates• Final Project – Milestone 1• Due: 7 Sep 23:59 (1st day of mid-term break)• Grace period ends: 8 Sep 13:00• A2• Due: 21 Sep 23:59 (1st day back after mid-term

break)• Grace period ends: 22 Sep 13:00• All time are in Canberra time• Submissions: Wattle, save as draft

Cheating is NOT Tolerated• Cheating includes intention to outsource

assignments• E.g.: Uploading assignment questions to a “tutoring

service” website, even if you end up not using their answers

• In A1, the majority of issues are found because of copying answers from “tutoring service(s)”, which happened to be wrong!!! • Dropping the class does not mean the

misconduct process will stop nor the student’s record of misconduct will be removed. If proven, it will stay in the record!

TopicsüBinary Search Tree• Heaps• AVL Tree• Red-black Tree

TopicsüBinary Search Tree• HeapsüWhat is it?üHeapify• Insertion & Building• Extract/Deletion• Applications• AVL Tree• Red-black Tree

• We’ll compute the total time as the number of times max-heapify is called multiply by the cost of each max-heapify:

𝑇 𝑛 = $!"#

$%& '𝑛

2!()𝑂(ℎ)

≤ 𝑐. $!"#

$%& '𝑛2!ℎ

≤ 𝑐. 𝑛. ∑!"#* !+!

UseCLRSeq.A.8.∑!"#* !+!=

"#

), "## = 2

≤ 𝑐. 𝑛. 2≤ 𝑐-. 𝑛= 𝑂(𝑛)

Time complexity for build-max-heap

TopicsüBinary Search Tree• HeapsüWhat is it?üHeapifyüInsertion & Building• Extract/Deletion• Applications• AVL Tree• Red-black Tree

ExtractMax• Intuitively:• Output the root node• Swap the root node with the last node in the heap (i.e.,

the bottom and right most leaf)• Decrease the heap size by 1, essentially removing the

last leaf node (which is now the same as the root node we just extracted)

• Heapify the new root of the tree

ExtractMax• Pseudo-code [CLRS pp.163]

• The algorithm only needs to traverse a path of the tree once. Hence, the complexity 𝑂(log 𝑛)

Example

TopicsüBinary Search Tree• HeapsüWhat is it?üHeapifyüInsertion & BuildingüExtract/Deletion• Applications• AVL Tree• Red-black Tree

Applications: HeapSort• Heapsort:• Build the heap and extract the heap element one by

one• Complexity: 𝑂(𝑛 log 𝑛)• Building the heap takes 𝑂(𝑛)• Heapifying the rest of the heap every time an

element in extracted takes a total 𝑂(𝑛 log 𝑛)• In practice, QuickSort (assuming proper

implementation) is faster than HeapSort• But, worst case of heapsort is 𝑛 log 𝑛 & in-place

Applications: Priority Queue• A priority queue is a data structure that:• Maintains a set S, where each element is

associated with a key• Has the following operations:• Insert(𝑆, 𝑥) : Inserts element x into the set S• Maximum(𝑆) : Returns an element of S with the

largest key• ExtractMax(𝑆) : Removes and returns an element of

S with the largest key• IncreaseKey (𝑆, 𝑥, 𝑘): Increase the key of 𝑥 to 𝑘

TopicsüBinary Search Tree• HeapsüWhat is it?üHeapifyüInsertion & BuildingüExtract/DeletionüApplications• AVL Tree• Red-black Tree

TopicsüBinary Search TreeüHeaps• AVL Tree• Red-black Tree

Next: AVL Tree