memoization

Why does memoization not improve the runtime of Merge Sort?

允我心安 提交于 2019-12-05 17:30:36
Why does memoization not improve the runtime of Merge Sort? I have this question from Assignment task. But as far as I know, Merge Sort uses divide and conquer approach (no overlapping subproblems) but Memoization is based on dynamic programing (with overlapping subproblem). I know the runtime of Merge Sort is O(nlogn) . I even searched on web search engines and there is no result for this question. Is this question wrong? If it sounds wrong but why would a professor give a wrong question in assignment? If the question is not wrong or my understanding about the question, Merge Sort and

What would be the time complexity of counting the number of all structurally different binary trees?

我的梦境 提交于 2019-12-05 13:02:04
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key values 1...numKeys, how many structurally unique binary search trees are possible that store those keys? Strategy: consider that each value could be the root. Recursively find the size of the left and right subtrees. */ public static int countTrees(int numKeys) { if (numKeys <=1) { return(1); } else { // there will be one value at the root, with whatever remains // on the left and right each forming their own subtrees. // Iterate through all the values that

Is there a convention for memoization in a method call?

你说的曾经没有我的故事 提交于 2019-12-05 11:39:41
问题 I want to avoid reevaluation of a value in method call. Untill now, I was doing this: def some_method @some_method ||= begin # lot's of code end end But it ends up quite ugly. In some code, I saw something like the following: def some_method @some_method ||= some_method! end private def some_method! # lot's of code end I don't like the bang ( ! ) at the end, so I came up with this: def some_method @some_method ||= _some_method end private def _some_method # lot's of code end Is prepending

Caching and pre-fetching expiring promises in Javascript

柔情痞子 提交于 2019-12-05 05:00:39
Promises are my preferred way of managing my asynchronous code in Javascript. Memoize (memoizee on npm) is a Javascript library for easily caching & pre-fetching results of functions. Ideally I want to combine the best of both, and have the ability to "expire" a Promise and pre-fetch a new Promise result (when the cache is touched and near to expiring). Memoize can do this, but it wasn't built with Promises in mind. (I understand that Promises have a built-in "forever-cache" as is their nature, but forever is too long for my application) My best attempt to do this so far is as follows (node.js

A thread-safe memoize decorator

末鹿安然 提交于 2019-12-05 01:30:10
问题 I'm trying to make a memoize decorator that works with multiple threads. I understood that I need to use the cache as a shared object between the threads, and acquire/lock the shared object. I'm of course launching the threads: for i in range(5): thread = threading.Thread(target=self.worker, args=(self.call_queue,)) thread.daemon = True thread.start() where worker is: def worker(self, call): func, args, kwargs = call.get() self.returns.put(func(*args, **kwargs)) call.task_done() The problem

Using rails presenters - memoizable getting deprecated in 3.1 - use ||= instead?

ぃ、小莉子 提交于 2019-12-04 22:56:58
问题 Issue: To avoid creating multiple objects or multiple queries when possible. I am using Presenters with rails as a Best Practice. I am following advice that says that it would be good to use "extend ActiveSupport.Memoizable" (and then memoize :method(s) to use them) over setting up items with @the_record = record ||= @record style because of a couple of issues - false or nil not getting stored so the query gets called again and also that memoizable uses the cache better (i.e. uses it!).

Optimization of Function Calls in Haskell

自古美人都是妖i 提交于 2019-12-04 22:49:54
Not sure what exactly to google for this question, so I'll post it directly to SO: Variables in Haskell are immutable Pure functions should result in same values for same arguments From these two points it's possible to deduce that if you call somePureFunc somevar1 somevar2 in your code twice, it only makes sense to compute the value during the first call. The resulting value can be stored in some sort of a giant hash table (or something like that) and looked up during subsequent calls to the function. I have two questions: Does GHC actually do this kind of optimization? If it does, what is

Broken Calculator

て烟熏妆下的殇ゞ 提交于 2019-12-04 21:54:07
Problem Statement: There is a broken calculator. Only a few of the digits [ 0 to 9 ] and operators [ +, -, *, / ] are working. A req no. needs to be formed using the working digits and the operators. Each press on the keyboard is called an operation. = operator is always working and is used when the req no. is formed using operators. -1 needs to be printed in case the req no. cannot be formed using the digits and the operators provided OR exceeds the max no. of operations allowed. At no point in time during the calculation of the result, the no. should become negative or exceed 999 [ 0 <=

Is there an automatic way to memoise global polymorphic values in Haskell?

主宰稳场 提交于 2019-12-04 18:21:10
问题 Polymorphic "constants", like 5 :: Num a => a , aren't really constants but functions of a dictionary argument. Hence, if you define primes :: Num n => [n] primes = ... Bad example of course, there's no good reason here to have it polymorphic... what I'm really interested is if you try to globally memoise a nontrivial polymorphic function, with e.g. memo-trie s. then this sequence won't be shared between calls from different sites, which isn't nice in terms of performance. (Isn't this the

How to add fields that only cache something to ADT?

旧时模样 提交于 2019-12-04 16:13:09
问题 Often I'm in the need of adding fields to an ADT that only memoize some redundant information. But I haven't figured out completely how to do it nicely and efficiently. The best way to show the problem is to make an example. Suppose we're working with untyped lambda terms: type VSym = String data Lambda = Var VSym | App Lambda Lambda | Abs VSym Lambda And from time to time we need to compute the set of free variables of a term: fv :: Lambda -> Set VSym fv (Var v) = Set.singleton v fv (App s t