Determine a Theta bound on the running time

Assignment Help Theory of Computation
Reference no: EM132263659

Mathematical Expression and Reasoning for Computer Science Assignment -

Problem 1 - Printing multiples

Consider the following algorithm:

1 def print_multiples(n: int) -> None:

2 """Precondition: n > 0."""

3 for d in range(1, n + 1): # Loop 1

4 for multiple in range(0, n, d): # Loop 2 (also, see Python Note below)

5 print(multiple)

Python Note: range(0, n, d) consists of the multiples of d ≥ 0 and < n: 0, d, . . . , d(⌈n/d⌉ - 1).

(a) Find, with proof, a Theta bound for i=1n⌈n/i⌉ in terms of elementary functions (e.g., n, log n, 2n, etc.). You may not use any properties of Big-Oh/Omega/Theta; instead, only use their definitions, as well as the following Facts (clearly state where you use them):

i=1n 1/i ∈ Θ(log n) (Fact 1)

∀ x ∈ R, x ≤⌈x⌉ < x + 1 (Fact 2) 

(b) Using your answer to part (a), analyse the running time of print_multiples to determine a Theta bound on the running time.

(c) Now consider the following variation on the above algorithm.

1 def print_multiples2(n: int) -> None:

2 """Precondition: n > 0."""

3 for d in range(1, n + 1): # Loop 1

4 for multiple in range(0, n, d): # Loop 2

5 print(multiple)

6

7 if d % 5 == 0:

8 for i in range(d): # Loop 3

9 print(i)

Analyse the running time of print_multiples2 to determine a Theta bound on the running time. To simplify your analysis, you can focus on just counting the steps taken by Lines 5 and 9.

You may use your work from previous parts of this question, and the summation formula i=1n i = (n(n+1))/2.

Problem 2 - Varying running times, input families, and worst-case analysis

1 def alg(lst: List[int]) -> None:

2 n = len(lst)

3 i = 1

4 j = 1

5 while i < n: # Loop 1

6 if lst[i] % 2 == 0:

7 i = i + 1

8 j = j * 2

9 else:

10 i = i * 2

11 for k in range(j): # Loop 2

12 print(k)

To simplify your analyses for this question, ignore the costs of Lines 2-4 and Line 10. (Of course, you'll have to understand what these lines do to analyse the running time, but you don't need to count them as "steps.")

(a) Find, with proof, an input family for alg whose running time is Θ(2n).

(b) Find, with proof, an input family for alg whose running time is Θ(log n x 2√n), where the else branch of Loop 1 runs Θ(log n) times.

You can use the fact that log n - log(log n) ∈ Θ (log n) in your proof.

(c) Prove that the worst-case running time of alg is O(2n) (note that this matches the worst-case lower bound we can derive from part (a)).

Hint: unlike other examples we've looked at in class, there are no "early returns" here. Instead, use what you've learned about analysing loops with non-standard loop variable changes, and remember that you don't need exactly 2n steps, just an at most O(2n).

Problem 3 - Rearrangements, best-case analysis

We define the best-case running time of an algorithm func as follows:

BCfunc(n) = min{running time of executing func(x) |x ∈ In}

Note that this is analogous to the definition of worst-case running time, except we use min instead of max.

(a) Review the definitions of what it means for a function f : N → R≥0 to be an upper bound or lower bound on the worst-case running time of an algorithm.

Then, let In denote the set of all inputs of size n for rearrange, and write two symbolic definitions:

(i) What it means for a function f : N → R≥0 to be an upper bound on the best-case running time of an algorithm.

(ii) What it means for a function f : N → R≥0 to be a lower bound on the best-case running time of an algorithm.

Clearly state which definition is which in your answer? Your definitions should be very similar to the definitions we mentioned above from the Course Notes.

Hint: it is easier to first translate the simpler statements "M is an upper bound on the minimum of set S" and "M is a lower bound on the minimum of set S" to make sure you have the right idea.

