Chapter 5: Polynomials and Sparse Matrices

Data Structures and Algorithms with C · Study Questions
Polynomial representationpaddProduct with linked listsTriple representationfast_transposeSparse multiplicationCSR
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Polynomial ADT and Representation

Question 1: Two Representations

The polynomial A(x) = x¹⁰⁰⁰ + 1 is to be stored. At least how many numbers are kept in the coefficient-array representation (coef[MAX_DEGREE]) and in the term-array representation ({coef, expon} pairs), excluding the degree field?

D: The coefficient array needs 1001 coefficients for x⁰ … x¹⁰⁰⁰; 999 of them are zero. The term array holds only the 2 nonzero terms, each a coefficient and an exponent: 4 numbers. For a sparse polynomial the term array is a big win; for a polynomial with all coefficients nonzero it takes twice the space.

Multiple Choice · Polynomial Addition

Question 2: The Result of padd

A = 3x⁵ + 2x² + 1 and B = 4x⁴ − 2x² + 6x are kept in order in the array P. What is the result D of padd, and how many attach calls are made?

A: Comparisons (5, 4) → 3x⁵ of A; (2, 4) → 4x⁴ of B; (2, 2) → 2 − 2 = 0, not attached; (0, 1) → 6x of B. B is exhausted, 1 is copied from A: 4 attach calls. B also attaches the zero-sum term; D adds terms of different exponents.

Step-by-Step Trace · Polynomial Addition

Question 3: Tracing padd

For A = 2x⁶ + 5x³ − x and B = x⁶ − 5x³ + 3x² + 4, which terms does padd(0, 2, 3, 6, &sd, &fd) write into the array P?

2x⁶05x³1−x2x⁶3−5x³43x²54678910ABDsasb

A = P[0..2], B = P[3..6]. The result is written from avail = 7: *sd = 7.

2x⁶05x³1−x2x⁶3−5x³43x²5463x⁶78910ABDsasb

(6, 6) equal: c = 2 + 1 = 3 → P[7] = 3x⁶; both advance.

2x⁶05x³1−x2x⁶3−5x³43x²5463x⁶78910ABDsasb

(3, 3) equal: c = 5 − 5 = 0 → not attached.

2x⁶05x³1−x2x⁶3−5x³43x²5463x⁶73x²8910ABDsasb

(1, 2): the exponent of B is larger → 3x² is attached, only sb advances.

2x⁶05x³1−x2x⁶3−5x³43x²5463x⁶73x²8−x910ABDsb

(1, 0): the exponent of A is larger → −x is attached. sa = 3 > fa: the loop ends.

2x⁶05x³1−x2x⁶3−5x³43x²5463x⁶73x²8−x9410ABD

The remaining 4 of B is copied. D = P[7..10] = 3x⁶ + 3x² − x + 4: 4 rounds, 4 attach calls, O(n + m).

void padd(int sa, int fa, int sb, int fb,
          int *sd, int *fd) {
  float c; *sd = avail;
  while (sa <= fa && sb <= fb)
    switch (COMPARE(P[sa].expon,
                    P[sb].expon)) {
    case -1: COPY(sb); sb++; break;
    case  1: COPY(sa); sa++; break;
    case  0: c = P[sa].coef + P[sb].coef;
      if (c) attach(c, P[sa].expon);
      sa++; sb++;
    }
  for (; sa <= fa; sa++) COPY(sa);
  for (; sb <= fb; sb++) COPY(sb);
  *fd = avail - 1;
}

Output Question · Polynomial Addition

Question 4: The Truncated Coefficient

attach was mistakenly written as void attach(int coef, int expon). If A = 2.5x² + 0.5x and B = 1.5x² + 1 (float coefficients) are added with padd, what is D?

Show answer

D = 4x² + 0x + 1 (3 terms).

  • (2, 2) equal: c = 2.5 + 1.5 = 4.0 → attach(4, 2): correct, because the sum is an integer.
  • (1, 0): A is larger → COPY passes 0.5 to an int parameter: it is truncated to 0. A term with coefficient 0 enters the result.
  • 1 is copied from B.

The correct result is 4x² + 0.5x + 1. A term with a zero coefficient also breaks the polynomial invariant, ‘only nonzero terms’. The parameter must be float.

Multiple Choice · Polynomial Addition

Question 5: Overflow in the Shared Array

MAX_TERMS = 101 and avail = 95. Two polynomials with 4 and 5 terms and no common exponents are added with padd. Which attach call gives the overflow message?

B: With no common exponent, 4 + 5 = 9 attach calls are needed. The free slots are 95–100: 6 attach calls fit; at the 7th call avail = 101 ≥ MAX_TERMS. Since the space of old polynomials is never reclaimed, the shared P array needs compaction; a linked list removes this problem.

Spot the Bug · Polynomial as a Linked List

Question 6: free First, Link Afterwards

delete_next_term has been abbreviated as below. What goes wrong? What is the correct order?

void delete_next_term(
        poly_pointer prev) {
  free(prev->link);
  prev->link = prev->link->link;
}
Show answer

