You are on page 1of 78

Course notes for

csc 165 h:

Mathematical Expression and Reasoning for Computer Science

Fall 2012

Gary Baumgartner

Danny Heap

Richard Krueger

Franois Pitt c

Department of Computer Science University of Toronto

hese notes re liensed under gretive gommons ettriutionD xonEgommerilD xo herivtives QFH nported vienseF ou my opyD distriuteD nd trnsmit these notes for free nd without seeking spei permission from the uthorsD s long s you ttriute the work to its uthorsD you do not use it for ommeril purposesD nd you do not lter it in ny wyF eny other use of these notes requires the express written permission of the uthorsF isit http://creativecommons.org/licenses/by-nc-nd/3.0/ for full detilsF
Copyright c 2012 by Gary Baumgartner, Danny Heap, Franois Pitt c

Course notes for csc 165 h

Contents
1 Introduction

IFI IFP IFQ IFR IFS

ht9s csc 165 h outc F F F F F F F F rumn versus tehnil ommunition rolemEsolving F F F F F F F F F F F F F F snspirtionl puzzles F F F F F F F F F F F ome mthemtil prerequisites F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F S F U F V F W F IH F F F F F F F F F F F F F F F F F F F F F F F F F F F F F
15

Logical Notation

PFI PFP PFQ PFR PFS PFT PFU PFV PFW PFIH PFII PFIP PFIQ PFIR PFIS PFIT PFIU PFIV PFIW

niversl quntition F F F F F F F F F ixistentil quntition F F F F F F F F ropertiesD setsD nd quntition F F F entenesD sttementsD nd predites F smplitions F F F F F F F F F F F F F F F F untition nd implition together uous truth F F F F F F F F F F F F F F F iquivlene F F F F F F F F F F F F F F F F estriting domins F F F F F F F F F F F gonjuntion @endA F F F F F F F F F F F F hisjuntion @yrA F F F F F F F F F F F F F xegtion F F F F F F F F F F F F F F F F F F ymoli grmmr F F F F F F F F F F F F ruth tles F F F F F F F F F F F F F F F F utologyD stisilityD unstisility F vogil rithmeti4 F F F F F F F F F F F ummry of mnipultion rules F F F F F wultiple quntiers F F F F F F F F F F F F wixed quntiers F F F F F F F F F F F F F

IS IT IT IV IW PP PQ PQ PR PR PR PS PT PU PU PV PW QH QH QQ QR QS QU QU QU QV QV QW RH

Proofs

QFI QFP QFQ QFR QFS QFT QFU QFV QFW QFIH

ht is proofc F F F F F F F F F F F F F F F F F F F F hiret proof of universllyEquntied implition F F en odd exmple of diret proof F F F F F F F F F F F F sndiret proof of universllyEquntied implition F hiret proof of universllyEquntied predite F F F roof y ontrdition F F F F F F F F F F F F F F F F F hiret proof struture of the existentil F F F F F F F wultiple quntiersD implitionsD nd onjuntions F ixmple of proving sttement out sequene F ixmple of disproving sttement out sequene Q

33

Course notes for csc 165 h

QFII QFIP QFIQ QFIR QFIS


4

xonEoolen funtion exmple F F F F F F F F ustituting known results F F F F F F F F F F roof y ses F F F F F F F F F F F F F F F F F F fuilding formule nd tking formule prt ummry of inferene rules F F F F F F F F F F gorretnessD running time of progrms F F F F finry @se PA nottion F F F F F F F F F F F F voop invrint for se P multiplition F F F unning time of progrms F F F F F F F F F F F viner serh F F F F F F F F F F F F F F F F F F un time nd onstnt ftors F F F F F F F F esymptoti nottionX wking figEy preise F glulus3 F F F F F F F F F F F F F F F F F F F F yther ounds F F F F F F F F F F F F F F F F F F esymptoti nottion nd lgorithm nlysis F snsertion sort exmple F F F F F F F F F F F F F yf lgorithms nd stokrokers F F F F F F F F ixerises for symptoti nottion F F F F F F F ixerises for lgorithm nlysis F F F F F F F F sndution interlude F F F F F F F F F F F F F F F he prolem F F F F en impossile proof edutions F F F F F gountility F F F F F higonliztion F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

F F F F F F F F F F F F F F F F F F F F F F F F F

RI RI RP RR RT
49

Algorithm Analysis and Asymptotic Notation

RFI RFP RFQ RFR RFS RFT RFU RFV RFW RFIH RFII RFIP RFIQ RFIR RFIS SFI SFP SFQ SFR SFS

RW RW SH SP SP SQ SR ST SU SW TH TI TR TS TT TW TW UI UP US

A Taste of Computability Theory

69

Chapter 1

Introduction
1.1 What's csc 165 h about?

sn ddition to hkingD omputer sientists hve to e le to understnd progrm speitionsD essD nd their workmte9s odeF hey lso hve to e le to write lerD onise doumenttion for othersF sn our ourse you9ll work onX  Expressing yourself lerly @using inglish nd mthemtil expressionAF  
Understanding Deriving

tehnil douments nd logil expressionsF

onlusions from logil rgumentsD severl proof tehniquesF

 Analyzing progrm eienyF sn this ourse we re out communicating preciselyX  Knowing nd sying wht you menF  Understanding wht others sy nd menF e wnt this ourse to help you during your university reer whenever you need to red nd understnd tehnil mteril | ourse textooksD ssignment speitionsD etcF
Who needs csc 165 h?
You

need this ourse if you doX


memorize have have

mthY troule explining wht you re doing in mthemtil or tehnil questionY troule understnding word prolemsF

You

need this ourse if you don'tX


like

reding mth textooks to lern new mthY enjoy tlking out strt x nd y just s muh s when onrete exmples re given for x nd y Y have redit for csc 238 h in your demi historyD or intend to tke csc 240 hF S

Course notes for csc 165 h

Why does CS need mathematical expressions and reasoning?

e ll enjoy hkingD tht is designing nd implementing interesting lgorithms on omputersF erhps not ll of us ssoite this with the sort of strt thinking nd mnipultion of symols ssoited with mthemtisF rowever there is useful twoEwy ontmintion etween mthemtis nd omputer sieneF rere re some exmples of rnhes of mthemtis tht tint prtiulr rnhes of gomputer ieneX Computer graphics use multiEvrile lulusD projetive geometryD liner lgerD physisEsed modE elling Numerical analysis uses multivrile lulus nd liner lger Cryptography uses numer theoryD eld theory Networking uses grph theoryD sttistis Algorithms use omintorisD proilityD set theory Databases use set theoryD logi AI uses set theoryD proilityD logi Programming languages use set theoryD logi
How to do well in csc 165 h
Check

