1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s...

Post on 31-Dec-2015

215 views 0 download

Tags:

Transcript of 1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s...

1

Lecture 11Lecture 11

POLYNOMIALS and Tree sortPOLYNOMIALS and Tree sort

2

INTRODUCTIONINTRODUCTION

EVALUATING POLYNOMIAL EVALUATING POLYNOMIAL FUNCTIONSFUNCTIONS

Horner’s methodHorner’s method PermutationPermutation Tree sortTree sort

3

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.

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.

6

7

8

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.

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

3 3 -1 2 0 -10

3

3 3 -1 2 0 -10

- 9

3 -10

3 3 -1 2 0 -10

- 9 30

3 -10 32

3 3 -1 2 0 -10

- 9 30 - 96

3 -10 32 - 96

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

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

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

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.

19

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.

21

22

23

24

25

26

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

28

A Binary TreeA Binary Tree

Q

V

T

K S

A E

L

29

How many leaf nodes?How many leaf nodes?

Q

V

T

K S

A E

L

30

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

Q

V

T

K S

A E

L

31

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

Q

V

T

K S

A E

L

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

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

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 . . .. . .

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’

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’

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’

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’

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’

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’

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’

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’

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’

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

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

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