1 lecture 11 polynomials and tree sort 2 introduction evaluating polynomial functions horner’s...

46
1 Lecture 11 Lecture 11 POLYNOMIALS and Tree sort POLYNOMIALS and Tree sort

Upload: bernadette-armstrong

Post on 31-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

1

Lecture 11Lecture 11

POLYNOMIALS and Tree sortPOLYNOMIALS and Tree sort

Page 2: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

2

INTRODUCTIONINTRODUCTION

EVALUATING POLYNOMIAL EVALUATING POLYNOMIAL FUNCTIONSFUNCTIONS

Horner’s methodHorner’s method PermutationPermutation Tree sortTree sort

Page 3: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

3

Page 4: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

4

INTRODUCTIONINTRODUCTION

The problems examined in this lecture are The problems examined in this lecture are polynomial evaluation (with and without polynomial evaluation (with and without preprocessing of the coefficients), preprocessing of the coefficients), polynomial multiplication, and multiplication polynomial multiplication, and multiplication of matrices and vectorsof matrices and vectors..

Several algorithms in this lecture use the Several algorithms in this lecture use the divide-and-conquer method: evaluating a divide-and-conquer method: evaluating a polynomial with preprocessing of polynomial with preprocessing of coefficients, Strassen’s matrix multiplication coefficients, Strassen’s matrix multiplication algorithm.algorithm.

Page 5: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

5

EVALUATING POLYNOMIAL EVALUATING POLYNOMIAL FUNCTIONSFUNCTIONS

Consider the polynomial:Consider the polynomial:P(x) = aP(x) = annx^x^nn + a + an-1n-1x^x^n-1n-1 + … + a + … + a11x +ax +a00

with real coefficients and n>=1with real coefficients and n>=1

Suppose the coefficients of aSuppose the coefficients of a00, a, a11, …, a, …, ann and x and x are given and that the problem is the are given and that the problem is the evaluate p(x).evaluate p(x).

In this section we look at some algorithms In this section we look at some algorithms and some lower bounds for this problem.and some lower bounds for this problem.

Page 6: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

6

Page 7: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

7

Page 8: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

8

Page 9: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

Synthetic Division is a process whereby the quotient and remainder can be determined when a polynomial function f is divided by g(x) = x - c.

Page 10: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

Use synthetic division to find the quotient and remainder when f x x x x

g x x

( )

( ) .

3 2 10

3

4 3 2 is divided by

x x 3 3

3 3 -1 2 0 -10

Page 11: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

3 3 -1 2 0 -10

3

Page 12: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

3 3 -1 2 0 -10

- 9

3 -10

Page 13: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

3 3 -1 2 0 -10

- 9 30

3 -10 32

Page 14: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

3 3 -1 2 0 -10

- 9 30 - 96

3 -10 32 - 96

Page 15: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

3 3 -1 2 0 -10

- 9 30 - 96 288

3 -10 32 - 96 278

Quotient:

Remainder: 278

3 10 32 963 2x x x

f(-3) = 278

Page 16: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

16

AlgorithmsAlgorithms

The obvious way to solve the The obvious way to solve the problem is to compute each term problem is to compute each term and add it to the sum of the others and add it to the sum of the others already computed. The following already computed. The following algorithm does thisalgorithm does this

Page 17: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

17

Algorithm Polynomial Evaluation—Term by Algorithm Polynomial Evaluation—Term by TermTerm

InputInput: : The coefficients of polynomial p(x) in the array The coefficients of polynomial p(x) in the array a; n>=0, the degree of p; and x, the point at which to a; n>=0, the degree of p; and x, the point at which to evaluate p.evaluate p.

OutputOutput: The value of p(x).: The value of p(x).

float poly(float[] a, int n, float x)float poly(float[] a, int n, float x)float p, xpower;float p, xpower;int i;int i;p = a[0]; xpower = 1;p = a[0]; xpower = 1;

for (i = 1; i <= n; i++)for (i = 1; i <= n; i++)xpower = xpower * x;xpower = xpower * x;p+=a[i] * xpower;p+=a[i] * xpower;return p;return p;

NoteNote: This algorithm does 2n multiplications and n : This algorithm does 2n multiplications and n additionsadditions

Page 18: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

18

Horner’s methodHorner’s method

The key to Horner’s method for The key to Horner’s method for evaluating p(x) is simply a particular evaluating p(x) is simply a particular factorization of p:factorization of p:

p(x) = (…((ap(x) = (…((annx = ax = an-1n-1)x + a)x + an-2n-2)x+…+a)x+…+a11)x + a)x + a00..

The computation is done in a short loop The computation is done in a short loop with only n multiplications and n with only n multiplications and n additions.additions.

Page 19: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

19

Page 20: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

20

Algorithm Algorithm Polynomial Evaluation – Horner’s Polynomial Evaluation – Horner’s MethodMethod

InputInput: a, n, and x as in Algorithm 12.1.: a, n, and x as in Algorithm 12.1. OutputOutput: The value of p(x).: The value of p(x).

float hornerPoly(float[]a, int n, float x)float hornerPoly(float[]a, int n, float x)

float p;float p; int i;int i;

