Introduction to Algorithms, Third Edition


-2 Searching an unsorted array



Download 4,84 Mb.
Pdf ko'rish
bet99/618
Sana07.04.2022
Hajmi4,84 Mb.
#534272
1   ...   95   96   97   98   99   100   101   102   ...   618
Bog'liq
Introduction-to-algorithms-3rd-edition

5-2
Searching an unsorted array
This problem examines three algorithms for searching for a value
x
in an unsorted
array
A
consisting of
n
elements.
Consider the following randomized strategy: pick a random index
i
into
A
. If
AŒi 
D
x
, then we terminate; otherwise, we continue the search by picking a new
random index into
A
. We continue picking random indices into
A
until we find an
index
j
such that
AŒj 
D
x
or until we have checked every element of
A
. Note
that we pick from the whole set of indices each time, so that we may examine a
given element more than once.
a.
Write pseudocode for a procedure R
ANDOM
-S
EARCH
to implement the strat-
egy above. Be sure that your algorithm terminates when all indices into
A
have
been picked.


144
Chapter 5
Probabilistic Analysis and Randomized Algorithms
b.
Suppose that there is exactly one index
i
such that
AŒi 
D
x
. What is the
expected number of indices into
A
that we must pick before we find
x
and
R
ANDOM
-S
EARCH
terminates?
c.
Generalizing your solution to part (b), suppose that there are
k
1
indices
i
such that
AŒi 
D
x
. What is the expected number of indices into
A
that we
must pick before we find
x
and R
ANDOM
-S
EARCH
terminates? Your answer
should be a function of
n
and
k
.
d.
Suppose that there are no indices
i
such that
AŒi 
D
x
. What is the expected
number of indices into
A
that we must pick before we have checked all elements
of
A
and R
ANDOM
-S
EARCH
terminates?
Now consider a deterministic linear search algorithm, which we refer to as
D
ETERMINISTIC
-S
EARCH
. Specifically, the algorithm searches
A
for
x
in order,
considering
AŒ1; AŒ2; AŒ3; : : : ; AŒn
until either it finds
AŒi 
D
x
or it reaches
the end of the array. Assume that all possible permutations of the input array are
equally likely.
e.
Suppose that there is exactly one index
i
such that
AŒi 
D
x
. What is the
average-case running time of D
ETERMINISTIC
-S
EARCH
? What is the worst-
case running time of D
ETERMINISTIC
-S
EARCH
?
f.
Generalizing your solution to part (e), suppose that there are
k
1
indices
i
such that
AŒi 
D
x
. What is the average-case running time of D
ETERMINISTIC
-
S
EARCH
? What is the worst-case running time of D
ETERMINISTIC
-S
EARCH
?
Your answer should be a function of
n
and
k
.
g.
Suppose that there are no indices
i
such that
AŒi 
D
x
. What is the average-case
running time of D
ETERMINISTIC
-S
EARCH
? What is the worst-case running
time of D
ETERMINISTIC
-S
EARCH
?
Finally, consider a randomized algorithm S
CRAMBLE
-S
EARCH
that works by
first randomly permuting the input array and then running the deterministic lin-
ear search given above on the resulting permuted array.
h.
Letting
k
be the number of indices
i
such that
AŒi 
D
x
, give the worst-case and
expected running times of S
CRAMBLE
-S
EARCH
for the cases in which
k
D
0
and
k
D
1
. Generalize your solution to handle the case in which
k
1
.
i.
Which of the three searching algorithms would you use? Explain your answer.


Notes for Chapter 5
145

Download 4,84 Mb.

Do'stlaringiz bilan baham:
1   ...   95   96   97   98   99   100   101   102   ...   618




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish