Write a recursive function definition for the function

Assignment Help Theory of Computation
Reference no: EM13866910

Assignment: Introduction to the Theory of Computation

Note that this is an assignment and can be submitted in groups.  In fact, it is highly encouraged.  This is long and designed for a group. But, keep in mind that if you divide the problems up and each person only does their share, you will be in big trouble during the midterm and final exams, when something similar comes up and you have no idea how to solve it. Do yourself a big favour and actively participate in the development of solutions to all problems.

Being aware of the fact that you have an exam in the middle your allotted time for this assignment, we subtracted 1 problem from the usual load (of 5 problems for each assignment). Still, we strongly encourage you not to postpone this to a point that you will stress yourself over finishing it in time for the deadline. Keep in mind that we cannot grant extensions. According to the rules of "arts and science", we cannot change the due date of an assignment unless it is put to a vote in all 3 classes, which is practically impossibility.

1. Suppose we are given n coins, all of which are identical, except one which is a counterfeit. Further, assume that the counterfeit coin has a different weight compared to the genuine coins. Consider the problem of determining which of the coins is the counterfeit, by using a balance scale. In a single weighing, we are allowed to place some coins on one side and some on the other, and compare the weights. There are 3 possible results: the scale balances, the left side is heavier, or the right side is heavier. We are interested in solving the problem using the fewest number of weighings (imagine you have to pay per weighing and you are trying to spend the least amount of money on this).

(a) For this part, assume that you know (as a fact) that the counterfeit coin is heavier than a genuine coin. Propose a divide and conquer algorithm that would find the counterfeit coin with the minimum possible number of weighings.

(b) Write a recurrence relation for the function C(n) that determines how many weighings your algorithm performs for an instance of size n (that is when the total number of coins is n), and then provide a closed form answer for the recurrence.

Note: we are not after the running time. we are after the number of times you need to use the scale, so we are not looking for an approximate answer (big-O) here. We are looking for an exact answer. But, keep in mind that this exact answer refers to the worst-case scenario for the number of weighings the algorithm requires. You don't have to prove your closed form, but show your work on how you got the answer.

(c)  Repeat part (a), but now with the assumption that you don't know whether the counterfeit coin is heavier or lighter than the genuine coins. Think carefully about your base case. It may be tempting to go with a simple variation of your answer from (a) for this. Resist the temptation, because there is a better algorithm.

(d)  Similar to part (b), write a recurrence relation for the function C(n) that determines how many weighings your algorithm from (c) performs for an instance of size n, and then provide a closed form answer for the recurrence.

For simplicity, assume n = 2k (is a power of 2) for parts (a) and (b) and assume n = 3k for parts (c) and (d). Note that this is also giving you a hint at what the algorithms should look like. Think about the hint. Present your algorithms in a simple pseudo code style (as in the style of the other algorithms given to you in this assignment, and also the algorithms you have in your course notes).

2. Consider the following pseudo code for a function Hop, where the input n ≥ 1 is an integer. We are interested in understanding what the function will print. As you can see there are 4 different print statements in the pseudo code and the code prints through these statements. For each of the strings "eeny", "meeny", "miny", and "moe" (hmmmm!), we would like to know how many times the string is printed if a call is made to Hop(n), for all n. So, again, we are interested in an exact number of printed statements and not the runtime or its asymptotic complexity. As a programmer, you may be interested in measuring things other than execution time about your program: for example, memory consumption.

There are several ways of doing this. You can solve this problem one string at a time, ignoring the rest of the print statements. That is a valid solution. Or, you can be a little smarter about it, and do less recurrence- solving type of work. Let's do that! For the purposes of this problem, you may assume that every n is a (non-negative) power of 2.

Hop(n) {

1       print("eeny")

2       if (n == 1) { return }

3       for (i = 1 to i n) {

4                print("meeny")

5                if (i is even) {

6                print("miny") 7            }

8                if (i == |n/3|) {

9                         Hop(n/2)

10                       print("moe")

11              } else if (i == |2n/3|) {

12                       Hop(n/2)

13              print("moe") 14                }

15     }

}

