Chapter 6 Transform and Conquer.

Slides:



Advertisements
Similar presentations
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Advertisements

AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
Chapter 6: Transform and Conquer
CS202 - Fundamental Structures of Computer Science II
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
BCA-II Data Structure Using C
Data Structures – LECTURE Balanced trees
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Search Trees.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Red Black Trees
Chapter 6 Transform-and-Conquer
CS202 - Fundamental Structures of Computer Science II
Chapter 11: Multiway Search Trees
Lecture 16 Multiway Search Trees
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.
Introduction Applications Balance Factor Rotations Deletion Example
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree.
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Sorting.
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance [eg sorted] of the same problem.
CS202 - Fundamental Structures of Computer Science II
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.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Copyright © Aiman Hanna All rights reserved
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
CSE 332: Data Abstractions AVL Trees
Lecture No.20 Data Structures Dr. Sohail Aslam
Advanced Implementation of Tables
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CSC 380: Design and Analysis of Algorithms
การวิเคราะห์และออกแบบขั้นตอนวิธี
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Chapter 6 Transform and Conquer

Transform and Conquer

Presorting Many Questions about a list is easier to answer if the list is sorted. So Presorting is an idea to sort the list before applying the actual method on that. for sorting list :- O(n log n) (Merge sort)

Example 1 Checking element uniqueness in an array:- Brute force:- algorithm compared pairs of the array’s element until either two equal elements were found or no more pair remains. Worst case efficiency O(n2) Presorting-based algorithm Stage 1: sort by efficient sorting algorithm (e.g. merge sort) Stage 2: scan array to check pairs of adjacent elements

Example 2 Computing a Mode:- A mode is a value that occur most often in a given list of numbers. Brute Force:- Scan complete list and count occurrence. Worst case :- O(n2) for comparison and (n-1) for finding highest occurrence element.

Efficiency:- O(n lg n)

Example 3 :- Searching Problem: Search for a given K in A[0..n-1] Presorting-based algorithm: Stage 1 Sort the array by an efficient sorting algorithm Stage 2 Apply binary search Efficiency: Θ(nlog n) + O(log n) = Θ(nlog n) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted? Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-4

Balanced Search Trees Attractiveness of binary search tree is marred by the bad (linear) worst-case efficiency. Two ideas to overcome it are: -to rebalance binary search tree when a new insertion makes the tree “too unbalanced” --AVL trees --red-black trees -to allow more than one key and two children --2-3 trees --2-3-4 trees --B-trees Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-14

Binary Search Trees (BST) A data structure for efficient searching, insertion and deletion Binary search tree property For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X

Binary Search Trees A binary search tree Not a binary search tree

Balanced trees: AVL(Adelson-Velskii and Landis‘) Tree Definition An AVL tree is a binary search tree in which, for every node, the difference between the heights of its left and right subtrees, called the balance factor, is at most 1 (with the height of an empty tree defined as -1) Tree (a) is an AVL tree; tree (b) is not an AVL tree Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-15

Rotations Single R-rotation Double LR-rotation If a key insertion violates the balance requirement at some node, the subtree rooted at that node is transformed via one of the four rotations. (The rotation is always performed for a subtree rooted at an “unbalanced” node closest to the new leaf.) Single R-rotation Double LR-rotation A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-16 Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

General case: Single R-rotation 6-17 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6

General case: Double LR-rotation 6-18 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6

Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 AVL tree construction Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-19

AVL tree construction - an example (cont.) 6-20 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6

Analysis of AVL trees N(h-1) + N(h-2)  N(h) --Search and insertion are O(log n) --Deletion is more complicated but is also O(log n) --Disadvantages: --frequent rotations --complexity N(h-1) + N(h-2)  N(h) Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-21

Multiway Search Trees k1 < k2 < … < kn-1 < k1 [k1, k2 ) Definition A multiway search tree is a search tree that allows more than one key in the same node of the tree. Definition A node of a search tree is called an n-node if it contains n-1 ordered keys (which divide the entire key range into n intervals pointed to by the node’s n links to its children): Note: Every node in a classical binary search tree is a 2-node k1 < k2 < … < kn-1 < k1 [k1, k2 )  kn-1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-22

2-3 Tree Definition A 2-3 tree is a search tree that -- may have 2-nodes and 3-nodes -- height-balanced (all leaves are on the same level) A 2-3 tree is constructed by successive insertions of keys given, with a new key always inserted into a leaf of the tree. If the leaf is a 3-node, it’s split into two with the middle key promoted to the parent. Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-23

2-3 tree construction – an example Construct a 2-3 tree the list 9, 5, 8, 3, 2, 4, 7 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-24

Analysis of 2-3 trees -- Search, insertion, and deletion are in (log n) -- The idea of 2-3 tree can be generalized by allowing more keys per node -- 2-3-4 trees -- B-trees Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-25

Minimum Maximum Problem:- How many comparisons are necessary to determine the minimum of a set of n elements? No of Comparisons = n-1

Better way for finding Minimum and Maximum:- Strategy:- Rather than procession each elements of the input but comparing it against the current minimum and maximum, at a cost of 2 comparisons per elements. We process elements in pairs. Compare pairs of element from input first with each other, and then compare the smaller to the current minimum and the larger to the current maximum, at a cost of comparisons for every 2 elements. Initial values for current minimum and maximum depends on whether n is odd or even. If n is odd, than initial value is first element and than process the rest on pairs. If n is even than initial value is comparison of first 2 element and rest in pairs. No of comparison :-