0% found this document useful (0 votes)
13 views28 pages

DS - Polynomial Comparision

DS polynomial

Uploaded by

sajood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views28 pages

DS - Polynomial Comparision

DS polynomial

Uploaded by

sajood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

POLYNOMIALS

Comparison with Array representation and Linked List representation


Polynomials A(X)=3X20+2X5+4, B(X)=X4+10X3+3X2+1
p( x )  a1 x e1  ...  an x en
Structure Polynomial is
objects: a set of ordered pairs of <ei,ai> where ai in Coefficients and ei in
Exponents, ei are integers >= 0
functions:
for all poly, poly1, poly2  Polynomial, coef Coefficients, expon Exponents
Polynomial Zero( ) ::= return the polynomial,
p(x) = 0
Boolean IsZero(poly) ::= if (poly) return FALSE
else return TRUE
Coefficient Coef(poly, expon) ::= if (expon  poly) return its
coefficient else return Zero
Exponent Lead_Exp(poly) ::= return the largest exponent in
poly
Polynomial Attach(poly,coef, expon) ::= if (expon  poly) return error
else return the polynomial poly
with the term <coef, expon>
inserted

CHAPTER 2 2
Polynomial Remove(poly, expon) ::= if (expon  poly) return the
polynomial poly with the
term whose exponent is
expon deleted
else return error
Polynomial SingleMult(poly, coef, expon) ::= return the polynomial
poly • coef • xexpon
Polynomial Add(poly1, poly2) ::= return the polynomial
poly1 +poly2
Polynomial Mult(poly1, poly2) ::= return the polynomial
poly1 • poly2
End Polynomial
Abstract data type Polynomial

3
Polynomial Addition
data structure 1: #define MAX_DEGREE 101
typedef struct {
int degree;
float coef[MAX_DEGREE];
} polynomial;
/* d =a + b, where a, b, and d are polynomials */
d = Zero( )
while (! IsZero(a) && ! IsZero(b)) do {
switch COMPARE (Lead_Exp(a), Lead_Exp(b)) {
case -1: d =
Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b));
b = Remove(b, Lead_Exp(b));
break;
case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, Lead_Exp(b));
if (sum) {
Attach (d, sum, Lead_Exp(a));
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
}
break;
4
case 1: d =
Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a));
a = Remove(a, Lead_Exp(a));
}
}
insert any remaining terms of a or b into d

advantage: easy implementation


disadvantage: waste space when sparse

Initial version of padd function

5
Data structure 2: use one global array to store all
polynomials
Array representation of two polynomials
A(X)=2X1000+1
B(X)=X4+10X3+3X2+1
starta finisha startb finishb avail

coef
2 1 1 10 3 1

exp
1000 0 4 3 2 0

0 1 2 3 4 5 6
specification representation
poly <start, finish>
A <0,1>
B <2,5> 6
storage requirements: start, finish, 2*(finish-start+1)
nonparse: twice as much as (1)
when all the items are nonzero

MAX_TERMS 100 /* size of terms array */


typedef struct {
float coef;
int expon;
} polynomial;
polynomial terms[MAX_TERMS];
int avail = 0;

7
Add two polynomials: D = A + B
void padd (int starta, int finisha, int startb, int finishb,
int * startd, int *finishd)
{
/* add A(x) and B(x) to obtain D(x) */
float coefficient;
*startd = avail;
while (starta <= finisha && startb <= finishb)
switch (COMPARE(terms[starta].expon,
terms[startb].expon)) {
case -1: /* a expon < b expon */
attach(terms[startb].coef, terms[startb].expon);
startb++
break;

CHAPTER 2 8
case 0: /* equal exponents */
coefficient = terms[starta].coef +
terms[startb].coef;
if (coefficient)
attach (coefficient, terms[starta].expon);
starta++;
startb++;
break;
case 1: /* a expon > b expon */
attach(terms[starta].coef, terms[starta].expon);
starta++;
}

CHAPTER 2 9
/* add in remaining terms of A(x) */
for( ; starta <= finisha; starta++)
attach(terms[starta].coef, terms[starta].expon);
/* add in remaining terms of B(x) */
for( ; startb <= finishb; startb++)
attach(terms[startb].coef, terms[startb].expon);
*finishd =avail -1;
}

Analysis: O(n+m)
where n (m) is the number of nonzeros in A(B).
Function to add two polynomial

10
void attach(float coefficient, int exponent)
{
/* add a new term to the polynomial */
if (avail >= MAX_TERMS) {
fprintf(stderr, “Too many terms in the polynomial\n”);
exit(1);
}
terms[avail].coef = coefficient;
terms[avail++].expon = exponent;
}
to add a new term

Problem: Compaction is required


when polynomials that are no longer needed.
(data movement takes time.)
11
Polynomials –LIKED LIST Representation
A( x )  a m1 x e m1
 a m 2 x e m 2
... a0 x e
0

Representation
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef;
int expon;
poly_pointer link;
};
poly_pointer a, b, c;

coef expon link

12
Example
 a= 3x14+ 2x8+ 1
 b= 8x14- 3x10+ 10x6

3 14 . 2 8 . 1 0 null
b

8 14 . -3 10 . 10 6 null

13
Adding Polynomials

3 14 2 8 1 0 NULL
a
8 14 -3 10 10 6 NULL
b
11 14 NULL a->expon == b->expon
d
3 14 2 8 1 0 NULL
a
8 14 -3 10 10 6 NULL
b
11 14 -3 10 NULL a->expon < b->expon
d
14
Adding Polynomials (Continued)

3 14 2 8 1 0 NULL
a
8 14 -3 10 10 6 NULL
b
11 14 -3 10 2 8 NULL
d
a->expon > b->expon

15
Alogrithm for Adding Polynomials
poly_pointer padd(poly_pointer a, poly_pointer b)
{
poly_pointer front, rear, temp;
int sum;
rear =(poly_pointer)malloc(sizeof(poly_node));
if (IS_FULL(rear)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
front = rear;
while (a && b) {
switch (COMPARE(a->expon, b->expon)) {

16
case -1: /* a->expon < b->expon */
attach(b->coef, b->expon, &rear);
b= b->link;
break;
case 0: /* a->expon == b->expon */
sum = a->coef + b->coef;
if (sum) attach(sum,a->expon,&rear);
a = a->link; b = b->link;
break;
case 1: /* a->expon > b->expon */
attach(a->coef, a->expon, &rear);
a = a->link;
}
}
for (; a; a = a->link)
attach(a->coef, a->expon, &rear);
for (; b; b=b->link)
attach(b->coef, b->expon, &rear);
rear->link = NULL;
temp = front; front = front->link; free(temp);
return front;
}
Delete extra initial node.

17
Analysis

(1) coefficient additions


0  additions  min(m, n)
where m (n) denotes the number of terms in A (B).
(2) exponent comparisons
extreme case
em-1 > fm-1 > em-2 > fm-2 > … > e0 > f0
m+n-1 comparisons
(3) creation of new nodes
extreme case
m + n new nodes
summary O(m+n)

18
Attach a Term
void attach(float coefficient, int exponent,
poly_pointer *ptr)
{
/* create a new node attaching to the node pointed to
by ptr. ptr is updated to point to this new node. */
poly_pointer temp;
temp = (poly_pointer) malloc(sizeof(poly_node));
if (IS_FULL(temp)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
temp->coef = coefficient;
temp->expon = exponent;
(*ptr)->link = temp;
*ptr = temp;
}

19
A Suite for Polynomials
e(x) = a(x) * b(x) + d(x)
poly_pointer a, b, d, e; read_poly()
.
. print_poly()
. padd()
a = read_poly();
b = read_poly(); psub()
d = read_poly(); pmult()
temp = pmult(a, b);
e = padd(temp, d);
print_poly(e);

20
Erase Polynomials
void earse(poly_pointer *ptr)
{
/* erase the polynomial pointed to by ptr */
poly_pointer temp;
while (*ptr) {
temp = *ptr;
*ptr = (*ptr)->link;
free(temp);
}
}

O(n)

21
Circularly Linked Lists
circular list vs. chain

ptr
3 14 2 8 1 0

avail
ptr

temp

avail
... NULL

22
Maintain an Available List
poly_pointer get_node(void)
{
poly_pointer node;
if (avail) {
node = avail;
avail = avail->link:
}
else {
node = (poly_pointer)malloc(sizeof(poly_node));
if (IS_FULL(node)) {
printf(stderr, “The memory is full\n”);
exit(1);
}
}
return node;
}

23
Maintain an Available List (Continued)
void ret_node(poly_pointer ptr)
{
ptr->link = avail;
avail = ptr;
}
void cerase(poly_pointer *ptr)
{
poly_pointer temp;
if (*ptr) {
temp = (*ptr)->link;
(*ptr)->link = avail;
avail = temp;
*ptr = NULL;
}
}

Independent of # of nodes in a list O(1) constant time


24
4.4.4 Representing Polynomials As Circularly Linked Lists

avail

     
ptr
temp

     NULL

avail

Returning a circular list to the avail list

25
Head Node

Represent polynomial as circular list.


(1) zero

a -1

Zero polynomial
(2) others
a
-1 3 14 2 8 1 0

a  3x  2x  1
14 8

26
Another Padd
poly_pointer cpadd(poly_pointer a, poly_pointer b)
{
poly_pointer starta, d, lastd;
int sum, done = FALSE;
starta = a;
a = a->link;
b = b->link;
d = get_node();
d->expon = -1; lastd = d;
do {
switch (COMPARE(a->expon, b->expon)) {
case -1: attach(b->coef, b->expon, &lastd);
b = b->link;
break;

27
Another Padd (Continued)

case 0: if (starta == a) done = TRUE;


else {
sum = a->coef + b->coef;
if (sum) attach(sum,a->expon,&lastd);
a = a->link; b = b->link;
}
break;
case 1: attach(a->coef,a->expon,&lastd);
a = a->link;
}
} while (!done);
lastd->link = d;
return d;
}

28

You might also like