Lecture 11 - Polynomials and Tree Sort
Download
Report
Transcript Lecture 11 - Polynomials and Tree Sort
Lecture 11
POLYNOMIALS and Tree sort
1
INTRODUCTION
EVALUATING POLYNOMIAL
FUNCTIONS
Horner’s method
Permutation
Tree sort
2
3
INTRODUCTION
The problems examined in this lecture are
polynomial evaluation (with and without
preprocessing of the coefficients), polynomial
multiplication, and multiplication of matrices
and vectors.
Several algorithms in this lecture use the
divide-and-conquer method: evaluating a
polynomial with preprocessing of coefficients,
Strassen’s matrix multiplication algorithm.
4
EVALUATING POLYNOMIAL
FUNCTIONS
Consider the polynomial:
P(x) = anx^n + an-1x^n-1 + … + a1x +a0
with real coefficients and n>=1
Suppose the coefficients of a0, a1, …, an and x
are given and that the problem is the
evaluate p(x).
In this section we look at some algorithms and
some lower bounds for this problem.
5
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
4
3
2
f ( x ) 3 x x 2 x 10 is divided by
g ( x ) x 3.
x 3 x 3
3 3
-1
2
0
-10
3 3
3
-1
2
0
-10
3 3
-1
-9
3 -10
2
0
-10
3 3
-1
2
-9
3 - 10
30
32
0
- 10
3 3
-1
-9
3 - 10
2
0
30 - 96
32 - 96
- 10
3 3
-1
-9
3 - 10
2
0
- 10
30 - 96
32 - 96
288
278
Quotient: 3x 10 x 32 x 96
Remainder: 278
3
f(-3) = 278
2
Algorithms
The obvious way to solve the problem is
to compute each term and add it to the
sum of the others already computed.
The following algorithm does this
16
Algorithm Polynomial Evaluation—Term by Term
Input: The coefficients of polynomial p(x) in
the array a; n>=0, the degree of p; and x,
the point at which to evaluate p.
Output: The value of p(x).
float poly(float[] a, int n, float x)
float p, xpower;
int i;
p = a[0]; xpower = 1;
for (i = 1; i <= n; i++)
xpower = xpower * x;
p+=a[i] * xpower;
return p;
Note: This algorithm does 2n multiplications
and n additions
17
Horner’s method
The key to Horner’s method for evaluating
p(x) is simply a particular factorization of p:
p(x) = (…((anx = an-1)x + an-2)x+…+a1)x + a0.
The computation is done in a short loop with
only n multiplications and n additions.
18
19
Algorithm Polynomial Evaluation – Horner’s
Method
Input: a, n, and x as in Algorithm 12.1.
Output: The value of p(x).
float hornerPoly(float[]a, int n, float x)
float p;
int i;
p = a[n];
for (i = n – 1; i>= 0; i--)
p = p * x + a[i];
return p;
Thus simply by factoring p we have cut the
number of multiplications in half without
increasing the number of additions.
20
21
22
23
24
25
26
Binary Tree
A binary tree is a structure in which:
Each node can have at most two
children, and in which a unique path
exists from the root to every other
node.
The two children of a node are called the
left child and the right child, if they
exist.
27
A Binary Tree
V
Q
L
T
E
K
A
S
28
How many leaf nodes?
V
Q
L
T
E
K
A
S
29
How many descendants of Q?
V
Q
L
T
E
K
A
S
30
How many ancestors of K?
V
Q
L
T
E
K
A
S
31
Implementing a Binary Tree
with Pointers and Dynamic Data
V
Q
L
T
E
K
A
S
32
Each node contains two
pointers
template< class ItemType >
struct TreeNode {
ItemType info;
// Data member
TreeNode<ItemType>* left;
// Pointer to left child
TreeNode<ItemType>* right; // Pointer to right child
};
NULL
. left
‘A’
. info
6000
. right
33
A Binary Search Tree (BST) is . . .
A special kind of binary tree in which:
1. Each node contains a distinct data value,
2. The key values in the tree can be compared
using “greater than” and “less than”, and
3. The key value of each node in the tree is
less than every key value in its right subtree,
and greater than every key value in its left
subtree.
34
Shape of a binary search
tree . . .
Depends on its key values and their order of
insertion.
Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’
order.
in that
The first value to be inserted
is put into the root
‘J’
node.
35
Inserting ‘E’ into the BST
Thereafter, each value to be inserted begins by
comparing itself to the value in the root node,
moving left it is less, or moving right if it is
greater. This continues at each level until it can
be inserted as a new leaf.
‘J’
‘E’
36
Inserting ‘F’ into the BST
Begin by comparing ‘F’ to the value in the root node,
moving left it is less, or moving right if it is
greater. This continues until it can be inserted as
a leaf.
‘J’
‘E’
‘F’
37
Inserting ‘T’ into the BST
Begin by comparing ‘T’ to the value in the root
node, moving left it is less, or moving right if it is
greater. This continues until it can be inserted as
a leaf.
‘J’
‘T’
‘E’
‘F’
38
Inserting ‘A’ into the BST
Begin by comparing ‘A’ to the value in the root
node, moving left it is less, or moving right if it is
greater. This continues until it can be inserted as
a leaf.
‘J’
‘T’
‘E’
‘A’
‘F’
39
What binary search tree . . .
is obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’
in that order?
‘A’
40
Binary search tree . . .
obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’
in that order.
‘A’
‘E’
‘F’
‘J’
‘T’
41
Another binary search tree
‘J’
‘T’
‘E’
‘A’
‘H’
‘M’
‘P’
‘K’
Add nodes containing these values in this order:
‘D’
‘B’
‘L’
‘Q’
‘S’
‘V’
‘Z’
42
Is ‘F’ in the binary search tree?
‘J’
‘T’
‘E’
‘A’
‘D’
‘B’
‘V’
‘M’
‘H’
‘Z’
‘P’
‘K’
‘L’
‘Q’
‘S’
43
Inorder Traversal: A E H J M T Y
Print second
tree
‘J’
‘
A’
‘
E’ ‘
H’
Print left subtree first
‘
T’
‘
M’
‘
Y’
Print right subtree last
44
Preorder Traversal: J E A H T M Y
Print first
tree
‘J’
‘
A’
‘
E’ ‘
H’
Print left subtree second
‘
T’
‘
M’
‘
Y’
Print right subtree last
45
Postorder Traversal: A H E M Y T J
Print last
tree
‘J’
‘
A’
‘
E’ ‘
H’
Print left subtree first
‘
T’
‘
M’
‘
Y’
Print right subtree second
46