design and analysis of algorithms – chapter 61 transform and conquer dr. ying lu [email protected]...

31
Design and Analysis of Algorithms – Chapter 6 1 Transform and Conquer Dr. Ying Lu [email protected] RAIK 283: Data Structures & RAIK 283: Data Structures & Algorithms Algorithms

Upload: quentin-phillips

Post on 19-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 1

Transform and Conquer

Dr. Ying [email protected]

RAIK 283: Data Structures & RAIK 283: Data Structures & AlgorithmsAlgorithms

Page 2: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 2

Giving credit where credit is due:Giving credit where credit is due:• Most of the lecture notes are based on the slides from Most of the lecture notes are based on the slides from

the Textbook’s companion websitethe Textbook’s companion website– http://www.aw-bc.com/info/levitinhttp://www.aw-bc.com/info/levitin

• Some examples and slides are based on lecture notes Some examples and slides are based on lecture notes created by Dr. Ben Choi, Louisiana Technical University created by Dr. Ben Choi, Louisiana Technical University and Dr. Chuck Cusack, Hope College and Dr. Ellen and Dr. Chuck Cusack, Hope College and Dr. Ellen Walker from Hiram CollegeWalker from Hiram College

• I have modified many of their slides and added new I have modified many of their slides and added new slides.slides.

RAIK 283: Data Structures & RAIK 283: Data Structures & AlgorithmsAlgorithms

Page 3: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

An example problemAn example problem

Checking element uniqueness in an arrayChecking element uniqueness in an array

AlgorithmAlgorithm UniqueElements(A[0..n-1])UniqueElements(A[0..n-1])

for m = 0 to n-2 dofor m = 0 to n-2 do

for k = m+1 to n-1 dofor k = m+1 to n-1 do

if A[m] = A[k] return falseif A[m] = A[k] return false

return truereturn true

Can we come up with a better algorithm? Can we come up with a better algorithm?

Design and Analysis of Algorithms – Chapter 6 3

Page 4: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 4

Transform and ConquerTransform and Conquer

Solve problem by transforming it into:Solve problem by transforming it into: a more convenient instance of the same problem (a more convenient instance of the same problem (instance simplificationinstance simplification))

• presortingpresorting• Gaussian eliminationGaussian elimination

a different representation of the same instance (a different representation of the same instance (representation changerepresentation change))• balanced search treesbalanced search trees• heaps and heapsortheaps and heapsort• polynomial evaluation by Horner’s rulepolynomial evaluation by Horner’s rule

a a different problem altogether (different problem altogether (problem reductionproblem 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 programming linear programming

Page 5: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 5 5

Binary Search Trees (BSTs)Binary Search Trees (BSTs)

Binary Search Tree propertyBinary Search Tree property• A binary tree in which the key of an internal node is A binary tree in which the key of an internal node is

greater than the keys in its left subtree and less than or greater than the keys in its left subtree and less than or equal to the keys in its right subtree. equal to the keys in its right subtree.

Page 6: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 5 6

Analysis of BST OperationsAnalysis of BST Operations

All of the BST operations have time complexity All of the BST operations have time complexity ((hh), ), where where hh is the height of the tree is the height of the tree

However, in the worst-case, the height may be However, in the worst-case, the height may be ((nn) ) where where nn is the number of internal nodes is the number of internal nodes• For example, a long chain tree structureFor example, a long chain tree structure

For a tree as balanced as possible, O(logFor a tree as balanced as possible, O(log22 nn)) The objective is to make the tree as balanced as The objective is to make the tree as balanced as

possiblepossible• Technique: Binary Tree Rotations Technique: Binary Tree Rotations

Balanced search tree: AVL tree and 2-3 treeBalanced search tree: AVL tree and 2-3 tree

Page 7: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 7

Left- and Right-RotationsLeft- and Right-Rotations

The BST property still holds after a rotation.The BST property still holds after a rotation.

Page 8: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 8

Binary Tree RotationsBinary Tree Rotations

Left Rotation on (15, 25) Left Rotation on (15, 25)

Page 9: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 9

Balanced trees: AVL treesBalanced trees: AVL trees

For every node, difference in height between left and right For every node, difference in height between left and right subtrees is at most 1subtrees is at most 1

AVL property is maintained through AVL property is maintained through rotationsrotations, each time , each time the tree becomes unbalancedthe tree becomes unbalanced

lg lg nn ≤ ≤ hh ≤ 1.4404 lg ( ≤ 1.4404 lg (n n + 2) - 1.3277 + 2) - 1.3277 average:average: 1.01 lg 1.01 lg n + n + 0.1 for large 0.1 for large nn

A similar idea: red-black trees (height of subtrees is A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)allowed to differ by up to a factor of 2)

Page 10: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 10

Balance factorBalance factor

Algorithm maintains Algorithm maintains balance factorbalance factor (difference in height between a (difference in height between a node’s left and right subtrees) for each node. For example:node’s left and right subtrees) for each node. For example:

Page 11: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 11

In-class exerciseIn-class exercise

Page 225: Exercise 6.3.1Page 225: Exercise 6.3.1

Page 12: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 12

General case: single R-rotationGeneral case: single R-rotation

Page 13: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 13

When single L-rotation?When single L-rotation?

Page 14: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Single L-RotationSingle L-Rotation

Design and Analysis of Algorithms – Chapter 6 14

Page 15: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 15

Double LR-rotationDouble LR-rotation

Page 16: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 16

When double RL-rotation?When double RL-rotation?

Page 17: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Double RL-rotationDouble RL-rotation

Design and Analysis of Algorithms – Chapter 6 17

Page 18: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 18

AVL tree rotationsAVL tree rotations

Small examples:Small examples:• 1, 2, 31, 2, 3

• 3, 2, 1 3, 2, 1

• 1, 3, 21, 3, 2

• 3, 1, 23, 1, 2

Large example: 5, 6, 8, 3, 2, 4, 7Large example: 5, 6, 8, 3, 2, 4, 7

Page 19: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 19

In-class exerciseIn-class exercise

Construct an AVL tree for the list: 4, 5, 7, 2, 1, 3, 6 Construct an AVL tree for the list: 4, 5, 7, 2, 1, 3, 6 by successive insertions. by successive insertions.

Page 226: Exercise 6.3.4Page 226: Exercise 6.3.4• For each of the following lists, construct an AVL tree by For each of the following lists, construct an AVL tree by

inserting their elements successively, starting with the inserting their elements successively, starting with the empty tree. empty tree.

– A) 1, 2, 3, 4, 5, 6A) 1, 2, 3, 4, 5, 6

– B) 6, 5, 4, 3, 2, 1B) 6, 5, 4, 3, 2, 1

– C) 3, 6, 5, 1, 2, 4C) 3, 6, 5, 1, 2, 4

Page 20: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 20

Balanced trees: AVL treesBalanced trees: AVL trees

For every node, difference in height between left For every node, difference in height between left and right subtrees is at most 1and right subtrees is at most 1

AVL property is maintained through AVL property is maintained through rotationsrotations, , each time the tree becomes unbalancedeach time the tree becomes unbalanced

lg lg nn ≤ ≤ hh ≤ 1.4404 lg ( ≤ 1.4404 lg (n n + 2) - 1.3277 + 2) - 1.3277 average:average: 1.01 lg 1.01 lg n + n + 0.1 for large 0.1 for large nn

Disadvantage: needs extra storage for maintaining Disadvantage: needs extra storage for maintaining node balancenode balance

Page 21: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 21

2-3 Trees2-3 Trees

Relax constraint that a node has 2 childrenRelax constraint that a node has 2 children Allow 2-child nodes and 3-child nodesAllow 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)

All leaves are in the same levelAll leaves are in the same level

< x , y>

<=x >x and <=y >y

Page 22: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 22

Why 2-3 treeWhy 2-3 tree

Faster searching?Faster searching?• Actually, no. 2-3 tree is about as fast as an “equally Actually, no. 2-3 tree is about as fast as an “equally

balanced” binary tree, because you sometimes have to balanced” binary tree, because you sometimes have to make 2 comparisons to get past a 3-nodemake 2 comparisons to get past a 3-node

Easier to keep balanced?Easier to keep balanced?• Yes, definitely.Yes, definitely.• Insertion can split 3-nodes into 2-nodes, or promote 2-Insertion can split 3-nodes into 2-nodes, or promote 2-

nodes to 3-nodes to keep tree balanced!nodes to 3-nodes to keep tree balanced!

Page 23: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 23

Inserting into 2-3 TreeInserting into 2-3 Tree

As for binary tree, start by searching for the itemAs for binary tree, start by searching for the item If you don’t find it, and you stop at a 2-node, upgrade the If you don’t find it, and you stop at a 2-node, upgrade the

2-node to a 3-node.2-node to a 3-node. If you don’t find it, and you stop at a 3-node, you can’t just 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 add another value. So, replace the 3-node by 2 2-nodes and push the middle value up to the parent nodepush the middle value up to the parent node

Repeat recursively until you upgrade a 2-node or create a Repeat recursively until you upgrade a 2-node or create a new root. new root.

Page 24: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 24

An exampleAn example

Construction of a 2-3 tree for Construction of a 2-3 tree for • the list 9, 5, 8, 3, 2, 4, 7the list 9, 5, 8, 3, 2, 4, 7

Page 25: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 25

2-3 trees2-3 trees

When is a new root created?When is a new root created?

What is the height of a 2-3 tree? What is the height of a 2-3 tree?

Page 26: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 26

2-3 trees2-3 trees

When is a new root created?When is a new root created?

What is the height of a 2-3 tree? What is the height of a 2-3 tree? (logn) (refer to Page 224-225)(logn) (refer to Page 224-225)

Page 27: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 27

Why is this better?Why is this better?

Intuitively, you unbalance a binary tree when you add Intuitively, you unbalance a binary tree when you add height to one path significantly more than other height to one path significantly more than other possible paths.possible paths.

With the 2-3 tree insertion algorithm, you can only add With the 2-3 tree insertion algorithm, you can only add height to the tree when you create a new root, and this height to the tree when you create a new root, and this adds one unit of height to all paths simultaneously.adds one unit of height to all paths simultaneously.

Page 28: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 28

In-Class ExerciseIn-Class Exercise

31

Solution

Page 29: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms – Chapter 6 29

After-class exerciseAfter-class exercise

How to How to delete a node from a 2-3 Tree??

Page 30: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

In-Class Exercises (II)In-Class Exercises (II)

Design “transform and conquer” algorithmsDesign “transform and conquer” algorithms• 6.1.7 6.1.7

Design and Analysis of Algorithms – Chapter 6 30

• 6.1.8 6.1.8

Page 31: Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu ylu@cse.unl.edu RAIK 283: Data Structures & Algorithms

Priority Queue Implementation? Priority Queue Implementation?

A A priority queue priority queue is the abstract data type (ADT) of is the abstract data type (ADT) of an ordered set with the operations:an ordered set with the operations:• find element with highest priority find element with highest priority

• delete element with highest priority delete element with highest priority

• insert element with assigned priority insert element with assigned priority

What data structure should we use to implement a What data structure should we use to implement a priority queue efficiently?priority queue efficiently?

Design and Analysis of Algorithms – Chapter 6 31