Concept explainers
(a)
To reword the parallel loop in SUM-ARRAYS utilizing nested within the manner of MAT-VEC-MAIN-LOOP.
(a)
Explanation of Solution
The implementation of the parallel loop that contains a worth grain-size to be specified is as follows:
SUM − ARRAYS’ (A, B, C)
Grain − Size
Sync
ADD − SUBARRAY (A, B, C, i, j)
Observing the
(b)
To calculate the parallelism of the given implementation if we set grain-size=1.
(b)
Explanation of Solution
It can be concluded that each call to
SUM − ARRAYS’ (A, B, C)
(c)
To formulate the span of SUM-ARRAYS’ in terms of
(c)
Explanation of Solution
Assume g be the grain-size. The runtime of the function is
To get this we will perform calculus and get a derivative, we have
To further solve this, we set
Want to see more full solutions like this?
Chapter 27 Solutions
Introduction to Algorithms
- Pseudo Code shown in Figure Q1(a) is an algorithm for binary searching for an array with n number of elements. By applying this algorithm, show step by step approach on how to find number 11 in an array as depicted in Figure Q1(b). low + 0 high e n-1 while (low s high) do ix + (low + high) /2 if (t = Alix)) then return ix else if (t < A[ix]) then high e ix - 1 else low + ix + 1 return -1 Figure Ql(a) 2 4 6 9 11 12 | 25 [0] [1) [2] [3] (4) [5) [6] Figure Q1(b)arrow_forwardDesign and analyze a decrease-conquer algorithm for the following problem: o input: an array A[lo.. hi] whose elements are unique, and a positive integer k; o output: the kth smallest value in the array. (in C++)arrow_forwardGiven a sorted array containing 1, 2, 4, 8, 16, 32, 64, 128, 256, 512. Suppose we want to search for 512, a) How many comparisons are needed that compare the elements with 512 using Binary Search? b) How many comparisons are needed that compare the elements 512 using Linear Search?arrow_forward
- See the pseudo-code of the Binary Search using recursion below. Fill in the XXXX and YYYY in the code Algorithm BinarySearch (A,v,low,hi) Input: array A indexed from low to hi with items sorted from smallest to largest. We are searching for the item v Output: returns a location index of item v in array A; if v is not found, -1 is returned. if (low > hi) then return (-1); mid = (lo + hi)/2; if (A[mid] = v) then == return(mid); if (A[mid] < v) then return(BinarySearch(A, XXXX ,hi, v)); else return(BinarySearch(a, lo, YYYY, v)); О а. ХX: lo+1, YҮҮ: hi O b. XXXX: mid, YYYY: mid О с. ХXXX: lo, YYYY: hi O d. XXXX: lo+1, YYYY: hi-1 O e. XXXX: mid+1. YYYY: mid-1arrow_forwardBubble sort pseudocode: Bubblesort (A, n) // A is array, n items to sort For i = n-1 to 1 For j = 1 to i If A[j] < A[j+1] Swap (A[j], A[j+1]) (A) Determine the big-O running time (tight bound) of bubble sort in the left column. Show your derivation. This pseudocode assumes the array starts at index 1. Count comparisons as the critical operation. Your solution should count the exact number of comparisons as a function of n (number of items being sorted). In converting your formula for number of comparisons to a closed-form formula (as opposed to a summation formula), you do not need to show the derivation (such as writing the summation twice and dividing by two), though it is okay to do that. You do need to show the closed-form formula for exact number of comparisons, before simplifying it to a big-O representation. As a reference, study the stairs examples in the book. See figure 1.2, pg. 20. (B) Prove that f(n) = 1000n5 + 20000n2 + 32 is O(n6 ); Is this a tight upper bound? Why…arrow_forwardBubble sort pseudocode: Bubblesort (A, n) // A is array, n items to sort For i = n-1 to 1 For j = 1 to i If A[j] < A[j+1] Swap (A[j], A[j+1]) (A) Determine the big-O running time (tight bound) of bubble sort in the left column. Show your derivation. This pseudocode assumes the array starts at index 1. Count comparisons as the critical operation. Your solution should count the exact number of comparisons as a function of n (number of items being sorted). In converting your formula for number of comparisons to a closed-form formula (as opposed to a summation formula), you do not need to show the derivation (such as writing the summation twice and dividing by two), though it is okay to do that. You do need to show the closed-form formula for exact number of comparisons, before simplifying it to a big-O representation. (B) Prove that f(n) = 1000n5 + 20000n2 + 32 is O(n6 ); Is this a tight upper bound? Why or why not?arrow_forward
- In the following recursive function, A is the input array with size n. RANDOM(n) produces a uniformly random number between 1 and n. 1: function RECALG2(A, n) if n < 5 then return A[1] 2: for i + 1 to | yn] do A[i] <- A[i] – A[[i * /n]] s– A[1] k + RANDOM(n) if k < 2n/3 then s+ s+RECALG2(A,n) 3: 4: 5: 6: 7: 8: return s (a) Determine its worst case asymptotic time complexity. (b) Using probabilistic analysis, determine its average asymptotic time complexity.arrow_forwardConsider the following functions which both take as arguments three n-element arrays A, B, and C: СОMPARE-1(A, В, С) For i =1 to n For j= 1 to n If A(i] + C[i] > B[j] Return FALSE Return TRUE СOMPARE-2(A, В, С) aux := A[1] + C[1] For i = 2 to n If A[i] + C[i] > aux Then aux := A[i] + C[i] For j = 1 to n If aux > B[j] Return FALSE Return TRUE (a) When do these two functions return TRUE? (b) What is the worst-case running time for each function?arrow_forwardPython code for the following algorithms? Insertion Sort InsertionSort(A, n){for i 1 to ntemp a[i]for j i where j>0 and temp < a[j-1]a[j] a[j-1] // shift elementsa[j] temp // insert} Selection Sort SelectionSort(A, n) {for i 0 to n-1min ifor j i+1 to nif (A[j] < A[min])min jswap(A[i], A[min])}swap( &lhs, &rhs ){tmp lhslhs rhsrhs tmp}arrow_forward
- Apply bubble sort algorithm to the following array, and contents of arr[] after each pass. arr[] 65 55 35 25 45 15 After pass 1: After pass 2: After pass 3: After pass 4: After pass 5:arrow_forwardRank SortWrite an efficient parallel program that reads n integers from any input file and implements rank sort using p processors (p << n). Assume n is divisible by p.Given n integers stored in an array, rank sort computes rank of every a[j] as follows: Rank a[j] = no. of array elements that are ≤ a[j] for k = 0, 1, 2 … j-1 + no. of array elements that are < a[j] for k = j+1, j+2, j+3,… n – 1 Note: Every a[j] is compared with all array elements.arrow_forwardPlease help Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer. A subarray is a contiguous subsequence of the array. Example 1: Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray. Constraints: 1 <= nums.length <= 2 * 104 -10 <= nums[i] <= 10 The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education