Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms.

Slides:



Advertisements
Similar presentations
RAIK 283: Data Structures & Algorithms
Advertisements

AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
Design and Analysis of Algorithms – Chapter 41 Decrease and Conquer II Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Chapter 6: Transform and Conquer
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
Transform & Conquer Lecture 08 ITS033 – Programming & Algorithms
COSC 3100 Transform and Conquer
CS202 - Fundamental Structures of Computer Science II
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Binary Search Trees
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS 367 Introduction to Data Structures Lecture 8.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AA Trees.
Chapter 6 Transform-and-Conquer
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 6 Transform and Conquer.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Chapter 6: Transform and Conquer
Balanced Binary Search Trees
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
CS202 - Fundamental Structures of Computer Science II
Heaps and priority queues
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 62 b Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook’s companion websiteMost of the lecture notes are based on the slides from the Textbook’s companion website – Some examples and slides are based on lecture notes created by Dr. Ben Choi, Louisiana Technical University and Dr. Chuck Cusack, Hope College and Dr. Ellen Walker from Hiram CollegeSome examples and slides are based on lecture notes created by Dr. Ben Choi, Louisiana Technical University and Dr. Chuck Cusack, Hope College and Dr. Ellen Walker from Hiram College I have modified many of their slides and added new slides.I have modified many of their slides and added new slides. RAIK 283: Data Structures & Algorithms

An example problem b Checking element uniqueness in an array AlgorithmUniqueElements(A[0..n-1]) for m = 0 to n-2 do for k = m+1 to n-1 do if A[m] = A[k] return false if A[m] = A[k] return false return true b Can we come up with a better algorithm? Design and Analysis of Algorithms – Chapter 63

4 Transform and Conquer Solve problem by transforming it into: b a more convenient instance of the same problem (instance simplification) presortingpresorting Gaussian eliminationGaussian elimination b a different representation of the same instance (representation change) balanced search treesbalanced search trees heaps and heapsortheaps and heapsort polynomial evaluation by Horner’s rulepolynomial evaluation by Horner’s rule b a different problem altogether (problem reduction) compute lcm(m, n) = m*n/gcd(m,n)compute lcm(m, n) = m*n/gcd(m,n) reductions to graph problemsreductions to graph problems linear programminglinear programming

Design and Analysis of Algorithms – Chapter 55 Binary Search Trees (BSTs) b Binary Search Tree property A binary tree in which the key of an internal node is greater than the keys in its left subtree and less than or equal to the keys in its right subtree.A binary tree in which the key of an internal node is greater than the keys in its left subtree and less than or equal to the keys in its right subtree.

Design and Analysis of Algorithms – Chapter 56 Analysis of BST Operations b All of the BST operations have time complexity  (h), where h is the height of the tree b However, in the worst-case, the height may be  (n) where n is the number of internal nodes For example, a long chain tree structureFor example, a long chain tree structure b For a tree as balanced as possible, O(log 2 n) b The objective is to make the tree as balanced as possible Technique: Binary Tree RotationsTechnique: Binary Tree Rotations b Balanced search tree: AVL tree and 2-3 tree

Design and Analysis of Algorithms – Chapter 67 Left- and Right-Rotations b The BST property still holds after a rotation.

Design and Analysis of Algorithms – Chapter 68 Binary Tree Rotations b Left Rotation on (15, 25)

Design and Analysis of Algorithms – Chapter 69 Balanced trees: AVL trees b For every node, difference in height between left and right subtrees is at most 1 b AVL property is maintained through rotations, each time the tree becomes unbalanced b lg n ≤ h ≤ lg (n + 2) average: 1.01 lg n for large n b A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)

Design and Analysis of Algorithms – Chapter 610 Balance factor b Algorithm maintains balance factor (difference in height between a node’s left and right subtrees) for each node. For example:

Design and Analysis of Algorithms – Chapter 611 In-class exercise b Page 225: Exercise 6.3.1

Design and Analysis of Algorithms – Chapter 612 General case: single R-rotation

Design and Analysis of Algorithms – Chapter 613 When single L-rotation?

Single L-Rotation Design and Analysis of Algorithms – Chapter 614

Design and Analysis of Algorithms – Chapter 615 Double LR-rotation

Design and Analysis of Algorithms – Chapter 616 When double RL-rotation?

