assignment 1 marking · assignment 1 marking •assignment 1 marking is very lenient •for...

50
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!!!

Upload: others

Post on 13-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

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 Data Structure:

üBinary Search Tree ü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 Search Tree ü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 binary search tree, 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 leafEmpty 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 +𝑁+/0 + 𝑁+/1• Solve the recurrence

Solving the Recurrence• 𝑁+ = 1 + 𝑁+/0 + 𝑁+/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 Heap?• A heap is a binary tree 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• 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: 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 𝑘

Next week: Finish Off Transform & Conquer

ANDStarts Dynamic Programming