quiz solutions

April 27, 2018 | Author: api-347983176 | Category: Time Complexity, Algorithms, Algorithms And Data Structures, Graph Theory, Combinatorics
Share Embed Donate


Short Description

Download quiz solutions...

Description

Week 1 Quiz Solutions 1 point 

In the code fragment below, first and last are integer values and composite(x)  is a function that returns true if x is not a prime number and false otherwise.

i := 0; j := 0; k := 0; for (m := last; m >= first; m := m-1){ k := k + m; if (composite(m)){ i := i + m;   }else{ j := j + m; } }

At the end of the loop: k < i+j

k = i+j

k > i+j

 The relationship between  i,

j

and k depends on the values of first and last

Correct answer: (b) k = i+j

In each iteration, k is incremented and exactly one of i or j is incremented. This is independent of the values of

first and last.

1 point 

How many times is the comparison i  >= int i = main(){ !"ile i = n = } }

n performed in the following program?

00, n = 110; (i >= >= n){ n){ i-1; n+1;

 45  46  4  !"

Correct answer: (c) 47

 After j iterations, i is 200-j, n is 110+j. 110+j. olve 200 - j ! 110 110 + j to "et j ! #$. Add one test each each for the initial iteration %j ! 0& and the final final exit chec' %j ! #(&. 1 point 

 #n algorithm has two phases. phases. The first phase, initiali$ation, ta%es time &'n( log n). The second phase, which is the main computation, ta%es time &'n *). +hat is the most accurate description of the compleity of the overall algorithm?

 &'n( log n)  &'n*)  &'n* log n)  &'n* - log n)

Correct answer: (b) O(n 3)

The least efficient phase is )%n * &. 1 point 

et f'n) / 56"n 4 - *650n * - *n-0" and g'n) / *n 4 - 05n(. +hich of the following is true?

 f'n) is &'g'n)), but g'n) is not &'f'n))  g'n) is &'f'n)), but f'n) is not &'g'n))  f'n) is not &'g'n)) and g'n) is not &'f'n))  f'n) is &'g'n)) and g'n) is &'f'n))

Correct answer: () !(n) is O("(n)) an "(n) is O(!(n))

oth functions are )%n # & and are hence of the same order. order. 1 point 

if T'n) / n( 1 n then

 T'n) / &'n ()  T'n) / &'n ( log n)  T'n) / &'n *)  2one of the above

Correct answer: (c) #(n) = O(n3)

 n is )%n& ut is not )%lo" n&, so )%n* & is the est option.

