-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduction to Backtracking, recursion and sets
- Loading branch information
1 parent
4a4127e
commit 88e0d9b
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <iostream> | ||
|
||
void findSubsets(char *input, char *output, int i, int j) { | ||
|
||
//base case | ||
if(input[i] == '\0') { | ||
output[j] = '\0'; | ||
if(output[0]=='\0') | ||
std::cout << "NULL"; | ||
std::cout << output << std::endl; | ||
return; | ||
} | ||
|
||
//rec case | ||
//include the ith letter | ||
output[j] = input[i]; | ||
findSubsets(input, output, i+1, j+1); | ||
|
||
//exclude the ith letter | ||
output[j] = '\0'; | ||
findSubsets(input,output,i+1,j); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
int main(){ | ||
|
||
char s[100], output[100]; | ||
|
||
std::cin >> s; | ||
findSubsets(s,output,0,0); | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <string> | ||
|
||
void swap(char *a, char *b) { | ||
char tmp = *a; | ||
*a=*b; | ||
*b=tmp; | ||
} | ||
|
||
/* | ||
Given a string s, find all permutations of the string | ||
*/ | ||
|
||
void permutationsString(char *n1, int l, int r) { | ||
|
||
int i; | ||
if(l == r) | ||
std::cout << n1 << std::endl; | ||
|
||
for(i = l; i <= r; ++i) { | ||
swap((n1+l), (n1+i)); | ||
permutationsString(n1, l+1, r); | ||
swap((n1+l),(n1+i)); | ||
} | ||
|
||
} | ||
|
||
int main(){ | ||
|
||
char str[100]; | ||
std::cin >> str; | ||
int n = strlen(str); | ||
|
||
permutationsString(str, 0 , n-1); | ||
|
||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Principles of Inclusion and Exclusion | ||
|
||
-> Set Theory | ||
|
||
Set is a form to store elements that doesn't repeat | ||
Generally sets're sorted | ||
|
||
''' | ||
{AuB} = A + B - {A^B} -> union of the sets | ||
|
||
-> Generalised function | ||
|
||
''' | ||
Discovering the pattern of even and odd sets | ||
Let's see if we've 3 sets | ||
|
||
|AuBuC| -> A + B + C | ||
- |AB| - |AC| - |BC| | ||
+ |ABC| | ||
|
||
Now let's see for 4 sets | ||
|
||
|AuBuCuD| -> A + B + C + D | ||
- |AB| - |AC| - |BC| | ||
+ |ABC| + |ABD| + |BCD| + |ACD| | ||
- |ABCD| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
|
||
|
||
int factorial(int n) { | ||
|
||
if (n == 1) | ||
return 1; | ||
|
||
return n*factorial(n-1); | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
|
||
std::cout << factorial(5) << std::endl; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
|
||
int fib(int n) { | ||
|
||
if(n == 0 or n == 1) | ||
return n; | ||
|
||
return fib(n-1) + fib(n-2); | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
|
||
std::cout << fib(5) << std::endl; | ||
|
||
} |