complexity-theory

Time-complexity of recursive algorithm for calculating binomial coefficient

放肆的年华 提交于 2019-12-06 04:14:20
问题 I'm studying about algorithm complexity analysis. I have problem with unconformity or C(n, k) . int C(int n, int k){ if(n==k || k==0) return 1; return C(n-1, k) + C(n-1, k-1); } How can I determine its execution complexity or T(n) ? 回答1: The recurrence you are looking for is T(n,k) = T(n-1,k) + T(n-1,k-1) + O(1) with T(n,n) = T(n,0) = O(1) Obviously n is decreased by one every step. If we ignore (just for the moment) that there is a parameter k, basically the number of calls doubles every

search for multiple strings

霸气de小男生 提交于 2019-12-06 02:49:11
问题 I know of efficient ways to look for one string in a file (kmp), or various strings in a file (trie) But, for years now, I've been wondering if there is a way (and ocasionally thinking it impossible) to search multiple files for multiple strings Say I have a million files, and I want to answer queries like "find files that have the strings "banana", "motorboat" and "the white fox"". What would be an efficient algorithm ? Is there one ? Of course, it is possible to do such a search in linear

need help designing for search algorithm in a more efficient way

橙三吉。 提交于 2019-12-06 00:43:47
问题 I have a problem that involves biology area. Right now I have 4 VERY LARGE files(each with 0.1 billion lines), but the structure is rather simple, each line of these files has only 2 fields, both stands for a type of gene. My goal is: design an efficient algorithm that can achieves the following: Find a circle within the contents of these 4 files. The circle is defined as: field #1 in a line in file 1 == field #1 in a line in file 2 and field #2 in a line in file 2 == field #1 in a line in

What is the runtime complexity of Python's deepcopy()?

此生再无相见时 提交于 2019-12-05 22:44:58
I'm trying to improve the speed of an algorithm and, after looking at which operations are being called, I'm having difficulty pinning down exactly what's slowing things up. I'm wondering if Python's deepcopy() could possibly be the culprit or if I should look a little further into my own code. Looking at the code (you can too), it goes through every object in the tree of referenced objects (e.g. dict's keys and values, object member variables, ...) and does two things for them: see if it's already been copied, by looking it in id-indexed memo dict copy of the object if not The second one is O

How to solve the recursive complexity T(n) = T(n/4)+T(3n/4)+cn

旧巷老猫 提交于 2019-12-05 22:12:11
I am solving this recurrence using a recursion tree. The total cost of each level is n, and the depth of the tree is between log (n) base 4 and log (n) base 4/3 . Intuitively, I expect the solution to be at most the number of levels times the cost at each level. O(cn log (n) base 4/3) = O(n log n) . I was wondering if my approach towards the problem, and my solution is correct? Think of it this way: for the first log 4 n layers of the recursion tree, the sum of the work across those layers will be cn, because if you sum up the total sizes of all the subproblems, it should total n so the total

Can i check if subsequence faster then O(n*n)

↘锁芯ラ 提交于 2019-12-05 20:15:37
So my question is in topic's name. Does exists an algorithm that checks if B is subsequence of A faster, than O(N^2), for example O(NlogN) or simply O(N)? Only way found is simple brut-force for(int i = 0; i < a.Length - b.Length; i++) { if (IsSubsequence(a,b,i)) return i; } return -1; Here's a recursive characterization of David Eisenstat's algorithm. (Note that this algorithm is tail recursive and can therefore be written as a loop; I describe it as recursive because doing so is a nice way to understand the algorithm.) Define a sequence as either empty, or an item followed by a sequence.

How to find all brotherhood strings?

可紊 提交于 2019-12-05 19:46:40
I have a string, and another text file which contains a list of strings. We call 2 strings "brotherhood strings" when they're exactly the same after sorting alphabetically. For example, "abc" and "cba" will be sorted into "abc" and "abc", so the original two are brotherhood. But "abc" and "aaa" are not. So, is there an efficient way to pick out all brotherhood strings from the text file, according to the one string provided? For example, we have "abc" and a text file which writes like this: abc cba acb lalala then "abc" , "cba" , "acb" are the answers. Of course, "sort & compare" is a nice try

Fast algorithm for counting the number of acyclic paths on a directed graph

你。 提交于 2019-12-05 19:15:40
问题 In short, I need a fast algorithm to count how many acyclic paths are there in a simple directed graph. By simple graph I mean one without self loops or multiple edges. A path can start from any node and must end on a node that has no outgoing edges. A path is acyclic if no edge occurs twice in it. My graphs (empirical datasets) have only between 20-160 nodes, however, some of them have many cycles in them, therefore there will be a very large number of paths, and my naive approach is simply

why O(1) != O(log(n)) ? for n=[integer, long, …]

爷,独闯天下 提交于 2019-12-05 18:07:34
for example, say n = Integer.MAX_VALUE or 2^123 then O(log(n)) = 32 and 123 so a small integer. isn't it O(1) ? what is the difference ? I think, the reason is O(1) is constant but O(log(n)) not. Any other ideas ? If n is bounded above, then complexity classes involving n make no sense. There is no such thing as "in the limit as 2^123 approaches infinity", except in the old joke that "a pentagon approximates a circle, for sufficiently large values of 5". Generally, when analysing the complexity of code, we pretend that the input size isn't bounded above by the resource limits of the machine,

What is the worst case for KMP string search algorithm? [closed]

大城市里の小女人 提交于 2019-12-05 17:33:29
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . Closed 6 years ago . Can anyone suggest me a worst case "text string - pattern pair" for testing a KMP algorithm implementation? I would say a pattern like xx........x | n times | and a string like xxx.........xyx...........xy.... | n-1 times | | n-1 times | would be one of the worst cases, but it's still O(m+n) You can find anything