diff --git a/Recursion and Backtracking/1.md b/Recursion and Backtracking/Recursion_new.md similarity index 85% rename from Recursion and Backtracking/1.md rename to Recursion and Backtracking/Recursion_new.md index a43969c..ada3db1 100644 --- a/Recursion and Backtracking/1.md +++ b/Recursion and Backtracking/Recursion_new.md @@ -1,9 +1,10 @@ + Recursion ---------- Recursion - process of function calling itself directly or indirectly. - _Steps involved:_ + __Steps involved:__ - Base case - Self Work - Recursive Calls @@ -34,9 +35,9 @@ def pow(n, k): if k == 0: return 1 return n*pow(n, k - 1) ``` -_Time Complexity_: $O(n)$ +__Time Complexity__: $O(n)$ -_Optimised solution:_ +__Optimised solution:__ ```python def pow(n, k): if k == 0: return 1 @@ -53,7 +54,7 @@ To allow reuse of answers. -_Time Complexity_ (assuming all multiplications are O(1))? $O(\log_2 k)$ +__Time Complexity__ (assuming all multiplications are O(1))? $O(\log_2 k)$ Break it into 3 parts? k//3 and take care of mod1 and mod2. @@ -89,11 +90,10 @@ def subsets(A, i, aux): - How many leaf nodes? $2^n$ - one for each subset How many total nodes? $2^{n+1} - 1$ -Complexity? $O(2^n)$ +__Time Complexity__: $O(2^n)$ Subsets using Iteration ----------------------- @@ -149,6 +149,12 @@ For Array [0,1,2,3,4] Subsets in Lexicographical order, But don't print when going left - because already printed in parent. + + + + ```python def subsets(A, i, aux, p): if p: print(aux) @@ -158,8 +164,8 @@ def subsets(A, i, aux, p): no_take = subsets(A, i+1, aux, False) ``` -time: $O(2^n)$ -Space: $O(n^2)$, because we're creating new aux arrays. +__Time Complexity__: $O(2^n)$ +__Space Complexity__: $O(n^2)$, because we're creating new aux arrays. -- -- Number of Subsets with a given Sum @@ -173,6 +179,9 @@ The subsetSum problem can be divided into two subproblems. - Include the current element in the sum and recur (i = i + 1) for the rest of the array - Exclude the current element from the sum and recur (i = i + 1) for the rest of the array. + + ```python def subsetSum(A,N,cur_sum, i, target): if i == N: @@ -191,6 +200,8 @@ For eg, target = 6 1, 2, 3 is good, but 1, 2, 3, -1, 1 is also good. +__Time Complexity__: $O(2^n)$ +__Space Complexity__: $O(n)$ Number of Subsets with a given Sum (Repetition Allowed) --------------- @@ -200,7 +211,7 @@ Number of Subsets with a given Sum (Repetition Allowed) The subsetSum2 problem can be divided into two subproblems. - Include the current element in the sum and recur for the rest of the array. Here the value of i is not incremented to incorporate the condition of including multiple occurances of a element. - Exclude the current element from the sum and recur (i = i + 1) for the rest of the array. - +![IMG_0040](https://user-images.githubusercontent.com/35702912/66470200-c27db600-eaa6-11e9-8744-ca572d6000e1.jpg) ```python def subsetSum2(A,N,cur_sum, i, target): if i == N: @@ -214,6 +225,8 @@ def subsetSum2(A,N,cur_sum, i, target): no_take = subsetSum2(A,N,cur_sum, i+1, target) return take + no_take ``` +__Time Complexity__ : $O(2 ** (Target/MinElement))$ +__Space Complexity__: $O(Target/Min Element)$