The Algorithm Design Manual Second Edition


Stop and Think: Incremental Correctness



Download 5,51 Mb.
Pdf ko'rish
bet30/488
Sana31.12.2021
Hajmi5,51 Mb.
#273936
1   ...   26   27   28   29   30   31   32   33   ...   488
Bog'liq
2008 Book TheAlgorithmDesignManual

Stop and Think: Incremental Correctness

Problem: Prove the correctness of the following recursive algorithm for increment-

ing natural numbers, i.e. y



→ y + 1:

Increment(y)



if y = 0 then return(1) else

if (mod 2) = 1 then

return(2


· Increment( y/2 ))

else return(+ 1)

Solution: The correctness of this algorithm is certainly not obvious to me. But as

it is recursive and I am a computer scientist, my natural instinct is to try to prove

it by induction.

The basis case of = 0 is obviously correctly handled. Clearly the value 1 is

returned, and 0 + 1 = 1.

Now assume the function works correctly for the general case of n



1. Given

this, we must demonstrate the truth for the case of n. Half of the cases are

easy, namely the even numbers (For which (mod 2) = 0), since + 1 is explicitly

returned.

For the odd numbers, the answer depends upon what is returned by

Increment(

y/2 ). Here we want to use our inductive assumption, but it isn’t

quite right. We have assumed that increment worked correctly for n



− 1, but

not for a value which is about half of it. We can fix this problem by strengthening

our assumption to declare that the general case holds for all y

≤ n−1. This costs us

nothing in principle, but is necessary to establish the correctness of the algorithm.




1 . 3

R E A S O N I N G A B O U T C O R R E C T N E S S



17

Now, the case of odd (i.e. = 2+ 1 for some integer m) can be dealt with

as:

2

· Increment( (2+ 1)/2 ) = 2 · Increment(+ 1/2 )



=

2

· Increment(m)

=

2(+ 1)



=

2+ 2 = + 1

and the general case is resolved.


Download 5,51 Mb.

Do'stlaringiz bilan baham:
1   ...   26   27   28   29   30   31   32   33   ...   488




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