Download presentation
Presentation is loading. Please wait.
Published byQuentin Phillips Modified over 9 years ago
1
Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms
2
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 – http://www.aw-bc.com/info/levitin 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
3
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
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
5
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.
6
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
7
Design and Analysis of Algorithms – Chapter 67 Left- and Right-Rotations b The BST property still holds after a rotation.
8
Design and Analysis of Algorithms – Chapter 68 Binary Tree Rotations b Left Rotation on (15, 25)
9
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 ≤ 1.4404 lg (n + 2) - 1.3277 average: 1.01 lg n + 0.1 for large n b A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)
10
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:
11
Design and Analysis of Algorithms – Chapter 611 In-class exercise b Page 225: Exercise 6.3.1
12
Design and Analysis of Algorithms – Chapter 612 General case: single R-rotation
13
Design and Analysis of Algorithms – Chapter 613 When single L-rotation?
14
Single L-Rotation Design and Analysis of Algorithms – Chapter 614
15
Design and Analysis of Algorithms – Chapter 615 Double LR-rotation
16
Design and Analysis of Algorithms – Chapter 616 When double RL-rotation?
17
Double RL-rotation Design and Analysis of Algorithms – Chapter 617
18
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
19
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 6.3.4 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
20
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 ≤ 1.4404 lg (n + 2) - 1.3277 average: 1.01 lg n + 0.1 for large n b Disadvantage: needs extra storage for maintaining node balance
21
Design and Analysis of Algorithms – Chapter 621 2-3 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
22
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!
23
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.
24
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
25
Design and Analysis of Algorithms – Chapter 625 2-3 trees b When is a new root created? b What is the height of a 2-3 tree?
26
Design and Analysis of Algorithms – Chapter 626 2-3 trees b When is a new root created? b What is the height of a 2-3 tree? (logn) (refer to Page 224-225) (logn) (refer to Page 224-225)
27
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.
28
Design and Analysis of Algorithms – Chapter 628 In-Class Exercise 31 Solution
29
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
30
In-Class Exercises (II) b Design “transform and conquer” algorithms 6.1.76.1.7 Design and Analysis of Algorithms – Chapter 630 6.1.86.1.8
31
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.