Double RL-rotation Design and Analysis of Algorithms – Chapter 617

Design and Analysis of Algorithms – Chapter 618 AVL tree rotations b Small examples: 1, 2, 31, 2, 3 3, 2, 13, 2, 1 1, 3, 21, 3, 2 3, 1, 23, 1, 2 b Large example: 5, 6, 8, 3, 2, 4, 7

Design and Analysis of Algorithms – Chapter 619 In-class exercise b Construct an AVL tree for the list: 4, 5, 7, 2, 1, 3, 6 by successive insertions. b Page 226: Exercise For each of the following lists, construct an AVL tree by inserting their elements successively, starting with the empty tree.For each of the following lists, construct an AVL tree by inserting their elements successively, starting with the empty tree. –A) 1, 2, 3, 4, 5, 6 –B) 6, 5, 4, 3, 2, 1 –C) 3, 6, 5, 1, 2, 4

Design and Analysis of Algorithms – Chapter 620 Balanced trees: AVL trees b For every node, difference in height between left and right subtrees is at most 1 b AVL property is maintained through rotations, each time the tree becomes unbalanced b lg n ≤ h ≤ lg (n + 2) average: 1.01 lg n for large n b Disadvantage: needs extra storage for maintaining node balance

Design and Analysis of Algorithms – Chapter Trees b Relax constraint that a node has 2 children b Allow 2-child nodes and 3-child nodes With bigger nodes, tree is shorter & branchierWith bigger nodes, tree is shorter & branchier 2-node is just like before (one item, two children)2-node is just like before (one item, two children) 3-node has two values and 3 children (left, middle, right)3-node has two values and 3 children (left, middle, right) b All leaves are in the same level <=x>x and <=y>y

Design and Analysis of Algorithms – Chapter 622 Why 2-3 tree b Faster searching? Actually, no. 2-3 tree is about as fast as an “equally balanced” binary tree, because you sometimes have to make 2 comparisons to get past a 3-nodeActually, no. 2-3 tree is about as fast as an “equally balanced” binary tree, because you sometimes have to make 2 comparisons to get past a 3-node b Easier to keep balanced? Yes, definitely.Yes, definitely. Insertion can split 3-nodes into 2-nodes, or promote 2- nodes to 3-nodes to keep tree balanced!Insertion can split 3-nodes into 2-nodes, or promote 2- nodes to 3-nodes to keep tree balanced!

Design and Analysis of Algorithms – Chapter 623 Inserting into 2-3 Tree b As for binary tree, start by searching for the item b If you don’t find it, and you stop at a 2-node, upgrade the 2-node to a 3-node. b If you don’t find it, and you stop at a 3-node, you can’t just add another value. So, replace the 3-node by 2 2-nodes and push the middle value up to the parent node b Repeat recursively until you upgrade a 2-node or create a new root.

Design and Analysis of Algorithms – Chapter 624 An example b Construction of a 2-3 tree for the list 9, 5, 8, 3, 2, 4, 7the list 9, 5, 8, 3, 2, 4, 7

Design and Analysis of Algorithms – Chapter trees b When is a new root created? b What is the height of a 2-3 tree?

Design and Analysis of Algorithms – Chapter trees b When is a new root created? b What is the height of a 2-3 tree?  (logn) (refer to Page )  (logn) (refer to Page )

Design and Analysis of Algorithms – Chapter 627 Why is this better? b Intuitively, you unbalance a binary tree when you add height to one path significantly more than other possible paths. b With the 2-3 tree insertion algorithm, you can only add height to the tree when you create a new root, and this adds one unit of height to all paths simultaneously.

Design and Analysis of Algorithms – Chapter 628 In-Class Exercise 31 Solution

Design and Analysis of Algorithms – Chapter 629 After-class exercise b How to delete a node from a 2-3 Tree? delete a node from a 2-3 Treedelete a node from a 2-3 Tree

In-Class Exercises (II) b Design “transform and conquer” algorithms Design and Analysis of Algorithms – Chapter

Priority Queue Implementation? b A priority queue is the abstract data type (ADT) of an ordered set with the operations: find element with highest priorityfind element with highest priority delete element with highest prioritydelete element with highest priority insert element with assigned priorityinsert element with assigned priority b What data structure should we use to implement a priority queue efficiently? Design and Analysis of Algorithms – Chapter 631