Assignment 1 Marking • Assignment 1 marking is very lenient • For example, in Q2a. If you use Master’s Theorem on the bound if the recurrence function you want to solve, most likely, you’ll need to verify the solution provided by Master’s Theorem • We did not penalise those answers to Q2a that uses Master’s Theorem without verification. • However, we will not be that lenient in marking the final exam. So, please: • Take a look at the solution example for Part A (in the class website) • And make sure you understand them. If you don’t, ask!!! Total Marks for this class • Final Mark = 0.2*Max(A1, E) + 0.2*Max(A2, E) + 0.2*Max(A3, E) + 0.4*E • A1, A2, A3: mark for Assignment 1, 2, 3 respectively • E: marks for final exam • Tips: Please do NOT ignore assignments. You really do NOT want to rely all your marks solely on the final exam Assignment 1 Issue • When checking for plagiarism, we found A1 questions have been uploaded to a homework outsourcing site (masquerading as a “legitimate homework help” site) on 6, 18, 19, 21 Aug • We’ve contacted them. They send us the answers and investigations are on-going. • Btw., most of the answers are wrong!!! • So, please do risk analysis!!! • Risks of outsourcing: • Getting caught, which will stay in one’s academic record • Being blackmailed by the service provider • Gain: Wrong answers for a redeemable assignment!!! Assignment 2 • Due: Mon 30 Sep 23:59; Grace period Tue 1 Oct 13:00 • Additional clarifications: 10 Sep • We’ve sent the announcement via email (you need to be enrolled in piazza) • Please redownload if you have downloaded before 10 Sep • This week tutorial will solely be on help for A2 • Hope this alleviate the issue of not enough tutorial time spent for assignment help • Drop in session: • Friday 20 Sep, 27 Sep. Both on 8am-10am at CSIT N115/116 • If needed, you can join other tutorial groups • Please e-mail [email protected] first, to ensure the tutorial group you intend to attend will have space for you Before the Break üWhat is Transform & Conquer in Algorithm Design? üInstance Simplification üCommon method: Pre-sorting üA bit more about sorting üRepresentation Change üSupporting : üBinary Search üRed-Black Tree • AVL Tree • Heaps • Examples • Problem Reduction Today üWhat is Transform & Conquer in Algorithm Design? üInstance Simplification üCommon method: Pre-sorting üA bit more about sorting üRepresentation Change üSupporting Data Structure: üBinary üRed-Black Tree • AVL Tree • Heaps • Examples • Problem Reduction COMP3600/6466 – Algorithms Transform & Conquer 3 [Lev 6.3, CLRS ch. 6]

Hanna Kurniawati

https://cs.anu.edu.au/courses/comp3600/ Today • AVL Tree • What is it? • Transformation • Insertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion What is AVL Tree? • The first self-balancing , invented in 1962 by Adel’son-Vel’skii and Landis (written in Russian) • The goal is the same as in red-black tree, which is we want to have the height of the tree to be �(log �), with � being the #nodes in the tree • Intuitively, an AVL tree maintains balance by ensuring the height of the left and right sub-tree of all nodes in the tree are almost the same (i.e., differ by at most 1) What is AVL Tree? • More formally, an AVL tree is a binary search tree where • Each node is augmented with the balance factor of the node Balance factor of node � = height(leftSubtree(�)) – height(rightSubtree(�)) Height of a tree/sub-tree is the length of the longest path from the root of the tree/sub-tree to a leaf Empty height (e.g., when � has no left/right subtree) is -1 • The balance factor of each node in the tree must either be either 0, +1, or -1 Example Why Is The Height of AVL Tree �(log �)? • We’ll show the height of an AVL tree by computing the minimum number of nodes (aka worst case) that an AVL tree of height ℎ could have

• Suppose � is the minimum number of nodes for an AVL tree of height ℎ . • In AVL tree, the minimum number of nodes is achieved when every node in the tree have a balance factor of +/-1

• Therefore, � = 1 + � + � • Solve the recurrence Solving the Recurrence