(a) Write a recursive function definition for the function E(n), where E(n) stands for the number of times "eeny" is printed when we call Hop(n).

(b) Give a closed-form for E(n). You don't have to prove anything, but show your work.  The closed-form answer cannot drop  from  the sky.

(c) Let O(n) stand for the number of times "moe" is printed when we call Hop(n). Provide and equality that defines O(n) based on E(n). Justify your answer.

(d) Write a recursive function definition for the function M (n), where M (n) stands for the number of times "meeny" is printed when we call Hop(n).

(e) Give a closed-form for M (n). You don't have to prove anything, but show your work. The closed-form answer cannot drop from the sky.

(f) Let I(n) stand for the number of times "miny" is printed when we call Hop(n). Provide an equality that defines I(n) based on M (n). Justify your answer.

This problem is much easier than it seems. If you feel a bit lost, then try running the code (carefully so that you don't miss anything) over a few small inputs (such as 1, 2, . . . ) so that you get a sense of how things are working, and have a few concrete numbers to test your answers on after you are done. Just be careful when you count things, and about what your base cases are in each case. Keep in mind that the point of doing (c) and (f) is not to compute these in the same manner (through solving a recurrence) as (a) and (d). But, to use the code structure to relate these two numbers together using simple expressions.

3. Consider an array A with integer elements. The following algorithm recursively finds and returns the smallest element in A[b . . . e].

Min(A, b, e)
1    If (b = e)
2    return A[b] 3    m = [(b + e)/2]
4    x = Min(A, b, m)
5    y = Min(A, m + 1, e)
6    If (x < y)
7    return x
8    else
9    return y

(a) Write the appropriate precondition and postcondition that specify the correctness of this function.

(b) Prove that the function is correct by showing that your specification from part (a) is inductive (which is not the same as asking for a complete induction proof of the correctness statement).

4. For two strings u, v, we say that v = uR  if string v is the reverse of the string u. Formally,

|u| = |v| ∧ ∀ 0 ≤ i < |u|, u[i] = v[|u| - 1 - i]

where |u| stands for the length of string u, and u[i] is referring to the character that is in the ith position of the string (assuming that the positions start from 0 and go to |u| - 1, similar to arrays).

For example, abcde = (edcba)R. Also, if u = abcde, then |u| = 5 and u[3] = d. Consider the algorithm below that reverses a string u ∈ Σ:

algorithm REV(u)
2    l := |u|
3    if l ≤ 1
4    return u
6    m := l div 2
7    v := REV(u[0 . . . m - 1])
8    w := REV(u[m . . . |u| - 1])
9    return wv

where u[i . . . j] is the substring of u from position i to position j (both inclusive). We also assume that strings are indexed from 0 to the length of the string. The goal is to prove that algorithm REV correctly reverses a string.

(a) Write pre and post conditions for REV .

(b) Prove that REV is correct by proving that your specification from (a) is inductive (which is not the same as asking for a complete induction proof of the correctness statement).

Hint: be careful with part (b). You may find yourself using a fact in your proof that needs a proof. You may want to prove that fact as a separate  lemma.

Note: the following problem is extra. You can skip it and only do the 5 problems above and get a full mark on your assignment. So, this is not officially part of your assignment 2. But, if you do it, any extra marks you gain will count towards your final grade. For example, if you get a full mark on assignment 2 and do B2 as well, then the extra 10 points will get you approximately an extra 1.36 mark towards your final course grade. Think of this as a way for you to do more work to compensate for some lost mark from somewhere else.

There is a catch. This problem will be marked harshly. You either have a perfect solution and get the full 10 points, or you make a mistake somewhere (no matter how small it is) and get nothing (i.e. zero).  This only applies to the marking of B1. Since this is a bonus problem, we will demand perfection. Also, bonus problems should not be discussed on Piazza or during the office hours or the tutorials. You should do this on your own (i.e. within your group).

(B2) Given a sorted array of distinct integers (i.e. no two different array cells hold the same value), we would like to find out whether there is an index i for which A[i] = i. Assume indices start from 0.

(a) Give a divide-and-conquer algorithm that runs in time O(log n). You do not need to justify the runtime for us. Just convince yourself that your algorithm is fast enough.

(b) Prove that your algorithm is correct by providing the relevant correctness specification for it and proving that that specification is inductive.

Reference no: EM13866910

Questions Cloud

Reactive power output of the generator : a) The reactive power output of the generator b) The generator internal voltage (delta). dc) An equation for the electrical power delivered by the generator versus power angle
How do you feel about this nlrb decision : Trying to Strike a Balance- In order to bargain for the health and safety of employees, the Oil, Chemical and Atomic Workers Union demanded that several employers disclose the generic names of chemical substances used or produced, as well as the m..
How and where products are sold : General information about company such as brief history, number of employees, products they make, sales volume, how and where products are sold. Who are their customers, etc
Compute the amounts of interest paid : The Staggs Company has prepared its 2010 statement of cash flows. In conjunction with this statement, it plans to disclose the interest and income taxes it paid during 2010. The following information is available from its 2010 income statement and be..
Write a recursive function definition for the function : Write a recursive function definition for the function E(n), where E(n) stands for the number of times "eeny" is printed when we call Hop(n).
What are minerals : What are minerals? Sketch different types of mineral crystal forms and list different types of rock forming minerals. Describe with examples the importance of mineral identification in engineering applications.
Paper - same sex marriages : Write a paper on given topic, Topic - SAME SEX MARRIAGES
What are the five main parts of political risk : 1. What are the five (5) main parts of political risk?   2. How might each affect international business activities? Provide samplecases for each of the parts that have occurred, or are occurring in the globalmarket today.
How incidents of behavioral infractions will be addressed : Discuss how incidents of behavioral infractions will be addressed. Recommend how the needs of students and teachers will be met as it relates to student and teacher freedom and safety

