CS112 Fall 2011_ Problem Set 1

CS112 Fall 2011_ Problem Set 1 - 12/27/11 CS112 Fall 2011:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 12/27/11 CS112 Fall 2011: Problem Set 1 CS112 Fall 2011: Problem Se 1 Big O 1. E e ci e E 3.10, age 117 f he e b A . ead hee ee ac f de ce a he e a i a c e. E ach ead hee c e d e de , a d each c ia ce d e f he e a . The e a e de a dce a , he ead hee ha C id e a a g i h ha c ca ce each e a . Y a. b. c. d. e eed he ace a a e he W h a a e h e b a ic e a i dc Wh a i h e -ca e i g i ea a ac W h a i h e b ig O i g i e? I a g i h i ea , ad a ic, e he f he hi /he c e a dcc . a e a f each de , a d he a e age i g i e f hi a g i h . a d he i g i e? ( big O) f he e ba ic eai ? de ? SOLUT ION a. Ba ic e a i a e addi i a d di i i . b. E ach de a ( hich a a e ) i be added a , he e a e *c addi i f he a. E ach e a a e age addi i . i added E ach e a i be di ided e ac a e age c. O( c) d. LINE A R (NO e: The Big O i g i e i i ea c i e . S i ce h e e a e i e . S i ce h e e a e c e a i ea ia ce b he ad a ic a e, b he i i e. be , hi gi e a f de he INPUT SIZE i de he c* , c. S , c di i i . c, a d he 2. E e ci e 3.14, g 118 f he e b A ca d ga e ga ee a dec f ca d i a a a . Gi e a a g i h " h ff e" he dec h a a h e ca d f a i a e g ed ge he , a d i hi each i , he ca d a e i a ce di g de a --c ide he ace a he e a e d ca d . S i ce h e e a e 52 ca d , ace i ai e ca e e a ace if eeded. The c ai i ha a g i h be a fa a ib e . Wh a i ed i he aa c a e b ig O igi e f a g i h ? W h a a e h e b a ic e a i i ? I he a e age big O i g i e diffe e f he ca e ? SOLUT ION A ca e f e a a , each f e g h 13, e f each i . G h gh he igi a dec f f e d, a d each ca d i i a ia e i i i he i a a h ic h i b e g . S f ea e he ee f hea ig i de i i 11 i he hea a a . N e ha i ce he i de e f h e a a a e f 0h gh 12, he a a ii f a ca d i b e e e ha i www.cs.rutgers.edu/courses/112/classes/ /set1_sol.html 1/3 12/27/11 CS112 Fall 2011: Problem Set 1 face al e. (Q een' face al e i 12, o i The (b) i he co n a a p o i io n ill be 11.) big O nning ime i O(1). The ba ic o pe a io n a e (a) lo o king p a ca d in he deck a a , and i ing i in o a i a a . E ach o f he e ake ni ime. (W i ing ake ni ime beca e he a a i di ec l inde ed i h he ca d' face al e.) Since he e a e 52 lo o k p and 52 i e , o al n mbe o f ba ic o pe a io n , and he efo e he o al ni o f ime, i 104. Thi i a an , o he big O ime i O(1). 3. E e ci e E 3.11 , page 118 o f he e bo o k. T o peo ple co mpa e hei fa o i e pla li fo ma ching o ng . The fi pla li ha o ng , and h e e co n d h a . E ach i o ed in an a a , in no pa ic la o de . De c ibe an algo i hm o find he co mmo n o ng in he e li (in e ec io n). a. Wha i he o -ca e big O nning ime o f o algo i hm? M ake e o a e h e b a ic o p e a io n ed in o anal i o f nning ime. b. Wha i he be -ca e big O nning ime o f o algo i hm? E plain clea l , incl ding all bo o kkeeping needed o achie e hi be ca e. OL IO N A lgo i hm: Fo each o ng in he fi fo nd, add i o he o p li . pla li , do a linea Ba ic o pe a io n i a co mpa i o n be ea ch in he eco nd and if a ma ch i een a pai o f o ng . 1. In he o ca e, he e a e no co mmo n o ng . E e e o ng in he fi li ge co mpa ed i he e o ng in he eco nd, fo a o al o f * co mpa i o n , and he efo e, O( * ) 2. In he be ca e, he e i a ma im m n mbe o f co mmo n o ng , hich o ld be min( , ) (n mbe o f co mmo n o ng canno e ceed he leng h o f he malle li ). A l o , he lea n mbe o f ma che i made o find a ma ch. A gain, do ing a linea ea ch fo each o ng in he li again he o ng in he li , he fi o ng in he li ma che he fi o n g in h e li (1 co mpa o n), he eco nd o ng in he li ma che he eco nd o ng in he li (2 co mpa i o n ), and o o n. Thi gi e a o al o f: 123..mnmn +++.+i(,) hich i O(min( , )2) co mpa i o n , 4. E e ci e E 3.15, page 118 o f he e bo o k. The e i a high a ih hem. Fo in ance: Eis t: 1 Dsacs 5 itne: 2 3 The di ance f o m he ei 3 4 a 4 2 n mbe ed 0 o 5 8 - 1. Yo a e gi en a li o f he di ance be een 6 6 o f he high a o e i 1i 5 f o m e i 1 o 2i 3 and o o n. , , Yo ha e o de i e an algo i hm ha o ld calc la e he di ance be een an o e i . Imagine ha hi di ance f nc io n i called millio n o f ime b applica io n ha need hi info ma io n, o o need o make o algo i hm a fa a po ible. www.cs.rutgers.edu/courses/112/classes/ /set1_sol.html 2/3 12/27/11 CS112 Fall 2011: Problem Set 1 D e c ib e aa e e a g i h . Wh a i h e -ca e big O i g i e? M a e e i he a a i a d e a e he he a g i h . e aea he SOLUT ION A he f c i h ic h c a c a e h e d i a c e b e e e a ei i gig b e ca e d i i f i e , h e be a e hi be i c ea e a a a f di a ce f each e i f he a f he high a h ic h c a b e e d a e ca c a e h e e f fi di g di a ce be ee ei i c a i e. C ea i g he a a a e i ea i e (O( )) b i gi i a ec a i e (O(1)). Algorit hm for C reat ing t he Parit al Sum Ipt aryD o lnt n nu: ra , f egh , Otu:aryD o lnt n upt ra , f egh Satfo ei 1 tl nei,w cnfn tedsac btentesat tr rm xt , il xt e a id h itne ewe h tr o tehg wyadteei w aecnieigb adn tedsac bten f h ih a n h xt e r osdrn y dig h itne ewe sato hgwyt teei bfr tecretei (xml:dsac tr f iha o h xt eoe h urn xt Eape itne btensato hgwyt ei 0i cretei i ei 1 adte ewe tr f iha o xt f urn xt s xt ) n h dsac btenpeiu ei adcretei(ti 3frei 1. itne ewe rvos xt n urn xti s o xt ) W cncerydsrb i a e a lal ecie t s fr(=;i<n i+ { o i1 ; +) Di =Di1 +Di; [-] Ti cetsprilsm.S i yusatwt tefloig hs rae ata us o f o tr ih h olwn: 534286 tecnetdaryi h ovre ra s 581 1 2 2. 2428 Algorit hm for Finding t he dist ance bet een t o e it s Ipt aryDo lnt nei i ei j nu: ra f egh ,xt , xt . Otu:dsac btenei iadei j upt itne ewe xt n xt Dsac btenei iadjcnb esl cluae b Dj -Di. itne ewe xt n a e aiy acltd y The ba ic e a i a d i g i e f h i a g i h ca b e ca c a e d f diffe e c e e . O e f c e a i g h e a ia a a a da he f i gi fi d he di a ce be e e ei . The ba ic e a i f c ea i g he a i a a a i ca c a i g he di a ce f each e i he a f high a . I a e O(1) i e f each e i a d he e a e e i . The ef e, i a e O( ) i e ge a ia a a . I i he a e f a i ai . The ba ic e a i f fi di g he di a ce be ee e i af e e ha e he a ia a a i O(1) i e. I i he a e f he -ca e h e b e ca e . www.cs.rutgers.edu/courses/112/classes/ /set1_sol.html 3/3 ...
View Full Document

Ask a homework question - tutors are online