Week $ Quiz Solutions 1 point 

 #ssume that a 3 can process 0" operations per second. 7uppose you have to sort an array with 0" 6 elements. +hich of the following is true?

 7election sort will always ta%e more than (.5 hours while merge sort will always ta%e less than 0 second.  Insertion sort will always ta%e more than (.5 hours while 8uic%sort will always ta%e less than 0 second  Insertion sort could ta%e more than (.5 hours while merge sort will always ta%e less than 0 second.  7election sort could ta%e more than (.5 hours while 8uic%sort will always ta%e less than 0 second. Correct %nswer: (c) &nsertion sort coul take 'ore tan $* ours wile 'er"e sort will alwas take less tan 1 secon

)%n2  & is an upper ound for insertion insertion sort and selection sort, not a loer ound. ound. )%n lo" n& is an upper upper ound for mer"e sort, ut not for /uic'sort. /uic'sort. 1 point 

7uppose our aim is to sort an array in ascending order. +hich of the following statements is true?

 Input in ascending order is worst case for both selection sort and insertion sort.  Input in descending order is worst case for both selection sort and insertion sort.  Input in ascending order is worst case for insertion sort but not f or selection sort.  Input in descending order is worst case for selection sort but not f or insertion sort. Correct %nswer: (b) &n,ut in escenin" orer is worst case !or bot selection sort an insertion sort

 All inputs are orst case for selection sort. sort. escendin" order is orst case for insertion sort. sort. 1 point 

7uppose we want to sort an array in ascending order and we implement 8uic%sort so that we always choose the last element in the array as the pivot element. #ssume that the input is a permutation of 90, (, :, n;. + hich of the following would definitely be a worst case permutation of this input for this implementation of 8uic%sort?

 90, (,:, n; with all even numbers in random order followed by all odd numbers in random order.  90, (, . . . , n; in ascending order.  90,(,...,n; in some random order.  90, (, . . . , n; with all odd numbers in ascending order followed by all even numbers in random order. Correct %nswer: (b) -1. $. / . n0 in ascenin" orer

If the input sorted in ascendin" order, the pivot ill split the input at each sta"e into one se"ment of sie n1 and one empty se"ment. 1 point 

+hich of the following statements is not  true?  true?  'day,6"),'7hweta,0),'Ta >'day,6"),'7hweta,0 ),'Tari8,0),'7alma,(),'@ ri8,0),'7alma,(),'@rinda,5),'7unita rinda,5),'7unita,5)A ,5)A >'day,6"),'Tari8,0),'7hweta,0),'7 >'day,6"),'Ta ri8,0),'7hweta,0),'7alma,(),'7unita,5 alma,(),'7unita,5),'@rinda,5)A ),'@rinda,5)A Correct %nswer: (a) (5a.6).(#a (5a.6).(#ari2.71).(Sweta.71).(S ri2.71).(Sweta.71).(Sal'a.7$).(8rina.9*).(Sunita.9*) al'a.7$).(8rina.9*).(Sunita.9*)

%& 5ot sorted y second coordinate. %c& %6Tari/6,71& %6Tari/6,71& should come efore %6heta6,71&. %d& 6rinda6,8$& should come efore %6unita6,8$&

Week 3 Quiz Solutions 1 point 

 #n undirected graph graph D has 0( nodes and there is a path path from every verte to every other verte in D. 7uppose 7uppose v 0 and v( are two vertices in D. Then, what can we say about the shortest path from v0 to v(?

 The path has at most ( edges.  The path has at most 6* edges.  The path has at most 0( edges.  2one of the above. Correct %nswer: (c) #e ,at as at 'ost 1$7 e"es

3ith n vertices, a path cannot e lon"er than n1 ed"es ithout containin" a loop. 1 point 

 #n undirected graph graph D has !6 edges. The degree degree of a verte v, deg'v), is the number number of edges connected to v. 7uppose E / 9v0,v(,...,v*";. +hat is the value of deg'v 0)- deg'v () F F F - deg'v*")?

 !6

 0!(  (  *4 Correct %nswer: (b) 1;$

The sum of the de"rees counts each ed"e tice, once from each end, so the sum is 2 9 :( ! 1:2. 1 point 

+hich of the following is not true of depthGfirst search 'i-0A, rofit>i-(A - rice>i-0A G rice>iA G ()  ma'rofit>i-(A - rice>i-0A G rice>iA G (, rofit>i-*A - rice>i-(A G rice>iA G (, ... rofit>2A - rice>nG0A G rice>iA G (, rice>nA G rice>iA G ()  ma'rofit>i-0A, rofit>i-(A - rice>i-0A G rice>iA G (, rofit>i-*A - rice>i-(A G rice>iA G (, ... rofit>2A - rice>nG0A G rice>iA G ()  ma'rofit>i-0A, rofit>i-(A - rice>i-0A G rice>iA G (, rofit>i-*A - rice>i-(A G rice>iA G (, ... rofit>2A - rice>nG0A G rice>iA G (, rice>nA G rice>iA G () Correct answer: () 'aB(Aro!iti+1. Aro!iti+$ + Aricei+1 ? Aricei ? $. Aro!iti+3 + Aricei+$ ? Aricei ? $.  Aro!itE + Aricen?1 ? Aricei ? $. Aricen ? Aricei ? $)

Gonsider all possiilities and ta'e the maximum. 1 point 

+hat is the si$e of the memo table for this problem?

 2  2G0  2-0  2( Correct answer: (a) E

)ne entry per day, 5 days. 1 point 

+hat is a good order to compute the values of rofit>iA using dynamic programming?

 0A to rofit>2A  2A to rofit>0A  Lither from rofit>0A to rofit>2A or from rofit>2A to rofit>0A  2one of these Correct answer: (b) ro' Aro!itE to Aro!it1

rofit?iC depends on rofit?i+1C,...,rofit?5C and rofit?5C is ase case. 1 point 

How much time will dynamic programming ta%e to compute rofit>0A?

 &'2)

 &'2 log 2)  &'2()  &'2( log 2) Correct answer: (c) O(E$)

rofit?iC re/uires time )%5-i&, so overall time is 1+2+...5-1 1 point 

+hat is the value of rofit>0A in the eample given in the problem statement?

 6      ! Correct answer: (c) 9

uy on day 2, sell on day (, uy on day 7 sell on day 8 

Week 9 Quiz Solutions 1 point 

+hich of the following is not a linear constraint?

  R *y - (5  5 - 6y R 5  !  y - $  5y - *  ** Correct answer: (b) *B + 6B F *

In a non-linear constraint, e have to or more variales multiplied to"ether. 1 point 

+hen we model a graph problem using , mapping each path to a variable is not a good strategy becauseJ

 aths can be hard to compute.  +e have to be careful to avoid cycles.  # graph has eponentially many paths.  Ldges may be directed. Correct answer: (c) % "ra, as eB,onentiall 'an ,ats ,ats

Kraphs have exponentially many paths, so the H model ill have exponentially many variales. 1 point 

7uppose we compute the maimum sGt flow < in a networ%. Then, which of t he following is true of sGt cuts?

 
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF