Find the missing integer----programming pearls(second chapter)

    Given a tape that contains at most 1,000,000 twenty-bit integers in random order, find a twenty-bit integer
that isn't on the tape (and there must be at least one missing - Why?).

    a. How would you solve this problem with ample quantities of main memory?

    b. How would you solve it if you had two tape drives, but only a few dozen words of main memory?
    c. How would you solve it with only one tape drive (which contains the input), and only 48 words of main memory?

Solution:

   Here's the discussion + solution in Bentley's "Programming Pearls".


   [a.] Given a tape that contains at most one million twenty-bit integers in random order, we are to find
one twenty-bit integer not on the tape. (There must be at least one missing , because there are 2^20 or
1,048,576 such integers.) With ample main memory, we could use the bit-vector technique [...] and dedicate
131,072 8bit bytes to a bitmap representing the integers seen so far.

   [b.] The problem, however, also asks how we can find the missing integer if we have only a few dozen
words of main memory and several extra tape drives. To set this up as a binary search we have to define a
range, a representation for the elements within the range, and a probing method to determine which half of
 a range holds the missing integer . How can we do this?

    We'll use as the range a sequence of integers known to contain at least one missing element, and we'll
represent the range by a tape containing all the integers in it. The insight is that we can probe a range by
counting the elements above and below its midpoint: either the upper or the lower range has at most half
the elements in the total range. Because the total range has a missing element, the lesser half must also
have a missing element. These are most of the ingredients of a binary search algorithm for the problem;
try putting them together yourself before you peek at the solutions to see how Ed Reingold did it. These
uses of binary search just scratch the surface of its applications in programming. A root finder uses binary
search to solve a single-variable equation by successively halving an interval; numerical analysts call this
the bisection method. When the selection algorithm in Solution 10.9 partitions around a random element
and then calls itself recursively on all elements on one side of that element, it is using a "randomized" binary
search. Other uses of binary search include tree data structures, data processing algorithms that run on card
sorters (which use the corresponding decimal search), and program debugging (when a program dies a silent
death, where do you place print commands to home in on the guilty statement?). In each of these examples,
thinking of the program as a few embellishments on top of the basic binary search algorithm can give the
programmer that all-powerful aha!

    [Ed Reingold's solution of b. -- book, page 165]:


    It is helpful to view this binary search in terms of the twenty bits that represent each integer . In the first
pass of the algorithm we read the (at most) one million input integers and write those with a leading zero bit
to one tape and those with a leading one bit to another tape. One Probe / One of those two tapes contains
at most 500,000 integers, so we next use that tape as the current input and repeat the probe process, but
this time on the second bit. If the original input tape contains N elements, the first pass will read N integers,
the second pass at most N/2, the third pass at most N/4, and so on, so the total running time is proportional
to N. The missing integer could be found by sorting on tape and then scanning, but that would require time
proportional to N log N. This problem and solution are due to Ed Reingold of the University of Illinois.


    [c.] Here is an elegant, randomized solution. In our 48 8-words of main memory, we have enough space to
store 16 randomly, independently generated 20-bit words, sorted in increasing order (we only need a negligible
time to sort these words). In the remaining 48*8-16*20 = 64 bits we can keep a 1-bit marker (initially 0) for
the index of each random word (so we need 16 more bits). Pass once through the tape. For each word we read,
we check if it is already among our randomly generated words. (For this, we just make a binary search in a 16-long
sorted list - at most 5 comparisons). If we found that a word in memory is on the tape, we mark it, by making its bit
equal to 1 (so, we do at most one write operation per tape word). After the pass, we check to see if there is a word
among the 16 ones in memory, that was not found on the tape, by checking all the bits.If all bits are marked, we
repeat. Otherwise, output one word whose corresponding bit is 0.

   Let's compute now the probability that all the 16 random words are on the tape.

   Pr(x1, x2, ..., x16 are all on the tape) =

   = Pr(x1 is on the tape)*P(x2 is on the tape)*...*P(x16 is on the tape)

   = Pr(x1 is on the tape) ^ 16 (since the words are independently, identically distributed - obviously

                                                  uniform in the 20-bit word space)


   <= ( 10^6 / 2^20 )^16 = (5^6 / 2^14)^16 = (5^3/2^7)^32 ~ 0.468

    So, the probability of success is more than 50%. Since the number of passes is geometrically distributed, the expected
number of passes is then at most 2.

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值