SlideShare a Scribd company logo
Divide-and-Conquer
Multiply two polynomials
Presentation by
Hasanain ALshadoodee
Multiply two polynomials
Presentation by_Hasanain ALshadoodee
3
Introduction
 Multiplying Polynomials
To multiply two polynomials together, multiply every term
of one polynomial by every term of the other polynomial.
Divide-and-Conquer
Multiply two polynomials
 Problem
Given two polynomials represented by two arrays, write a function that
multiplies given two polynomials.
Input: A[] = {10, 5, 11}
B[] = {3, 2, 1}
Output: prod C[] = 10 X + 5 x1 + 11 x2
3 X + 2 x1 + 1 x2
= 30 + 10 * 2x1 + 10 * 1x1 + 5x1 * 3+ 5x1 * 2x1 + 5x1 * 1x2 + 11x2 * 3 +
11x2 * 2x1 + 11x2 * 1x2
polynomials
Presentation by_Hasanain ALshadoodee
4
Naïve Method
One by one consider every term of first polynomial
and multiply it with every term of second polynomial
this will take O(n2) time complexity .
Presentation by_Hasanain ALshadoodee
5
Algoritham
1) Create a product array prod[] of size m+n-1.
2) Initialize all entries in prod[] as 0.
3) Traverse array A[] and do following for every element A[i]
Traverse array B[] and do following for every element B[j]
so prod[i+j] = prod[i+j] + A[i] * B[j]
4) Return prod[].
Presentation by_Hasanain ALshadoodee
6
Analysis of Naïve Method
 Time complexity of the above solution is O(mn).
 If size of two polynomials same, then time complexity is
O(n2).
There are method to do multiplication faster that O(n2)
time.
This is method are based on Divide and conquer technique
.
Presentation by_Hasanain ALshadoodee
7
Divide and Conquer Method
Presentation by_Hasanain ALshadoodee
8
Let the two given polynomials be A and B.
For simplicity, Let us assume that the given two
polynomials are of
same degree and have degree in powers of 2,
i.e., n = 2i
* polynomial 'A' can be written as A0 + A1* xn/2
* polynomial 'B' can be written as B0 + B1* xn/2
Presentation by_Hasanain ALshadoodee
Example
polynomial A=2 + 3x + 6x2 - 2x3 + 5x4
polynomial B=1 - 6x + 7x2 + 2x3 + 8x4
Polynomial A=(2 + 3x) + (6 - 2x + 5x2 ) x2
A0 A1
polynomial B=(1 - 6x) + (7 + 2x + 8x2 ) x2
B0 B1
A * B = (A0 + A1*xn/2) * (B0 + B1*xn/2)
= A0*B0 + A0*B1*xn/2 + A1*B0*xn/2 + A1*B1*xn
= A0*B0 + (A0*B1 + A1*B0)xn/2 + A1*B1*xn
Example
 IN the example we can see divide and conquer method
requires 4 multiplication and O(n) time to add all 4
results .
T(n)=4T(n/2) + O(n)
The solution of recurrence is O(n2) .
But it can be reduced .
T(n)=3T(n/2) + O(n)
Using stressing matrix multiplication .
Presentation by_Hasanain ALshadoodee
10
Programming and Data Structures
Presentation by_Hasanain ALshadoodee
11
 Consider the following polynomials
Each term of the polynomial 1 must be multiplied
with each term of the polynomial
Multiplying each term means multiplying their coefficients
and adding their
1
2
Consider the following polynomials
Presentation by_Hasanain ALshadoodee
12
Resultant polynomial
Consider the following polynomials
Presentation by_Hasanain ALshadoodee
13
head1
head2
ptr2
ptr1
We need pointers (ptr1 and otr2) for traversal , so we also need a
nested loop as each term of the first polynomial must be multiplied
with every term of second polynomial.
Consider the following polynomials
Presentation by_Hasanain ALshadoodee
14
head1
head2
ptr2
ptr1
While (ptr1 != NULL)
 While (ptr2 != NULL)



Consider the following polynomials
Presentation by_Hasanain ALshadoodee
15
head1
head2
head3
Int res1 , res2 ;
Struct node * head3=NULL;
While (ptr1 != NULL)
 Ptr2=head2;
While (ptr2 != NULL)


 ptr1=ptr1-> link;
Presentation by_Hasanain ALshadoodee
16
Consider the following polynomials
Resultant
polynomial
We will get this polynomial of term executing the
cod be causes of insert function
References
 Section 5.6 of the text book “algorithm design” by Jon Kleinberg and Eva
Tardos.
 The original slides were prepared by Kevin Wayne. The slides are
distributed by Pearson Addison-Wesley.
 Wireless Algorithms, Systems, and Applications: 9th International
Conference .
 Crack GATE & ESE with Unacademy : Divide and Conquer: Multiply 2
Polynomials.
 Important Links
