Docsity

Polynomial - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Polynomial - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

The Polynomial ADT

  • Polynomial ADT
    • What is it? ( Recall it from mathematics )

An example of a single variable polynomial:

4x^6 + 10x^4 - 5x + 3

Remark: the order of this polynomial is 6 (look for highest exponent)

…This sum can be expanded to:

a n x n^ + a (n-1) x (n-1)^ + … + a 1 x^1 + a 0

Notice the two visible data sets namely: (C and E), where

  • C is the coefficient object [Real #].
  • and E is the exponent object [Integer #].
  • Now what?

By definition of a data types:

A set of values and a set of allowable operations on those values.

We can now operate on this polynomial the way we like…

  • Why implement this?

Calculating polynomial operations by hand can be very cumbersome. Take differentiation as an example:

d(23x^9 + 18x^7 + 41x^6 + 163x^4 + 5x + 3)/dx

= (239)x (9-1)^ + (187)x (7-1)^ + (41*6)x (6-1)^ + …

  • How to implement this?

There are different ways of implementing the polynomial ADT:

  • Array (not recommended)
  • Double Array (inefficient)
  • Linked List (preferred and recommended)
  • This is why arrays aren’t good to represent polynomials:
  • p3(x) = 16x^21 - 3x^5 + 2x + 6

6 2 0 0 -3 0 ………… 0 16

WASTE OF SPACE!

  • Advantages of using an Array:
    • only good for non-sparse polynomials.
    • ease of storage and retrieval.
  • Disadvantages of using an Array:
    • have to allocate array size ahead of time.
    • huge array size required for sparse polynomials. Waste of space and runtime.
  • p1(x) = 23x^9 + 18x^7 - 41x^6 + 163x^4 - 5x + 3
  • p2(x) = 4x^6 + 10x^4 + 12x + 8

23 18 -41 163 -5 3 4 10 12 8 9 7 6 4 1 0 6 4 1 0

Coefficient

Exponent

Start p1(x) Start p2(x)

End p1(x) End p2(x)

(^0 2 )

  • Advantages of using a double Array:
    • save space (compact)
  • Disadvantages of using a double Array:
    • difficult to maintain
    • have to allocate array size ahead of time
    • more code required for misc. operations.
  • Advantages of using a Linked list:
    • save space (don’t have to worry about sparse polynomials) and easy to maintain
    • don’t need to allocate list size and can declare nodes (terms) only as needed
  • Disadvantages of using a Linked list :
    • can’t go backwards through the list
    • can’t jump to the beginning of the list from the end. Docsity.com
  • Adding polynomials using a Linked list representation: (storing the result in p3)

To do this, we have to break the process down to cases:

  • Case 1: exponent of p1 > exponent of p
    • Copy node of p1 to end of p3. [go to next node]
  • Case 2: exponent of p1 < exponent of p
    • Copy node of p2 to end of p3. [go to next node] Docsity.com
  • Introducing Horner’s rule:
  • Suppose for simplicity we use an array to represent the following non-sparse polynomial:

4x^3 + 10x^2 + 5x + 3

  • Place it in an array, call it a[i], and compute it…

4x^3 + 10x^2 + 5x + 3

A general (and inefficient ) algorithm:

int Poly = 0; int Multiply; for (int i=0; i < a.Size; i++) { Multiply =1; for (int j=0; j<i; j++) { Multiply = x; } Poly += ma[i]; }

i j Poly 0 0 3 1 0 5x + 3 2 0 10x + 5x + 1 10x 2 + 5x + 3 3 0 4x + 10x 2 + 5x + 3 1 4x 2 + 10x 2 + 5x + 3

Time Complexity O(n^2 )^2 4x^3 + 10x^2 + 5x + 3