0% found this document useful (0 votes)
65 views

Basic Algorithms

This book is or basic shortest path and other algorithims

Uploaded by

Nitesh Chhabria
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Basic Algorithms

This book is or basic shortest path and other algorithims

Uploaded by

Nitesh Chhabria
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ALGORITHMS-1

Analysis, design approaches, searching, sorting, hashing, trees 1. Consider the ollo!ing three clai"s 1. #n $ %&" ' (#n"&, !here % and " are constants ). )n $ 1 ' O#)n& *. )n $ 1 ' O#)n& +hich o these clai"s are correct, #a& 1 and ) #-& 1 and * #c& ) and *

#d& 1,) and *

). The ollo!ing are the starting and ending ti"es o acti.ities A, /, C, 0, 1, 2, G3 and H respecti.ely in chronological order4 5as -s cs ae ds ce es s -e de gs ee e hs ge he6. Here, 7s denotes the starting ti"e and 7e denotes the ending ti"e o acti.ity 7. +e need to sched8le the acti.ities in a set o roo"s a.aila-le to 8s. An acti.ity can -e sched8led in a roo" only i the roo" is reser.ed or the acti.ity or its entire d8ration. +hat is the "ini"8" n8"-er o roo"s re98ired, #a& * #-& : #c& ; #d& < *. S8ppose each set is represented as a lin%ed list !ith ele"ents in ar-itrary order. +hich o the operations a"ong 8nion, intersection, "e"-ership, cardinality !ill -e the slo!est, #a& 8nion only #-& intersection, "e"-ership #c& "e"-ership, cardinality #d& 8nion, intersection :. Let A=l, ... , n> -e an array storing a -it #1 or ?& at each location, and #"& is a 8nction !hose ti"e co"ple7ity is (#"&. Consider the ollo!ing progra" rag"ent !ritten in a C li%e lang8age4 co8nter ' ?3 or #i'l3 i@n3 i$$& Ai #A=i> ' ' 1& co8nter $$3 else A #co8nter&3 co8nter ' ?3B B The co"ple7ity o this progra" rag"ent is #a& (#n)& #-& C#n log n& and O#n)& #c& (#n& #d& o#n& ;. The !orst case r8nning ti"e to search or an ele"ent in a -alanced -inary search tree !ith n)n ele"ents is #a& (#n log n& #-& (#n)n& #c& (#n& #d& (#log n&

<. Let +#n& and A#n& denote respecti.ely, the !orst case and a.erage case r8nning ti"e o an algorith" e7ec8ted on an inp8t o siDe n. +hich o the ollo!ing is AL+AES TRF1, #a& A#n& ' C#+#n&& #-& A#n& ' (#+#n&& #c& A#n& ' O#+#n&& #d& A#n& ' o#+#n&&

G. A list o n strings, each o length n, is sorted into le7icographic order 8sing the "erge-sort algorith". The !orst case r8nning ti"e o this co"p8tation is #a& O#n log n& #-& O#n) log n& #c& O#n) $ log n& #d& O#n)&

H. In the ollo!ing C 8nction, let n I ". int gcd #n, "&

A i #n J " '' ?B ret8rn "3 n ' n J "3 ret8rn gcd #", n&3 B Ho! "any rec8rsi.e calls are "ade -y this 8nction, #a& (#log n& #-& C#n& #c& (#log log n&

#d& (#Kn&

L. T!o alternati.e pac%ages A and / are a.aila-le or processing a data-ase ha.ing 1?% records. Mac%age A re98ires ?.???1 n) ti"e 8nits and pac%age / re98ires 1?n log1?n ti"e 8nits to process n records. +hat is the s"allest .al8e o % or !hich pac%age / !ill -e pre erred o.er A, #a& 1) #-& 1? #c& < #d& ; 1?. A s8--se98ence o a gi.en se98ence is N8st the gi.en se98ence !ith so"e ele"ents #possi-ly none or all& le t o8t. +e are gi.en t!o se98ences O="> and E=n> o lengths " and n, respecti.ely, !ith inde7es o O and E starting ro" ?. +e !ish to ind the length o the longest co""on s8--se98ence #LCS& o O="> and E=n> as l#", n&, !here an inco"plete rec8rsi.e de inition or the 8nction l#i, N& to co"p8te the length o the LCS o O="> and E=n> is gi.en -elo!4 l#i, N& ' ?, i either i' ? or N ' ? ' e7pr1, i i, N P ? and O=i - 1> ' E =N -1> ' e7pr), i i,N P ? and O=i - 1> Q E=N -1> +hich one o the ollo!ing options is correct, #a& e7pr1 R l#i-1, N& $ 1 #-& e7pr1 R l#i, N -1& #c& e7pr) R "a7 #l#i-1, N&, l#i, N-1&& #d& e7pr) R "a7 #l#i-1, N-1&, l#i, N&& 11. The !eight o a se98ence a? al ... an-1 o real n8"-ers is de ined as a?$a1S)$... an-1S)n-1. A s8-se98ence o a se98ence is o-tained -y deleting so"e ele"ents ro" the se98ence, %eeping the order o the re"aining ele"ents the sa"e. Let O denote the "a7i"8" possi-le !eight o a s8-se98ence o a? al ... an-1 and E the "a7i"8" possi-le !eight o a s8-se98ence o a1 a) ... an-1. Then O is e98al to4 #a& "a7 #E, a? $ E& #-& "a7 #E, a? $ ES)& #c& "a7 #E, a? $ )E& #d& a? $ ES) 1). An algorith" to ind the length o the longest "onotonically increasing se98ence o n8"-ers in an array A=?4 n-1> is gi.en -elo!. Let Li denote the length o the longest "onotonically increasing se98ence starting at inde7 i in the array InitialiDe Ln-1 ' 1. 2or all i s8ch that ? T i T n-)