Reviews

Write a Review

Theory of Computation Questions & Answers

  Consider a logic function with three outputs

Consider a logic function with three outputs,  A ,  B , and  C , and three inputs,  D ,  E , and  F . The function is defined as follows:  A  is true if at least one input is true,  B  is true

  Satisfy the properties - reflexive and symmetric

For the relations below, explain why the relation does or does not satisfy each of the properties reflexive,symmetric, antisymmetric, and transitive.

  1 produce a report of up to 500 words on the topic talent

1. produce a report of up to 500 words on the topic talent planning in operation. nbspnbspnbspnbsp please ensure that

  Derive a contradiction

State your assumptions for a proof by contradiction - Derive a contradiction.

  Write the predicate logic

Write the predicate singleChild(Name) which finds the name of single children - For this problem single children means no other child has the same father and mother.

  Finite-state machine design

Create a finite-state machine design to turn your FPGA development board into a simple programmable music box.

  How does automated system enhance relevance of information

How does the automated system enhance the relevance of the information provided?

  1 discuss your assumptions and beliefs as a leader discuss

1. discuss your assumptions and beliefs as a leader. discuss how these have changed or evolved while studying business

  Recent research has shown that a job and a competitive

recent research has shown that a job and a competitive remuneration package are not sufficient for attracting competent

  Extend the ac scanner

A floatdcl can be represented as either f or float, allowing a more Java-like syntax for declarations - a intdcl can be represented as eitheri or int.

  Farmers friend for their customer support systems

Create the two main documents that model the current processes at Farmers Friend for their Customer Support Systems (CSS).

  Front end and back end processes of office automation

Discuss the difference between the front end and back-end processes of office automation? Provide some examples in your workplace or that you come into contact with?

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