• Submit your work here: https://jovian.ai/learn/data-structures-and-algorithms-in-python/assignment/assignment-3-
sorting-and-divide-conquer-practice
• Ask questions and get help: https://jovian.ai/forum/c/data-structures-and-algorithms-in-python/assignment-3/89
• Lesson 3 video for review: https://jovian.ai/learn/data-structures-and-algorithms-in-python/lesson/lesson-3-sorting-
algorithms-and-divide-and-conquer
• Lesson 3 notebook for review: https://jovian.ai/aakashns/python-sorting-divide-and-conquer
• Algebra I #5.11, Multiply two Polynomials https://www.youtube.com/watch?v=VAELGq-FViY
• Application of Linked List (Multiplication of Two Polynomials) https://www.youtube.com/hashtag/linkedlist
Presentation by_Hasanain ALshadoodee
17
Presentation by_HasanainALshadoodee 18

More Related Content

PPT
2.Problems Problem Spaces and Search.ppt
PPTX
Dns resource record
PDF
Closest pair problems (Divide and Conquer)
PDF
Swarm intelligence and particle swarm optimization
PDF
Data and signals
PDF
Transport layer
PPTX
Lecture 17 Iterative Deepening a star algorithm
PPTX
Mask R-CNN
2.Problems Problem Spaces and Search.ppt
Dns resource record
Closest pair problems (Divide and Conquer)
Swarm intelligence and particle swarm optimization
Data and signals
Transport layer
Lecture 17 Iterative Deepening a star algorithm
Mask R-CNN

What's hot (7)

PDF
Solution of non-linear equations
PPTX
CMSC 56 | Lecture 5: Proofs Methods and Strategy
PPTX
Particle swarm optimization
PPSX
Flow Control
PPT
Definition ofvectorspace
PPTX
Unification and Lifting
DOCX
THE PIGEON HOLE PRINCIPLE or also known as DRAWER PRINCIPLE BY ALVIN OBLIGACION
Solution of non-linear equations
CMSC 56 | Lecture 5: Proofs Methods and Strategy
Particle swarm optimization
Flow Control
Definition ofvectorspace
Unification and Lifting
THE PIGEON HOLE PRINCIPLE or also known as DRAWER PRINCIPLE BY ALVIN OBLIGACION
Ad

Similar to Divide and-conquer multiply two polynomials (20)

PPT
Analysis Of Algorithms Ii
PPT
PDF
Prin digcommselectedsoln
PDF
Answers To Exercises Microeconomic Analysis Third Edition
PDF
Complex%20numbers
PPT
Calc 2.2a
PDF
Chapter 6
PDF
Proof of Kraft Mc-Millan theorem - nguyen vu hung
PPT
tutorial5.ppt
PDF
Introduction to probability solutions manual
PPT
Sequences And Series
PPT
countablesets Sequences summations proof by induction lecture.ppt
PPTX
Binomial Theorem
PDF
Number theory
PPT
PDF
M136 Numerical Analysis Mach. Rep.(2).pdf
PDF
Scilab help book 2 of 2
PDF
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
PPTX
Matlab polynimials and curve fitting
Analysis Of Algorithms Ii
Prin digcommselectedsoln
Answers To Exercises Microeconomic Analysis Third Edition
Complex%20numbers
Calc 2.2a
Chapter 6
Proof of Kraft Mc-Millan theorem - nguyen vu hung
tutorial5.ppt
Introduction to probability solutions manual
Sequences And Series
countablesets Sequences summations proof by induction lecture.ppt
Binomial Theorem
Number theory
M136 Numerical Analysis Mach. Rep.(2).pdf
Scilab help book 2 of 2
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Matlab polynimials and curve fitting
Ad

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Institutional Correction lecture only . . .
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Presentation on HIE in infants and its manifestations
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Supply Chain Operations Speaking Notes -ICLT Program
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Cell Structure & Organelles in detailed.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
GDM (1) (1).pptx small presentation for students
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
Institutional Correction lecture only . . .
human mycosis Human fungal infections are called human mycosis..pptx
VCE English Exam - Section C Student Revision Booklet
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Presentation on HIE in infants and its manifestations
102 student loan defaulters named and shamed – Is someone you know on the list?

Divide and-conquer multiply two polynomials

  • 3. Presentation by_Hasanain ALshadoodee 3 Introduction  Multiplying Polynomials To multiply two polynomials together, multiply every term of one polynomial by every term of the other polynomial.
  • 4. Divide-and-Conquer Multiply two polynomials  Problem Given two polynomials represented by two arrays, write a function that multiplies given two polynomials. Input: A[] = {10, 5, 11} B[] = {3, 2, 1} Output: prod C[] = 10 X + 5 x1 + 11 x2 3 X + 2 x1 + 1 x2 = 30 + 10 * 2x1 + 10 * 1x1 + 5x1 * 3+ 5x1 * 2x1 + 5x1 * 1x2 + 11x2 * 3 + 11x2 * 2x1 + 11x2 * 1x2 polynomials Presentation by_Hasanain ALshadoodee 4
  • 5. Naïve Method One by one consider every term of first polynomial and multiply it with every term of second polynomial this will take O(n2) time complexity . Presentation by_Hasanain ALshadoodee 5
  • 6. Algoritham 1) Create a product array prod[] of size m+n-1. 2) Initialize all entries in prod[] as 0. 3) Traverse array A[] and do following for every element A[i] Traverse array B[] and do following for every element B[j] so prod[i+j] = prod[i+j] + A[i] * B[j] 4) Return prod[]. Presentation by_Hasanain ALshadoodee 6
  • 7. Analysis of Naïve Method  Time complexity of the above solution is O(mn).  If size of two polynomials same, then time complexity is O(n2). There are method to do multiplication faster that O(n2) time. This is method are based on Divide and conquer technique . Presentation by_Hasanain ALshadoodee 7
  • 8. Divide and Conquer Method Presentation by_Hasanain ALshadoodee 8 Let the two given polynomials be A and B. For simplicity, Let us assume that the given two polynomials are of same degree and have degree in powers of 2, i.e., n = 2i * polynomial 'A' can be written as A0 + A1* xn/2 * polynomial 'B' can be written as B0 + B1* xn/2
  • 9. Presentation by_Hasanain ALshadoodee Example polynomial A=2 + 3x + 6x2 - 2x3 + 5x4 polynomial B=1 - 6x + 7x2 + 2x3 + 8x4 Polynomial A=(2 + 3x) + (6 - 2x + 5x2 ) x2 A0 A1 polynomial B=(1 - 6x) + (7 + 2x + 8x2 ) x2 B0 B1 A * B = (A0 + A1*xn/2) * (B0 + B1*xn/2) = A0*B0 + A0*B1*xn/2 + A1*B0*xn/2 + A1*B1*xn = A0*B0 + (A0*B1 + A1*B0)xn/2 + A1*B1*xn
  • 10. Example  IN the example we can see divide and conquer method requires 4 multiplication and O(n) time to add all 4 results . T(n)=4T(n/2) + O(n) The solution of recurrence is O(n2) . But it can be reduced . T(n)=3T(n/2) + O(n) Using stressing matrix multiplication . Presentation by_Hasanain ALshadoodee 10
  • 11. Programming and Data Structures Presentation by_Hasanain ALshadoodee 11  Consider the following polynomials Each term of the polynomial 1 must be multiplied with each term of the polynomial Multiplying each term means multiplying their coefficients and adding their 1 2
  • 12. Consider the following polynomials Presentation by_Hasanain ALshadoodee 12 Resultant polynomial
  • 13. Consider the following polynomials Presentation by_Hasanain ALshadoodee 13 head1 head2 ptr2 ptr1 We need pointers (ptr1 and otr2) for traversal , so we also need a nested loop as each term of the first polynomial must be multiplied with every term of second polynomial.
  • 14. Consider the following polynomials Presentation by_Hasanain ALshadoodee 14 head1 head2 ptr2 ptr1 While (ptr1 != NULL)  While (ptr2 != NULL)   
  • 15. Consider the following polynomials Presentation by_Hasanain ALshadoodee 15 head1 head2 head3 Int res1 , res2 ; Struct node * head3=NULL; While (ptr1 != NULL)  Ptr2=head2; While (ptr2 != NULL)    ptr1=ptr1-> link;
  • 16. Presentation by_Hasanain ALshadoodee 16 Consider the following polynomials Resultant polynomial We will get this polynomial of term executing the cod be causes of insert function
  • 17. References  Section 5.6 of the text book “algorithm design” by Jon Kleinberg and Eva Tardos.  The original slides were prepared by Kevin Wayne. The slides are distributed by Pearson Addison-Wesley.  Wireless Algorithms, Systems, and Applications: 9th International Conference .  Crack GATE & ESE with Unacademy : Divide and Conquer: Multiply 2 Polynomials.  Important Links • Submit your work here: https://jovian.ai/learn/data-structures-and-algorithms-in-python/assignment/assignment-3- sorting-and-divide-conquer-practice • Ask questions and get help: https://jovian.ai/forum/c/data-structures-and-algorithms-in-python/assignment-3/89 • Lesson 3 video for review: https://jovian.ai/learn/data-structures-and-algorithms-in-python/lesson/lesson-3-sorting- algorithms-and-divide-and-conquer • Lesson 3 notebook for review: https://jovian.ai/aakashns/python-sorting-divide-and-conquer • Algebra I #5.11, Multiply two Polynomials https://www.youtube.com/watch?v=VAELGq-FViY • Application of Linked List (Multiplication of Two Polynomials) https://www.youtube.com/hashtag/linkedlist Presentation by_Hasanain ALshadoodee 17