the ourse we pge nd the ourse forum frequentlyF Understand the ourse informtion sheetF his is the doument tht we re ommitted to live y in this ourseF Get in the habit of sking questions nd ontriuting to the nswersF Spend time on this ourseF he model tht we instrutors ssume is tht you work n verge of V{IH hours per week on this ourse @Q{R in leture nd tutorilD nd S{T reviewing notesD working on ssigned prolemsD ttending oe hours @s requiredAD etcFA eny mteril tht9s new to you will require time for you to relly quire it nd use it on other oursesF Don't plagiarize. ssing o someone else9s work s your own is n demi oenseF elwys give generous nd omplete redit when you onsult other soures @ooksD we pgesD other studentsAF
About these notes

hese notes were originlly reted y qry fumgrtner @nd ontriuted to y mny othersAD expnded nd typeset y hnny rep nd ihrd uruegerD nd further expnded nd modied y prn$ois ittF hese notes re written to stnd lone nd over the mteril inluded in the present csc 165 h syllus without the need of supplementry textook @though it9s often dvntgeous to red nother perspetiveAF lese let us know of ny typos or errorsD or nything tht seems @unintentionllyA onfusing on the rst redD so we n mke pproprite orretionsF sn these notes you9ll nd numerous supersriptsF1 hese often indite nswers to questions worked out in letureD nd through the wonders of word proessingD those nswers re formtted s endnotes @t the end of the hpterAF yur motivtion isn9t so muh to give you whiplsh moving your gze etween the question nd the nswerD s to llow you to form your own nswer efore looking t our versionF T

Chapter 1. Introduction

1.2

Human versus technical communication

xturl lnguges @inglishD ghineseD eriD for exmpleA re rih nd full of potentil miguityF sn mny ses humns speking these lnguges shre lot of historyD ontextD nd ssumptions tht remove or redue the miguityF sf we don9t shre @or hoose to momentrily forgetA the history nd ontextD there is rih soure of humour in douleEmenings reted y nturl lngugesF por exmpleD onsider these hedlines listed on httpXGGwwwFdeprtmentsFuknellFeduGlinguistisGsemhedFhtmlX rostitutes ppel to ope srqi hed seeks rms olie egin mpign to run down jyEwlkers heth my use lonelinessD feelings of isoltion wo sisters reunite fter IV yers t hekout ounterF2 gomputers re notorious for lking sense of humourD nd we ommunite with them using extremely onE strined lnguges lled progrmming lngugesF sn progrmming lngugesD expressions ren9t expeted to e miguousF rumn tehnil ommunition out omputing must e similrly onstrinedF e hve to ssume less ommon history nd ontext is shred with the other humns prtiipting in tehnil ommunitionD nd mispled ssumptions n result in tstropheF e im for inresed precisionD tht is smller tolerne for miguityF e will use mathematical logicD preise lngugeD s form of ommunition in this ourseF wthemtiins shre ommon dilet to tlk unmiguously out prtiulr onepts in their work @e.g.D dierentile funtions re ontinuous4AF yften ordinry words @ontinuous4A re used with reE stritedD or speilD meningsF he sme word my hve dierent tehnil mening in dierent mtheE mtil ontextsD for exmple group my men one thing in group theoryD nother in omintoril design theoryF ine tehnil lnguge is used etween humn eingsD some degree of miguity is tolertedD nd proly neessryF por exmpleD n udiene of tv progrmmers would not ojet to the sutle shift in the mening used for 4 in the following frgmentsX3
/** Sorts a in ascending order */ public void sort(int[] a) ...

versus
// sets a to 1 a = 1;

ine nother humn is reding our ommentsD this potentil for doule mening is enignF e omputer reding our omments wouldD of ourseD e unforgivingF roweverD tv progrmmers hve to ssume fmilE irity with progrmming from their udieneD to voiding driving others rzy y writing long omments @nd eing driven rzy y long omments written y othersAF sn this ourse we n9t ssume the ontext neessry to lwys onlude tht you know wht you9re syingD so you9ll hve to demonstrte it expliitlyF yn the other hndD you will lern to understnd somewht impreise sttements tht n e mde preise from the ontextF U

Course notes for csc 165 h

1.3

Problem-solving

yf ourseD s omputer sientist you re expeted to do more thn express yourself lerly out the lgorithmsD methodsD nd lsses tht you either develop or useF ou must lso work on solving new nd hllenging prolemsD sometimes without even knowing in dvne whether solution is possileF ou will lern to lne insights tht my not e fully rtiulteD with rigour tht onvines yourself nd others tht your insights re orretF ou need oth piees to sueedF e will try to teh some tehniques tht inrese your hnes of gining insight into mthemtil prolems tht you enounter for the rst timeF elthough these tehniques ren9t gurnteed to sueed for every mthemtil prolemD they work often enough to e usefulF wuh of our pproh is sed on qeorge oly9s in row to olve itD4 nd other ooks following tht pprohF elthough you n nd lots of referenes to this on the weD here9s pris of oly9s pprohX e Understand the problem: wke sure you know wht is eing skedD nd wht informtion you9ve een givenF st helps to reEstte the prolem @sometimes severl timesA in your own wordsD perhps repreE senting it in dierent wys or drwing some digrmsF Plan a solution: erhps you9ve seen similr prolemF ou might e le to use either its resultD or the method of solving itF ry working kwrdsX ssume you9ve solved the prolem nd try to dedue the nextEtoElst step in solving itF ry solving simpler version of the prolemD perhps solving smll or prtiulr sesF Carry out your plan: ee whether your pln for solution leds somewhereF st my e neessry to repet prts of the erlier stepsF hen you9re stukD try to rtiulte extly wht you9re missingF Review any solution you achieve: vook k on ny piees of the puzzle you solveD try to rememer wht led to rekthroughs nd wht loked progressF grefully test your solution until you9re onvined @nd n onvine skeptil peerA tht you9ve got solutionF ixtend the solved prolem to new prolemsF xotie how these steps dier from our usul pttern of either voiding work on prolem @string t lnk pgeAD or diving in without plnF he very ide of seprting the pln for nding solution from the t of nding solution seems weird nd unnturlF e9ll dd further unnturl suggestionX you should keep reord @notes or journlA of your prolemEsolving ttemptsF his turns out to e useful oth in solving the prolem t hnd nd lterD relted prolemsF rere9s n exmple of relElife prolem @evesdropping on streetrA tht you might pply oly9s pproh toF ou9re swinging from the grip on streetr during rush hourD nd you her the following onverstion frgments ehind youD etween persons e nd fX Person A: s hven9t seen you in ges3 row old re your three kids nowc Person B: he produt of their ges @in yersA is QTF ou egin to suspet tht f is diult onverstion prtnerF Person A: ht doesn9t relly nswer my questionF F F Person B: ellD the sum of their ges @in yersA is | t this point re engine goes y nd osures the rest of the nswerF Person A: ht still doesn9t relly tell me how old they reF Person B: ellD the eldest plys pinoF Person A: ykyD s seeD so their ges re | t this point you hve to get oD nd you miss the nswerF V

Chapter 1. Introduction

1.4

Inspirational puzzles

es inspirtion to the usefulness of mthemtil logi nd resoning to solving prolemsD we sumit to you the following puzzlesF ih is relted to prolems ommon in omputer sieneD nd is interesting in its own rightF he diulty of these puzzles vries widelyD nd we intentionlly give no indition of their presumed diulty nor their solutionsF fy the end of this ourseD you will likely e le to solve most of these puzzles @indeed mny you my e le to solve nowAF  3 boxes uppose you re ontestnt on gme show nd you re presented with Q oxesF snside one is prizeD whih you will win if you hose the orret oxF he gme goes thuslyX you hoose oxD nd the host opens one of the remining oxes whih is emptyF ou my then swith your hoie to the remining ox or sty with your originl hoieF hih ox would you hoose for the est hne of winning the prizec hyc  3 labelled boxes sn the next roundD you re gin presented with Q oxesD one ontining smll prizeF his timeD you must hoose one oxD nd if you hoose orretlyD you win the prizeF yn loser exmintionD you notie lel on eh oxF he prize is not hereF he prize is hereF he prize is not hereF hih ox do you hoosec  2 labelled boxes sn the next roundD you re presented with just two oxesD with one ontining prizeF he host explins tht two stgehndsD edm nd frinD pk the oxesF edm lwys puts true sttement on the oxD nd frin lwys puts flse sttement on the oxF ou don9t know who pked the oxesD or even if they were oth pked y the sme person or dierent peopleF he oxes syX he prize is not hereF ixtly one ox ws pked y frinF hih ox do you hoosec  2 labelled boxes surprise sn the nl roundD you re presented with two more oxesF he host tells you one ox ontins the grnd prizeD ut if you hoose the wrong oneD you lose everythingF he lels syX he prize is not hereF ixtly one sttement on the oxes is flseF ou reson tht if the sttement on the right ox is trueD the left ox sttement must e flseD so the prize is in the left oxF sf the sttement on the right ox is flseD either oth sttements re true or oth re flseF hey nnot oth e trueD sine the one on the right is flseF o oth re flseD nd the prize must e in the left oxF o you hoose the left oxF ou open it nd it9s empty3 he host lims he didn9t lie to youF ht9s wrongc @he dierene etween this nd the previous puzzle is sutle ut si nd essentil for rigorous tretment of logiFA  Knights and Knaves yn the islnd of knights nd knvesD every inhitnt is either knight or knveF unights lwys tell the truthD nd knves lwys lieF ou ome ross two inhitntsD let9s ll them A nd B F IF erson A sysX s m knve or B is knightF4 gn you determine wht A nd B rec PF erson A sysX e re oth knvesF4 ht re A nd B c QF erson A sysX sf B is knveD then s9m knightF4 erson B sysX e re dierentF4 ht re A nd B c W

Course notes for csc 165 h

RF ou sk AX ere you oth knightsc4 A nswers either es or xoD ut you don9t know enough to solve the prolemF ou then sk B X ere you oth knvesc4 B nswers either es or xoD nd now you know the nswerF ht re A nd B c SF A nd B re gurding two doorsD one leding to tresure nd one leding to feroious lion whih will surely et youF ou must hoose one doorF ou my sk one gurd one yesGno question efore hoosing doorF ht question do you skc ss it esier if you know one is knight nd one is knve @ut you don9t know whih is whihAc 
Mother Eve

heoremX here is womn on irth suh tht if she eomes sterileD the whole humn re will die out euse ll women will eome sterileF roofX iither ll women will eome sterile or notF sf yesD then ny womn stises the theoremF sf noD then there is some womn tht does not eome sterileF he is then the one suh tht if she eomes sterile @ut she does notAD the whole humn re will die outF ss this rgument onviningc
Santa Claus

ifeX nt glus existsD if s m not mistkenF rusndX ell of ourse nt glus existsD if you re not mistkenF ifeX rene my sttement is trueF rusndX yf ourse3 ifeX o s ws not mistkenD nd you dmitted tht if s ws not mistkenD then nt glus existsF herefore nt glus existsF ere you onvinedc hy or why notc
Daemon in a Pentagon

here is pentgon nd t eh vertex there is n integer numerF he numers n e negtiveD ut their sum is positiveF e demon living inside the pentgon mnipultes the numers with the following tomi tionF sf it spots negtive numer t one of the vertiesD it dds tht numer to its two neighours nd negtes the numer t the originl vertexF rove tht no mtter wht numers we strt withD eventully the demon nnot hnge ny of the numersF
Some mathematical prerequisites

1.5

rere re some mthemtil oneptsD nd nottionD tht we9ll ssume you re omfortle with during this ourseF e won9t neessrily e tehing this mterilD so the onus is on you to mke sure you relly re omfortle with this mteril nd if notD to sk out itF ou my lso wnt to refer to this setion when justifying onlusions in proofs you write up for this ourseF
Set theory and notation

e set is olletion of H or more things4F hese things re lled elements of the set nd re often presented s list surrounded y urly rkets @resAD with omm etween eh elementF Z: he integersD or whole numers f: : : ; P; I; H; I; P; : : : gF N: he nturl numers or nonEnegtive integers fH; I; P; : : : gF xotie tht the onvention in gomputer iene is to inlude H in the nturl numersD unlike in some other disiplinesF Z+ : he positive integers fI; P; Q; : : : gF IH

Chapter 1. Introduction

Z : he negtive integers fI; P; Q; : : : gF Z : he nonEzero integers f: : : ; P; I; I; P; : : : g a Z fHg a Z Z+ F Q: he rtionl numers @rtios of integersAD omprised of fHgD Q+ @positive rtionlsAD nd Q @negtive rtionlsAF he set of ll numers of the form p=qD where p P Z nd q P Z F R: he rel numersD omprised of fHgD R+ @positive relsAD nd R @negtive relsAF he set of ll numers of the form m:d1 d2 d3 D where m P Z nd d1 ; d2 ; d3 ; : : : P fH; I; P; Q; R; S; T; U; V; WgF x P A: x is n element of AD4 or x is in AF4 A  B : A is suset of B F4 ivery element of A is lso n element of B F A a B : A equls B F4 A nd B ontin extly the sme elementsD in other words A  B nd B  AF A B : A union B F4 he set of elements tht re in either AD or B D or othF A B : A intersetion B F4 he set of elements tht re in oth A nd B F A n B or A B : A minus B F4 he set of elements tht re in A ut not in B @the set diereneAF jAj: rdinlity of AF4 he numer of elements in AF ? or fg: he empty setF4 e set tht ontins no elementsF fy onventionD for any set AD ?  A @we will see logil justition for this ft when we disuss vacuous truth in etion PFUAF @AA: he power set of AF4 he set of ll susets of AF por exmpleD suppose A a fUQ; gD then @AA a f?; fUQg; fg; fUQ; ggF fx X P @xAg or fx j P @xAg: he set of ll x for whih P @xA is trueF4 por exmpleD fx P Z X os@xA > Hg a f: : : ; R; P; H; P; R; : : : g @even integersAF
Number theory

sf m nd n re nturl numersD with n Ta HD then there is extly one pir of nturl numers @q; rA suh thtX m a qn C r; n > r ! H: e sy tht q is the quotient of m divided y nD nd r is the remainderF e lso sy tht m mod n a rF sn the speil se where the reminder r is zero @so m a qnA we sy tht n divides m nd write n j mF e sy tht n is divisor of m @e.g.D R is divisor of IPAF gonvine yourself tht ny nturl numer is divisor of HD nd tht I is divisor of ny nturl numerF e nturl numerD pD is prime if it hs extly two positive divisorsF hus P; Q; S; U; II re ll prime ut I is not @too few positive divisorsA nd W is not @too mny positive divisorsAF here re innitely mny primesD nd ny integer greter thn I n e expressed @in extly one wyA s produt of one or more primesF
Functions

e9ll use the stndrd nottion f X A 3 B to sy tht f is funtion from set A to B F sn other wordsD for every x P A there is n ssoited f @xA P B F rere re some ommon numerEtheoreti funtions long with their propertiesF e9ll use the onvention tht vriles x; y P R wheres m; n P Z+ F minfx; yg: minimum of x or yF4 he smller of x or yF ropertiesX minfx; yg x nd minfx; yg yF mxfx; yg: mximum of x or yF4 he lrger of x or yF ropertiesX x mxfx; yg nd y mxfx; ygF II

Course notes for csc 165 h

jxj: solute vlue of xD4 whih is x; if x ! H x; if x < H xotie tht similr nottion is used for the rdinlity of setD so you hve to py ttention to the ontextF gd@m; nA: gretest ommon divisor of m nd nF4 he lrgest positive integer tht divides oth m nd nF lm@m; nA: lest ommon multiple of m nd nF4 he smllest positive integer tht is multiple of oth m nd nF ropertyX gd@m; nA lm@m; nA a mnF x or oor@xA: he lrgest integer tht is not lrger thn xD Vx P R; y a x D y P Z y
x @Vz P Z; z xAz yA

dxe or eil@xA: he smllest integer tht is not smller thn xD Vx P R; y a dxe D y P Z y ! x @Vz P Z; z ! x A z ! yA
Inequalities
For any

m; n P Z: m < n if nd only if m C I x; y; z; w P R: < yz sf x < yD then bxz a yz b Xxz > yz


V bxz b `

nD nd m > n if nd only if m ! n C IF

For any

 sf x < y nd w

z D then x C w < y C z F

if z > H  if z a H if z < H  sf x < y nd y z @or x y nd y < z AD then x < z F  jx C yj jxj C jyjF his is n exmple of the triangle inequalityF
Exponents and logarithms
For any

a; b; c P R+ : a a log c if nd only if b
b

a cF

For any

x P R+ :

ln x a log x nd lg x a log2 x
e

For any

a; b; c P R+

and

n P Z+ :

b a b1 b b ab + @b A a b b =b a b b0 a I a b a @abA
n
=n a c a a c c ac a a c c

blogb a a a log b log @acA a log a C log c log @a A a c log a log @a=cA a log a log c log I a H
a a b b b b c b b b b b b

IP

Chapter 1. Introduction

Chapter 1 Notes

vike thisF 2 he word ppel4 in the rst hedline hs two meningsD so one interprettion is tht the ope is fond of prostitutesD nd nother is tht prostitutes hve sked the ope for somethingF he words hed4 nd rms4 eh hve two meningsD so one interprettion is tht the ody prt ove some srqi person9s shoulders is looking for the ppendges elow their shouldersD nd nother is tht the most senior srqi is looking for weponsF he phrse run down4 n men either hitting with r or looking forF sn the fourth hedlineD it9s not ler to whom deth uses loneliness nd isoltionX the ded person or their survivorsF sn the fth hedline it9s not ler whether the hekout line ws moving really slowlyD or tht the hekout ounter ws just the lotion of their reunionF 3 sn the rst frgment a4 mens the ojet referred to y the vlue in aF4 sn the seond a4 mens the vrile aF4
1

IQ

Course notes for csc 165 h

IR

Chapter 2

Logical Notation
2.1 Universal quantification

gonsider the following tle tht ssoites employees with propertiesX el mle THDHHH fetty femle SHH grlos mle RHDHHH houg mle QHDHHH illen femle SHDHHH plo femle PHDHHH glims out individul ojets n e evluted immeditely @el is mleD plo mkes PHDHHHAF fut the tulr form lso llows lims out the entire dtse to e onsideredF por exmpleX ivery employee mkes less thn UHDHHHF ss this lim truec o long s we restrit our universe to the six employeesD we n determine the nswerF1 hen lim is mde out ll the ojets @in this ontextD humns re ojets3A eing onsidered @i.e.D in our universe4AD this is lled Universal QuantificationF he mening is tht we mke expliit the logil quntity @we quntify4A every memer of lss or universeF inglish eing the slippery ojet it is llows severl wys to sy the sme thingX ih employee mkes less thn UHDHHHF ell employees mke less thn UHDHHHF imployees mke less thn UHDHHHF2 yur universe @aka domin4A is the given set of six employeesF hen we sy everyD we men everyF his is not lwys true in inglishD for exmple ivery dy s hve homeworkD4 proly doesn9t onsider the dys preeding your irth or fter your dethF xow onsiderX ih employee mkes t lest IHDHHHF ss this lim truec row do you knowc3 e single ounterEexmple is suient to refute universllyEquntied limF ht out the following limX ell femle employees mke less thn SSDHHHF ss this lim truec estrit the domin nd hek eh seF4 ht out IS
Employee Gender Salary

Course notes for csc 165 h

ivery employee tht erns less thn SSDHHH is femlec5 row out this limX ivery mle employee mkes less thn SSDHHHF st worked for femlesF6 xotie ptternF o disprove universllyEquntied sttement you need just one ounterEexmpleF o prove one you need to onsider every element in dominF e universllyEquntied sttement of the form ivery P is Q needs single counter-example to disproveD nd verition tht every element of the domin is n example to proveF
2.2 Existential quantification

rere9s nother sort of limX ome employee erns over SUDHHHF et rst this lim doesn9t seem to e out the whole dtseD ut just out n employee who erns over SUDHHH @if tht employee existsD nd el does existAF fut wht outX here is n employee who erns less thn SUDHHHF his lim is lso trueD nd it is veried y ny of the employees in the set ffetty; grlos; houg; illen; plogF st9s not lim out ny prtiulr employee in tht veEmemer setD ut rther lim tht the set isn9t emptyF elthough the nonEempty set might hve mny memersD one exmple of memer of the set is enough to show tht it9s not emptyF xow onsiderX ome employee erns over VHDHHHF his lim is flseF here isn9t n employee in the dtse who erns over VHDHHHF o show the set of employees erning over VHDHHH is emptyD you hve to onsider every employee in the universe nd demonstrte tht they don9t ern over VHDHHHF sn everydy lnguge existentil quntition is expressed sX here is G exists G n G some G t lest one F F F suh tht G for whih F F F D or por G n G some G t lest one F F F D F F F xote tht the inglish word some4 is lwys used inclusively hereD so some ojet is P 4 is true if every ojet is P F he lims re out the existence of one or more elements of domin with some propertyD nd they re exmples of existential quntitionF ixistentil quntition requires you to exhiit just one example of n element with the property to proveD ut it requires you to onsider the entire domin to show tht every element is counter-example to disproveF he ntiEsymmetry etween universl nd existentil quntition my e etter understood y swithE ing our point of view from properties to the sets of elements hving those propertiesF
2.3 Properties, sets, and quantification

vet9s look t tht tle ginF IT

Chapter 2. Logical Notation

el mle THDHHH fetty femle SHH grlos mle RHDHHH houg mle QHDHHH illen femle SHDHHH plo femle PHDHHH ying tht el is mle is equivlent to sying el elongs to the set of mlesF ymolilly we might write el P M or M @elAF st9s useful nd nturl to interhnge the ides of properties nd setsF sf we denote the set of employees s E D the set of femle employees s F D the set of mle employees s M D nd the set of employees who ern less thn SSDHHH s LD then we hve nottion for onisely @nd preiselyA evluting lims suh s M @ploAD7 or L@grlosAF8 o fr the nottion doesn9t seem to hve hieved muhD ut how outX iverything in F is lso in L @in other nottionD F  LAc o our universllyEquntied lim tht ll femles mke less thn SSDHHH turns into lim out susetsF e lredy hve some intuition out susetsD so let9s put it to work y drwing enn digrm @see pigure PFIAF wke sure you re solid on the mening of susetF4 ss set lwys suset of itselfc9 ss the empty set @the set with no elementsA suset of ny setc10
E L F
Doug Betty Flo Ellen Carlos Al

Employee

Gender

Salary

pigure PFIX he only elements of F re lso elements of LD so F  LF sn this prtiulr digrmD the mximum numer of regions onsistent with F  L re oupiedX three out of the four regions re oupiedF xow onsider the lim omething in M is lso in LX there is some mle who does not ern less thn SSDHHH he omplement of L is sometimes denoted LD nd mens elements tht re not in LF yne wy to denote something in M is lso in L4 in set nottion is M L Ta ? | sying something4 is in oth sets is the sme s sying their intersetion is nonEemptyF xowD you should e le to ompre this to the denition of suset to see tht this is sme s sying tht M is not suset of LD or M T LF he ntiEsymmetry of universl nd existentil quntition eomes systemtiX  ivery P is Q mens P  QF o prove this lim you need to onsider every element of P nd show they re lso elements of QF o disprove this limD you need to nd just one element of P tht is not n element of QF  ome P is Q mens P T QF o prove this you need to nd just one P tht isn9t nonEQ @ roundE out wy of sying nd just one P tht is QAF o disprove itD you must onsider every P nd show they re lso nonEQsF IU

end of week 1

Course notes for csc 165 h

2.4

Sentences, statements, and predicates

ell the tle of employees with their genders nd slries from oveX
Employee

el fetty grlos houg illen plo

Gender

mle femle mle mle femle femle

Salary

THDHHH SHH RHDHHH QHDHHH SHDHHH PHDHHH

xow onsider the following limsX Claim PFI: he employee mkes less thn SSDHHHF Claim PFP: ivery employee mkes less thn SSDHHHF gn you deide whether oth lims re true or flsec11 he si dierene etween the two lims is tht glim PFI is out prtiulr employeeD nd it is true or flse depending on the ernings of tht employeeD wheres glim PFP is out the entire set of employeesD E D nd it is true or flse depending on where tht set of employees stnds in reltion to the set LD those who ern over SSDHHHF glim PFI is lled sentenceF st my refer to unquntied ojets @for exmple the employee4AF yne the ojets re speied @sustitutions re mde for the vrile@sAAD sentene is either true or flse @ut never othAF glim PFP is lled statementF st doesn9t refer to ny unquntied vrilesD nd it is either true or flse @never othAF ivery sttement is senteneD ut not every sentene is sttementF sf you wnt to mke it expliit tht sentene refers to unquntied ojetsD you my ll it n open senteneF4 hus sentene is sttement if nd only if it is not openF niversl quntition trnsformed glim PFI into glim PFPD from n open sentene out n unspeied element of the set of employeesD into sttement out the @speied in the dtseA sets of imployees nd those erning over SSDHHHF
Symbols

ymols re useful when they mke expressions lerer nd highlight ptterns in similr expressionsF e lredy moved in the diretion of mking our logil expressions symoli y nming sets E @employeesAD F @femlesAD nd L @those erning less thn SSDHHHAF xming gives us onise expression for these setsD nd it emphsizes the similr roles these sets plyF e introdue more symolism into our sentenesD sttementsD nd predites nowF es progrmmer you rete sentene every time you dene oolen funtionF sn logiD predite is oolen funtionF por onveniene you n nme your prediteD nd you n dene it y showing how it evlutes its inputD using symol to stnd for generi inputF por exmpleD if L is the set of employees erning less thn SSDHHH L@xAX x P LF xotie how similr this is to dening funtion in progrmming lnguge in terms of how it evlutes its prmetersF he symol x is useful in the denition | it holds the prenthesesD @ nd A4D prt so tht we n see tht extly one vlue is neededD nd it shows where to plug tht vlue into the denitionF xotie tht this denition would men the sme things if we repled the symol x with the symol y or the symol y3 F he symol x doesn9t speify ny vlue tht helps determine whether our predite evlutes to true or flseF yur open sentene oveD glim PFID is equivlent to L@xA | we n9t evlute it without sustituting something from the set E for xF L@grlosA is trueD L@elA is flseF IV

Chapter 2. Logical Notation

glim PFP is equivlent to for ll employees xD L@xAF4 he phrse for ll employees x4 qunties the vrile xD nd hnges the lim from n open sentene out unspeied x to sttement out sets E nd LD whih were speied in the dtse oveF yf ourseD in this ontextD employees4 refers to those in our dtseD nd not ny other employeesF e n indite universl quntition symolilly s VD red s for llF4 his mkes sense if we speify the universe @dominA from whih we re onsidering ll4 ojetsF ith this nottionD glim PFP n e written V employeesD the employee mkes less thn SSDHHHF hings eome lerer if we introdue nme for the unspeied employeeX V employees xD x mkes less thn SSDHHHF ine this sttement my eventully e emedded in some lrger nd more omplited strutureD we n dd to the revity nd lrity y dding it more nottionF vet E denote the set of employeesD nd L@xA denote the predite x mkes less thn SSDHHHF4 xow glim PFP eomesX Vx P E; L@xAF e n do something similr with existentil quntitionF e n trnsform L@xA into sttement y sying there is some element of E tht lso elongs to LX here exist employees who ern less thn SSDHHHF Wx P E; L@xAF he symol we use for there exists4 is WF his is sttement out the sets E nd L @it sys they hve ommonD nonEempty susetAD nd not sttement out individul elements of those setsF he symol x doesn9t stnd for prtiulr elementD it rther indites tht there is t lest one element ommon to E nd LF
2.5 Implications

gonsider lim of the form if n employee is mleD then he mkes less thn SSDHHHF his is lled n implicationF st sys tht for employeesD eing mle implies mking less thn SSDHHHF12 his is universl quntition in disguiseD sine it ould e urtely reEexpressed s ivery mle employee erns less thn SSDHHHD4 or Vx P E M; L@xAF xotie tht the implition mles implies less thn SSDHHH4 hs the sme eet s restriting the domin y interseting E with M in the universllyEquntied sttementF roweverD it turns out to e onvenient sometimes to keep the implition mle implies less thn SSDHHH4 seprte from the dominF sn this wyD we n onsider the implition s prt of universes other thn E @perhps H D the set of humnsD or X a fhoug; grlosgAF eprting the implition from the surrounding universe lso mens we don9t hve to dene set for eh prediteD so we ould hve M @xA implies L@xA4 without neessrily dening the sets M nd L @lthough we ould lwys ome up with suitle denitions if we needed toAF tust s with universl quntitionD the only wy to disprove the implition if P then Q4 is to show n instne where P is true ut Q is flseF sfD in every possile instneD we hve either notEP or QD then the implition if P then Q4 is trueF sn the implition if P then QD4 we ll P the antecedent @sometimes the assumptionAD nd Q the consequent @sometimes the conclusionAF ine logil implition orrows the inglish word ifD4 we need to rejet some of the ommon inglish uses of if4 tht we don9t men when if4 is used in logiF sn logi ifF F F then4 tells you nothing out IW

Course notes for csc 165 h

uslityF sf it rined yesterdyD then the sun rose todyD4 is true implitionD ut the @possileA rin didn9t use the @ertinA rising of the sunF elsoD when my mother told me if you et your vegetlesD then you n hve dessertD4 she lso ment otherwise you9ll get no dessertF4 sn ordinry inglishD my mother used ifF F F then4 to men if nd only ifF F F thenF4 sn logi we use the more onstrined meningF e wnt sf P then Q4 to men ivery P is QF4 ht does every P is Q4 tell usc sn our dtse exmpleX Claim PFQ: sf n employee is femleD then she mkes less thn SSDHHHF glim PFQ disusses three setsD E D the set of employeesD F D the set of femle employeesD nd LD the set of employees mking less thn SSDHHHF glim PFQ impliitly invokes universl quntitionD so it is more thn lim out prtiulr employeeF he enn digrm pigure PFI indites the sitution orresponding to our tleF sf you hd no ess to either the tle or the enn digrmD ut only knew the glim PFQ ws trueD wht would you know out IF F D the set of femle employeesc ht else does the implition tell you out illen if you only know tht illen is femlec PF LD the set of employees erning less thn SSDHHHc ht do you know out fetty @if you only know she9s in LA or grlos @if you only know he9s in LAc QF F D the set of mle employeesc hink out oth houg nd elF RF L @the omplement of LAD the set of employees mking SSDHHH or moreF unowing P implies Q4 tells us nothing more out some setsD13 however it does tell us more out othersF14 uppose you hve new employee qrnx @from domin short of vowelsAD plus our enn digrm @PFIAF hih region of the enn digrm would you dd qrnx to in order to mke glim PFQ flsec15 yne tht region is oupiedD does it mtter whether ny of the other regions re oupied or notc16
More symbols

e n write implition symolilly s AD red impliesF4 xow P implies Q4 eomes P A QF glim PFQ ould now e reEwritten s n employee is femle A tht employee mkes less thn SSDHHHF
Contrapositive

he contrapositive of P A Q is XQ A XP @X is the symol for negtionAF sn inglish the ontrpositive of ll P isGre Q4 is ll nonEQ isGre nonEP F4 ut nother wyD the ontrpositive of P implies Q4 is nonEQ implies nonEP F4 he ontrpositive of glim PFQ is n employee doesn9t mke less thn SSDHHH A tht employee is not femleF orD given the struture of the domin E of employeesX n employee mkes t lest SSDHHH A tht employee is mleF hoes the ontrpositive of glim PFQ tell us everything tht glim PFQ itself doesc ghek the enn digrm @PFIAF hoes every enn digrm tht doesn9t ontrdit glim PFQ lso not ontrdit the ontrpositive of glim PFQc17 gn you pply the ontrpositive twiec o do this it helps to know tht pplying negtion @XA twie toggles the truth vlue twie @s9m not not going mens s9m goingAF hus the ontrpositive of the ontrpositive of P A Q is the ontrpositive of XQ A XP D whih is XXP A XXQD equivlent to P A QF PH

Chapter 2. Logical Notation

Converse

he onverse of P A Q is Q A P F sn wordsD the onverse of P implies Q4 is Q implies P F4 en implition nd its onverse don9t men the sme thingF gonsider the enn digrm pigure PFIF ould it work s enn digrm for L A F c18 gonsider n exmple where the @impliitA domin is the set of pirs of numersD perhps R RF Claim PFR: x a I A xy a y     sf we know x a ID then we know xy a yF sf we know x Ta ID then we don9t know whether or not xy a yF sf we know xy a yD then we don9t know whether or not x a IF sf we know xy Ta yD then we know x Ta IF

he ontrpositive of glim PFR isX xy Ta y A x Ta IF ghek the four points we knew from glim PFRD nd see whether we know the sme ones from the ontrE positive @it my e helpful to red them in reverse orderAF ht out the onversec xy a y A x a I with equivlent ontrpositive x Ta I A xy Ta y F he onverse of glim PFR is not equivlent to glim PFRD for exmple onsider the pir @S; HAD tht is x a S nd y a HF sndeedD glim PFR is trueD while its onverse is flseF
Implication in everyday English

rere re some wys of sying P implies Q4 in everydy lngugeF sn eh seD try to think out wht is eing quntiedD nd wht predites @or perhps setsA orrespond to P nd QF  sf P D then QF sf nomintedD s will not stndF4 sf you think s9m lyingD then you9re lir34  henever P D then QF henever s her tht songD s think out ie remF4 s get herturn whenever s et supper too lteF4  P is suientGenough for Q hierentiility is suient for ontinuityF4 wthing ngerprints nd motive re enough for guiltF4  gn9t hve P without Q here re no rights without responsiilitiesF4 ou n9t sty enrolled in csc 165 h without pulseF4  P requires Q uessful progrmming requires skillF4 PI

Course notes for csc 165 h

 por P to e trueD Q must e true G needs to e true G is neessry o pss csc 165 hD student needs to get RH7 on the nlF4  only if G only when Q s9ll go only if you insistF4
P

por the nteedent @P A look for ifD4 whenD4 enoughD4 suientF4 por the onsequent @QA look for thenD4 requiresD4 mustD4 needD4 neessryD4 only ifD4 whenF4 sn ll sesD hek whether the expeted mening in inglish mthes the mening of P A QF sn other wordsD you9ve got n implition ifD in every possile instneD either P is flse or Q is trueF
2.6 Quantification and implication together

o fr we hve onsidered n implition to e universl quntition in disguiseX


Claim

PFS: sf n employee is mleD then tht employee mkes less thn SSDHHHF

he inglish indenite rtile n4 signls tht this mens ivery mle employee mkes less thn SSDHHHD4 nd this losed sentene is either true or flseD depending on the domin of employeesF his n e expressed s Vx P E; M @xA A L@xAD nd we n seprte the por ll employeesD4 portion from the if the employee is mleD then the employee mkes less thn SSDHHHD4 portionF ymolillyD we n think out Vx P E seprtely from M @xA A L@xAD giving us some exiility out whih vlues we might sustitute for xF his llows us to express the unquntied implitionX
Claim

PFT: sf the employee is mleD then tht employee mkes less thn SSDHHHF

he inglish denite rtile the4 often signls n unspeied vlueD nd hene n open senteneF e ould trnsform glim PFT k into glim PFS y prexing it with por every employeeD F F F 4
Claim

PFU: por every employeeD if the employee is mleD then tht employee mkes less thn SSDHHHF

ine the lim is out mle employeesD we re tempted to sy Vm P M; L@mAD whih would e orret if the only mles we were onsidering were those in E | Vm P E M; L@mA would ertinly pture wht we menF sing tht pproh we would restrit the domin tht we re universlly quntifying over y interseting with other dominsF roweverD it is often onvenient to restrit in nother wyX set our domin to the lrgest universe in whih the predites mke senseD nd use implition to restrit furtherF e don9t hve to void resoning out nonEmles when we sy Ve P E; M @eA A L@eAD nd we get the sme mening s Vm P E M; L@mAF st lso often hppens tht the predite expressed y M @eA doesn9t netly trnslte into set tht n e interseted with set E D so the universlly quntied implition formt n e hndyF por exmpleD Vn P N; n > H A I=n P R mens the sme things s Vn P N n fHg; I=n P RD ut expressing the set N n fHg seems more wkwrd thn using universllyEquntied implitionD nd there re much worse sesF row do you feel out verifying glim PFT for ll six vlues in E D whih re trueGflsec19 ho you feel unomfortle sying tht the implitions with flse nteedents re truec smplitions re strngeD espeilly when we onsider them to involve uslity @whih we don9t in logiAF gonsiderX
Claim

PFV: sf it rins in oronto on tune PD QHHUD then there re no loudsF

ss glim PFV true or flsec ould your nswer hnge if you ould wit the required numer of dedesc ht if you wited nd tune PD QHHU were ompletely dry dy in orontoD is glim PFV true or flsec20 PP

Chapter 2. Logical Notation

2.7

Vacuous truth

e use the ft tht the empty set is suset of ny setF vet x P R @the domin is the rel numersAF ss the following implition true or flsec 2 Claim PFW: sf x Px C P a HD then x > x C SF e nturl tendeny is to proess x > x CS nd think tht9s impossileD so the implition is flseF4 roweverD there is no rel numer x suh tht x2 Px CP a HD so the nteedent is flse for every rel xF henever the nteedent is flse nd the onsequent is either true or flseD the implition s whole is trueF enother wy of thinking of this is tht the set where the nteedent is true is empty @vuousAD nd hene suset of every setF uh n implition is sometimes lled vacuously trueF sn generlD if there re no P sD we onsider P A Q to e trueD regrdless of whether there re ny QsF enother wy of thinking of this is tht the empty set ontins no ounterexmplesF se this sort of thinking to evlute the following limsX21 Claim PFIH: ell employees mking over VHDHHH re femleF Claim PFII: ell employees mking over VHDHHH re mleF Claim PFIP: ell employees mking over VHDHHH hve supernturl powers nd pink toenilsF
2.8 Equivalence

uppose el quits the domin E F gonsider the lim Claim PFIQ: ivery mle employee mkes etween PSDHHH nd RSDHHHF ss glim PFIQ truec ht is its onversec22 ss the onverse truec hrw enn digrmF he two properties desrie the sme set of employeesY they re equivalentF sn everydy lngugeD we might sy en employee is mle if nd only if the employee mkes etween PSDHHH nd RSDHHHF4 his n e deomposed into two sttementsX Claim PFIR: en employee is mle if the employee mkes etween PSDHHH nd RSDHHHF Claim PFIS: en employee is mle only if the employee mkes etween PSDHHH nd RSDHHHF rere re some other everydy wys of expressing equivleneX  P i Q @i4 eing n revition for if nd only if4AF  P is neessry nd suient for QF  P A QD nd onverselyF ou my lso her  P extly G preisely when Q por exmpleD if our domin is RD you might sy x2 C Rx C R a H preisely when x a PF4 iquivlene is getting t the smeness4 @so fr s our domin goesA of P nd QF e my dene properties P nd Q dierentlyD ut the sme memers of the domin hve these properties @they dene the sme setsAF ymolilly we write P D QF o now en employee is mle D he mkes etween PSDHHH nd RSDHHHF yddlyD our @flseA glim PFW is n equivleneD sine the implitions re vuously true in oth diretionsX x2 Px C P a H D x > x C SF PQ

Course notes for csc 165 h

2.9

Restricting domains

smplitionD quntitionD onjuntion @ndD4 represented y the symol AD nd set intersetion re tehniques tht n e used to restrit dominsX  ivery D tht is lso P is lso Q4 eomes Vx P D; P @xA A Q@xAD whih we use more ommonly thn the equivlent Vx P D P; Q@xA @ht9s the dierene etween this nd Vx P D; P @xA Q@xAcA  ome D tht is lso P is lso Q4 eomes Wx P D; P @xA Q@xAD whih we use more ommonly thn the equivlent Wx P D P; Q@xA @ht9s the dierene etween this nd Wx P D; P @xA A Q@xAcA
2.10 Conjunction (And)

e use @nd4A to omine two sentenes into new sentene tht lims tht oth of the originl sentenes re trueF sn our employee dtseX Claim PFIT: he employee mkes less thn USDHHH nd more thn PSDHHHF glimPFIT is true for el @who mkes THDHHHAD ut flse for fetty @who mkes SHHAF sf we identify the sentenes with predites tht test whether ojets re memers of setsD then the new predite tests whether someody is in oth the set of employees who mkes less thn USDHHH nd the set of employees who mke more thn PSDHHH | in other wordsD in the intersetionF ss it oinidene tht resemles @only more pointyAc xotie thtD symolillyD P Q is true extly when oth P nd Q re trueD nd flse if only one of them is true nd the other is flseD or if oth re flseF e need to e reful with everydy lnguge where the onjuntion nd4 is used not only to join sentenesD ut lso to smer4 sujet over ompound prediteF sn the following sentene the sujet here4 is smered over pen4 nd telephoneX4 Claim PFIU: here is pen nd telephoneF sf we let O e the set of ojetsD p@xA men x is penD nd t@xA men x is telephoneD then the ovious mening of glim PFIU isX23 here is pen nd there is telephoneF4 fut pednt who hs een oserving the trend where phones eome inresingly smller nd diult to use might think glim PFIU mensX24 here is penEphoneF4 rere9s nother exmple whose miguity is ll the more striking sine it ppers in ontext @mtheE mtisA where one would expet miguity to e shrply restritedF he solutions reX x < IH nd x > PH x > IH nd x < PH he uthor mens the union of two sets in the rst seD nd the intersetion in the seondF e use in the seond seD nd disjuntion @or4A in the rst seF
2.11 Disjunction (Or)

he disjuntion or4 @written symolilly s A joins two sentenes into one tht lims tht t lest one of the sentenes is trueF por exmpleD he employee is femle or mkes less thn RSDHHHF PR

Chapter 2. Logical Notation

his sentene is true for plo @she mkes PHDHHH nd is femleA nd true for grlos @who mkes less thn RSDHHHAD ut flse for el @he9s neither femleD nor does he mke less thn RSDHHHAF sf we viewed this or9ed4 sentene s predite testing whether someody elonged to t lest one of the set of employees who re femle4 or the set of employees who ern less thn RSDHHHD4 then it orresponds to the unionF es mnemoniD the symols nd resemle eh otherF ristorillyD the symol omes from the vtin word vel4 mening orF e use to inlude the se where more thn one of the properties is trueY tht isD we use n inclusiveorF sn everydy inglish we sometimes sy ndGor4 to speify the sme thing tht this ourse uses or4 forD sine the mening of or4 n vry in inglishF he sentene iither we ply the gme my wyD or s9m tking my ll nd going home nowD4 doesn9t inlude oth possiilities nd is n exlusiveEorX one or the otherD ut not othF4 en exlusiveEor is sometimes dded to logil systems @syD inside omputerAD ut we n use negtion nd equivlene to express the sme thing25 nd void the omplition of hving two dierent types of orF4
2.12 Negation

end of week 2

e9ve mentioned negtion few times lredyD nd it is simple oneptD ut it9s worth exmining it in detilF he negtion of sentene simply inverts its truth vlueF he negtion of sentene P is written s XP D nd hs the vlue true if P ws flseD nd hs the vlue flse if P ws trueF xegtion gives us powerful wy to hek our determintion of whether sttement is trueF por exmpleD we n hek tht Claim PFIV: ell employees mking over VHDHHH re femleF is true y verifying tht its negtion is flseF he negtion of glim PFIV is Claim PFIW: xot ll employees mking over VHDHHH re femleF e nnot nd ny employees mking over VHDHHH tht re not femle @in ftD we nnot nd ny employees mking over VHDHHH t ll3AD so this sentene must e flseD mening the originl must e trueF ou should feel omfortle resoning out why the following re equivlentX  X@Wx P D; P @xA Q@xAA D Vx P D; @P @xA A XQ@xAAF sn wordsD xo P is Q4 is equivlent to ivery P is nonEQF4  X@Vx P D; P @xA A Q@xAA D Wx P D; @P @xA XQ@xAAF sn wordsD xot every P is Q4 is equivlent to here is some P tht is nonEQF4 ometimes things eome lerer when negtion pplies diretly to the simplest predites we re disussingF gonsider Claim PFPH: Vx P D; Wy P D; P @x; y A ht does it men for glim PFPH to e flseD i.e.D X@Vx P D; Wy P D; P @x; yAAc st mens there is some x for whih the reminder of the sentene is flseX Claim PFPI: X@Vx P D; Wy P D; P @x; y AA D Wx P D; X@Wy P D; P @x; y AA o now wht does the negted suEsentene menc st mens there re no y9s for whih the reminder of the sentene is trueX Claim PFPP: Wx P D; X@Wy P D; P @x; y AA D Wx P D; Vy P D; XP @x; y A here is some x tht for every y mkes P @x; yA flseF es negtion @XA moves from left to rightD it ips universl quntition to existentil quntitionD nd vie versF ry it on the symmetril ounterprt Wx P D; Vy P D; P @x; yAD nd onsider PS

Course notes for csc 165 h

X@Wx P D; Vy P D; P @x; yAA D Vx P D; X@Vy P D; P @x; yAA sf it9s not true tht there exists n x suh tht the reminder of the sentene is trueD then for ll x the reminder of the sentene is flseF gonsidering the remining susenteneD if it9s not true tht for ll y the reminder of the susentene is trueD then there is some y for whih it is flseX X@Wx P D; Vy P D; P @x; yAA D Vx P D; Wy P D; XP @x; yA por every x there is some y tht mkes P @x; yA flseF ry omining this with implitionD using the rule we disussed erlierD plus heworgn9s lwX X@Wx P D; Vy P D; @P @x; yA A Q@x; yAAA D X@Wx P D; Vy P D; @XP @x; yA Q@x; yAAA
2.13 Symbolic grammar

ith onnetives suh s implition @AAD onjuntion @AD nd disjuntion @A dded to quntiersD you n form very omplex preditesF sf you require these omplex predites to e unmiguousD it helps to impose strit onditions on wht expressions re llowedF e synttilly orret sentene is sometimes lled wellEformed formul @revited wAF xote tht syntti orretness hs nothing to do with whether sentene is true or flseD or whether sentene is open or losedF he syntx @or grmmr rulesA for our symoli lnguge n e summrized s followsX  eny predite is wF  sf P is wD so is XP F  sf P nd Q re wsD so is @P QAF  sf P nd Q re wsD so is @P QAF  sf P nd Q re wsD so is @P A QAF  sf P nd Q re wsD so is @P D QAF  sf P is w @possily open in vrile xA nd D is setD then @Vx P D; P A is wF  sf P is w @possily open in vrile xA nd D is setD then @Wx P D; P A is wF  xothing else is wF hese rules re reursiveD nd tell us how we9re llowed to uild ritrrily omplex sentenes in our symoli lngugeF he rst rule is lled the se se nd speies the most si sentene llowedF he rules following the se se re reursive or indutive rulesX they tell us how to rete new legl sentene from smller legl sentenesF he lst rule is losure ruleD nd sys we9ve overed everythingF sn prtieD we wnt to void writing expressions with mny prenthesesD so we use precedence to dismigute expressions tht re missing prenthesesF sn the grmmr oveD preedene dereses from top to ottomF sn other wordsD in the sene of prenthesesD prentheses must e dded to suEexpressions ner the top before those ner the ottomF por exmpleD the expression elowX Vx P D; P @xA XQ@xA A R@xA must e understood s follows | where we hve indited the order in whih prentheses were put inD ording to the order of preedene oveX

Vx P D; 3 @2 P @xA @1 XQ@xAA1 A2 A R@xA 3

ou should e le to onvert more looselyEstrutured predite into wD or w into more looselyE strutured prediteD whenever it9s onvenientF PT

Chapter 2. Logical Notation

2.14

Truth tables

redites evlute to either true or flse one they re ompletely speied @ll unknown vlues re lled inAF sf you uild omplex predites from simpler onesD using onnetivesD it9s importnt to know how to evlute the omplex predite sed on the evlution of fullyEspeied vrints of the simpler predites it is uilt out ofF e powerful tehnique for determining the possile truth vlue of omplex predite is the use of truth tablesF sn truth tleD we write ll possile truth vlues for the predites @how mny rows do you needc26 AD nd ompute the truth vlue of the sttement under eh of these truth ssignmentsF ih of the logil onnetives yield the following truth tlesF
Q P Q P Q P AQ P DQ p p p p p p p p p p p e often rek omplex sttements into simpler susttementsD ompute the truth vlue of the sustteE mentsD nd omine the truth vlues k into the more omplex sttementsF por exmpleD we n verify the equivlene @P A @Q A RAA D @@P QA A RA using the following truth tleX P

XP p p

A @Q A RA P Q @P QA A R @P A @Q A RAA D @@P QA A RA p p p p p p p p p p p p p p p p p p p p p p ine the rightmost olumn is lwys trueD our sttement is lw of logiD nd we n use it when mnipuE lting our symoli sttementsF
P Q R P
2.15 Tautology, satisfiability, unsatisfiability

QAR

xotie tht in the previous setionD we didn9t speify domins or even menings for P or QD nor worry out wht vlues might reple unspeied symols within P or QF ith truth tles we explored ll possile worlds4 @ongurtions of truth ssignments to P nd QAF his is known s tautologyX you n9t drem up dominD or mening for predites P nd Q tht provides ounterEexmpleD sine the truth tles re identilF his is dierent fromD syD @P A QA D @Q A P AD whih will e true for some hoie of dominD predites P nd QD nd vlues of domin elementsD so we sy this sttement is satisfiableF fut in this seD there re lso hoies of domins ndGor predites in whih it is flseD so it is not tutologyF fe refulX sying tht sttement is stisle only tells us tht it is possile for it to e trueD without sying nything out whether or not it is lso possile for it to e flse @i.e.D whether or not it is lso tutologyAF ht out something for whih no dominsD preditesD or vlues n e hosen to mke it truec uh sttement would e unsatisfiable @or contradictionAF PU

Course notes for csc 165 h

2.16

Logical \arithmetic"

sf we identify nd with set intersetion nd union @for the sets where the predites they re onneting re trueAD it9s ler tht they re associative nd commutativeD so P Q D Q P nd P Q D Q P P @Q RA D @P QA R nd P @Q RA D @P QA R wye it more surprising is tht we hve distributive laws for eh opertion over the otherX P @Q RA D @P QA @P RA P @Q RA D @P QA @P RA e n lso simplify expressions using identity nd idempotency lwsX identity: P @Q XQA D P D P @Q XQA idempotency: P P D P D P P
DeMorgan's Laws

hese lws n e veried either y truth tleD or y representing the sentenes s enn digrms nd tking the omplementF entene s1 s2 is flse extly when t lest one of s1 or s2 is flseF ymolillyX X@s1 s2 A D @Xs1 Xs2 A entene s1 s2 is flse extly when oth s1 nd s2 re flseF ymolillyX X@s1 s2 A D @Xs1 Xs2 A fy using the ssoitivity of nd D you n extend this to onjuntions nd disjuntions of more thn two sentenesF
Implication, bi-implication, with

:; _, and ^

sf we shde enn digrm so tht the lrgest possile portion of it is shded without ontrditing the implition P A QD we gin some insight into how to express implition in terms of negtion nd unionF he region tht we n hoose ojet x from so tht P @xA A Q@xA is P Q nd this esily trnsltes to XP QF his gives us n equivleneX @P A QA D @XP QA xow use heworgn9s lw to negte the implitionX X@P A QA D X@XP QA D @XXP XQA D @P XQA ou n use enn digrm or some of the lws introdued erlier to show tht iEimplition n e written with D D nd XX @P D QA D @@P QA @XP XQAA heworgn9s lw tells us how to negte thisX X@P D QA D X@@P QA @XP XQAA D D @@XP QA @P XQAA PV

Chapter 2. Logical Notation

Transitivity of universally-quantified implication

gonsider Vx P D; @@P @xA A Q@xAA @Q@xA A R@xAAA @s hve put the prentheses to mke it expliit tht the implitions re onsidered efore the AF ht does this sentene imply if onsidered in terms of P D QD nd RD the susets of D where the orresponding predites re truec27 e n lso work this out using the logil rithmeti rules we introdued oveX write @@P @xA A Q@xAA @Q@xA A R@xAAA A @P @xA A R@xAA using only ; ; nd XD nd show tht it is tutology @lwys trueAF elterntivelyD use heworgn9s lwD the distriutive lwsD nd nything else tht omes to mind to show tht the negtion of this sentene is ontrditionF husD implition is trnsitiveF e similr trnsformtion is tht Vx P D; @P @xA A @Q@xA A R@xAAA D Vx P D; @@P @xA Q@xAA A R@xAAF xotie this is stronger thn the previous result @n equivlene rther thn n implitionAF his sttement n e proven with the help of truth tlesF
2.17 Summary of manipulation rules

he following is summry of the si lws nd rules we use for mnipulting forml sttementsF ry proving eh of them using enn digrms or truth tlesF identity lws P @Q XQA @A P P @Q XQA @A P idempoteny lws P P @A P P P @A P ommuttive lws P Q @A Q P P Q @A Q P @P D QA @A @Q D P A ssoitive lws @P QA R @A P @Q RA @P QA R @A P @Q RA distriutive lws P @Q RA @A @P QA @P RA P @Q RA @A @P QA @P RA ontrpositive P A Q @A XQ A XP implition P A Q @A XP Q equivlene @P D QA @A @P A QA @Q A P A doule negtion X@XP A @A P heworgn9s lws X@P QA @A XP XQ X@P QA @A XP XQ implition negtion X@P A QA @A P XQ equivlene negtion X@P D QA @A X@P A QA X@Q A P A quntier negtion X@Vx P D; P @xAA @A Wx P D; XP @xA X@Wx P D; P @xAA @A Vx P D; XP @xA quntier distriutive lws Vx P D; P @xA Q@xA @A @Vx P D; P @xAA @Vx P D; Q@xAA (where R does not contain variable x) Wx P D; P @xA Q@xA @A @Wx P D; P @xAA @Wx P D; Q@xAA Vx P D; R Q@xA @A R @Vx P D; Q@xAA Vx P D; R Q@xA @A R @Vx P D; Q@xAA Wx P D; R Q@xA @A R @Wx P D; Q@xAA Wx P D; R Q@xA @A R @Wx P D; Q@xAA vrile renming Vx P D; P @xA @A Vy P D; P @yA (where y does not appear in P (x)) Wx P D; P @xA @A Wy P D; P @yA PW

Course notes for csc 165 h

2.18

Multiple quantifiers

wny sentenes we wnt to reson out hve mixture of preditesF por exmple Claim PFPQ: ome femle employee mkes more thn PSDHHHF e n mke few denitionsD so let E e the set of employeesD Z e the integersD sm@e; kA e e mkes slry of more thn kD nd f @eA e e is femleF xow s ould rewriteX Claim PFPQ @symbolically): We P E; f @eA sm@e; PSHHHAF st seems it inexile to omine e mking slryD nd n inequlity ompring tht slry to PSHHHD prtiulrly sine we lredy hve voulry of predites for ompring numersF e n rene the ove expression so tht we let s@e; kA e e mkes slry kF xow s n rewrite ginX Claim PFPQ @rewritten): We P E; Wk P Z; f @eA s@e; kA k > PSHHHF xotie tht the following re ll equivlent to glim PFPQX Wk P Z; We P E; f @eA s@e; kA k > PSHHH We P E; f @eA @Wk P Z; s@e; kA k > PSHHHA his is euse is ommuttive nd ssoitiveD nd the two existentil quntiers ommuteF
2.19 Mixed quantifiers

sf you mix the order of existentil nd universl quntiersD you my hnge the mening of senteneF gonsider the tle elow tht shows who respets whoX
A A B C D E F

sf we wnt to disuss this tle symolillyD we n denote the domin of people y P D nd the predite x respets y4 y r@x; yAF gonsider the following open senteneX Claim PFPR: Wx P P; r @x; y A @tht is y is respeted y someody4A sf we prepended the universl quntier Vy P P to glim PFPRD would it e truec es usulD hek eh element of the dominD olumnEwiseD to see tht it isF28 ymolillyD Claim PFPS: Vy P P; Wx P P; r @x; y A or iveryody hs someody who respets himGherF4 ou n hve dierent x9s depending on the yD so lthough every olumn hs dimond in some rowD it need not e the sme row for eh olumnF ht would the predite e tht lims tht some row works for eh olumnD tht row is full of dimondsc29 xow we hve to hek whether there is someone who respets everyoneX Claim PFPT: Wx P P; Vy P P; r @x; y A QH

Chapter 2. Logical Notation

ou will nd no suh rowF he only dierene etween glim PFPS nd glim PFPT is the order of the quntiersF he onvention we follow is to red quntiers from left to rightF he existentil quntier involves mking hoieD nd the hoie my vry ording to the quntiers we hve lredy prsedF es we move rightD we hve the opportunity to tilor our hoie with n existentil quntier @ut we ren9t oliged toAF gonsider this numeril exmpleX Claim PFPU: Vn P N; Wm1 P N; Wm2 P N; n a m1 m2 F his sys tht every nturl numer hs two divisorsF ht does it men if you swith the order of the existentilly quntied vriles with the universlly quntied vrilec ss it still truec ht @if nythingA would you need to dd to sy tht every nturl numer hs two distint divisorsc30
Chapter 2 Notes

end of week 3

esD y verifying the lim for eh employeeF 2 fut ontrst the mening of dierentile funtions re ontinuous4 @every dierentile funtion is ontinuousD no exeptionA with the mening of irds y4 @most irds yD ut there re some exeptionsAF 3 fetty mkes SDHHHD whih is wellEknown to e less thn IHDHHHF 4 estrit to femlesD nd eh one mke less thn SSDHHHF 5 plseF houg nd grlos re ounterexmplesF 6 fut it is flse for mlesF el is ounterEexmpleF 7 plseD hek the tleF 8 rueD hek the tleF 9 esD sine it inludes only elements of itselfF hon9t onfuse subset with proper subsetF 10 esD indeed it is suset of every setF he reson is tht it ontins no element tht ould e outside nother setF 11 glim PFI depends on who you men y he employeeF4 sf you speify elD glim PFI is flseD ut if you speify illenD glim PFI is trueF glim PFP is quntiedD so it depends on the entire universe of employeesF glim PFP is flse euse you n nd t lest I ounterexmpleF 12 en untrue implition in the universe we9re onsideringD due to the ounterEexmple elF 13 P @the omplement of P AD nd QF 14 P @we know it9s suset of QA nd Q @the omplement of QD we know it9s suset of P AF 15 edd qrnx to F L @F outside LAF xow qrnx is ounterEexmple to the lim tht every femle employee mkes less thn SSDHHHF 16 xoF gounterEexmple qrnx mkes the implition flseD nd dding other dt doesn9t hnge thisF 17 esF he only enn digrm tht ontrdits glim PFQ or its ontrpositive is one tht hs t lest one element in F outside of LF
1

QI

Course notes for csc 165 h

xoD euse there re elements in L F @houg nd grlosAF 19 e need to verify the following limsX
18

     
20

sf el is mleD then el mkes less thn SSDHHHF sf fetty is mleD then fetty mkes less thn SSDHHHF sf grlos is mleD then grlos mkes less thn SSDHHHF sf houg is mleD then houg mkes less thn SSDHHHF sf illen is mleD then illen mkes less thn SSDHHHF sf plo is mleD then plo mkes less thn SSDHHHF

rueD regrdless of the loud situtionF sn logi P A Q is flse extly when P is true nd Q is flseF ell other ongurtions of truth vlues for P nd Q re true @ssuming tht we n evlute whether P nd Q re true or flseAF 21 ell these lims re trueD lthough possily misledingF eny lim out elements of the empty set is trueD sine there re no ounterexmplesF 22 ivery employee who mkes etween PSDHHH nd RSDHHH is mleF 23 Wx P O; p@xA Wy P O; t@yAD or even Wx P O; Wy P O; p@xA t@yAF 24 Wx P O; p@xA t@xA 25 P exlusiveEor Q4 is the sme s P notEequivlentEto QF4 26 sf you hve n preditesD you need P rows @every omintion of nd pAF 27 st implies tht P is suset of RD sine P  Q nd Q  RF st is not equivlentD sine you n ertinly hve P  R without P  Q or R  QF 28 rueD there9s dimond in every olumnF 29 sf we were thinking of the row orresponding to xD then Vy P P; r@x; yAF 30 Vn P N; Wm1 P N; Wm2 P N; n a m1 m2 m1 Ta m2 F xot true for n a IF
n

QP

Chapter 3

Proofs
3.1 What is a proof?

e proof is n rgument tht onvines someone who is logilD reful nd preiseF he form nd detil of proof n depend on the udiene @for exmpleD whether our udiene hs s muh generl mth knowledgeD nd whether we9re writing in inglish or our symoli formAD ut the fundmentls re the sme whether we9re tlking mthemtisD omputer sieneD physil sienesD philosophyD or writing n essy in literture lssF e proof ommunites wht @nd howA someone understndsD to sve others time nd eortF If you
don't understand why something is true, don't expect to be able to prove it!

row do you go out writing proofc qenerllyD there re two steps or phses to reting proofX IF nderstnding why something is trueF his step typilly requires some retivity nd multiple ttempts until n pproh worksF ou should sk yourself why you re onvined something is trueD nd try to express your thoughts preisely nd logillyF his step is the most importnt @nd requires the most eortAD nd n e done in the shower or s you lie wke in ed @the two most produtive thinking spotsAF ometimes we ll this finding a proofF PF riting up your understndingF fe reful nd preiseF ivery sttement you write should e true in the ontext it9s writtenF st is often helpful to use our forml symoli formD to ensure you9re reful nd preiseF yften you will detet errors in your understndingD nd it9s ommon to then go k to step I to rene your understndingF his is when we re writing up a proofF ometimes these steps n e ominedD nd often these steps feedk on eh otherF es we try to write up our understndingD we disover wD return to step I nd rene our understndingD nd try writing ginF tudents re often surprised tht most of the work oming up with proof is understnding why someE thing is trueF sf you go k to our denition of wht is proofD this should e oviousX to onvine someoneD we rst need to onvine ourselves nd order our thoughts preisely nd logillyF ou will see tht one we gin good understndingD proofs nerly write themselvesF
Taxonomy of results

e lemma is smll result needed to prove something we relly re outF e theorem is the min result tht we re out @t the momentAF e corollary is n esy @or sid to e esyA onsequene of nother resultF e conjecture is something suspeted to e trueD ut not yet provenF1 en axiom is something we ssert to e trueD without justition | usully euse it is selfEevidentF42 QQ

Course notes for csc 165 h

3.2

Direct proof of universally-quantified implication

e wnt to mke onvining rguments tht sttement is trueF e9re llowed @foredD tullyA to use previously proven sttements nd xiomsF por exmpleD if D is the set of rel numersD then we hve plenty of rules out rithmeti nd inequlities in our tooloxF prom these sttementsD we wnt to extend wht we knowD eventully to inlude the sttement we9re trying to proveF vet9s exmine how we might go out doing thisF gonsider n implition we would like to prove tht is of the formX c1: Vx P D; p@xA A q @xA wny lredyEknownEtoEeEtrue sttements re universlly quntied implitionsD hving n identil struE ture to c1F e9d like to nd mong them hinX c2.0: Vx P D; p@xA A r1 @xA c2.1: Vx P D; r1 @xA A r2 @xA FF F Vx P D; r @xA A q@xA hisD in n stepsD proves c1D using the trnsitivity of implitionF e more exile wy to summrize tht the hin c2.0,: : : ,c2.n proves c1 is to ite the intermedite implitions tht justify eh intermedite stepF rere you write the proof tht Vx P D; p@xA A q@xA sX essume x P DF 5 x is generi element of D essume p@xAF 5 x hs property pD the nteedent hen r1 @xAF 5 y c2.0 hen r2 @xAF 5 y c2.1 FF F hen q@xAF 5 y c2.n hen p@xA A q@xAF 5 ssuming nteedent leds to onsequent hen Vx P D; p@xA A q@xAF 5 we only ssumed x is generi D his form emphsizes wht eh existing result dds to our understndingF end when it9s ovious whih result ws usedD we n just void mentioning it @ut e refulD one person9s ovious is nother9s mysteryAF elthough this form seems to tlk out just one prtiulr xD y not ssuming nything more thn x P D nd p@xAD it pplies to every x P D with p@xAF he indenttion shows the sope of our ssumptionsF hen we ssume tht x P DD we re in the world4 where x is generi element of DF here we ssume p@xAD we re in the world4 where p@xA is ssumed trueD nd we n use tht to derive onsequenesF
c2.n:
n

Hunting the elusive direct proof

sn generlD the diulty with diret proof is there re lots of known results to onsiderF he ft tht result is true my not help your prtiulr line of rgument @there re mnyD mnyD mny true ut irrelevnt ftsAF sn prtieD to nd hin from p@xA to q@xAD you gther two lists of results out xX IF results tht p@xA impliesD nd PF results tht imply q@xA our fervent hope is tht some result ppers on oth listsD sine then you9ll hve hinF QR

Chapter 3. Proofs

FF F s2 @xA s1 @xA q @xA enything tht one of the r implies n e dded to the rst listF enything tht implies one of the s n e dded to the seond listF ht does this look like in pituresc sn enn digrms we n think of the r s sets tht ontin p nd myD or my notD e ontined in q @the ones tht ren9t ontined in q re ded endsAF yn the other hndD the s re ontined in q nd myD or my notD ontin p @the ones tht don9t re ded endsAF e hope to nd pth of ontinment from p to qF enother wy to visulize this is y hving the r represented s treeF sn one tree we hve root pD with hildren eing the r tht p impliesD nd their hildren eing results they implyF sn seond tree we hve root qD with hildren eing the results tht imply qD nd their hildren eing results tht imply themF sf the two trees hve ommon nodeD we hve hinF ere you done when you nd hinc xoD you write it upD tidying s you goF emove the results tht don9t ontriute to the nl hinD nd ite the results tht tke you to eh intermedite link in the hinF
i i i i i i

p@xA r1 @xA r2 @xA

What do

xow your two lists hve the form Vx P D; p@xA A @r1 @xA r2 @xA r @xAA Vx P D; @s @xA s1 @xAA A q@xA ine p@xA implies ny nd4 of the r D you n just ollet them in your hed until you nd known resultD sy r1 @xA r2 @xA A r @xAD nd then dd r @xA to the listF yn the other hndD if you hve result on the rst list of the form r1 @xA r2 @xAD you n dd them seprtely to the listF yn the seond listD use the sme pproh ut sustitute for F eny result on the rst list n e spuriously or9ed4 with nythingX r1 @xA A @r1 @xA l@xAA is lwys trueF yn the seond listD we n spuriously nd4 nythingD sine @s1 @xA l@xAA A s1 @xAF sf we hve disjuntion r1 @xA r2 @xA on the rst listD we n use it if we hve result tht @r1 @xA r2 @xAA A q @xAD or the pir of results r1 @xA A q @xAD nd r2 @xA A q @xAF
m k i k k

and

do?

3.3

An odd example of direct proof

uppose you re sked to prove tht every odd nturl numer hs squre tht is oddF ypilly we don9t see ll the links in the hin from n is odd4 to n2 is odd4 instntlyD so we engge in thoughtful wishing @like wishful thinkingD only with muh etter reputtionAF e strt y writing the outline of the proof we would like to hveD to lrify wht informtion we9ve gotD wht we lkD nd hope to ll in the gpsX essume n P NF essume n is oddF FF F hen n2 is oddF hen n is odd An2 is oddF hen Vn P N; n is odd An2 is oddF QS

Course notes for csc 165 h

F trt srthing wy t oth ends of the FF @the it tht represents the hin of results we need to ll inAF ht does it men for n2 to e oddc ellD if there is nturl numer k suh tht n2 a Pk C ID then n2 is odd @y denition of odd numersAF edd tht to the end of the listF imilrlyD if n is oddD then there is nturl numer j suh tht n a Pj C I @y denition of odd numersAF st seem unpromising to tke the squre root of Pk CID so insted rry out the lmostEutomti squring of Pj CIF xowD on our rst listD we hve thtD for some nturl numer j D n2 a Rj 2 CRj CIF sing some lger @distriutivity of multiplition over dditionAD this mens tht for some nturl numer j D n2 a P@Pj 2 CPj ACIF sf we let k from our seond list e Pj 2 C Pj D then we ertinly stisfy the restrition tht k e nturl numer @they re losed under multiplition nd dditionAD nd we hve linked the rst list to the seondF rere9s n exmple of how to formt your nished hinX essume n P NF 5 n is generi nturl numer essume n is oddF 5 n typil odd nturl numer henD Wj H P ND n a Pj H C IF 5 y denition of n odd vet j P N e suh tht n a Pj C IF 5 nme it j hen n2 a Rj 2 C Rj C I a P@Pj 2 C Pj A C IF 5 denition of n2 nd some lger hen Wk P N; n2 a Pk C IF 5 Pj 2 C Pj P ND sine N losed under C; hen n2 is oddF 5 y denition of n2 odd hen n is odd An2 is oddF 5 when s ssumed n oddD s derived n2 odd hen Vn P ND n odd A n2 oddF 5 sine n ws generi nturl numer row out the onverseD Vn P N; if n2 is oddD then n is oddF sf we try reting hinD it seems it s though the nturl diretion is wrongX somehow we9d like to go from q k to pF ht equivlent of n implition llows us to do thisc3 ou set up the proof of the ontrpositive of the onverse @whew3A very similrly to the proof oveD mostly hnging odd4 to evenF4 ry it outF
Another example of direct proof

vet R e the set of rel numersF roveX Vx P R; x > H A I=@x C PA < Q truture the proof s eforeX essume x P RF 5 x is typil rel numer essume x > HF 5 nteedent FF 5 prove I=@x C PA < Q F hen I=@x C PA < QF 5 get here somehow hen x > H A I=@x C PA < QF 5 ssume nteedentD derived onsequent hen Vx P R; x > H A I=@x C PA < QF 5 only ssume x ws typil element of R yf ourseD you need to unwrp the suEproof tht I=@x C PA < QX essume x P RF 5 x is typil element of R essume x > HF 5 nteedent hen x C P > PF 5 x > HD dd P to oth sides hen I=@x C PA < I=PF 5 reiprols reverse inequlityD nd re dened for numers > P hen I=@x C PA < QF 5 sine I=@x C PA < I=P nd I=P < Q hen x > H A I=@x C PA < QF 5 ssumed nteedentD derived onsequent hen Vx P R; x > H A I=@x C PA < QF 5 x ws ssumed to e typil element of R ss the onverse true @wht is the onverseAc4 QT

Chapter 3. Proofs

3.4

Indirect proof of universally-quantified implication

ell tht p A q is equivlent to its ontrpositiveD Xq AXpF his mens tht proving one proves the otherF his is lled n indiret proofF4 he outline formt of n indiret proof of Vx P D; p@xA A q@xA is essume x P DF 5 x is typil element of D essume Xq@xAF 5 negtion of the consequent3 FF F hen Xp@xAF 5 negtion of the antecedent3 hen Xq@xA A Xp@xAF 5 ssuming Xq@xA leds to Xp@xA hen p@xA A q@xAF 5 implition is equivlent to ontrpositive hen Vx P D; p@xA A q@xAF 5 x ws typil element of D his is useful pprohD for exmpleD in proving tht Vn P N; n2 is odd An is oddF
3.5 Direct proof of universally-quantified predicate

hen no implition is sttedD then we don9t ssume @supposeA nything out x other thn memership in the dominF por exmpleD Vx P D; p@xA hs this proof strutureX essume x P DF FF 5 prove p@xA F hen p@xAF hen Vx P D; p@xAF 5 x ws ssumed to e typil element of D
3.6 Proof by contradiction

ometimes you wnt to prove onlusionD QD without ny suitle hypothesisD P to imply itF yne pproh is to sy if everything we lredy know is true is ssumedD then Q followsF4 row do you hoose whih prtiulr portion of everything we lredy know is true4 to fous onc vet logi help fous your rgumentF ymolilly you n represent everything we lredy know is true4 s huge onjuntion of sttementsD P a P1 P2 P F o now we im to prove P A Q using the contrapositiveX XQ A XP F trt y ssuming tht Q is falseD nd then show tht something you lredy know to e true must e flse | ontrdition3 ine P a P1 P2 P is huge onjuntion of sttementsD its negtion is huge disjuntion XP a XP1 XP2 XP D so you don9t need to know in dvne whih of them is ontrditedF ou just follow your @edutedA noseF rere9s the generl formtX essume XQF 5 in order to derive ontrdition FF 5 some steps leding to ontrditionD sy XP F hen XP F 5 ontrditionD sine P is known to e true hen QF 5 sine ssuming XQ leds to ontrdition iulid used this tehnique over PDHHH yers go to prove tht there re innitely mny prime numersF fefore looking t iulid9s proofD you might experiment with proving this ft diretlyF vet9s strt y nming some of the setsGpredites we9ll need for this proofX  P a fp P N X p hs extly two ftorsg  spX Vn P N; jP j > n sn spite of ppernesD sp is not good ndidte for mthemtil indution @whih we9ll see lter in this ourseAF roweverD let9s try XspX
m m m j

QU

Course notes for csc 165 h

end of week 4
3.7

essume XspX Wn P N; jP j 6 nF 5 to derive ontrdition hen there is nite listD p1 ; : : : ; p of elements of P F 5 t most n elements in the list hen s n tke the produt pH a p1 p F 5 nite produts re wellEdened hen pH is the produt of some nturl numers P nd greterF 5 H; I ren9t primesD P; Q re hen pH > IF 5 pH is t lest T hen pH C I > PF 5 dd I to oth sides hen Wp P P; p divides pH C IF 5 every integer > P @suh s pH C IA hs prime divisor vet p0 P P e suh tht p0 divides pH C IF 5 instntite existentil hen p0 is one of p1 ; : : : ; p F 5 y ssumptionD the only primes hen p0 divides pH C I pH a IF 5 divisor of eh term divides dierene hen I P P F gontrdition3 5 I is not prime hen spF 5 ssume Xsp4 leds to ontrdition
k k k

Direct proof structure of the existential

gonsider the exmple Wx P R; x3 C Px2 C Qx C R a PF ine this is the existentilD we need only nd single exmple to show tht the sttement is trueF e struture the proof s followsX vet x a IF 5 hoose prtiulr element tht will work hen x P RF 5 verify tht the element is in the domin hen x3 C Px2 C Qx C R a @IA3 C P@IA2 C Q@IA C R a I C P Q C R a PF 5 sustitute I for x hen Wx P R; x3 C Px2 C Qx C R a PF 5 we gve n exmple he generl form for diret proof of Wx P D; p@xA isX vet x a : : : 5 hoose prtiulr element of the domin hen x P DF 5 this my e oviousD otherwise prove it FF 5 prove p@xA F hen p@xAF 5 you9ve shown tht x stises p Wx P D; p@xAF 5 introdue existentil
3.8 Multiple quantifiers, implications, and conjunctions

gonsider Vx P D; Wy P D; p@x; yAF he orresponding proof struture isX essume x P DF 5 typil element of D vet y a : : : 5 hoose n element tht works FF F hen y P DF 5 verify tht y P D FF F hen p@x; y AF 5 y stises p@x; yA hen Wy; p@x; yAF 5 introdue existentil hen Vx P D; Wy P D; p@x; yAF 5 introdue universl rere9s onrete exmpleF uppose we hve mystery funtion f D mystery onstnts a nd lD nd the following sttement @s hve dded prentheses to indite the onventionl prsingAX
x x x

Ve P R; e > H A @Wd P R; d > H @Vx P R; H < jx aj < d A jf @xA lj < eAA sf we wnt to prove this trueD struture the proof s follows: : :5 QV

Chapter 3. Proofs

sf we wnt to prove the sttement falseD we rst negte itD nd then use one of our proof formts @s use the equivlenes X@p A qA D @p XqA nd X@p qA D @p A XqAAX We P R; e > H Vd P R; d > H A Wx P R; H < jx aj < d jf @xA lj > e yf ourseD this negtion involved severl pplitions of rules we lredy knowD nd now its proof my e written stepEyEstepF xotie thtD in the middle of our proofD we hd 4 to proveF
3.9 Example of proving a statement about a sequence

gonsider the sttementX Claim QFI: Wi P N; Vj P N; a 6 i A j < i nd the sequeneX (A1) H; I; R; W; IT; PS; : : : e9ll use the onvention tht sequenes re indexed y nturl numers @rell tht N a fH; I; P; : : : gD strting t zero just s omputers ountA nd a is the element of the sequene indexed y iD so a0 a HD a1 a ID a2 a RF vooking t the pttern of @eIAD we n write losed form4 formul for a F6 e should of ourse try to understnd QFID y putting it in nturl inglishD pituring tles nd digrmsD thinking of ode tht ould hek itD trying it on vrious exmplesD etcF o understnd whether it is true or flse for @eIA we should use this understndingD inluding tring itF fut let9s fous on the form tht proof tht QFI is true ould tkeF his my even help us understnd QFIF e hve een justifying existentils with n exmpleF oD our proof should strt o something likeX vet i a F hen i P NF FF F e leve ourselves lnk to ll inX spei vlue of iF e lso need to mke sure the i is in NF yften it will e ovious nd we will simply note itF sf notD we9ll tully need to put in proofF xextD we need to prove something for ll j in NF es syntti onvenieneD we prove something for ll j 9s in N y proving it for some unknown j in NF sf we9re reful to not ssume nything out whih j we hveD our proof will hndle ll j 9sF vet i a F hen i P NF 5 hoose helpful one essume j P NF 5 j is typil element of N FF F xotie this time we assume j is in NF ou n imgine W nd V s prt of gmeX  Wx P DX e get to pik xD ut hve to follow the rules nd pik from DF  Vx P DX yur opponent will pik xD ut we n ssume they will follow the rules nd pik from DF e n9t mke ny ssumptions here out whih one from D they will pikF qoing k to our proof strutureD we hveX vet i a F hen i P NF essume j P NF 5 typil element of N essume a 6 iF FF F hen j < iF
j i i j

QW

Course notes for csc 165 h

F e leve ourselves room @the FFA for proof of j < iF yne we ll in vlue of iD the proof of j < i my use three ftsX the vlue we hose for iD j P ND nd a 6 iF efter little thoughtD we deide tht setting i a P is good ideD sine then a 6 i is only true for j a H nd j a ID nd these re smller thn PF e it of experimenttion shows tht the ontrpositiveD X@j < iA A X@a 6 a A is it esier to work withF vet i a PF hen i P NF 5 P P N essume j P NF 5 typil element of N essume X@j < iAF 5 nteedent for ontrpositive hen j > PF 5 negtion of j < i when i a P hen a a j 2 > P2 a RF 5 sine a a j 2 D nd j > P hen a > PF 5 sine R > P hen X@j < iA A X@a 6 PAF 5 ssuming nteedent leds to onsequent hen a 6 P A j < iF 5 implition equivlent to ontrpositive hen Vj P N; a 6 i A j < iF 5 introdue universl hen Wi P N; Vj P N; a 6 i A j < iF 5 introdue existentil
j j j i j j j j j j j

3.10

Example of disproving a statement about a sequence

gonsider now the sttementX Claim QFP: Wi P N; Vj P N; j > i A a a a nd the sequeneX (A2) H; H; I; I; P; P; Q; Q; R; R; S; S; T; : : : vet9s disprove itF ss disproof whole new topic hnkfully noF e simply prove the negtionX H Claim 3.2 : Vi P N; Wj P N; j > i a Ta a es usulD we sketh in the outline of the proof rstX essume i P NF vet j a F hen j P NF FF F hen j > i a Ta a F hen Wj P N; j > i a Ta a F 5 introdution of existentil hen Vi P N; Wj P N; j > i a Ta a F 5 introdution of universl yur opponent piks iD ut we get to pik j F end we re llowed to mke j depend on iF nfortuntelyD while writing up the proof we n9t wit for someone to tully pik iF o how does it help usc e get to desrie generl strtegy for how we would pik prtiulr j if we knew whih prtiulr iF sn other wordsD j n e desried s funtion of iF sn progrmming termsD i is in sope when we pik j X it hs een delred nd n e seen from where we delre j F xotie tht j is not in sope when we delre iX so when we piked i for QFID we weren9t llowed to use j F sf we write progrm tht uses vrile efore it9s delred nd initilizedD the progrm doesn9t even ompileF his is mjor errorF sf you write proof tht does thisD it will lmost ertinly e wrong | nd you will most likely lose lot of mrks3 xow we re left with proving j > i a Ta a @notie we wrote this t the ottomF F F we must hve een thinking hedAF ht form does the proof of onjuntion tkec7
j i j i j i j i j i j i

RH

Chapter 3. Proofs

essume i P NF vet j a F hen j P NF FF F hen j > iF FF F hen a Ta a F hen j > i a Ta a F hen Wj P N; j > i a Ta a F hen Vi P N; Wj P N; j > i a Ta a F o nish this oD we need to hoose vlue for j F sf we hoose wiselyD the rest of the proof flls into pleF8 ht elementry property of rithmeti will we requirec9
j i j i j i j i

3.11

Non-boolean function example

xonEoolen funtions nnot tke the ple of predites @sine predites re expeted to return true or flse vlueA in proofF row should nonEoolen funtions e usedc hene x X R 3 R yX x @oor of x4A is the lrgest integer 6 xF xow we n form the sttementX Claim QFQ: Vx P R; x < x C I st mkes sense to pply x to elements of our dominD or vriles tht we hve introduedD nd to evlute it in predites suh s <4 ut x itself is not vrileD nor senteneD nor prediteF e n9t @sensilyA sy things suh s Vx P R or Vx P R; x x C IF he struture of QFQ is diret proof of universllyEquntied prediteX10 essume x P RF 5 x is typil element of R vet y a xF 5 introdue vrile hen y is the lrgest integer 6 xD so y 6 xF 5 denition of oor ine x < x C ID y < x C IF 5 trnsitivity of < o x < x C IF 5 sine y a x hen Vx P R; x < x C IF 5 sine x ws typil element of R sn some ses you need to rek down sttement suh s y is the lrgest integer 6 x4X y P Z y 6 x @Vz P Z; z 6 x A z 6 y A e didn9t need ll three prts of the denition for our proof oveD nd in prtie we don9t lwys hve to return to denitions when deling with funtionsF por exmpleD we my hve n existing resultD suh sX Vx P R; x > x I row would you prove this resultD using the threeEprt version of the denition of xc
3.12 Substituting known results

ivery proof would eome unmngely long if we hd to inlude inline4 ll the results tht it depended onF e inevitly refer to stndrd results tht re either universlly known @mong mth wonksA or n esily e looked upF ometimes we need to prove smll tehnil result in order to prove something lrgerF ou my view the smller result s helper method @usully returning oolen resultsA tht you use to uild lrger method @your igger proofAF o mke things modulrD you should e le to ll4 or refer to the smller resultF en exmple ours if we wnt to reEyle something proved erlierX RI

Course notes for csc 165 h

Vx P R; x > H A I=@x C PA < QF e wnt to use this in proving Vy P R; y Ta H A I=@y2 C PA < QF he templte to ll in is essume y P RF essume y Ta HF FF F hen I=@y2 C PA < QF hen y Ta H A I=@y2 C PA < QF hen Vy P R; y Ta H A I=@y2 C PA < QF F xow we hve to ll in the FF prtX
Theorem 1:

end of week 5

essume y P RF 5 y is typil element of R essume y Ta HF 5 y positive hen y2 P R nd y2 > HF 5 R losed under D squres of nonEzero rels hen y2 > HD sine y2 Ta H nd y2 > HF 5 only rel numer whose squre is H is H hen I=@y2 C PA < QF 5 y heorem I hen y Ta H A I=@y2 C PA < QF 5 introdution of A hen Vy P R; y Ta H A I=@y2 C PA < QF 5 introdution of universl
3.13 Proof by cases

o prove A A B D it n help to tret some A9s dierently thn othersF por exmpleD to prove tht x2 C x is even for ll integers xD you might proeed y noting tht x2 C x is equivlent to x@x C IAF et this point our resoning hs to rnhX t lest one of the ftors x or x C I is even @for integer xAD ut we n9t ssume tht prtiulr ftor is even for every integer xF o we use proof y sesF11 his is speil se of n or4 luse eing the nteedent of n implitionD i.e.D if you wnt to prove @A1 A2 A A A B F his ould hppen ifD long the wy to proving A A B you use the ft tht A A @A1 A AF xow you need to prove A1 A B D A2 A B; : : : ; A A B F xotie tht in setting this up it is not neessry tht the A e disjoint @mutully exlusiveAD just tht they over A @think of A eing suset of the union of the A AF yne wy to generte the ses is to rek up the domin D a D1 D D so A is the predite tht orresponds to the set D AF xow you hve n equivleneD A D A1 A F e very ommon se ours when the domin prtitions into two prtsD D a D1 D1 D so you n rewrite A s @A D1 A @A XD1 A | we9re using the nottion slightly here y treting D1 oth s set nd s prediteD s we9ve done eforeF rere9s the generl form of proving something y sesX AB gse IX essume AF FF F hen C F gse PX essume B F FF F hen C F ine A B nd in oth @llA ses we onluded C D then C F ememer tht we need one se for eh disjuntD so if we knew A1 A D we9d need n sesF hen you9re reding @or writingA proofsD often the word ssume4 is omitted when dening the seF hough it might sy gse x < kD4 rememer tht x < k is n ssumptionD thus opens new indenttion @sopeA levelF
n n n i i n i i n n

RP

Chapter 3. Proofs

Law of the excluded middle

yften we wnt to proeed y sesD ut don9t hve disjuntion hndy to useF e n lwys introdue one using the vw of the ixluded widdleF his lw of logi sttes tht formul is either true or false | there9s nothing etween @or in the middle4AF husD for ny formul P D the following is sure to e trueX P XP sn your proofD you n then split into two ses depending on whether P is true or flseF tust e sure to negte P orretly3
Example proof using cases

uppose we wnted to prove the following sttementX if n is n integer then n2 C n is evenF vet9s formlize wht we men y the term integer n is even4X por n P ZD let even@nA men Wk P Z; n a PkF vet9s formlize wht we9re provingX 2 Claim QFR: Vn P Z; Wk P Z; n C n a Pk F xotiing tht n2 C n a n@n CIAD we onsider whether n is odd or evenF e know tht every integer is either odd or evenD so let9s stte this formllyX @BA Vn P Z; @Wk P Z; n a Pk C IA @Wk P Z; n a PkAF xow to the proof of our limF sn it we will know tht n existentil is trueD nd we will wnt to use tht knowledgeF e my sk the existentil to return4 n exmple elementD whih we get to nme nd use @we nme it k0 so tht it won9t onit with ny other elements we9re tlking outAF essume n P ZF 5 n is typil nturl numer hen @Wk P Z; n a Pk C IA @Wk P Z; n a PkAF 5 y @BAD n P Z gse IX essume Wk P Z; n a Pk C IF vet k0 P Z e suh tht n a Pk0 C IF 5 instntite existentil hen n2 C n a n@n C IA a @Pk0 C IA@Pk0 C PA a P@Pk0 C IA@k0 C IAF hen Wk P Z; n2 C n a PkF 5 k a @Pk0 C IA@k0 C IA P Z gse PX essume Wk P Z; n a PkF vet k0 P Z e suh tht n a Pk0 F 5 instntite existentil hen n2 C n a n@n C IA a Pk0 @Pk0 C IA a Pk0 @Pk0 C IAF hen Wk P Z; n2 C n a PkF 5 k a k0 @Pk0 C IA P Z hen Wk P Z; n2 C n a PkF 5 true in ll @othA possile ses hen Vn P Z; Wk P Z; n2 C n a PkF 5 introdution of universl
Proving

_ using cases

vet9s prove tht the squre of n integer is triple or one more thn tripleF 2 2 Claim QFS: Vn P N; @Wk P N; n a Qk A @Wk P N; n a Qk C IAF sf we know P QD we n prove disjuntion R S y sesD s followsX P Q gse IX essume P F FF F hen RF RQ

Course notes for csc 165 h

gse PX essume QF FF F hen S F hus R S F12 sf we lredy hve some P Q we n useD then those re the ovious ses to onsiderD though we still hve to deide etween the two wys of piring them up with R nd S F sn generl thoughD piking P nd Q tht work depends ompletely on ontextF hen onstruting proof struturesD stndrd strtegy is to use XP for QX the vw of the ixluded widdle ensures this is trueD nd it is the simplest yet still generl strutureF his of ourse generlizes to more thn two sesX if we know P1 P2 P D nd we wnt to prove Q1 Q D then we n do ses for eh P D in eh se proving Q F e don9t hve to prove ll the Q D nd we n prove some of them in more thn one seF o prove our limD we wnt to use prt of the eminder heoremX
n m i j j

@BA Vn P N; @Wk P N; n a Qk n a Qk C I n a Qk C PA e now proeed with our proof of the lim y sesF yne se is left for you to do s n exeriseF essume n P NF 5 n is typil element of N hen Wk P N; n a Qk n a Qk C I n a Qk C PF 5 y @BA vet k0 P N e suh tht n a Qk0 n a Qk0 C I n a Qk0 C PF gse IX essume n a Qk0 F 2 2 hen Q@Qk0 A a Wk0 a n2 F 5 lger 2 hen Wk P N; n2 a QkF 5 k a Qk0 P N gse PX essume n a Qk0 C IF 2 2 hen Q@Qk0 C Pk0 A C I a Wk0 C Tk C I a n2 F 5 lger 2 2 hen Wk P N; n a Qk C IF 5 k a Qk0 C Pk0 P N gse QX essume n a Qk0 C PF @ixeriseFA hen @Wk P N; n2 a QkA @Wk P N; n2 a Qk C IAF 5 true in ll possile ses hen Vn P N; @Wk P N; n2 a QkA @Wk P N; n2 a Qk C IAF 5 introdution of universl
3.14 Building formulae and taking formulae apart

o fr we9ve een onentrting on proving more nd more omplited sentenesF his mkes senseD sine the sentene we9ve proving determines the struture our proof will tkeF por eh of the logil onnetives nd quntiersD we9ve seen strutures tht llow us to onlude ig sttements from smller onesF he inferene rules tht llow us to do this re olletively lled introduction rulesD sine they llow us to introdue new sentenes of prtiulr typeF fut rrely do we prove things diretly from preditesF e often hve to use known theorems nd results or seprtely proven lemms to redue the length of our proofs to mngele size @n you imgine lwys hving to prove P C P a R from primitive sets eh time you use this ftcAF qood theorems re useful in numer of settingsD nd typilly use numer of onnetives nd quntiersF unowing how to rek omplex sentenes down is eqully importnt s knowing how to uild omplex sentenes upF tust s there re inferene rules llowing us to introdue newD omplex sentenesD there re inferene rules llowing us to rek sentenes down in formlD preise nd vlid wyF hese rules re olletively lled elimination rulesD sine they llow us to eliminte onnetives nd quntiers we don9t wnt nymoreF wost rules should e firly strightEforwrd nd should mke sense to you t this pointY if notD you should review your mnipultion rulesF RR

Chapter 3. Proofs

Double negation elimination

e n9t do muh to remove one negtion @unless we n move it further insideAD ut we know how to get rid of two negtionsF sndeedD this ws mnipultion rule from the previous hpterD ut we n lso tret it s resoning ruleX if we know XXA is trueD we know A is trueF
Conjunction elimination

xerly s esy s negtionD how n we rek up onjuntionc sf we know A B D wht n we onludec13


Existential elimination (or instantiation)

e might know tht Wx P D; B D where B likely mentions x somewhere insideF sn other wordsD we know B is true for some element in DD ut we don9t know whih oneF row n we proeedc e9d proly like to sy something out tht element in D tht B is true forD ut how do we know whih element it isc e don9t relly need to know whih element B is true forD only tht it existsF e n proeed y using B with every referene to x repled y new vrile xH @just nottion to distinguish it from xAF
Disjunction elimination

AB

itself nnot e splitD s we don9t know whih prt of the disjuntion is trueF roweverD if we lso know XAD we n onlude B must e trueF enlogouslyD with XB we n onlude AF enother good wy to del with disjuntion is proof by casesD whih we disussed oveF

Implication elimination

uppose we know A A B F sf we re le to show A is trueD then we ould immeditely onlude B F his is perhps the most si resoning strutureD nd hs fny ltin nmeX modus ponens @mening mode tht rms4AF his form is the sis to dedutive rgument @you n imgine herlok rolmes using modus ponens to revel the riminlAF yn the other hndD if we knew XB D we ould still get something from A A B X we9d e le to onlude XAF his form of resoning is using the ontrpositive nd is known s modus tollens @vtin for mode tht denies4AF e n lso ppel to the mnipultion rules to rewrite A A B s disjuntionD XA B D nd expnd this formul s desiredF
Bi-implication elimination

o tke prt sentene like A D B D we simply exploit its equivlene to @A A B A @B A AA nd expnd it ppropritelyF sf we lso know AD we n skip some work nd diretly onlude tht B must e true @using the implition A A B hidden in the iEimplitionAF vikewiseD if we lso knew XAD we ould onlude XB F ih of these properties re esily proven using preeding rulesF
Universal elimination (or instantiation)

uppose you know tht Vx P D; B @xAF row n we use this ft to help prove other thingsc his sentene sys B @xA is true for ll memers of domin DF o we ould use this s mening huge onjuntion over ll the elements of D a fd1 ; d2 ; d3 ; : : : gX B @d1 A B @d2 A B @d3 A : : : RS

Course notes for csc 165 h

prom this expnsion @even if we n9t write it14 A it9s ler tht if a P DD we n onlude tht B @aA is trueF his is sometimes lled universl instntitionD or universl speiliztionD sine we9re llowed to onlude speilized sttement from our generl sttementF sntuitivelyD wht holds for everything must hold for ny spei thingF ypillyD a will hve een mentioned lredyD nd you9ll wnt to express tht a hs some spei property @in this seD B @aAAF
3.15 Summary of inference rules

here re severl si nd derived rules we9re llowed to use in our proofsF wost of them re summrized elowF por eh ruleD if you know @hve lredy shownA everything tht is ove the lineD you re llowed to onlude nything tht9s elow the lineF
Introduction rules
[

XI] negtion introdution essume A FF F ontrdition XA [I] onjuntion introdution


A B AB

AI] implition introdution @diretA essume A FF F


B AAB

[ I]

@indiretA essume XB FF F XA AAB

I] disjuntion introdution
A AB BA A XA

DI] equivleneGiEimplition introdution AAB BAA ADB AE] implition elimintion @wodus @wodus onensA ollensA AAB AAB A XB B XA

V universl introdution essume a P D FF F P @aA Vx P D; P @xA [WI] existentil introdution P @aA aPD Wx P D; P @xA

Elimination rules

XE] negtion elimintion A XXA XA A ontrdition [E] onjuntion elimintion AB


[ [

[ E]

[ E]

E] disjuntion elimintion AB AB XA XB
B A

A B

DE] equivleneGiEimplition elimintion ADB AAB BAA

universl elimintion Vx P D; P @xA aPD P @aA existentil elimintion Wx P D; P @xA vet a P D suh tht P @aA FF F

st my surprise you to lern tht y this pointD we9ve overed ll of the si proof tehniques you will need during your undergrdute reer @nd eyondAF here is one si proof tehnique tht we hve yet to RT

Chapter 3. Proofs

over @mthemtil indutionA ut it is more properly the min sujet of the ourse csc 236 hF @hough we will disuss it little it in the next hpterFA qiven thisD you my feel tht the proofs we9ve worked on so fr hve een nowhere ner s omplited s wht you might nd in your lulus textookD for exmpleF fut if you tke the time to exmine the struture of ny suh proofD you will most likely nd tht ll of the tehniques it uses were overed in this hpterF he omplexity of these proofs stemD not from using more omplex tehniquesD ut from their sle nd their reline on numerous other results nd omplex denitionsF his is no dierent from the ontrst etween smll progrms nd lrge onesX oth re written using the sme progrmming lngugeD whih provides just smll set of uilding loks4 | onditionlsD loopsD funtionsD etcF he omplexity of lrger progrms stems minly from their size ndGor their reline on numerous externl lirriesF fering this in mindD you now hve ll of the tools required to understnd nd ppreite some of the deepest nd most eutiful results in the theory of omputtion @see ghpter SAF fut rstD in the next hpterD we9ll pply those tools to something more onreteX the nlysis of lgorithmsF
Chapter 3 Notes

end of week 6

rere9s n exmple of onjeture whose proof hs evded the est minds for lmost US yers | mye you9ll prove itc hene f @nAD for n P ND s followsX @ n=P; n even; f @nA a Qn C I; n odd: hen dene f +1 @nA s f @f @nAAD for ll k P ND with speil se f 0 @nA a nF @o f 1 @nA a f @nAD f 2 @nA a f @f @nAAD etcFA Conjecture: Vn P N; n > I A Wk P N; f @nA a IF isy to stteD ut @so frA hrd to prove or disproveF 2 por exmpleD for ny two points on the plneD there is extly one line tht psses through oth points4 is n xiom in iulidin geometryF 3 he ontrpositiveF 4 Vx P R; I=@x C PA < Q A x > HF plseD for exmple let x a RD then I=@R C PA a I=P < Q ut R > HF T sndeedD every x < P is ounterEexmpleF 5 essume e P RF 5 typil element of R essume e > HF 5 nteedent vet d a : : : 5 something helpfulD proly depending on e hen d P RF 5 verify d is in the domin hen d > HF 5 show d is positive essume x P RF 5 typil element of R essume H < jx aj < d F 5 nteedent FF F hen jf @xA lj < eF 5 inner onsequent hen H < jx aj < d A @jf @xA lj < eAF 5 introdue implition hen Vx P R; H < jx aj < d A @jf @xA lj < eAF 5 introdue universl hen Wd P R; d > H @Vx P R; H < jx aj < d A @jf @xA lj < eAAF 5 introdue existentil henD e > H A @Wd P R; d > H @Vx P R; H < jx aj < d A @jf @xA lj < eAAAF hen Ve P R; e > H A @Wd P R; d > H @Vx P R; H < jx aj < d A @jf @xA lj < eAAAF
1
k k k e e e e e e e e

RU

Course notes for csc 165 h

sn other wordsD formul tht depends only on iF sn this seD we see tht a a i2 F 7 e need to prove oth piees of onjuntionF 8 ry j a i C PF 9 Va P N; Vb P N; b > H A a C b > aF 10 essume x P RF FF F hen x < x C IF hen Vx P R; x < x C IF 11 essume x P ZF 5 x is typil integer iither x is even or x is oddF gse IX essume x is evenF hen x@x C IA is evenF 5 if x is multiple of PD so is x@x C IA gse PX essume x is oddF hen x C I is evenF 5 if x leves reminder ID x C I leves reminder H hen x@x C IA is evenF 5 if x C I is multiple of PD so is x@x C IA hen x@x C IA is evenF 5 true in ll @othA possile ses hen Vx P Z; x@x C IA is evenF 5 introdue universl 12 snsted of onluding R in one se nd S in the otherD we re tully onluding R S in oth sesD nd then we ring R S outside the ses euse we onluded it in eh seD nd one of the ses must holdF @ememer tht one we onlude tht R is trueD we n immeditely onlude tht R S is trueFA o this is extly the sme struture we9ve seen eforeF 13 e know tht A is true nd tht B is trueF 14 ell our sentenes re nite in lengthD so if our domin D is innite @like the nturl numers or rel numersAD we n9t tully write this expnsion downF ht9s the reson why we need universl quntier in our logi systemF
6
i

RV

Chapter 4

Algorithm Analysis and Asymptotic Notation


4.1 Correctness, running time of programs

o fr we hve een proving sttements out dtsesD mthemtis nd rithmetiD or sequenes of numersF hough these types of sttements re ommon in omputer sieneD you9ll proly enounter lgorithms most of the timeF yften we wnt to reson out lgorithms nd even prove things out themF ouldn9t it e nie to e le to prove tht your progrm is orretc ispeilly if you9re progrmming hert monitor or xee sperftc sn this hpter we9ll introdue numer of tools for deling with omputer lgorithmsD formlizing their expressionD nd tehniques for nlyzing properties of lgorithmsD so tht we n prove orretness or prove ounds on the resoures tht re requiredF
4.2 Binary (base 2) notation

vet9s rst think out numersF sn our everydy lifeD we write numers in deiml @se IHA nottion @lthough s herd of one kid who lerned to use the ngers of her left hnd to ount from H to QI in se PAF sn deimlD the sequene of digits PHQWS represents @prsing from the rightAX S C W@IHA C Q@IHHA C H@IHHHA C P@IHHHHA a S@IH0 A C W@IH1 A C Q@IH2 A C H@IH3 A C P@IH4 A ih position represents power of IHD nd IH is lled the baseF ih position hs digit from H; W representing how mny of tht power to ddF hy do we use IHc erhps due to hving IH ngers @howeverD humns t vrious times hve used se THD se PHD nd mixed se PHDIV @wynsAAF sn the lst se there were @IHSA20 18 dys in the yerF eny integer with solute vlue greter thn I will work @so experiment with se PAF gonsider using P s the se for our nottionF ht digits should we usec1 e don9t need digits P or higherD sine they re expressed y hoosing dierent position for our digits @just s in se IHD where there is no single digit for numers IH nd greterAF rere re some exmples of inry numersX @IHHIIA2 represents I@P0 A C I@P1 A C H@P2 A C H@P3 A C I@P4 A a @IWA10
;

RW

Course notes for csc 165 h

e n extend the ideD nd imitte the deiml point @with inry point4cA from se IHX @IHII:IHIA2 a IW S V 2 row did we do thtc rere re some questionsX  row do you multiply two se IH numersc3 ork out QU RQF  row do you multiply two inry numersc4  ht does right shifting4 @eliminting the rightEmost digitA do in se IHc5  ht does right shifting4 do in inryc6  ht does the rightmost digit tell us in se IHc sn inryc gonvert some numers from deiml to inry nottionF ry SUF e9d like to represent SU y dding either H or I of eh power of P tht is no greter thn SUF o SU a QP C IT C V C I a @IIIHHIA2 F e n lso ll in the inry digitsD systemtillyD from the ottom upD using the 7 opertor from ython @the reminder fter division opertorD t lest for positive rgumentsAX SU 7 P a I so @cccccIA2 @SU IA=P a PV 7 P a H so @ccccHIA2 PV=P a IR 7 P a H so @cccHHIA2 IR=P a U 7 P a I so @ccIHHIA2 @U IA=P a Q 7 P a I so @cIIHHIA2 @Q IA=P a I 7 P a I so @IIIHHIA2 eddition in inry is the sme s @only dierent fromF F F A ddition in deimlF tust rememer tht @IA2 C @IA2 a @IHA2 F sf we dd two inry numersD this tells us when to rry4 IX IHII C IHII IHIIH P row mny SEdigit inry numers re there @inluding those with leding HsAc hese numers run from @HHHHHA2 through @IIIIIA2 D or H through QI in deiml | QP numersF enother wy to ount them is to onsider tht there re two hoies for eh digitD hene P5 strings of digitsF sf we dd one more digit we get twie s mny numersF ivery digit doules the rnge of numersD so there re two IEdigit inry numers @H nd IAD four PEdigit inry numers @H through QAD V QEdigit inry numers @H through UAD nd so onF everse the questionX how mny digits re required to represent given numerF sn other wordsD wht is the smllest integer power of P needed to exeed given numerc log2 x is the power of P tht gives Plog2 a xF ou n think of it s how mny times you must multiply I y P to get xD or roughly the numer of digits in the inry representtion of xF @he preise numer of digits needed is dlog2 @x C IAe | whih hppens to e equl to @log2 xA C I for ll positive vlues of xAF
log
x

4.3

Loop invariant for base 2 multiplication

sntegers re nturlly represented on omputer in inryD sine gte n e in either n on or o @I or HA positionF st is very esy to multiply or divide y PD sine ll we need to do is perform left or right shift @n esy hrdwre opertionAF imilrlyD it is lso very esy to determine whether n integer is even or oddF utting these togetherD we n write multiplition lgorithm tht uses these fst opertionsX SH

Chapter 4. Algorithm Analysis and Asymptotic Notation

def mult(m,n): """ Multiply integers m and n. """ # Precondition: m >= 0 x = m y = n z = 0 # loop invariant: z while x != 0: if x % 2 == 1: x = x >> 1 # x y = y << 1 # y = mn - xy z = z + y # x is odd = x / 2 (right shift) = y * 2 (left shift)

# post condition: z = mn return z

efter reding this lgorithmD there is no reson you should elieve it tully multiplies two integersX we9ll need to prove it to youF vet9s onsider the preondition rstF o long s m is positive nturl numerD nd n is n integerD the progrm lims to workF he postondition sttes tht z D the vlue tht is returnedD is equl to the produt of m nd n @tht would e nieD ut we9re not onvinedAF vet9s look t the stted loop invrintF e loop invariant is reltionship etween the vriles tht is lwys true t the strt nd t the end of loop itertion @we9ll need to prove thisAF st9s suient to verify tht the invrint is true t the strt of the rst itertionD nd verify tht if the invrint is true t the strt of ny itertionD it must e true t the end of the itertionF fefore we strt the loopD we set x a mD y a n nd z a HD so it is ler tht z a mn xy a mn mn a HF xow we need to show tht if z a mn xy efore exeuting the ody of the loopD nd x Ta HD then fter exeuting the loop odyD z a mn xy is still true @n you write this sttement formllycAF rere9s sketh of proofX essume x ; y ; z ; x +1 ; y +1 ; z +1 ; m; n P ZD where x represents the vlue of vrile x t the eginning of the ith itertion of the loopD nd similrly for the other vriles nd susriptsF @xote tht there is no need to susript m; nD sine they ren9t hnged y the loopFA essume z a mn x y F gse IX essume x oddF hen z +1 a z C y D x +1 a @x IA=PD nd y +1 a Py F o mn x +1 y +1 a mn @x IA=P Py @sine x is oddA a mn x y C y az Cy a z +1 : gse PX essume x evenF hen z +1 a z D x +1 a x =PD nd y +1 a Py F o mn x +1 y +1 a mn x =P Py a mn x y az a z +1 : ine x is either even or oddD in ll ses mn x +1 y +1 a z +1 hus mn x y a z A mn x +1 y +1 a z +1 F ine x ; x +1 ; y ; y +1 ; z ; z +1 ; m; n re ritrry elementsD Vx ; x +1 ; y ; y +1 ; z ; z +1 ; m; n P Z; mn x y a z A mn x +1 y +1 a z +1 F e should proly verify the postondition to fully onvine ourselves of the orretness of this lgorithmF e9ve shown the loop invrint holdsD so let9s see wht we n onlude when the loop termintes @i.e.D
i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i

SI

Course notes for csc 165 h

when x a HAF fy the loop invrintD z a mn xy a mn H a mnD so we know we must get the right nswer @ssuming the loop eventully termintesAF e should now e firly onvined tht this lgorithm is in ft orretF yne might now wonderD how mny itertions of the loop re ompleted efore the nswer is returnedc elsoD why is it neessry for m > Hc ht hppens if it isn9tc
4.4 Running time of programs
P

por ny progrm P nd ny input xD let t @xA denote the numer of steps4 P tkes on input xF e need to speify wht we men y stepF4 e step4 typilly orresponds to mhine instrutions eing exeutedD or some indition of time or resoures expendedF gonsider the following @somewht ritrryA ounting for ommon progrm stepsX method call: I step C steps to evlute eh rgument C steps to exeute the methodF return statement: I step C steps to evlute return vlueF if statement: I step C steps to evlute onditionF assignment statement: I step C steps to evlute eh sideF arithmetic, comparison, boolean operators: I step C steps to evlute eh operndF array access: I step C steps to evlute indexF member access: P stepsF constant, variable evaluation: I stepF xotie tht none of these steps4 @exept for method llsA depend on the size of the input @sometimes denoted with the symol nAF he smllest nd lrgest steps ove dier from eh other y onstnt of out SD so we n mke the dditionl simplifying ssumption tht they ll hve the sme ost | IF
4.5 Linear search

vet9s use liner serh s n exmpleF


def LS(A,x): """ Return an index i such that x == L[i]. i = 0 # (line 1) while i < len(A): # (line 2) if A[i] == x: # (line 3) return i # (line 4) i = i + 1 # (line 5) return -1 # (line 6) Otherwise, return -1. """

vet9s tre funtion llD LS([2,4,6,8],4)X vine IX I step @i=0A vine PX I step @0 < 4A vine QX I step @A[0] == 4A vine SX I step @i = 1A SP

Chapter 4. Algorithm Analysis and Asymptotic Notation

vine PX I step @1 < 4A vine QX I step @A[1] == 4A vine RX I @return 1A o t @P; R; T; V; RA a UF xotie tht if the rst index where x is found is j D then t @A; xA will ount lines PD QD nd S one for eh index from H to j I @j indiesAD nd then ount lines PD QD R for index j D nd so t @A; xA will e I C Qj C QF sf x does not pper in AD then t @A; xA a I C Q len@AA C PD euse line I exeutes oneD lines PD QD nd S exeutes one for eh index from H to len@AA ID nd then lines P nd T exeuteF e wnt mesure tht depends on the size of the inputD not the prtiulr inputF here re three stndrd wysF vet P e progrmD nd let I e the set of ll inputs for P F henX Average-case complexity: the weighted verge over ll possile inputs of size nF sn generl A @nA a t @xA p@xA
LS LS LS LS P P

where p@xA is the proility tht input x is enounteredF essuming ll the inputs re eqully likelyD this simplies4 to t @xA of size A @ nA a numer of inputs of size n @hiult to omputeFA Best-case complexity: min@t @xAAD where x is n input of size nF sn other wordsD B @nA a minft @xA j x P I size@xA a ngF @wostly uselessFA Worst-case complexity: mx@t @xAAD where x is n input of size nF sn other wordsD W @nA a mxft @xA j x P I size@xA a ngF @eltively esy to omputeD nd gives performne gurnteeFA ht is ment y input size4c his depends on the lgorithmF por liner serhD the numer of elements in the rry is resonle prmeterF ehnilly @in csc 463 hD for exmpleAD the size is the numer of its required to represent the input in inryF sn prtie we use the numer of elements of input @length of rryD numer of nodes in treeD etcFA
P x n P P P P P P P

of size

end of week 7

4.6

Run time and constant factors

hen lulting the running time of progrmD we my know how mny si steps4 it tkes s funtion of input sizeD ut we my not know how long eh step tkes on prtiulr omputerF e would like to estimte the overll running time of n lgorithm while ignoring onstnt ftors @like how fst the g isAF oD for exmpleD if we hve Q mhinesD where opertions tke QsD Vs nd HFSsD the three funtions mesuring the mount of time requiredD t@nA a Qn2 D t@nA a Vn2 D nd t@nA a n2 =P re onsidered the smeD ignoring @to within4A onstnt ftors @the time required lwys grows ording to qudrti funtion in terms of the size of the input nAF o view this nother wyD think k to the liner serh exmple in the previous setionF he worstEse running time for tht lgorithm ws given y the funtion W @nA a Qn C Q SQ

Course notes for csc 165 h

fut wht extly does the onstnt Q4 in front of the n4 representc yr the dditive CQ4c xeither vlue orresponds to ny intrinsi property of the lgorithm itselfY rtherD the vlues re onsequenes of some ritrry hoies on our prtD nmelyD how mny steps4 to ount for ertin ython sttementsF omeone ounting dierently @e.g.D ounting more thn I step for sttements tht ess list elements y indexA would rrive t dierent expression for the worstEse running timeF ould their nswer e more4 or less4 orret thn oursc xeitherX oth nswers re just s impreise s one nother3 his is why we wnt to ome up with tool tht llows us to work with funtions while ignoring onstnt multipliersF he nie thing is tht this mens tht lower order terms n e ignored s well3 o f @nA a Qn2 nd g @nA a Qn2 C P re onsidered the smeD4 s re h@nA a Qn2 C Pn nd j @nA a Sn2 F xotie tht Vn P N; n > I A f @nA 6 g@nA 6 h@nA 6 j @nA ut there9s lwys onstnt ftor tht n reverse ny of these inequlitiesF elly wht we wnt to mesure is the growth rte of funtions @nd in omputer sieneD the growth rte of funtions tht ound the running time of lgorithmsAF ou might e fmilir with inry serh nd liner serh @two lgorithms for serhing for vlue in sorted rryAF uppose one omputer runs inry serh nd one omputer runs liner serhF hih omputer will give n nswer rstD ssuming the two omputers run t roughly the sme g speedc ht if one omputer is muh fster @in terms of g speedA thn the otherD does it et your nswerc ht if the rry is rellyD relly igc
How large is \sufficiently large?"

ss inry serh etter lgorithm thn liner serhc7 st depends on the size of the inputF por exmpleD suppose you estlished tht liner serh hs omplexity L@nA a Qn nd inry serh hs omplexity B @nA a W log2 nF por the rst few nD L@nA is smller thn B @nAF roweverD ertinly for n > IHD B @nA is smllerD inditing less work4 for inry serhF hen we sy lrge enough4 nD we men we re disussing the asymptotic ehviour of the omplexity funtion @i.e.D the ehviour s n grows towrd innityAD nd we re prepred to ignore the ehviour ner the originF
4.7 Asymptotic notation: Making Big-O precise

e dene R>0 s the set of nonnegtive rel numersD nd dene R+ s the set of positive rel numersF rere is preise denition of he set of funtions tht re eventully no more thn f D to within onstnt ftor4X >0 @i.e.D ny funtion mpping nturls to nonnegtive relsAD let Definition: por ny funtion f X N 3 R y@f A a g X N 3 R>0 j Wc P R+ ; WB P N; Vn P N; n > B A g@nA 6 cf @nA : ying g P y@f A sys tht g grows no fster thn f 4 @or equivlentlyD f is n upper ound for g4AD so long s we modify our understnding of growing no fster4 nd eing n upper ound4 with the prtie of ignoring onstnt ftorsF xow we n prove some theoremsF uppose g@nA a Qn2 C P nd f @nA a n2 F hen g P y@f AF o e more preiseD we need to prove the sttement Wc P R+ ; WB P N; Vn P N; n > B A Qn2 C P 6 cn2 F st9s enough to nd some c nd B tht work4 in order to prove the theoremF pinding c mens nding ftor tht will sle n2 up to the size of Qn2 C PF etting c a Q lmost worksD ut there9s tht nnoying dditionl term PF gertinly Qn2 CP < Rn2 so long s n > PD sine n > PA n2 > PF o pik c a R nd B a P @other vlues lso workD ut we like the ones we thought of rstAF xow onot proof of Wc P R+ ; WB P N; Vn P N; n > B A Qn2 C P 6 cn2 : SR

Chapter 4. Algorithm Analysis and Asymptotic Notation

vet cH a R nd B H a PF hen cH P R+ nd B H P NF essume n P N nd n > B H F 5 diret proof for n ritrry nturl numer hen n2 > B H 2 a RF 5 squring is monotoni on nturl numers hen n2 > PF hen Qn2 C n2 > Qn2 C PF 5 dding Qn2 to oth sides of the inequlity hen Qn2 C P 6 Rn2 a cH n2 5 reEwrite hen Vn P N; n > B H A Qn2 C P 6 cH n2 5 introdue V nd A hen Wc P R+ ; WB P N; Vn P N; n > B A Qn2 C P 6 cn2 F 5 introdue W @twieA oD y denitionD g P y@f AF
A more complex example

vet9s prove tht Pn3 Sn4 C Un6 is in y@n2 Rn5 C Tn8 AF e egin withX vet cH a F hen cH P R+ F vet B H a F hen B H P NF essume n P N nd n > B H F 5 ritrry nturl numer nd nteedent hen Pn3 Sn4 C Un6 6 : : : 6 cH @n2 Rn5 C Tn8 AF hen Vn P N; n > BiH A Pn3 Sn4 C Un6 6 cH @n2 Rn5 C Tn8 AF 5 introdue A nd V reneD Wc P R+ ; WB P N; Vn P N; n > B A Pn3 Sn4 C Un6 6 c@n2 Rn5 C Tn8 AF 5 introdue W o ll in the : : : we try to form hin of inequlitiesD working from oth endsD simplifying the expressionsX Pn3 Sn4 C Un6 6 Pn3 C Un6 @drop Sn4 euse it doesn9t help us in n importnt wyA 6 Pn6 C Un6 @inrese n3 to n6 euse we hve to hndle n6 nywyA a Wn6 6 Wn8 @simpler to ompreA a P@W=PAn8 @get s lose to form of the simplied end resultX now hoose cH a W=PA a Pcn8 a cH @Rn8 C Tn8 A @reding ottom upX derese Rn5 to Rn8 euse we hve to hndle n8 nywyA 6 cH @Rn5 C Tn8 A @reding ottom upX drop n2 euse it doesn9t help us in n importnt wyA H @n2 Rn5 C Tn8 A 6c e never needed to restrit n in ny wy eyond n P N @whih inludes n > HAD so we n ll in cH a W=PD B H a HD nd omplete the proofF
To prove or not to prove. . .

ht would it men to prove n4 P y@Qn2 Ac wore preiselyD we hve to prove the negtion of the sttement = Wc P R+ ; WB P N; Vn P N; n > B A n4 6 cQn2 F fefore you onsider the proof struture tht followsD you might nd it useful to work out tht negtionF essume c P R+ nd B P NF 5 ritrry positive rel numer nd nturl numer vet n0 a F FF F o n0 P NF SS

Course notes for csc 165 h

FF F o n0 > B F FF F o n4 > cQn2 F 0 0 hen Vc P R+ ; VB P N; Wn P N; n > B n4 > cQn2 F F rere9s our hin of inequlities @the third FFAX end n4 > n3 @don9t need full power of n4 A 0 0 0 a n0 n2 @mke form s lose s possileA 0 > c Qn2 @if we mke n0 > Qc nd n0 > HA 0 xow pik n0 a mx@B; dQc C IeAF F he rst FF isX ine c > HD Qc C I > HD so dQc C Ie P NF ine B P ND mx@B; dQc C IeA P NF F he seond FF isX

mx@B; dQc C IeA > B F e lso note just efore the hin of inequlitiesX n0 a mx@B; dQc C IeA > dQc C Ie > Qc C I > QcF ome points to note reX  hon9t solve4 for n until you9ve mde the form of the two sides s lose s possileF  ou9re not extly solving for nX you re nding ondition of the form n > tht mkes the desired inequlity trueF ou might nd yourself using the mx4 funtion lotF
One last example

vet g@nA a P nd f @nA a nF s wnt to show tht g TP y@f AF essume c P R+ D ssume B P NF 5 ritrry vlues vet n0 a F F F hen n0 P NF F F F hen n0 > B F F F F hen P 0 > cn0 F hen Vc P R; VB P N; Wn P N; n > B g@nA > cf @nAF 5 introdue V oD s n onlude tht g TP y@f AF he triky prt in this proof is to nd the vlue of n0 F nfortuntelyD there is no elementry wy to do this | one of the esiest wys involves n old friendF F F F
n n

4.8

Calculus!

sntuitivelyD igEyh nottion expresses something out how two funtions ompre s n tends towrd innityF fut we know of nother mthemtil notion tht ptures similr @though not identilA ideX the onept of limitF ST

Chapter 4. Algorithm Analysis and Asymptotic Notation

vet9s work out how these two onepts re reltedF hen we study whether or not f P y@gA @for ritrry funtions f; g X N 3 R>0 AD we re working with the inequlity f @nA 6 cg@nA4F es long s g@nA Ta HD this is equivlent to studying the inequlity f @nA=g@nA 6 c4F sntuitivelyD we would like to know how the funtion f @nA=g @nA ehves s n tends towrd innityF his is extly wht limits express3 wore preiselyD rell the following denitionD for ll L P R>0 X @nA @ nA lim f @nA a L @A V" P R+ ; Wn0 P N; Vn P N; n > n0 A L " < f @nA < L C " 3I g g nd the following speil seX @nA @ nA lim f @nA a I @A V" P R+ ; Wn0 P N; Vn P N; n > n0 A f @nA > " 3I g g xow suppose tht lim 3I f @nA=g@nA a LF sntuitivelyD this tells us tht f @nA=g@nA % LD for n lrge enoughF4 sn tht seD f @nA % Lg@nA for n lrge enoughD so we should e le to prove tht f P y@gAX essume lim 3I f @nA=g@nA a LF hen Wn0 P N; Vn P N; n > n0 A L I < f @nA=g@nA < L C IF 5 denition of limit for " a I hen Wn0 P N; Vn P N; n > n0 A f @nA 6 @L C IAg@nAF hen f P y@gAF 5 denition of yD with B a n0 nd c a L C I reneD lim 3I f @nA=g@nA a L A f P y@gAF xote tht limits re stronger4 thn igEyhX they express more restritive property of funtions f nd gF 2 por exmpleD x2 sin@xA P y@x2 A even though lim 3I sin( ) is undenedF 2
n n n n n x x x x

Wrapping it up

qetting k to our erlier exmpleD we n now omplete the proofF ell tht g@nA a P nd f @nA a nF e rely on the ft tht lim 3I P =n a IF8 essume c P R+ D ssume B P NF 5 ritrry vlues hen Wn0 P N; Vn P N; n > n0 A P =n > cF 5 denition of lim 3I P =n a I with " a c vet n0 e suh tht Vn P N; n > n0 A P =n > cD nd nH a mx@B; n0 AF hen nH P NF hen nH > B F 5 y denition of mx hen P > cnH euse P =nH > cF 5 y the rst line oveD sine nH > n0 hen nH > B g@nH A > cf @nH AF 5 introdue hen Wn P N; n > B g@nA > cf @nAF 5 introdue W hen Vc P R; VB P N; Wn P N; n > B g@nA > cf @nAF 5 introdue V
n n n n n n n n
0

end of week 8

4.9

Other bounds

fy nlogy with y@f AD onsider two other denitionsX >0 D let Definition: por ny funtion f X N 3 R @f A a g X N 3 R>0 j Wc P R+ ; WB P N; Vn P N; n > B A g@nA > cf @nA : o sy g P @f A4 expresses the onept tht g grows t lest s fst s f 4 @f is lower ound on gAF >0 D let Definition: por ny funtion f X N 3 R @f A a g X N 3 R>0 j Wc1 P R+ ; Wc2 P R+ ; WB P N; Vn P N; n > B A c1 f @nA 6 g@nA 6 c2 f @nA : o sy g P @f A4 expresses the onept tht g grows t the sme rte s f 4 @f is tight ound for gD or f is oth n upper ound nd lower ound on g AF SU

Course notes for csc 165 h

Some theorems

rere re some generl results tht we now hve the tools to proveF  f P y@f AF  @f P y@gA g P y@hAA A f P y@hAF  g P @f A D f P y@gAF  g P @f A D g P y@f A g P @f AF est your intuition out figEy y doing the srth work4 to nswer the following questionsX  ere there funtions f; g suh tht f P y@gA nd g P y@f A ut f Ta gc9  ere there funtions f; g suh tht f TP y@gAD nd g TP y@f Ac10 o show tht @f P y@gA g P y@hAA A f P y@hAD we need to nd onstnt c P R+ nd onstnt B P ND tht stisfyX Vn P N; n > B A f @nA 6 ch@nA: ine we hve onstnts tht sle h to g nd then g to f D it seems ler tht we need their produt to sle g to f F end if we tke the mximum of the two strting pointsD we n9t go wrongF wking this preiseX >0 D we hve @f P y@g A g P y@hAA A f P y@hAF Theorem RFI: por ny funtions f; g; h X N 3 R essume f P y@gA g P y@hAF o f P y@gAF o Wc P R+ ; WB P N; Vn P N; n > B A f @nA 6 cg@nAF 5 y def9n of f P y@gA vet c P R+ ; B P N e suh tht Vn P N; n > B A f @nA 6 c g@nAF o g P y@hAF o Wc P R+ ; WB P N; Vn P N; n > B A g@nA 6 ch@nAF 5 y def9n of g P y@hA vet c P R+ ; B P N e suh tht Vn P N; n > B A g@nA 6 c h@nAF vet cH a c c F vet B H a mx@B ; B AF henD cH P R+ @euse c ; c P R+ A nd B H P N @euse B ; B P NAF essume n P N nd n > B H F hen n > B @y denition of mxAD so g@nA 6 c h@nAF hen n > B @y denition of mxAD so f @nA 6 c g@nA 6 c c h@nAF o f @nA 6 cH h@nAF reneD Vn P N; n > B H A f @nA 6 cH h@nAF hereforeD Wc P R+ ; WB P N; Vn P N; n > B A f @nA 6 ch@nAF o f P y@gAD y denitionF o @f P y@gA g P y@hAA A f P y@hAF o show tht g P @f A D f P y@gAD it is enough to note tht the onstntD cD for one diretion is positiveD so its reiprol will work for the other diretionF11 >0 D we hve g P @f A D f P y@g AF Theorem RFP: por ny funtions f; g X N 3 R
g g g g h h h h g h g h g h g h h g h g g h

Proof:

Proof:

@y denitionA o show g P @f A D g P y@f A g P @f AD it9s relly just mtter of unwrpping the denitionsF SV

g P @f A @A Wc P R+ ; WB P N; Vn P N; n > B A g@nA > cf @nA @A WcH P R+ ; WB H P N; Vn P N; n > B H A f @nA 6 cH g@nA @A f P y@gA

@letting cH a I=c nd B H a B A

@y denitionA

Chapter 4. Algorithm Analysis and Asymptotic Notation

Theorem Proof:

RFQ: por ny funtions f; g X N 3 R>0 D we hve g P @f A D g P y@f A g P @f AF

g P @f A D @y denitionA Wc1 P R+ ; Wc2 P R+ ; WB P N; Vn P N; n > B A c1 f @nA 6 g@nA 6 c2 f @nAF D@omined inequlityD nd B a mx@B1 ; B2 AA Wc P R+ ; WB1 P N; Vn P N; n > B1 A g@nA > c1 f @nA 1 Wc2 P R+ ; WB2 P N; Vn P N; n > B2 A g@nA 6 c2 f @nA D @y denitionA g P @f A g P y@f A

rere9s n exmple of orollry tht reyles some of the theorems we9ve lredy proven @so we don9t hve to do the gruy workAF o show g P @f AD f P @gAD s reEuse theorems proved ove nd the ommuttivity of X
Corollary:

por ny funtions f; g X N 3 R>0 D we hve g P @f A D f P @gAF


g P @f A @A g P y@f A g P @f A @A g P y@f A f P y@gA @A f P y@gA g P y@f A @A f P y@gA f P @gA @A f P @gA

Proof:

@y RFQA @y RFPA @y ommuttivity of A @y RFPA @y RFQA

4.10

Asymptotic notation and algorithm analysis

xote tht symptoti nottion @the figEyD figED nd figE denitionsA ound the symptoti growth rtes of functionsD s n pprohes innityF yften in omputer siene we use this symptoti nottion to ound funtions tht express the running times of lgorithmsD perhps in est se or in worst seF esymptoti nottion does not express or ound the worst se or est se running time diretlyD only the functions expressing these vluesF his distintion is sutleD ut ruil to understnding oth running times nd symptoti nottionF o when we sy tht U is n upper ound on the worstEse running time of some progrm P D denoted T @nAD we menX
P

P y@U A @A Wc P R+ ; WB P N; Vn P N; n > B A T @nA 6 cU @nA @A Wc P R+ ; WB P N; Vn P N; n > B A mxft @xA j x P I size@xA a ng 6 cU @nA @A Wc P R+ ; WB P N; Vn P N; n > B A Vx P I; size@xA a n A t @xA 6 cU @nA @A Wc P R+ ; WB P N; Vx P I; size@xA > B A t @xA 6 cU @size@xAA
P P P P P P

sn other wordsD to show tht T P y@U @nAAD you need to nd onstnts c nd B nd show tht for n ritrry input x of size nD P tkes t most c U @nA stepsF sn the other diretionD when we sy tht L is lower ound on the worstEse running time of lgorithm P D SW

Course notes for csc 165 h

we menX
T

P @LA @A Wc P R+ ; WB P N; Vn P N; n > B A mxft @xA j x P I size@xA a ng > cL@nA @A Wc P R+ ; WB P N; Vn P N; n > B A Wx P I; size@xA a n t @xA > cL@nA
P P P p

sn other wordsD to prove tht T P @LAD we hve to nd onstnts cD B nd for ritrry nD nd n input x of size nD for whih we n show tht P tkes t lest cL@nA steps on input xF
4.11 Insertion sort example

rere is n intuitive12 sorting lgorithmX


def IS(A): """ Sort the elements of A in non-decreasing order. """ i = 1 # (line 1) while i < len(A): # (line 2) t = A[i] # (line 3) j = i # (line 4) while j > 0 and A[j-1] > t: # (line 5) A[j] = A[j-1] # (line 6) j = j-1 # (line 7) A[j] = t # (line 8) i = i+1 # (line 9)

vet9s nd n upper ound for T @nAD the mximum numer of steps to snsertion ort n rry of size nF e9ll use the proof formt to prove nd nd the ound simultneously | during the ourse of the proof we n ll in the neessry vlues for c nd B F e show tht T @nA P y@n2 A @where n a len@AAAX vet cH a F vet B H a F hen cH P R+ nd B H P NF essume n P ND A is n rry of length nD nd n > B H F hen lines S{U exeute t most n I timesD whih we n overestimte t Qn stepsD plus I step for the lst loop testF hen lines P{W tke no more thn n@S C QnA C I a Sn C Qn2 C I stepsF o Qn2 C Sn C I 6 cH n2 @ll in the vlues of cH nd B H tht mkes this so | setting cH a W; B H a I should doAF ine n is the length of n ritrry rry AD Vn P N; n > B H A T @nA 6 cH n2 @so long s B H > IAF ine cH is positive rel numer nd B H is nturl numerD Wc P R+ ; WB P N; Vn P N; n > B A T @nA 6 cn2 F o T P y@n2 A @y denition of y@n2 AAF imilrlyD we prove lower oundF peillyD T P @n2 AX vet cH a F vet B H a F hen cH P R+ nd B H P NF essume n P N nd n > B H F vet AH a n I; : : : ; I; H @notie tht this mens n > IAF hen t ny point during the outside loopD AH H::@i IA ontins the sme elements s efore ut sorted @i.e.D no element from AH @i C IA::@n IA hs een exmined yetAF
IS IS IS IS IS IS

TH

Chapter 4. Algorithm Analysis and Asymptotic Notation

hen the inner while loop mkes i itertionsD t ost of Q steps per itertionD plus I for the nl loop hekD sine the vlue AH i is less thn ll the vlues AH H::@i IAD y onstrution of the rryF hen the inner loop mkes stritly greter thn Pi C ID or greter thn or equl to Pi C PFD stepsF hen @sine the outer loop vries from i a I to i a n I nd we hve n I itertions of lines Q nd RD plus one itertion of line IAD we hve tht t @nA > ICQCSC C@Pn IAC@Pn CIA a n2 @the sum of the rst n odd numersAD so long s n is t lest RF o there is some rry A of size n suh tht t @AA > cH n2 F his mens T @nA > cH n2 @setting B H a R; cH a I will doAF ine n ws n ritrry nturl numerD Vn P N; n > B H A T @nA > cH n2 F ine cH P R+ nd B H is nturl numerD Wc P R+ ; WB P N; Vn P N; n > B A T @nA > cn2 F o T P @n2 A @y denition of @n2 AAF prom these proofsD we onlude tht T P @n2 AF
IS IS IS IS IS IS IS

end of week 9

4.12

Of algorithms and stockbrokers

uppose you hve list of integersD like Q; S; U; I; P; H; Q; P; ID nd you wish to nd the mximum sum of ny slie of the list @in the ython sense of the term slie4AF por exmpleD perhps the integers represent hnges in the prie of shres of your fvourite stokD nd solving this prolem would tell you the mximum prot tht might e hieved y purhsing nd selling the stok t the right timesF row n we solve this prolemc yne ovious solution is to exmine every possile slie of the originl list nd to ompute the sum of eh oneD keeping trk of the mximum sum we enounterF sf you implement this lgorithm in ython | you should try it for yourselfD it9s n exellent wy to prtie writing loops3 | you might end up with something like the followingF @xote tht the ode elow is not prtiulrly idiomti | for instneD we ould hve used forEloops insted of whileEloopsD nd we ould hve lled the sum uiltEin funtion rther thn write loopF fut these re cosmetic dierenesX the ode performs the sme workD nd the urrent version hs the dvntge tht ll of tht work is expliitD mking it esier to ount for when we nlyze the running timeAF
def max_sum(L): max = 0 # line 1 # To generate all non-empty slices [i:j] for list L, i must take on values # from 0 to len(L)-1, and j must take on values from i+1 to len(L). i = 0 # line 2 while i < len(L): # line 3 j = i + 1 # line 4 while j <= len(L): # line 5 # Compute the sum of L[i:j]. sum = 0 # line 6 k = i # line 7 while k < j: # line 8 sum = sum + L[k] # line 9 k = k + 1 # line 10 # Update max if appropriate. if sum > max: # line 11 max = sum # line 12 j = j + 1 # line 13 i = i + 1 # line 14 # At this point, weve examined every slice. return max # line 15

TI

Course notes for csc 165 h

he orretness of this ode should e firly ovious from the ommentsD exept perhps for one thingX wht if the input L is list tht ontins only negtive integersD like P; I; P; Qc houldn9t the mximum sum of slie e some negtive integerD like Ic o why do we initilize max to 0c13 xowD let9s nlyze the worstEse running time of max_sumF sntuitivelyD T @nA P y@n3 A @where n a len(L)A euse of the triplyEnested loopsD eh one of whih itertes no more thn n timesF ss T @nA P @n3 Ac his is perhps not so oviousD ut we will show tht it is indeed the seF o egin the nlysisD we must deide extly whih opertions to ount nd how muh to ount for eh oneF e must strike lneX if our ounting is too neEgrinedD we risk getting ogged down in rithmeti tht is irrelevntY if it is too orseD we risk ignoring signint frtions of the work rried out y the lgorithmF qiven how the lgorithm is writtenD resonle middle ground is to ount one step4 for eh line of ode tht is exeutedF
T @nA P y@n3 A

enlyzing the loops insideEoutD we nd thtX  he loop on lines V{IH itertes j i times @one for eh vlue of k a i; i CI; : : : ; j IAF end j i 6 n @sine j 6 n nd i > HAF end the loop exeutes Q steps t eh itertionF o the innermost loop exeutes no more thn Qn stepsF  he loop on lines S{IQ itertes n i times @one for eh vlue of j a i CI; i CP; : : : ; nAF end n i 6 n @sine i > HAF end the loop exeutes t most U C Qn steps t eh itertionX lines SD TD UD VD IID IPD IQ eh exeute t most oneD in ddition to the 6 Qn steps exeuted y the inner loop @line S lwys exeutes one more time thn the numer of itertions of the inner loopD when the loop ondition eomes flseY line IP my or my not exeuteD ut it ertinly exeutes no more thn one for eh itertionAF o the middle loop exeutes at most Un C Qn2 stepsF  he loop on lines Q{IR itertes n times @one for eh vlue of i a H; I; : : : ; n IAF end the loop exeutes t most R C Un C Qn2 steps t eh itertionX lines QD RD SD IR eh exeute t most oneD in ddition to the 6 Un C Qn2 steps exeuted y the middle loopF o the outer loop exeutes no more thn Rn C Un2 C Qn3 stepsF  vines ID PD QD nd IS eh exeute oneD so the lgorithm exeutes 6 R C Rn C Un2 C Qn3 steps in totlD for every input list LF xow we re redy to write the proof formllyF essume n P N nd n > I nd L is list with len(L) = nF es rgued oveD lines V{IH perform t most Qn stepsF hen lines S{IQ perform t most @U C QnA n 6 IHn2 steps @s rgued oveAF hen lines Q{IR perform t most @R C IHn2 A n 6 IRn3 steps @s rgued oveAF hen the entire lgorithm performs t most R C IRn3 6 IVn3 stepsF ine n nd L were ritrryD Vn P N; n > I A T @nA 6 IVn3 F rene Wc P R+ ; WB P N; Vn P N; n > B A T @nA 6 cn3 D i.e.D T @nA P y@n3 AF @xote tht this proof relly is not omplete without the rough work oveD whih we simply did not other to repetFA
T @nA P @n3 A

he lgorithm hs three nested loopsD so it9s tempting to think tht it is ovious4 tht the running time is @n3 AF fut onsider thisX there re mny pirs i; j for whih the loop for k performs few itertions @when j i is smllAD so we re overEounting when we sy tht the loop for k performs t most4 n itertionsF he question isX y how muh re we overEountingc TP

Chapter 4. Algorithm Analysis and Asymptotic Notation

o show tht T @nA P @n3 AD we hve to onvine ourselves tht the loop over k relly does iterte t lest some frtion of n timesD for t lest some frtion of n2 mny pirs i; j F here re mny wys to do thisY here is one tht is reltively simple @splitting up the rnge of vlues for i; j; k evenlyAF  he loop over i itertes t lest dn=Qe times | for eh of the vlues i a H; I; : : : ; dn=QeF @ellyD we know it performs more thn this mny itertionsD ut sine we9re working on proving lower ound it9s oky to underEestimte here | we9ll soon see why it mkes sense to underEestimte in this wyFA  por eh of these itertions of the outer loop iD the loop for j itertes t lest dn=Qe times | for eh of the vlues j a n dn=Qe C I; : : : ; nF @eginD we know tht the loop relly performs more work thn this ut we9re deliertely ignoring some of the work in order to gurntee ertin rnge of vlues for kD in the quest for our lower oundFA  por eh of these pirs i; j D the inner loop for k itertes over the vlues i; i C I; : : : ; j IF his will lwys e t lest dn=Qe mny itertionsD sine i 6 dn=Qe nd j > n dn=QeCIF end eh itertion of the innermost loop performs t lest I step @more thn thtD rellyD ut gin we9re underEestimtingAF pormllyD ssume n P N nd n > QF vet L a I; P; : : : ; nF hen L is list of length nF es rgued oveD the ll max_sum(L) performs t lest dn=Qe dn=Qe dn=Qe > n3 =PU mny stepsF ine this hppens speilly for list LD T @nA > n3 =PUF 5 y denition of worstEse4 ine n ws ritrryD Vn P N; n > Q A T @nA > n3 =PUF rene Wc P R+ ; WB P N; Vn P N; n > B A T @nA > cn3 D i.e.D T @nA P @n3 AF
Doing better

sf you exmine the lgorithmD you might notie one somewht ovious ineienyF sn se you nnot see itD or to onrm tht we9re thinking of the sme thingD try the following exeriseX tre through the exeution of the lgorithm on input L a Q; S; U; I; P; H; Q; P; ID when i a I @with mx a UAF ou should notie tht the lgorithm omputes sum a LID then sum a LI C LPD etcF fut eh timeD it strts overX for exmpleD to ompute LI C LP C C LSD the lgorithm ompletely disrds the previous vlue of sum @equl to LI C LP C LQ C LRA nd strts dding LI nd LP ndF F F F e ould sve omputing time if we kept the old vlue nd simply dded LS to it3 he generl ide is to dd vlues to running totl every time tht the vlue of j hngesD insted of hving seprte inner loopF ee if you n implement this hnge on your ownF ou might end up with something like the followingF
def faster_max_sum(L): max = 0 # line 1 # Generate all non-empty slices [i:j+1] for list L, where i takes on values # from 0 to len(L)-1, and j takes on values from i to len(L)-1. i = 0 # line 2 while i < len(L): # line 3 sum = 0 # line 4 j = i # line 5 while j < len(L): # line 6 sum = sum + L[j] # line 7 if sum > max: # line 8 max = sum # line 9 j = j + 1 # line 10 i = i + 1 # line 11 # At this point, weve examined every slice. return max # line 12

TQ

Course notes for csc 165 h

row does this hnge the running timec ou should e le to onvine yourself tht the new lgorithm9s running time is @n2 A | ig improvement over the rst lgorithmF he detils re left s n exeriseF fefore we turn the pge on this prolemD you my wonderX is this the most eient wy to solve itc es it turns outD there is lever lgorithm tht n gure out the mximum sum in worstEse time @nA | in other wordsD with just one loop over the vlues3 ther thn spoil the funD we9re going to let you puzzle this one outF F F F14
4.13 Exercises for asymptotic notation

IF rove or disprove the following limsX @A Un3 C IIn2 C n P y@n3 A15 @A n2 C ITS P @n4 A @A n3 P y@n A @dA n P y@n log2 nA @eA Vk P N; k > I A log n P @log2 nA @ n3 ; PF hene g@nA a p=ITS n < ITS F xote tht Vx P R; x 6 dxe < x C IF 5 ; Tn n > ITS rove tht g P y@n2 5 AF QF vet p e the set of funtions from N to R>0 F rove the following theoremsX @A por f; g P pD if g P @f A then g2 P @f 2 AF @A Vk P N; k > I A Vd P R+ ; d log n P @log2 nAF16 xotie tht @A mens tht ll logrithms eventully grow t the sme rte @up to multiplitive onstntAD so the se doesn9t mtter @nd n e omitted inside the symptoti nottionAF RF vet p e the set of funtions from N to R>0 F rove or disprove the following limsX @A Vf P p ; Vg P p ; f P y@gA A @f C gA P @gA @A Vf P p ; Vf H P p ; Vg P p ; @f P y@gA f H P y@gAA A @f C f H A P y@gA SF por eh funtion f in the left olumnD hoose one expression y@gA from the right olumn suh tht f P y@g AF se eh expression extly oneF
n k : k

@iA @iiA @iiiA @ivA @vA @viA @viiA @viiiA @ixA @xA

P @n5 C UA@n5 UA P 4 log 2 P 2 +1 log2 5 P V C 12 P P3 +1 P n3 P 5 log2 ( +1) P 1+ log2 3 @n PA log2 @n3 C RA P


2n4 +1 n3 +2n 1
n n n n n n n n n n n n

QP P
n

@A @A @A @dA @eA @fA @gA @hA @iA @jA TR

y@ 1 A y@IA y@log2 nA y@nA y@n log2 nA y@n2 A y@n10 A y@P A y@IH A y@n A
n n n n

Chapter 4. Algorithm Analysis and Asymptotic Notation

4.14

Exercises for algorithm analysis

IF rite detiled nlysis of the worstEse running time of lgorithm faster_max_sumF PF rite detiled nlysis of the worstEse running time of the following lgorithmF
def mystery1(L): """ L is a non-empty list of length len(L) = n. """ tot = 0 i = 0 while i < len(L): if L[i] > 0: tot = tot + L[i] i = i + 1 return tot

QF rite detiled nlysis of the worstEse running time of the following lgorithmF
def mystery2(L): """ L is a non-empty list of length len(L) = n. """ i = 1 while i < len(L) - 1: j = i - 1 while j <= i + 1: L[j] = L[j] + L[i] j = j + 1 i = i + 1

RF rite detiled nlysis of the worstEse running time of the following lgorithmF
def mystery3(L): """ L is a non-empty list of length len(L) = n. """ i = 1 while i < len(L): print L[i] i = i * 2

SF rite detiled nlysis of the worstEse running time of the following lgorithmF
def mystery4(L): """ L is a non-empty list of length len(L) = n. """ i = 0 while i < len(L): if L[i] % 2 == 0: j = i while j < len(L): L[j] = L[j] + 1 j = j + 1 i = i + 1
end of week 10

TS

Course notes for csc 165 h

4.15

Induction interlude

uppose P @nA is some predite of the nturl numersD ndX @A


P @HA @Vn P N; P @nA A P @n C IAA:

ou should ertinly e le to show tht @A implies P @HAD P @IAD P @PAD in ft P @nA where n is ny nturl numer you hve the ptiene to follow the hin of results to otinF sn ftD we feel tht we n turn the rnk4 enough times to show tht @A implies P @nA for ny nturl numer nF his is lled the riniple of imple sndution @sAF st isn9t provedD it is n xiom tht we ssume to e trueF rere9s n pplition of the s to some funtions we9ve enountered eforeF P @nA: P > PnF s9d like to prove tht Vn; P @nAD using the sF rere9s wht s doX 0 Prove P @HA: P @HA sttes tht P a I > P@HA a HD whih is trueF Prove Vn P N; P @nA A P @n C IA: essume n P NF 5 ritrry nturl numer essume P @nAD tht is P > PnF 5 nteedent hen n a H n > HF 5 nturl numers re nonEnegtive +1 Case 1 @ssume n a HAX hen P a P1 a P > P@n C IA a PF Case 2 @ssume n > HAX hen n > IF 5 n is n integer greter thn H hen P > PF 5 sine n > ID nd P is monotone inresing hen P +1 a P C P > Pn C P a P@n C IAF 5 y previous line nd sr P @nA hen P +1 > P@n C IAD whih is P @n C IAF 5 true in oth possile ses hen P @nA A P @n C IAF 5 introdue A hen Vn P N; P @nA A P @n C IAF 5 introdue V s now onludeD y the sD Vn P N; P @nAD tht is P > PnF ht hppens to indution for predites tht re true for ll nturl numers fter ertin pointD ut untrue for the rst few nturl numersc por exmpleD P grows muh more quikly thn n2 D ut P3 is not lrger thn Q2 F ghoose n ig enoughD thoughD nd it is true thtX
n n n n n n n n n n n

P @nA X P > n2 :
n

ou n9t prove this for ll nD when it is flse for n a P; n a QD nd n a RD so you9ll need to restrit the domin nd prove tht for ll nturl numers greter thn RD P @nA is trueF e don9t hve slik wy to restrit domins in our symoli nottionF vet9s onsider three wys to restrit the nturl numers to just those greter thn RD nd then use indutionF Restrict by set difference: yne wy to restrit the domin is y set diereneX Vn P N n fH; I; P; Q; Rg; P @nA eginD we9ll need to prove P @SAD nd then tht Vn P N n fH; I; P; Q; Rg; P @nA A P @n C IAF Restrict by translation: e n lso restrit the domin y trnslting our prediteD y letting Q@nA a P @n C SAD tht isX Q@nA X P +5 > @n C SA2 xow our tsk is to prove Q@HA is true nd tht for ll n P ND Q@nAA Q@n CIAF his is simple indutionF
n

TT

Chapter 4. Algorithm Analysis and Asymptotic Notation

Restrict using implication:

enother method of restrition uses implition to restrit the domin where we lim P @nA is true | in the sme wy s for sentenesX Vn P N; n > S A P @nA: he expnded predite Q@nA X n > S A P @nA now ts our pttern for simple indutionD nd ll we need to do is proveX IF Q@HA is true @it is vuously trueD sine H > S is flseAF PF Vn P N; Q@nA A Q@n C IAF his reks into sesF  sf n < RD then Q@nA nd Q@n CIA re oth vuously true @the nteedents of the implition re flseD sine n nd n CI re not greter thnD nor equl toD SAD so there is nothing to proveF  sf n a RD then Q@nA is vuously trueD ut Q@n CIA hs true nteedent @S > SAD so we need to prove Q@SA diretlyX P5 > S2 is trueD sine QP > PSF  sf n > RD we n depend on the ssumption of the onsequent of Q@n IA eing true to prove Q@nAX P a P 1 C P 1 @denition of P A > P@n IA2 @nteedent of Q@n IAA a Pn2 Pn C P a n2 C n@n PA C P > n2 C P > n2 @sine n > R > PA
n n n n

efter ll tht workD it turns out tht we need prove just two thingsX IF P @SA PF Vn P ND sf n > RD then P @nA A P @n C IAF his is the sme s eforeD exept now our se se is P @SA rther thn P @HAD nd we get to use the ft tht n > S in our indution step @if we need itAF hihever rgument you9re omfortle withD notie tht simple indution is silly the smeX you prove the se se @whih my now e greter thn HAD nd you prove the indution stepF
Chapter 4 Notes

prom H to @P IAD if we work in nlogy with se IHF 2 o prse the H:IHI prtD lulte H:IHI a I@P1 A C H@P2 A C I@P3 AF 3 ou should e le to look up this lgorithm in n elementry shool textookF 4 me s the previous exeriseD ut only write numers tht hve H9s nd I9sD nd do inry dditionF 5 snteger divide y IHF 6 snteger divide y PF 7 fetter in the sense of time omplexityF 8 epplying l9rpitl9s uleD lim 3I 2 a lim 3I ln(2)2 a ID euse lim 3I P a lim 3I n a IF o 1 9 ureD f a n2 D g a Qn2 C PF
1
n n
n n n n n n

TU

Course notes for csc 165 h

10

ureF f nd g don9t need to oth e monotoniD so let f @nA a n2 nd


g @ nA a
@

n; n even n3 ; n odd

o not every pir of funtions from N 3 R>0 n e ompred using figEyF 11 vet9s try the symmetril presenttion of iEimplitionF 12 ut not prtiulrly eientF F F 13 hon9t forget the empty slice LH X H @or Li X i for ny index iD for tht mtterAF his is vlid slie for ny list nd its sum is HD whih is greter thn ny negtive integerF 14 rintX you don9t need loop to gure out the mximum sum of ny slie tht ends t index iF 15 he lim is trueF vet cH a VF hen cH P R+ F vet B H a IPF hen B H P NF essume n P N nd n > B H F hen n3 a n n2 > IP n2 a IIn2 C n2 > IIn2 C nF 5 sine n > B H a IP hus cH n3 a Vn3 a Un3 C n3 > Un3 C IIn2 C nF o Vn P ND n > B H A Un3 C IIn2 C n 6 cH n3 F ine B H is nturl numerD WB P N; Vn P N; n > B A Un3 C IIn2 C n 6 cH n3 F ine cH is rel positive numerD Wc P R+ ; WB P N; Vn P N; n > B A Un3 C IIn2 C n 6 cn3 F fy denitionD Un3 C IIn2 C n P y@n3 AF
16

essume k P N nd k > IF essume d P R+ F st sues to rgue tht d log n P @log2 nAF vet cH1 a log2 F ine k > ID log2 k Ta H nd so cH1 P R+ F vet cH2 a log2 F fy the sme resoningD cH2 P R+ F vet B H a IF hen B H P NF essume n P N nd n > B H F hen cH1 log2 n a log2 log2 n a d log22 a d log n 6 d log nF log woreoverD d log n 6 d log22 a log2 log2 n a cH2 log2 nF log reneD Vn P N; n > B H A cH1 log2 n 6 d log n 6 cH2 log2 nF hus Wc1 P R+ ; Wc2 P R+ ; WB P N; Vn P N; n > B A c1 log2 n 6 d log fy denitionD d log n P @log2 nAF husD Vd P R+ ; d log n P @log2 nAF rene Vk P N; k > I A Vd P R+ ; d log n P @log2 nAF
k d k k d d n k k k k n k d k k k k k k

n 6 c2 log2 nF

TV

Chapter 5

A Taste of Computability Theory


5.1 The problem

elgorithms @implemented s omputer progrmsA n rry out mny omplex tsksF ome of the more interesting ones onern omputers nd progrms themselvesD e.g.D ompilingD interpretingD nd other mE nipultions of soure odeF rere is one prtiulr tsk tht we would like to rry outF
def halt(f,i): """Return True iff f(i) eventually halts.""" return True # replace this stub with correct code

xote tht funtion halt is wellEdenedX halt is pssed referene to some other ython funtion f long with n input iD nd it must return True if f(i) eventully hlts @normllyD or euse of rshAY False if f(i) eventully gets stuk in some innite loopF hese re the only two possile ehviours for the ll f(i) | we ignore ny possiility of hrdwre filure @whih ould not e deteted from within halt nywyAF sn other wordsD we re interested in the conceptual ehviour of f itselfD under idel onditions for its exeutionF fefore you red the next setionD see if you n ome up with n implementtion tht works | even if it9s just t highElevel nd not fully written out in ythonF es guideD think out the vlue returned y your ode for the ll halt(blah,5)D or for the ll halt(blah,8)D where blah is the following funtionF
def blah(x): if x % 2 == 0: while True: else return x pass

5.2

An impossible proof

ht if we told you tht it is impossile to implement funtion haltc xote tht this is very strong lim to mkeX we9re not just sying we don9t know how to write halt4Y we9re sying nobody n write haltD everD euse it simply nnot e done43 feuse this is suh strong limD it is dunting to proveF elsoD how n we even prove tht something is not possible c ouldn9t tht require us to rgue out every possile wy to try to rry out this tskc1 etD we will e le to do just tht using only the proof tehniques we9ve seen so frF rere is howF F F TW

Course notes for csc 165 h

essume halt exists @i.e.D it is fully written out in ythonAF hen onsider the following funtionF
def confused(f): def halt(f,i): ...copy/paste the full code for halt here... if halt(f,f): while True: pass else: return False # line 1 # line 2 # line 3

xote tht this is orret ython funtion @ssuming we9ve psted in the ode for haltAF2 xowD we skX wht is the ehviour of the ll confused(confused)c iither confused(confused) hlts or confused(confused) does not hltF3 gse IX essume confused(confused) hltsF hen halt(confused,confused) returns True @on line IAF 5 y denition of halt hen confused(confused) goes into n innite loop @on line PAF o confused(confused) hlts A confused(confused) does not hltF gse PX essume confused(confused) does not hltF hen halt(confused,confused) returns False @on line IAF 5 y denition of halt hen confused(confused) returns False @on line QAF o confused(confused) does not hlt A confused(confused) hltsF reneD confused(confused) hlts D confused(confused) does not hltF 5 Ds glerlyD this is ontrditionF 5 of the form p D Xp henD y ontrditionD halt does not exist3 his is suh ounterEintuitive result tht it is tempting to dismiss it t rstF fut tke the time to think through eh step of the proofD nd you will see tht they re ll orretF woreoverD this result does not expose wekness in ythonX the ext sme rgument would pply to every possile progrmming lnguge @in ftD to things tht we would not even ll progrmming lnguges4AF
Historical context

he proof we just showed ws disovered y the mthemtiins eln uring nd elonzo ghurh @indeE pendently of eh otherAD k in the lte IWQH9s | efore omputers even existed3 hey rgued tht every possile lgorithm n e expressed using smll set of primitive opertions @Elulus4 in the se of ghurh nd uring whines4 in the se of uringAF henD they rried out the rgument ove sed on those primitive opertionsF sf you elieve tht ython is ple of expressing every possile lgorithm @nd it isAD then the rgument shows tht some prolems nnot e solved y ny lgorithmF @hink out itX wht fetures of ython did we need in our proofc4 A
Definitions

here is something ounterEintuitive out funtion haltX we n desrie wht the ll halt(f,i) is supposed to returnD even though the preeding rgument shows tht there is no wy to implement funtion halt in ython @or in ny other lngugeD for tht mtterAF his is dierent from every other ython funtion you9ve thought outX usullyD you strt with vgueD intuitive ide of wht you wnt to omplishD nd you turn it into working piee of ython odeF fut in this seD the proess does not rry throughX we n dene the ehviour of funtion haltD ut it nnot e implementedF his shows tht there re oneErgument funtions whose ehviour n e dened lerlyD ut tht nnot e omputed y ny lgorithmF rere is some stndrd terminology regrding these issuesF UH

Chapter 5. A Taste of Computability Theory

e funtion f X A 3 B is computable if it n e implemented in ythonD i.e.D if there exists ython funtion f suh tht for ll a P AD f(a) returns the vlue of f @aAF puntions for whih this is not possile @e.g.D haltA re lled non-computableF xote tht this denition pplies only to wellEdened mthemtil funtions f X A 3 B | those for whih there is extly one vlue f @aA for every a P AF he distintion etween omputle4 nd nonEomputle4 hs nothing to do with the de nition of the funtion f F therD it distinguishes etween funtions whose vlues n e lulted y n lgorithmD nd those for whih this is not possileF
Definition:

5.3

Reductions

o halt is not omputleF fut it9s not the end of the world if this one funtion nnot e omputedD is itc nfortuntelyD nonEomputle funtions re like ugsX when you disover oneD you quikly relize tht there re mny more roundF F F sn this setionD we9ll explin how to use the ft tht halt is not omputleD to show tht other funtions re lso nonEomputleF por exmpleD onsider the funtion initialized(f,v)D whose vlue is True when vrile v is gurnteed to e initilized efore its rst useD whenever ython funtion f is lled @no mtter wht input is pssed to fA | initialize(f,v) is False if there is even one input i suh tht the ll f(i) ttempts to use the vlue of v efore it hs een initilizedF xote tht there is sutleD ut importntD distintion to e mdeX we don9t wnt initialized(f,v) to e True when there is just possibility tht vrile v my e used efore its initiliztion in f | we wnt to know tht this tully hppens during the ll f(i) for some input iF por exmpleD onsider the following funtionsX
def f1(x): return x + 1 print y def f2(x): return x + y + 1

hile it is the se tht f1 ontins sttement tht uses vrile y efore it is initilizedD this sttement n never tully e exeuted | so initialized(f1,y) is TrueD vuouslyF yn the other hndD vrile y is tully used efore its initiliztion in f2 | so initialized(f2,y) is FalseF Claim SFI: he funtion initialized is nonEomputleF
Proof:

por ontrditionD ssume tht initialized is omputleD i.e.D it n e implemented s ython funtionF e wnt to show tht this ssumption leds to ontrditionF wore speilly in this seD we wnt to reh the ontrdition tht halt is omputleF oD onsider the following progrmF
def halt(f,i): def initialized(g,v): ...code for initialized goes here... # Put some code here to scan the code for f and figure out # a variable name v that does not appear anywhere in f. def f_prime(x): # Ignore the argument x, call f with the fixed argument i # (the one passed in to halt). f(i) print v return not initialized(f_prime,v)

UI

Course notes for csc 165 h

end of week 11

@elthough we left out some of the detils in this ode | the prt tht is ommented out | it can e lled in nd the result is vlid ython progrm | ssuming tht the ode for initialized n e lledEinD of ourseFA sf f(i) hltsD then f_prime(i) will exeute the sttement print vD so initialized(f_prime,v) returns False nd halt(f,i) returns TrueF sf f(i) does not hltD then f_prime(x) never exeutes print v @no mtter wht vlue x hsAD so initialized(f_prime,v) returns True nd halt(f,i) returns FalseF fut there is no ython implementtion of funtion haltD s we9ve lredy shown3 reneD there is no ython implementtion of funtion initializedF vet9s step through the preeding rgument to etter understnd itF  e wnt to prove tht initialized is non omputle @i.e.D tht there is no ython ode to ompute itAD sed on the ft tht halt is nonEomputleF e deide to use proof y ontrditionD so we egin y ssuming tht initialized is omputleF yur gol is now to derive ontrditionF en ovious ndidte is the sttementX halt is omE putleF4  o show tht halt is omputle mens to show tht there exists n lgorithm to ompute itF he esiest wy to hieve this is to desrie n expliit lgorithm for haltF sn other wordsD our gol is to nd wy to ompute haltD given supposed lgorithm for initializedF  he trik to hieving this is ontined in the denition of funtion f_prime in the rgumentX this funtion is vlid ython ode nd it hs een dened with the property tht the ll f(i) hlts i vrile v is used without eing initilized in f_primeF woreoverD this property does not depend on our knowledge of whether or not f(i) hltsF  reneD we know tht our implementtion of halt will return the orret vlueD under the ssumption tht initialized hs een implemented orretlyF  he ontrdition follows immeditelyD whih onludes the proofF xote tht the ritil step in this rgument | the one tht requires the most retivity nd insight | is to nd wy to tie together the ft tht the ll f(i) hlts4 with the ft tht vrile v is lwys initilized efore its use within funtion f_primeF4 yne we gure out how to hieve thisD the struture of the rest of the proof is strightforwrdF his kind of rgument is lled reduction nd n e used to show tht mny other funtions re nonEomputleD y proving onditionl sttements of the formX If f is omputle @i.e.D there is ython funtion f tht omputes f AD then h is lso omputle @i.e.D we n write python funtion h tht omputes hAF iking h to e funtion known to e nonEomputle @like haltA immeditely gives us tht f is lso nonEomputleD y ontrpositiveF
5.4 Countability

he ide for funtion confused in the proof tht halt is nonEomputle is n exmple of diagonalization rgumentD rst used y the mthemtiin qeorg gntor to show tht the set of rel numers is lrger thn the set of nturl numersF xow thtD in itselfD my seem like strnge sttement to mkeX how n the set of rel numers e lrger4 thn the set of nturl numersc hon9t they oth ontin innitely mny numersc ht does it even men to ompre the sizes of innitely lrge setsc UP

Chapter 5. A Taste of Computability Theory

vet9s tke things one step t time nd sle k to simpler innite setsF gonsider N @the set of nturl numersA nd Z @the set of integersAF fetween the twoD whih set is largerc he nswer seems oviousX Z ontins N s proper susetD so Z must e lrgerF fut now think out thisX whih set hs lrger sizec xow the nswer is not so oviousX for nite sets size4 is nturl numerD ut how do we mesure the size of innite setsc edding or removing one element from n innite set does not hnge its sizeX it9s still inniteF o re ll innite sets the sme sizec end wht does innite size4 even menc vet9s think through this more refullyF hen we count the elements in setD wht we re relly doing is associating a number with each element X you n esily imgine yourself pointing to elements one fter the other while sying oneD twoD threeD F F F 4 he esiest wy to formlize this ide involves the notions of oneEtoEone nd onto funtionsF Definition: uppose f X A 3 B @i.e.D f is funtion tht ssoites n element f @aA P B to eh element a P AAF hen we sy tht  f is one-to-one if Va1 P A; Va2 P A; f @a1 A a f @a2 A A a1 a a2 @i.e.D f gives distint vlues to dierent elements of AAY  f is onto if Vb P B; Wa P A; f @aA a b @i.e.D every element in B n e rehed4 from t lest one element of AAF por exmpleD  the funtion f X N 3 N dened y f @xA a x=P C P is neither oneEtoEone @f @PA a Q a f @QAA nor onto @there is no x P N suh tht f @xA a IAY  the funtion f X N 3 N dened y f @xA a Px is oneEtoEone @f @xA a f @yA A x a yA ut not onto @there is no x P N suh tht f @xA a IAY  the funtion f X N 3 N dened y f @xA a x=P is not oneEtoEone @f @PA a I a f @QAA ut it is onto @for ll n P ND f @PnA a nAY  the funtion f X N 3 N dened y f @xA a IH x if x 6 IHY f @xA a x otherwise is oth oneEtoEone @the numers fH; : : : ; IHg get mpped to fIH; : : : ; Hg nd fII; IP; : : : g remin unhngedA nd onto @Vn P N; @n 6 IH A f @IH nA a nA @n > IH A f @nA a nAAF @ou n nd out more out oneEtoEone nd onto funtions in etion SFP of ellemn9s row to rove stD4 or on ikipediFA sing these oneptsD the ide of ounting4 n e formlized s followsX Definition: et A is countable if IF there is funtion f X A 3 N tht is oneEtoEoneD or equivlentlyD PF there is funtion f X N 3 A tht is ontoF por exmpleD Claim SFP: Z is ountleF vet @ n=P if n is evenD f @nA a @I nA=P if n is oddF hen f X N 3 ZF xextD we show tht f is ontoF UQ

Course notes for csc 165 h

essume x P ZF hen x 6 H or x > HF gse IX essume x 6 HF vet nH a I PxF hen x 6 H A Px > H A nH > H so nH P NF elsoD nH a P@xA C I so nH is oddF hen f @nH A a @I nH A=P a @I @I PxAA=P a Px=P a xF rene Wn P N; f @nA a xF gse PX essume x > HF vet nH a PxF hen x > H A nH > H so nH P NF elsoD nH a Px is evenF hen f @nH A a nH =P a Px=P a xF rene Wn P N; f @nA a xF sn ll sesD Wn P N; f @nA a xF ine x ws ritrryD Vx P Z; Wn P N; f @nA a xD i.e.D f is ontoF @xote tht f is not oneEtoEone | n you see whyc5 A reneD there is funtion f X N 3 Z tht is ontoD i.e.D Z is ountleF snformllyD we often show tht set A is ountle y giving n rgument tht it is possile to list every element in the set | this orresponds to giving funtion f X N 3 A tht is ontoD though the funtion my not e written out lgerillyF ht mtters most is to mke sure tht there is some systemti wy to list the elements of A nd tht the list inludes every element of A t lest oneF por exmpleD we my rgue tht Z is ountle y exhiiting the following list @rellyD more list pattern euse of the F F F 4AX Z X H; I; I; P; P; Q; Q; : : : smpliitlyD this denes funtion f X N 3 Z | just think of the list s n enumertion of f 9s vlues @f @HA a H; f @IA a I; f @PA a I; : : : A yne we see the ptternD it is ovious tht the list inludes every possile integerD i.e.D the impliit funtion f is ontoF reneD Z is ountleF ht out nite setsD e.g.D fa; b; cgc ss it ountlec esD euse of ondition I in the denition of ountilityX the funtion f @aA a ID f @bA a PD f @cA a Q is esily proved to e oneEtoEoneF essume x; y P fa; b; cg nd f @xA a f @yAF hen f @xA a f @yA a I or f @xA a f @yA a P or f @xA a f @yA a QF 5 these re the only vlues in the domin of f gse IX essume f @xA a f @yA a IF hen x a y a a so x a yF gse PX essume f @xA a f @yA a PF hen x a y a b so x a yF gse QX essume f @xA a f @yA a QF hen x a y a c so x a yF sn every seD x a yF rene Vx P fa; b; cg; Vy P fa; b; cg; f @xA a f @yA A x a yD i.e.D f is oneEtoEoneF ht out other innite setsc ere they ll ountlec gonsider the set Q of rtionl numers @i.e.D numers of the form p=q for integers p; q with q Ta HAF xumerillyD we know tht this set is muh dierent from Z or NX the rtionl numers re dense @there re innitely mny rtionl numers etween ny two other rtionl numersAF his dierene might seem to imply tht Q is not ountleD for how ould we hope to list ll of the rtionl numersc roweverD note tht our informl denition of ountility does not require tht we e le to list the elements in ny kind of numeril order | in ftD our list for Z ove ws not ordered numerillyF UR

Chapter 5. A Taste of Computability Theory

sn order to show tht Q is ountleD we rst prove lemmF + Claim SFQ: Q is ountle @where Q+ is the set of positive rtionl numersAF e give n informl list4 rgumentF vet f @nA e dened impliitly y the following list proessX list frtions p=q in inresing order of p C q D strting with p C q a PD nd in inresing order of p within eh suElist with xed vlue of p C q F he rst few terms of the list re s followsX  suElist HX I=ID @frtions p=q where p C q a PA  suElist IX I=P; P=ID @frtions p=q where p C q a QA  suElist PX I=Q; P=P; Q=ID @frtions p=q where p C q a QA  ::: hen f @nA X N 3 Q+ F elsoD f @nA is ontoX every frtion p=q with p > H; q > H is eventully listed @in position p of suElist numer p C q PAF reneD y denitionD Q+ is ountleF Claim SFR: Q is ountleF xote tht Q a fHg Q Q+ F vet f @nA e the funtion from lemm SFQ @showing Q+ is ountleAF henD the following is omplete list of QD i.e.D it denes funtion f H X N 3 Q tht is ontoX H; f @HA; f @HA; f @IA; f @IA; f @PA; f @PA; : : : a H; I=I; I=I; I=P; I=P; P=I; P=I; : : : fy this pointD you my strt to think tht every set is ountleD nd tht the notion is meninglessF roweverD this turns out not to e the seF o see thisD rst try to ome up with omplete list of R @the set of rel numersA | to mke it esierD you my egin with just R+ D s we did for QF sf you run into diultiesD rememer tht  every rel numer r P R n e written s n innite deiml expnsion of the form r a m:d1 d2 d3 : : : D where m P Z nd d1 ; d2 ; d3 ; : : : P fH; I; P; Q; R; S; T; U; V; Wg nd the expnsion does not end with repetE ing W9sY  every innite deiml expnsion of the form r a m:d1 d2 d3 : : : D where m P Z nd d1 ; d2 ; d3 ; : : : P fH; I; P; Q; R; S; T; U; V; Wg nd the expnsion does not end with repeting W9sD denes unique rel numer r P RF he provision tht the deiml expnsion does not end with repeting W9s is onsequene of the positionl nottion systemD nd the ft tht H:W : : : a I:H : : : reneD every deiml expnsion tht ends with innitely mny W9s is equivlent to deiml expnsion tht ends with innitely mny H9s insted | ut ll other deiml expnsions re dierent from eh otherF
5.5 Diagonalization

Cantor's proof

ry s you mightD you will not e le to provide omplete list of RF qeorg gntor showed tht this ws impossile through the following proofD lled diagonalization argumentF Claim SFS: R is unountleF US

Course notes for csc 165 h

Proof:

por ontrditionD ssume tht R is ountleF hen there is funtion f X N 3 R tht is ontoF 5 y denition of ountle4 e n visulize funtion f s followsX for every nturl numer n P ND f @nA is rel numerD represented s n innite deiml expnsion tht does not end with repeting W9sX
f @HA a i0 :d0 0 d0 1 d0 2 d0 f @IA a i1 :d1 0 d1 1 d1 2 d1 f @PA a i2 :d2 0 d2 1 d2 2 d2
; ; ; ; ; ; ; ; n ;n ;n ;n

FF F a f @nA a i FF F a
i;j

:d

n;0

n;1

FF F FF F

n;2

d
n;n

where i0 ; i1 ; : : : P Z re the integer prts of the rel numers f @HA; f @IA; : : : D nd Vi P N; Vj P ND d P fH; I; P; Q; R; S; T; U; V; WgF xowD let r a H:d0 d1 d2 d D where Vi P ND
n

a I if d a H; H otherwise:
i;i

hen r P RF 5 r is n innite deiml tht does not end with repeting W9s hen Wk P N; f @kA a rF 5 f is onto ine f @kA a i :d 0 d 1 d 2 d nd r a H:d0 d1 d2 d D this implies i a H nd Vn P ND
k k; k; k; k;n n k

k;n

a d a I if d a H; H otherwise:
n;n n

a d a I if d a H; H otherwise; i.e.D d a H D d a ID ontrdition3 henD y ontrditionD R is not ountleF


d
k;k k;k k k;k k;k

fut thenD

ke the time to study this proof littleF F F ou should notie the following key elementsX  he onstrution of rel numer r suh tht Vn P N; f @nA Ta rF  he ft tht this onstrution is rried out for n ritrry funtion f X N 3 RF ou should e le to use these ides to write diret proof tht there does not exist ny funtion f X N 3 R tht is onto | thus proving diretly tht R is unountleF
Diagonalization and computability

he onstrution of rel numer r in the lst proof is done using digonliztionF he reson for this nme eomes ovious if we visulize how r is onstrutedX eh digit d of r is dened to e dierent from t lest one digit d in the deiml expnsion of the rel numer f @iAF
i i;i

UT

Chapter 5. A Taste of Computability Theory

FF F a f @ nA a i FF F a

f @HA a f @IA a f @PA a

ra

d0 i0 : d0 0 i1 : d1 0 i2 : d2 0
; ; ; n

H:

d1 d0 1 d1 1 d2 1
; ; ;

d2 d0 2 d1 2 d2 2
; ; ;

:d

n;0

n;1

FF F FF F

d d0 d1 d2 d

n ;n ;n ;n

n;2

n;n

feuse there re innitely mny digits in the deiml expnsion of r @one for eh nturl numer n P NAD r is dierent from the rel numer f @nA for every n P NF xowD rememer tht we strted this disussion in the ontext of the proof tht funtion halt nnot e implemented in ythonF fk thenD s mentioned tht this proof ws n exmple of digonliztion | s will now explin whyF pirstD notie tht halt tkes two rgumentsX oneErgument ython funtion f nd n input i for fF glerlyD there re innitely mny oneErgument ython funtions fD ut wht kind of innityX ountle or unountlec o nswer this questionD we need spei point of viewX rememer tht every ython funtion n e written down s text leD i.e.D nite sequene of hrtersD from xed set of hrtersF o e speiD let9s ssume the hrter set is pEVD whih ontins PST dierent hrters | the ext detils of the enoding used for the set of hrters don9t mtterX wht mtters is tht the set of possile hrters is xedD i.e.D the same nite set of hrters n e used to write down every possile ython funtionF oD every ython funtion n e written down s sequene of hrters in pEVD nd every suh sequene of hrters is relly just n integer in disguiseX just tret eh hrter s digit4 in seEPST nottionF sf you think out itD wht we9ve just done in the rgument ove is to dene funtion
g X oneErgument ython funtions 3 N

tht is oneEtoEoneX dierent ython funtions get ssigned to dierent numersD euse their soure ode must dier y t lest one hrterF o y denitionD the set of oneErgument ython funtions is ountE le | tullyD the rgument is more generl thn thisX it shows tht the set of all ython progrms is ountleF fy the denition of ountilityD this mens tht there is some other funtion
g H X N 3 oneErgument ython funtions

tht is ontoD i.e.D it is possile to list every oneErgument ython funtionX gH @HA; gH @IA; gH @PA; : : : | to mke the nottion esier to understndD we9ll use susripts f0 a gH @HA; f1 a gH @IA; f2 a gH @PA; : : : in the rest of the rgumentF ueep in mind tht eh f is tully just string X the soure ode for some oneErgument ython funtionF o it mkes sense to onsider the funtion ll f @f A for i; j P NX we re simply lling the ython funtion whose soure ode is given y f with the string f s rgumentF xowD onsider the following tle of ehvioursD where we list oneErgument ython funtions down the left side nd inputs for those funtions ross the top | our list only ontins spei kinds of inputs @those tht re listings for oneErgument ython funtionsAD nd it is therefore not omplete list of ll possile inputsF he entry t row f nd olumn f sttes whether the ll f @f A hlts or not @the vlues shown elow re just n exmpleAF
n i j i j i j i j

UU

Course notes for csc 165 h

end of week 12

f0 f1 f2 f hlts hlts loops hlts loops loops loops hlts loops hlts hlts loops FF FF F F f hlts hlts hlts hlts FF FF F F @xote thtD when we ssume tht halt n e oded up in ythonD this implies tht every entry in this tle n e omputed y haltFA he funtion confused is reted y simple proess of digonliztion over the tleX just use halt to gure out the ehviour of f @f A nd mke confused do the oppositeF sf halt existsD then so does confusedD ut confused nnot e ny of the elements of the list f0 ; f1 ; f2 ; : : : | y onstrutionD its ehviour diers from eh one of these funtionsF his is how we get ontrditionF xote tht this rgument lso shows tht every list of oneErgument funtions is inompleteF sf you look t if gin more refullyD you should see tht the rgument n e interpreted s proof tht the set of oneErgument funtions @mening funtions9 ehvioursA is unountleF ine we9ve lredy rgued tht the set of ython progrms is ountleD this mens there re unountly mny funtions tht nnot e implemented in ython3 e9ve rely srthed the surfe of this fsinting topiF sf you nd it interesting | nd who wouldn9t3 | then you n nd out muh more in the ourses csc 463 h nd csc 438 hF f0 f1 f2
n n i i

Chapter 5 Notes

esD it wouldF end it turns out to e possile to give just suh n rgumentD s you9ll seeF 2 esD ython does llow suh nested4 funtion denitionsX it simply mkes halt dened lolly within confusedD just like for vrilesF 3 xotie this is just n pplition of ixluded widdleF 4 puntionsD onditionlsD nd loopsF e used nested funtionsD4 ut tht is not stritly neessryF 5 f @HA a H=P a H a @I IA=P a f @IAF
1

UV

You might also like