Docsity

Learning Polynomials, Tree Sort & Synthetic Division: Horner's Method & Binary Trees, Slides of Data Structures and Algorithms

Partial preview of the text

Download Learning Polynomials, Tree Sort & Synthetic Division: Horner's Method & Binary Trees and more Slides Data Structures and Algorithms in PDF only on Docsity!

Lecture 11

POLYNOMIALS and Tree sort

  • INTRODUCTION
  • EVALUATING POLYNOMIAL FUNCTIONS
  • Horner’s method
  • Permutation
  • Tree sort

INTRODUCTION

  • The problems examined in this lecture arepolynomial 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 withpreprocessing of coefficients, Strassen’s matrix multiplication algorithm.

EVALUATING POLYNOMIAL FUNCTIONS Consider the polynomial: P(x) = a n x^ n + a n-1x^ n-1 + … + a 1 x +a 0 with real coefficients and n>=

Suppose the coefficients of agiven and that the problem is the evaluate p(x). 0 , a 1 , …, a n and x are

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

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 + 3 = x − − ( 3 )

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

Algorithm Polynomial Evaluation—Term by Term

  • Input n>=0, the degree of p; and x, the point at which to : The coefficients of polynomial p(x) in the array a;
  • evaluate p. Output : The value of p(x). float poly(float[] a, int n, float x) float p, xpower; int i; for (i = 1; i <= n; i++)^ p = a[0]; xpower = 1; xpower = xpower * x; return p;^ p+=a[i] * xpower; Note: This algorithm does 2n multiplications and n additions

Algorithm Polynomial Evaluation – Horner’s Method

  • • InputOutput : a, n, and x as in Algorithm 12.1.: 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 ofmultiplications in half without increasing the number of additions.