2inally the length o the longest "onotonically increasing se98ence is "a7#L?, Ll, U, Ln$1&. +hich o the ollo!ing state"ents is TRF1, #a& The algorith" 8ses dyna"ic progra""ing paradig" #-& The algorith" has a linear co"ple7ity and 8ses -ranch and -o8nd paradig" #c& The algorith" has a non-linear polyno"ial co"ple7ity and 8ses -ranch and -o8nd paradig" #d& The algorith" 8ses di.ide and con98er paradig" 1*. 2o8r "atrices Ml, M), M* and M: o di"ensions p 7 9, 9 7 r, r 7 s and s 7 t respecti.ely can

-e "8ltiplied in se.eral !ays !ith di erent n8"-er o total scalar "8ltiplications. 2or e7a"ple !hen "8ltiplied as #M1 7 M)& 7 #M* 7 M:&, the total n8"-er o scalar "8ltiplications is p9r $ rst $ prt. +hen "8ltiplied as ##M1 7 M)& 7 M*& 7 M:, the total n8"-er o sca1ar "8ltiplications is p9r $ prs $ pst. I p ' 1?, 9 ' 1??, r ' )?, s ' ; and t ' H?, then the "ini"8" n8"-er o scalar "8ltiplications needed is #a& ):H??? #-& ::??? #c& 1L??? #d& );??? 1:. The 8s8al (#n)& i"ple"entation o Insertion Sort to sort an array 8ses linear search to identi y the position !here an ele"ent is to -e inserted into the already sorted part o the array. I instead, !e 8se -inary search to identi y the position, the !orst case r8nning ti"e !ill #a& re"ain (#n)& #-& -eco"e (#n #log n&)& #c& -eco"e (#n log n& #d& -eco"e (#n& 1;. Ho! "any distinct -inary search trees can -e created o8t o : distinct %eys, #a& ; #-& 1: #c& ): #d& *; 1<. In a co"plete %-ary tree, e.ery internal node has e7actly % children. The n8"-er o lea.es in s8ch a tree !ith n internal nodes is #a& n % #-& #n V 1&% $ l #c& n #% V 1& $ 1 #d& n #% V 1& 1G. S8ppose there are log n sorted lists o nSlog n ele"ents each. The ti"e co"ple7ity o prod8cing a sorted list o all these ele"ents is4 #Hint4 Fse a heap data str8ct8re& #a& O#n log log n& #-& (#n log n& #c& C#n log n& #d& C#n*S)& 1H. The "edian o n ele"ents can -e o8nd in O#n& ti"e. +hich one o the ollo!ing is correct a-o8t the co"ple7ity o 98ic% sort, in !hich "edian is selected as pi.ot, #a& (#n& #-& (#n log n& #c& (#n)& #d& (#n*& 1L. The "a7i"8" n8"-er o -inary trees that can -e or"ed !ith three 8nla-eled nodes is #a& 1 #-& ; #c& : #d& * )?. The %eys 1), 1H, 1*, ), *, )*, ; and 1; are inserted into an initially e"pty hash ta-le o length 1? 8sing open addressing !ith hash 8nction h#%& ' % "od 1? and linear pro-ing. +hat is the res8ltant hash ta-le,

#a&

#-&

#c&

#d&

You might also like