How does O transform this sum like that?

I'm in the process of self-studying the CLRS book. My mathematical background is poor so I'm trying to learn the maths as I go along too.

I don't understand the math in CLRS section 6.4 where they go from:

$$\sum_{h=0}^{\lg n} \frac{n}{2^{h+1}} O(h)$$

to:

$$O\left(n \sum_{h=0}^{\lg n} \frac{h}{2^h}\right).$$

I understand that the $n$ is moved out to before the sum. But how does the remaining $1/2^{h+1}$ become $h/2^h$?

• You seem to have forget the $O(h)$ factor. This explains where the $h$ comes from. Since $1/2^{h+1}$ and $1/2^h$ differ only by a constant factor, they are interchangeable in big O notation. – Yuval Filmus Sep 6 '16 at 1:33
• Just as a tip: the CLRS book is great at making people losing the big intuition about algorithms because if focus too much on math formalism instead of focusing on understanding. If you are having this problem, try the book Algorithms and Data Structures by Robert Lafore. It is unbeatable in terms of pedagogical approach. I can say this from first person experience :) – user5193682 Sep 6 '16 at 6:00
• @user9589 YMMV; I think it's often the other way around. – Raphael Sep 6 '16 at 8:40
• Your issue is purely with the mathematics of Landau notation; heaps don't come into the picture here at all. – Raphael Sep 6 '16 at 8:40

Intuitively, the $O$ can swallow any constant factor. Here, we do so because the sum we get is on the cheat sheet and we can proceed more easily.

Formally, we need to apply the definition of $O$, once in either direction. I do not think the first term makes a whole lot of sense. What the authors probably mean is

$\qquad\displaystyle \sum_{h=0}^{\lg n} \frac{n}{2^{h+1}} \cdot f(h)$

with some $f \in O(h)$. Then it remains to note that

$\qquad\displaystyle \sum_{h=0}^{\lg n} \frac{n}{2^{h+1}} \cdot f(h) \quad\leq\quad \sum_{h=0}^{\lg n} \frac{n}{2^{h+1}} \cdot c \cdot h \quad=\quad \frac{c}{2} \cdot n \cdot \sum_{h=0}^{\lg n} \frac{h}{2^{h}}$

for all $n \geq n_0$, with $c,n_0$ the constants from $f \in O(h)$ (unfold the definition). Applying the definition again concludes the proof.

Note that if we had $f \in \Theta(h)$, we would get a similar lower bound and could thus show a $\Theta$-bound for the sum.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7732652,"math_prob":0.9988189,"size":914,"snap":"2019-43-2019-47","text_gpt3_token_len":310,"char_repetition_ratio":0.13186814,"word_repetition_ratio":0.014084507,"special_character_ratio":0.3358862,"punctuation_ratio":0.074257426,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999145,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-22T09:00:07Z\",\"WARC-Record-ID\":\"<urn:uuid:f34610cc-c9d6-4b6b-975d-e557b480cbc4>\",\"Content-Length\":\"143146\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b69f07d3-1582-4552-98a5-2096d2557a4b>\",\"WARC-Concurrent-To\":\"<urn:uuid:08a76989-2b87-4fdc-9e32-0faf4e549ad9>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/63184/how-does-o-transform-this-sum-like-that\",\"WARC-Payload-Digest\":\"sha1:ZLF64AR7GUXG2TLEC76UHW4J3ENAEEJ5\",\"WARC-Block-Digest\":\"sha1:TYNGERVX5ZEBMZTJ35QVGTPQUEHOMHU6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570987813307.73_warc_CC-MAIN-20191022081307-20191022104807-00018.warc.gz\"}"}