The second line reads prev->link->link, but prev->link has just been freed: a field of a released node is read (undefined behavior). Often the old value is still there and the code seems to work.

void delete_next_term(
        poly_pointer prev) {
  poly_pointer victim =
        prev->link;
  if (!victim) return;
  prev->link = victim->link;
  free(victim);   /* last */
}

Also, if there is no node to delete (prev->link == NULL), the abbreviated version accesses NULL->link.

Step-by-Step Trace · Polynomial as a Linked List

Question 7: Two Deletions in poly_mult

poly_mult multiplies the lists (x + 2) and (x² − 2x + 4). How does the result list change after each product?

x³rstart,r,start

The result is empty: x · x² = x³ becomes the first node; rstart = r = start.

x³−2x²rstart,r,start

x · (−2x) = −2x²: place_term finds no term with exponent 2, so it appends after r.

x³−2x²4xrrstart,start

x · 4 = 4x is appended. r stays at −2x².

x³4xrstart,r,start−2x² + 2x² = 0 → deleted

p = 2, the search starts at start (x³). 2 · x² = 2x²: coefficient −2 + 2 = 0 → delete_next_term.

x³rstart,r,start4x − 4x = 0 → deleted

2 · (−2x) = −4x: 4 − 4 = 0, this node is deleted too.

x³8rstart,r,start

2 · 4 = 8 is appended. The result is x³ + 8: n·m = 6 products, 2 deletions.

poly_pointer poly_mult(poly_pointer pstart,
                       poly_pointer qstart) {
  poly_pointer p, q, r = NULL;
  poly_pointer start = NULL, rstart = NULL;
  for (p = pstart; p; p = p->link) {
    r = start;
    for (q = qstart; q; q = q->link) {
      int expon = p->expon + q->expon;
      int coef = p->coef * q->coef;
      if (rstart) r = place_term(r, expon, coef);
      else rstart = r = start =
             new_term(expon, coef, NULL);
      if (q == qstart) start = r;
    }
  }
  return rstart;
}

Multiple Choice · Polynomial as a Linked List

Question 8: Multiplication Methods

A has n terms and B has m. For each term of A a separate sorted list of its products with B is built, and these n lists are finally merged two at a time. What is the complexity?

B: Each of the n lists has at most m terms; in pairwise merging at most n·m terms are processed at every level and there are lg n levels: O(m·n·lg n). C is the cost of merging each list into the result one after another (it grows more expensive as the intermediate result grows); A is adding each term by scanning from the start. The number of levels is determined by the number of lists n, not by m.

Multiple Choice · Sparse Matrix

Question 9: When Does the Triple Representation Pay Off?

A 10 × 10 integer matrix is stored as ⟨row, col, value⟩ triples with a header M[0]. For at most how many nonzero entries t does the triple representation take less space than the 2D array?

C: The triples hold 3(t + 1) integers; 3(t + 1) < 100 ⇔ t + 1 ≤ 33 ⇔ t ≤ 32. For t = 33 it is 102 > 100. Roughly, it pays off if fewer than a third of the matrix is filled; if the header row (+1) is forgotten, A is found.

Classic Question · Sparse Matrix

Question 10: Converting to Triples

Write the triple table of the 3 × 5 matrix below (including M[0]). Does the triple representation save memory for this matrix?

     0  1  2  3  4
0: [ 0  4  0  0  7 ]
1: [ 2  0  0  0  1 ]
2: [ 0  9  0  0  0 ]
Show answer
rowcolvalue
M[0]355
M[1]014
M[2]047
M[3]102
M[4]141
M[5]219

The triples are sorted by row first, then by column; M[0] holds the number of rows, columns and terms.

Memory: 3 · (5 + 1) = 18 integers, the 2D array 15 integers. Since 5 of the 15 cells (a third) are filled, the triple representation does not pay off here.

Step-by-Step Trace · Transposing

Question 11: Tracing fast_transpose

The triples of a 3 × 5 sparse matrix, M[0] = (3, 5, 5), (0, 1, 4), (0, 4, 7), (1, 0, 2), (1, 4, 1), (2, 1, 9), are given to fast_transpose. How do row_terms, start_pos and T get filled?

row_termsstart_pos1021020324T[0](5, 3, 5)[1][2][3][4][5]

Number of terms in each column: two each in columns 1 and 4, columns 2 and 3 are empty.

row_termsstart_pos110221042043244T[0](5, 3, 5)[1][2][3][4][5]

Prefix sums: the empty columns 2 and 3 also start at 4.

row_termsstart_pos110231042043244T[0](5, 3, 5)[1][2](1, 0, 4)[3][4][5]

Column 1 → j = start_pos[1]++ = 2: it is read first, then incremented.

row_termsstart_pos110231042043254T[0](5, 3, 5)[1][2](1, 0, 4)[3][4](4, 0, 7)[5]

Column 4 → T[4].

row_termsstart_pos120231042043254T[0](5, 3, 5)[1](0, 1, 2)[2](1, 0, 4)[3][4](4, 0, 7)[5]

Column 0 → T[1].