p = a[n];p = a[n]; for (i = n – 1; i>= 0; i--)for (i = n – 1; i>= 0; i--) p = p * x + a[i];p = p * x + a[i]; return p;return p;

Thus simply by factoring p we have cut the number of Thus simply by factoring p we have cut the number of multiplications in half without increasing the number of multiplications in half without increasing the number of additions.additions.

Page 21: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

21

Page 22: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

22

Page 23: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

23

Page 24: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

24

Page 25: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

25

Page 26: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

26

Page 27: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

27

A binary tree is a structure in which:A binary tree is a structure in which:

Each node can have at most two children, Each node can have at most two children, and in which a unique path exists from and in which a unique path exists from the root to every other node.the root to every other node.

The two children of a node are called the The two children of a node are called the left childleft child and the and the right child, right child, if they exist.if they exist.

Binary TreeBinary Tree

Page 28: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

28

A Binary TreeA Binary Tree

Q

V

T

K S

A E

L

Page 29: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

29

How many leaf nodes?How many leaf nodes?

Q

V

T

K S

A E

L

Page 30: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

30

How many descendants of How many descendants of Q?Q?

Q

V

T

K S

A E

L

Page 31: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

31

How many ancestors of K?How many ancestors of K?

Q

V

T

K S

A E

L

Page 32: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

32

Implementing a Binary Tree Implementing a Binary Tree with Pointers and Dynamic with Pointers and Dynamic DataData

Q

V

T

K S

A E

L

Page 33: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

33

Each node contains two Each node contains two pointers pointers

template< class ItemType >struct TreeNode {

ItemType info; // Data member

TreeNode<ItemType>* left; // Pointer to left child

TreeNode<ItemType>* right; // Pointer to right child

};

. left . info . right

NULL ‘A’ 6000

Page 34: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

34

A special kind of binary tree in which:A special kind of binary tree in which:

1. Each node contains a distinct data value,1. Each node contains a distinct data value,

2. The key values in the tree can be 2. The key values in the tree can be compared using “greater than” and “less compared using “greater than” and “less than”, andthan”, and

3. The key value of each node in the tree is3. The key value of each node in the tree is

less than every key value in its right less than every key value in its right subtreesubtree, and , and greater than every key value greater than every key value in its left subtree.in its left subtree.

A Binary Search Tree (BST) is A Binary Search Tree (BST) is . . .. . .

Page 35: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

35

Depends on its key values and their order of Depends on its key values and their order of insertion.insertion.

Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.that order.

The first value to be inserted is put into the The first value to be inserted is put into the root node.root node.

Shape of a binary search Shape of a binary search tree . . .tree . . .

‘J’

Page 36: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

36

Thereafter, each value to be inserted begins Thereafter, each value to be inserted begins by comparing itself to the value in the root by comparing itself to the value in the root node, moving left it is less, or moving right node, moving left it is less, or moving right if it is greater. This continues at each level if it is greater. This continues at each level until it can be inserted as a new leaf. until it can be inserted as a new leaf.

Inserting ‘E’ into the BSTInserting ‘E’ into the BST

‘J’

‘E’

Page 37: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

37

Begin by comparing ‘F’ to the value in the Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving root node, moving left it is less, or moving right if it is greater. This continues until it right if it is greater. This continues until it can be inserted as a leaf. can be inserted as a leaf.

Inserting ‘F’ into the BSTInserting ‘F’ into the BST

‘J’

‘E’

‘F’

Page 38: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

38

Begin by comparing ‘T’ to the value in the Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving root node, moving left it is less, or moving right if it is greater. This continues until it right if it is greater. This continues until it can be inserted as a leaf. can be inserted as a leaf.

Inserting ‘T’ into the BSTInserting ‘T’ into the BST

‘J’

‘E’

‘F’

‘T’

Page 39: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

39

Begin by comparing ‘A’ to the value in the Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving root node, moving left it is less, or moving right if it is greater. This continues until it right if it is greater. This continues until it can be inserted as a leaf. can be inserted as a leaf.

Inserting ‘A’ into the BSTInserting ‘A’ into the BST

‘J’

‘E’

‘F’

‘T’

‘A’

Page 40: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

40

is obtained by insertingis obtained by inserting

the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?order?

What binary search What binary search tree . . .tree . . .

‘A’

Page 41: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

41

obtained by insertingobtained by inserting

the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.order.

Binary search tree . . .Binary search tree . . .

‘A’

‘E’

‘F’

‘J’

‘T’

Page 42: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

42

Another binary search treeAnother binary search tree

Add nodes containing these values in this order:

‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’ ‘P’

Page 43: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

43

Is ‘F’ in the binary search Is ‘F’ in the binary search tree?tree?

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’

‘V’

‘P’ ‘Z’‘D’

‘Q’‘L’‘B’

‘S’

Page 44: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

44

Inorder Traversal: Inorder Traversal: A E H J M T YA E H J M T Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree last

Print second

Page 45: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

45

Preorder Traversal: Preorder Traversal: J E A H T M J E A H T M YY

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree second Print right subtree last

Print first

Page 46: 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort

46

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree second

Print last

Postorder Traversal: A H E M Y T J