• � = 1 + � + � Today • AVL Tree üWhat is it? • Transformation • Insertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion Key to AVL • Similar to red-black tree, AVL tree relies on transformation operation, called rotation, to re-balance the tree Transformation: Rotation • Four types: 2 classes, 2 types in each class • Single Left Rotation (L- rotation) • L-rotation on node �: Rotate the edge connecting � and its left child • Single Right Rotation (R- rotation): • Mirror image of L-rotation Transformation: Rotation • Four types: 2 classes, 2 types in each class • Double Left Right Rotation (LR-rotation) • LR-rotation of node �: L- rotation on the root of �’s left subtree followed by R- rotation on � • Double Right Left Rotation (RL-rotation) • Mirror image of LR-rotation

Today • AVL Tree üWhat is it? üTransformation • Insertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion Insertion • Essentially, perform binary search tree insertion and then rebalance whenever necessary • To insert a node � to an AVL tree T: • Insert � as if T is a usual binary search tree • Let � be the parent of the newly inserted node • While � is not root • If � violates the AVL property, rotate to restore AVL property • Update Balance Factor of � • Let � = �. ������ Insertion – Rebalancing Cases • Insertion of left-left grandchild cause inbalance • R-rotation

• Insertion of right-right grandchild cause inbalance • Same as above but other way around: L-rotation Insertion – Rebalancing Cases • Insertion of right-left grandchild cause inbalance • RL-rotation

• Insertion of left-right grandchild cause inbalance • Same as above but other way around: LR-rotation Example

Today • AVL Tree üWhat is it? üTransformation üInsertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion Deletion • Similar to insertion: Essentially, binary search tree deletion and then rebalance whenever necessary • Recall binary search tree deletion has 3 cases based on #children of the node being deleted • 0 children: Just delete à Nodes that might change height are those in the path from deleted node to root • 1 child: Delete it, connect child to parent à Nodes that might change height are those in the path from deleted node to root • 2 children: Replace the deleted node with its successor, successor leaf à Nodes that might change height are those in the path from deleted successor leaf to root • Rebalance along the path where nodes might change height Deletion – Rebalancing Cases • Left-left grandchild becomes too tall • R-rotation

• Right-right grandchild becomes too tall • Same as above but other way around: L-rotation Insertion – Rebalancing Cases • Right-left grandchild becomes too tall • RL-rotation

• Left-right grandchild becomes too tallcause inbalance • Same as above but other way around: LR-rotation Example Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion What is A ? • A heap is a that satisfies heap property: • A heap is a complete binary tree • The nodes contain data similar to binary search tree: • Each node has a key, in which node order are based on • Each node may contain additional information • The parent node has key greater than the keys in its children • Complete binary tree: A perfect binary tree where at the last level, some rightmost leaves may be missing • Perfect binary tree: A tree where all interior nodes have 2 children and all leaves are at the same level • Since a heap is a complete tree, it can be implemented easily with an array • Left & right children becomes index of the array that holds the left & right child nodes What is A Heap? • There’s 2 types of heap: Max-heap and Min-heap • The one we discussed in the previous slide is Max-heap • Throughout this class, we’ll assume the heap is Max-heap unless stated otherwise • Min-heap is similar to Max-heap but, the parent node has key less than the keys in its children Example What is A Heap? • Main Operations: • Heapify to ensure heap properties are satisfied • Insert a node to an existing heap • ExtractMax (for Min-heap, this operation is ExtractMin) to retrieve an element from the heap • All of the above operations are �(log �), where � is the number of nodes in the tree Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps üWhat is it? • Heapify • Insertion • Extract/Deletion Heapify: Maintaining Heap Property • Intuitively, heapify a node means traversing the tree from root until a suitable place (to ensure heap order) is found for the node • Pseudo-code [CLRS sec. 6.2] Example Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps üWhat is it? üHeapify • Insertion • Extract/Deletion Building a heap • Build a heap from an array of numbers, pseudo-code Example Inserting a node to a heap • Can also be used to build a heap when data is received dynamically • Intuitively, add a new node at the bottom right most heap and then find a suitable position for the new node • Pseudo-code [CLRS pp.164] Example Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps üWhat is it? üHeapify üInsertion • Extract/Deletion ExtractMax • Intutitively: • 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 Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion üHeaps üWhat is it? üHeapify üInsertion üExtract/Deletion Applications: • Heapsort: • Combine building the heap and extracting the heap element • 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 Applications: • A priority queue is a data structure that: • Maintains a 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 � Next week: Finish Off Transform & Conquer AND Starts