(b) Consider the following Python function.

1 def rearrange(lst: List[int]) -> None:

2 for i in range(2, len(lst)): # Loop 1

3 if i % 2 == 0:

4 j = i - 2

5 while j >= 0 and lst[j+2] < lst[j]: # Loop 2

6 lst[j+2], lst[j] = lst[j], lst[j+2] # Swap lst[j] and lst[j+2]

7 j = j - 2

8 else:

9 j = i - 2

10 while j >= 0 and lst[j+2] > lst[j]: # Loop 3

11 lst[j+2], lst[j] = lst[j], lst[j+2] # Swap lst[j] and lst[j+2]

12 j = j - 2

Analyse the best-case running time of rearrange to find a Theta bound for it. Your analysis should consist of two parts, proving matching upper and lower bounds on the best-case running time separately, and then using them to conclude a Theta bound.

To simplify your analysis, you only need to count the total number of steps taken inside Loops 2 and 3, and not the other operations (e.g., Lines 4 and 9).

Problem 4 - An average-case analysis

Note: we'll introduce this type of analysis with an example similar to this problem on Monday, March 18. We recommend prioritizing the other problems on this problem set until then.

Consider the following algorithm.

1 def find_one_or_two(lst: List[int]) -> Optional[int]:

2 """Return the index of the first 1 or 2 in lst, or None if neither of them appear."""

3 for i in range(len(lst)):

4 if lst[i] == 1 or lst[i] == 2:

5 return i

6

7 return None

We consider the following inputs for this function: for every n ∈ N, we define the set In to be the set of lists of length n that are permutations of the numbers {1, 2, . . . , n}. We know that |In| = n! (where n! = n x (n - 1) x · · · x 2 x 1).

Note that if n ≥ 1, then every list in In contains a 1 or 2, and so the algorithm find_one_or_two will always return an index.

For this question, you can use your answer for each part in all subsequent parts.

(a) Let n ∈ N, and assume n ≥ 2. Let I, j ∈ N, and assume i ≠ j, and that both are between 0 and n - 1, inclusive.

Find a formula, in terms of n, i, and/or j, for the exact number of lists 1st in In where 1st[i] equals 1 and 1st[j] equals 2. Prove that your formula is correct.

(b) Let n ∈ N, and assume n ≥ 2. Let i ∈ N, and assume i is between 0 and n - 1, inclusive. Find a formula, in terms of n and/or i, for the exact number of lists 1st in In where 1st[i] equals 1, and 2 appears in 1st at an index greater than i.

(c) Let n ∈ N, and assume n ≥ 2. Find an exact expression for the average running time of find_one_or_two on In.

Count each iteration of the loop as just 1 step, and include the last partial iteration (the one that executes return i) in your count. Note that for this set of inputs, the loop will always return early, so the final return None will never execute.

You may use the following formulas in your calculation, valid for all m ∈ N:

i=1m i = (m(m+1))/2

i=1m i2 = (m(m+1)(2m+1))/6

Hint: you can use your work from part (b), but make sure in your analysis here to consider the cases where 2 appears before 1 in the list.

Attachment:- Assignment File.rar

Reference no: EM132263659

Questions Cloud

