DS - Polynomial Comparision
DS - Polynomial Comparision
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
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
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
Representation
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef;
int expon;
poly_pointer link;
};
poly_pointer a, b, c;
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
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;
}
}
avail
ptr
temp
NULL
avail
25
Head Node
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)
28