mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 13:06:29 +00:00
Fix: Updated notes
This commit is contained in:
parent
8687e577a9
commit
f0c3fc406a
@ -1,9 +1,10 @@
|
|||||||
|
|
||||||
Recursion
|
Recursion
|
||||||
----------
|
----------
|
||||||
Recursion - process of function calling itself
|
Recursion - process of function calling itself
|
||||||
directly or indirectly.
|
directly or indirectly.
|
||||||
|
|
||||||
_Steps involved:_
|
__Steps involved:__
|
||||||
- Base case
|
- Base case
|
||||||
- Self Work
|
- Self Work
|
||||||
- Recursive Calls
|
- Recursive Calls
|
||||||
@ -34,9 +35,9 @@ def pow(n, k):
|
|||||||
if k == 0: return 1
|
if k == 0: return 1
|
||||||
return n*pow(n, k - 1)
|
return n*pow(n, k - 1)
|
||||||
```
|
```
|
||||||
_Time Complexity_: $O(n)$
|
__Time Complexity__: $O(n)$
|
||||||
|
|
||||||
_Optimised solution:_
|
__Optimised solution:__
|
||||||
```python
|
```python
|
||||||
def pow(n, k):
|
def pow(n, k):
|
||||||
if k == 0: return 1
|
if k == 0: return 1
|
||||||
@ -53,7 +54,7 @@ To allow reuse of answers.
|
|||||||
|
|
||||||
<img src="https://user-images.githubusercontent.com/35702912/66316190-d30e1f00-e934-11e9-8089-85c6dc69baa7.jpg" data-canonical-src="https://user-images.githubusercontent.com/35702912/66316190-d30e1f00-e934-11e9-8089-85c6dc69baa7.jpg" width="400" />
|
<img src="https://user-images.githubusercontent.com/35702912/66316190-d30e1f00-e934-11e9-8089-85c6dc69baa7.jpg" data-canonical-src="https://user-images.githubusercontent.com/35702912/66316190-d30e1f00-e934-11e9-8089-85c6dc69baa7.jpg" width="400" />
|
||||||
|
|
||||||
_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.
|
Break it into 3 parts? k//3 and take care of mod1 and mod2.
|
||||||
@ -89,11 +90,10 @@ def subsets(A, i, aux):
|
|||||||
<img src="https://user-images.githubusercontent.com/35702912/66323471-7a914e80-e941-11e9-84a9-11a333ac4f77.jpg" width="400"
|
<img src="https://user-images.githubusercontent.com/35702912/66323471-7a914e80-e941-11e9-84a9-11a333ac4f77.jpg" width="400"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
How many leaf nodes? $2^n$ - one for each subset
|
How many leaf nodes? $2^n$ - one for each subset
|
||||||
How many total nodes? $2^{n+1} - 1$
|
How many total nodes? $2^{n+1} - 1$
|
||||||
|
|
||||||
Complexity? $O(2^n)$
|
__Time Complexity__: $O(2^n)$
|
||||||
|
|
||||||
Subsets using Iteration
|
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.
|
But don't print when going left - because already printed in parent.
|
||||||
|
|
||||||
|
|
||||||
|
<img src="https://user-images.githubusercontent.com/35702912/66468106-3d44d200-eaa3-11e9-96e7-c6a050be1219.jpg" width="400"
|
||||||
|
/>
|
||||||
|
|
||||||
|
|
||||||
|
<img src="https://user-images.githubusercontent.com/35702912/66468119-42098600-eaa3-11e9-8f24-237be2a91d12.jpg" width="400"
|
||||||
|
/>
|
||||||
```python
|
```python
|
||||||
def subsets(A, i, aux, p):
|
def subsets(A, i, aux, p):
|
||||||
if p: print(aux)
|
if p: print(aux)
|
||||||
@ -158,8 +164,8 @@ def subsets(A, i, aux, p):
|
|||||||
no_take = subsets(A, i+1, aux, False)
|
no_take = subsets(A, i+1, aux, False)
|
||||||
```
|
```
|
||||||
|
|
||||||
time: $O(2^n)$
|
__Time Complexity__: $O(2^n)$
|
||||||
Space: $O(n^2)$, because we're creating new aux arrays.
|
__Space Complexity__: $O(n^2)$, because we're creating new aux arrays.
|
||||||
|
|
||||||
-- --
|
-- --
|
||||||
Number of Subsets with a given Sum
|
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
|
- 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.
|
- Exclude the current element from the sum and recur (i = i + 1) for the rest of the array.
|
||||||
|
|
||||||
|
|
||||||
|
<img src="https://user-images.githubusercontent.com/35702912/66469192-11c2e700-eaa5-11e9-9094-252ce842464a.jpg" width="400"
|
||||||
|
/>
|
||||||
```python
|
```python
|
||||||
def subsetSum(A,N,cur_sum, i, target):
|
def subsetSum(A,N,cur_sum, i, target):
|
||||||
if i == N:
|
if i == N:
|
||||||
@ -191,6 +200,8 @@ For eg,
|
|||||||
target = 6
|
target = 6
|
||||||
1, 2, 3 is good, but
|
1, 2, 3 is good, but
|
||||||
1, 2, 3, -1, 1 is also good.
|
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)
|
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.
|
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.
|
- 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.
|
- Exclude the current element from the sum and recur (i = i + 1) for the rest of the array.
|
||||||
|

|
||||||
```python
|
```python
|
||||||
def subsetSum2(A,N,cur_sum, i, target):
|
def subsetSum2(A,N,cur_sum, i, target):
|
||||||
if i == N:
|
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)
|
no_take = subsetSum2(A,N,cur_sum, i+1, target)
|
||||||
return take + no_take
|
return take + no_take
|
||||||
```
|
```
|
||||||
|
__Time Complexity__ : $O(2 ** (Target/MinElement))$
|
||||||
|
__Space Complexity__: $O(Target/Min Element)$
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user