row_termsstart_pos120231042043264T[0](5, 3, 5)[1](0, 1, 2)[2](1, 0, 4)[3][4](4, 0, 7)[5](4, 1, 1)

Second term of column 4 → T[5].

row_termsstart_pos120241042043264T[0](5, 3, 5)[1](0, 1, 2)[2](1, 0, 4)[3](1, 2, 9)[4](4, 0, 7)[5](4, 1, 1)

Second term of column 1 → T[3]. T is sorted by row: M was scanned once, O(cols + terms).

void fast_transpose(term M[], term T[]) {
  int i, j, nc = M[0].col;
  int nt = M[0].value;
  int row_terms[MAX_COL], start_pos[MAX_COL];
  T[0].row = nc;  T[0].col = M[0].row;
  T[0].value = nt;
  if (nt <= 0) return;
  for (i = 0; i < nc; i++) row_terms[i] = 0;
  for (i = 1; i <= nt; i++)
    row_terms[M[i].col]++;
  start_pos[0] = 1;
  for (i = 1; i < nc; i++)
    start_pos[i] = start_pos[i-1]
                 + row_terms[i-1];
  for (i = 1; i <= nt; i++) {
    j = start_pos[M[i].col]++;
    T[j].row = M[i].col;
    T[j].col = M[i].row;
    T[j].value = M[i].value;
  }
}

Multiple Choice · Transposing

Question 12: The Cost of transpose

A 1000 × 1000 matrix has 2000 nonzero entries. In the slow transpose from the slides, how many times is the comparison M[j].col == i made?

D: For each column (1000), the outer loop scans all terms (2000) in the inner loop: 1000 · 2000 = 2,000,000. fast_transpose finishes with two column scans and two term scans, about 2 · 2000 + 2 · 1000 = 6000 operations. Transposing as a 2D array would visit 10⁶ cells.

Multiple Choice · Transposing

Question 13: Prefix or Postfix?

What happens in fast_transpose if j = ++start_pos[M[i].col]; is written instead of j = start_pos[M[i].col]++;?

A: Prefix increments first: every term is written one slot to the right of its correct place. Columns do not collide, all shift by one: T[1] stays empty and the term of the last position overflows to T[t + 1] (T[2]…T[6] in the previous matrix). If the array is exactly t + 1 long, this write is out of bounds.

Classic Question · Sparse Matrix Multiplication

Question 14: Sparse Multiplication

A and B are 3 × 3; their triples are given below. Which triples of D does smult(a, bt, d) (bt is the transpose of B) produce? How many times is dot called?

A: (0,0,2) (0,2,1) (1,1,3) (2,0,5)
B: (0,1,4) (1,0,1) (2,1,2) (2,2,6)
Show answer

Rows of Bᵀ (columns of B): 0: (0, 1, 1) · 1: (1, 0, 4), (1, 2, 2) · 2: (2, 2, 6).

row of A · row of Bᵀcommon ksum
0 · 0—0
0 · 1k = 0, 22·4 + 1·2 = 10
0 · 2k = 21·6 = 6
1 · 0k = 13·1 = 3
2 · 1k = 05·4 = 20

D: (0, 1, 10), (0, 2, 6), (1, 0, 3), (2, 1, 20), header (3, 3, 4). The other pairs give 0 and are not stored. 3 rows of A × 3 rows of Bᵀ = 9 dot calls.

Multiple Choice · Sparse Matrix Multiplication

Question 15: Why Bᵀ First?

Why is the transpose of B taken first in sparse matrix multiplication (D = A · B)?

C: For dij, row i of A and column j of B are needed. In row-major triples the row of A is consecutive but the column of B is scattered; in Bᵀ this column becomes a consecutive row, and dot merges the two by the col field with two pointers, like padd. The result does not have to be sparse.

Classic Question · Other Representations

Question 16: Matrix–Vector Product with CSR

A 4 × 4 matrix is in CSR form: val = {3, 1, 4, 2, 5, 6}, col_ind = {0, 3, 1, 0, 2, 3}, row_ptr = {0, 2, 3, 3, 6}. What are the rows of the matrix? For x = (1, 2, 3, 4), what does the loop from the slides compute for y?

for (i = 0; i < rows; i++) {
  y[i] = 0;
  for (k = row_ptr[i];
       k < row_ptr[i+1]; k++)
    y[i] += val[k] * x[col_ind[k]];
}
Show answer
  • Row 0: val[0..1] → a00 = 3, a03 = 1.
  • Row 1: val[2] → a11 = 4.
  • Row 2: row_ptr[2] = row_ptr[3] = 3 → empty row, the inner loop never runs.
  • Row 3: val[3..5] → a30 = 2, a32 = 5, a33 = 6.

y = (3·1 + 1·4, 4·2, 0, 2·1 + 5·3 + 6·4) = (7, 8, 0, 41).

Memory: 6 + 6 + 5 = 17 integers (21 with triples). Work is O(rows + terms): each term is multiplied once. row_ptr is a prefix sum like start_pos in fast_transpose.

The End

Chapter 5: Polynomials and Sparse Matrices: Study Questions
Lecture slides: Chapter 5: Polynomials and Sparse Matrices · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.