catalan

Calculating the complexity of algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses

我的梦境 提交于 2021-02-11 07:50:48
问题 I would like your opinion on the time and space complexity of this algorithm I implemented (in Python) to calculate the complexity of algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses (see all valid combinations of n-pair of parenthesis) def find_par_n(n): s = set(["()"]) for i in range(2, n + 1): set_to_add = set() for str_ in s: set_temp = set() ana = set() for j in range(len(str_) + 1): str_c = str_[0:j] + '(' + str_[j:] if str_c in ana:

all possible numerical expressions

我与影子孤独终老i 提交于 2020-02-29 07:26:47
问题 one totally useless question: I bought a numerical game, it's made of two black dice plus 5 coloured ones. the two black ones form a 2 digits number, ranging from 11 to 66, the other 5 are the numbers you can use, combining them in all possible numerical expressions with the task to obtain the target number. for example, black 40 + 2: target 42 coloured 5 3 6 2 4, you can obtain the target by 5 3 + 6 * 4 2 + - (using RPN because it avoids brackets). now I'd like to use my pocket computer to

Code in Prolog generate all structurally distinct full binary trees with n node

China☆狼群 提交于 2020-01-06 08:07:14
问题 generate all structurally distinct full binary trees with n leaves in Prolog. The problem is given the number of leaves, output all distinct full binary trees. 'Full' here means any internal node must have two children, left and right. 回答1: To build all the trees through backtracking: full_tree(Leaves, [LTree, RTree]):- Leaves > 1, TLeaves is Leaves-1, between(1, TLeaves, LLeaves), RLeaves is Leaves-LLeaves, full_tree(LLeaves, LTree), full_tree(RLeaves, RTree). full_tree(1, '.'). This

Recursion for Catalan number to Memoized

好久不见. 提交于 2020-01-04 05:40:09
问题 I have been asked to write a recursive function that will calculate the Catalan number of monotonic lattice paths along the edges of a grid with n × n square cells, which do not pass above the diagonal (pic) I was not allowed to use for-loops, only recursive calls... This is what I did: public long C(int n) { if (n == 1) return 0; return C(n, 0, 0); } private long C(int n, int i, int j) { // CAN MOVE UP & RIGHT if (j - i > 0 && j + 1 <= n) return paths(n, i + 1, j) + paths(n, i, j + 1); //

Recurrence Approach : How can we generate all possibilities on braces?

心已入冬 提交于 2019-12-30 03:41:27
问题 How can we generate all possibilities on braces ? N value has given to us and we have to generate all possibilities. Examples: 1) if N == 1, then only one possibility () . 2) if N==2, then possibilities are (()), ()() 3) if N==3, then possibilities are ((())), (())(),()()(), ()(()) ... Note: left and right braces should match. I mean )( is INVALID for the N==1 Can we solve this problem by using recurrence approach ? 回答1: From wikipedia - A Dyck word is a string consisting of n X's and n Y's

Enumerate all full (labeled) binary tree

99封情书 提交于 2019-12-29 06:59:09
问题 I'm searching a practical algorithm for enumerating all full labeled binary tree. A full binary tree is a tree where all internal nodes has a degree 3, the leaves has degree 1 and the root has a degree 2. A labeled tree is a tree where all leaves has a unique label. Example: * |\ | \ * * /| |\ / | | \ T C D F 回答1: From comments, it is clear that the question is to enumerate rooted unordered labelled full binary trees. As explained in this paper, the number of such trees with n labels is (2n-3

What are the total number of possible ordered trees with N nodes?

强颜欢笑 提交于 2019-12-25 05:27:05
问题 For example for N=3, we can find easily by listing them all, but when asked for any arbitrary N value I am facing problem. 回答1: If you are looking at binary trees then, as mcdowella said, Choose(2n,n)/(n+1) (Catalan number) is the answer. If you are looking at arbitrary trees then it is probably n. n^(n-2) = n^(n-1), but I am not totally sure. Prufer's algo tells us that there are n^(n-2) labeled trees and any of the nodes can be made a root, thus we get the number n^(n-1). 回答2: This is

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

十年热恋 提交于 2019-12-22 08:08:31
问题 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

calculating catalan numbers using memoization

馋奶兔 提交于 2019-12-12 06:57:44
问题 I am tring to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change? def catalan_mem(n, memo = None): if n==0: return 1 if memo == None: memo = {} b=0 if n not in memo: for i in range (n): b+=((catalan_mem(i),memo)[0])*((catalan_mem(n-1-i),memo)[0]) memo[n]=b return memo[n] thank you! 回答1: If you call catalan_mem on an n in memo then you replace memo[n] with 0. This is probably not what you intend. Without parsing the logic further,

Printing mountain ranges algorithm

风格不统一 提交于 2019-12-12 03:56:21
问题 /\ / \ / \ /\/\ / \ /\ /\/ \ /\ / \/\ /\/\/\ for n=3 pairs of ups and downs I have 5 possible way to draw these mountains.(I should never go below the x=0 axis). I have the following long javascript code which works fine when I print these outputs in the console of the browser.However when I try to output them as html the ups and downs are not correctly aligned. Here is my Code: <html> <script> F=n=> { m = n+n outer: for (i=1; i < 1<<m; i+=2) { o=[] l=0; p=1; for (j = 1; j <1<<m; j+=j,p++) {