Count chromosomes and telomeres in each image : BB3802 Problem Solving and Data Analysis Assignment, Brunel University, London, UK. Count chromosomes and telomeres in each image
What role Stacy ethnicity play in her weight problems : Reflect: The Concept of Nutrition. What role, if any, does Stacy's ethnicity play in her weight problems? Why might Stacy's blood pressure be high
What is the probability of a 222rn atom decaying in lungs : What is the probability of a 222Rn atom decaying in our lungs? The atmospheric concentration of 222Rn may be assumed to be 1 pCi/L. In an average breath
Write dissertation on topic - Dog safety : Write 12500 words dissertation on topic - Dog safety: an investigation into the publics perception and understanding of dog temperament and aggressive behaviour
Determine a Theta bound on the running time : CSC165H1: Mathematical Expression and Reasoning for Computer Science Assignment - Problem Set, University of Toronto, Canada. Determine a Theta bound
Develop a paper to explores one of the sensorimotor system : Develop a paper which explores one of the sensorimotor systems (e.g., vision, touch, taste, smell, or attention.
What was the value gained from the experience : What is the Leader currently reading for their professional development? What lessons are he/she is gaining from the book/article?
What are some of the problems that poor writing causes : What are some of the problems that poor writing causes for businesses? Please do not copy your answer from the article.
Identify categories of interventions that may improve : Explain workplace design, causes of performance gaps, staffing requirements, and identify categories of interventions that may improve performance.

Reviews

len2263659

3/22/2019 5:04:04 AM

General instructions - Please read the following instructions carefully before starting the problem set. They contain important information about general problem set expectations, problem set submission instructions, and reminders of course policies. Your problem sets are graded on both correctness and clarity of communication. Solutions that are technically correct but poorly written will not receive full marks. Please read over your solutions carefully before submitting them. Solutions must be typeset electronically, and submitted as a PDF with the correct filename. Hand-written submissions will receive a grade of ZERO.

len2263659

3/22/2019 5:03:57 AM

Problem sets must be submitted online through MarkUs. If you haven't used MarkUs before, give yourself plenty of time to figure it out, and ask for help if you need it! If you are working with a partner, you must form a group on MarkUs, and make one submission per group. \I didn't know how to use MarkUs" is not a valid excuse for submitting late work. Your submitted file(s) should not be larger than 9MB. You might exceed this limit if you use a word processor like Microsoft Word to create a PDF; if it does, you should look into PDF compression tools to make your PDF smaller, although please make sure that your PDF is still legible before submitting! Submissions must be made before the due date on MarkUs. You may use grace tokens to extend the deadline; please see the Homework page for details on using grace tokens. The work you submit must be that of your group; you may not use or copy from the work of other groups, or external sources like websites or textbooks.

Write a Review

Theory of Computation Questions & Answers

  In this section of the final project you will focus on

in this section of the final project you will focus on location-related decisions taken by the company you have chosen

  Find cfgs for the languages

Find CFGs for the languages over the alphabet sigma = {a   b}:

  Prove the problem by contradiction

Let n > 1 be an integer. Prove by contradiction that if n is a perfect square, and then n + 3 cannot be a perfect square.

  Find language of nondeterministic finite-state automaton

Find a deterministic finite-state automaton that recognizes the same language as the nondeterministic finitestate automaton in Exercise.

  Write a vhdl module for a 6-bit accumulator with carry-in

Write a VHDL module for a 6-bit accumulator with carry-in (CI) and carry-out (CO). When Ad = o, the accumulator should hold its state. When Ad = 1, the accumulator should add the value of the data inputs D (plus CI) to the value already in the acc..

  Give both an fa and an re for l

In a string, a block is a substring in which all symbols are the same which can't be enlarged. For example, 0001100 have three blocks.

  Task 1 managing meetingswhat are symptoms of groupthink

task 1 managing meetingswhat are symptoms of groupthink and how can you assure groupthink will not become a problem in

  Construct a deterministic finite-state automaton

Construct a deterministic finite-state automaton that recognizes the set of all bit strings beginning with 01.

  Formulate the corresponding demand allocation problem

Extend the CPL model to the case of demand varying over the planning horizon. Assume that, once opened, a facility cannot be closed.

  Eliminate left recursion from the original grammar

Give a leftmost derivation for the string and implement the relational operators - operators would input a list of Val arguments and output a Val object.

  Design deterministic finite state transducers

Design deterministic finite state transducers that implement the subsequent context sensitive rules:

  Modify the syntax of a programming language

Sometimes it is necessary to modify the syntax of a programming language. This is done by changing the CFG that the language uses. What changes would have to be made to ac's CFG (Figure) to implement the following changes?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd