Question

In: Computer Science

In class we saw that the fast divide and conquer integer multiplication method takesO(nlog3) time. Thisis...

In class we saw that the fast divide and conquer integer multiplication method takesO(nlog3) time. Thisis because the the shifts and additions can be done in time O(n). Suppose we use a very inefficient method for doingthe shifts and additions, so these take timeO(n2). With this slower method for doing the shifts and additions, figureout how much time will the divide and conquer integer multiplication method take . You can assume that nothing elsechanges in the divide and conquer integer multiplication method.(a) What will be the new recurrence relation for divide and conquer integer multiplication, if we are using the inefficientmethod for doing the shifts and additions(b) What will be the solution to the new recurrence relation.

Solutions

Expert Solution

10

20.

120

In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. But lack of knowledge of certain essential tools bind us to do so. Some methods to achieve this task are mentioned in this article.

Converting string to number

Method 1 : Using stringstream class or sscanf()
Method 2 : String conversion using stoi() or atoi()
Both these methods have been discussed in detail in the this article.

Method 3 : Using boost lexical cast
Boost library offers an inbuild function “lexical_cast(“string”)”, which directly converts a string to number. It returns an exception “bad_lexical_cast” in case of invalid input.

filter_none

edit

play_arrow

brightness_4

//C++ code to demonstrate working of lexical_cast()

#include<iostream>

#include <boost/lexical_cast.hpp>// for lexical_cast()

#include <string> // for string

using namespace std;

int main()

{

   string str = "5";

   string str1 = "6.5";

   // Initializing f_value with casted float

   // f_value is 6.5

   float f_value = boost::lexical_cast<float>(str1);

   // Initializing i_value with casted int

   // i_value is 5

   int i_value = boost::lexical_cast<int>(str);

20


Related Solutions

3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
1. If we view the selection sort as an application of "divide and conquer" strategy, what...
1. If we view the selection sort as an application of "divide and conquer" strategy, what is the two-part partitioning of the index range [0, n)? Is it a balanced partition? 2. For the "divide and conquer" strategy for developing algorithm, do we pursue balanced partitioning or unbalanced partitioning? Why? 3. For the quicksort, the selection of pivot determines the quality of partitioning. Do we have any easy or cheap way for selecting a good pivot that always leads to...
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1?...
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1? + ?2?2 ... + ??−1?n-1 can be represented by an array ?[0. . ? − 1] of its n coefficients. Suppose P(x) and Q(x) are two polynomials of degree n-1, each given by its coefficient array representation. Their product P(x)Q(x) is a polynomial of degree 2(n-1), and hence can be represented by its coefficient array of length 2n-1. The polynomial multiplication problem is to...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
The “divide and average” method, an old time method for approximating the square root of any...
The “divide and average” method, an old time method for approximating the square root of any positive number a, can be formulated as x = (x + a/x) / 2 Write a well-structured M-file function based on the while…break loop structure to implement this algorithm. At each step estimate the error in your approximation as ε = abs(( Xnew − Xold )/Xnew Repeat the loop until e is less than or equal to a specified value. Design your program so...
We saw in class different sorting algorithms, and we generally assumed the input to be sorted...
We saw in class different sorting algorithms, and we generally assumed the input to be sorted wasj an array. Here, the provided input is an unordered linked list of n elements with integer values. We are interested in sorting this list in increasing order (smallest first), in O(n log n) worst case time, while using constant space (also called in-place sorting). Note that recursion requires additional space on the stack, so should EC330 Applied Algorithms and Data Structures for Engineers,...
we saw a C program called GuessNumber.c that generates a random integer between 1 and 1000...
we saw a C program called GuessNumber.c that generates a random integer between 1 and 1000 and asks the user to guess that number. Modify the program so that at the end of each round, it also prints the number of guesses made by the user to reach the answer as well as the best score so far, i.e., the minimum number of guesses used in any round since the program was started. The source code of the original version...
We saw in class that each LP can be transformed into an equivalent LP in any...
We saw in class that each LP can be transformed into an equivalent LP in any of the following two forms below: (1) maxcTx: Ax=b, x≥0 (2) maxcTx: Ax≤b. Can we always transform any LP in an LP of the form Prove your answer correct. maxcTx: Ax=b?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT