Skip to content

Commit 22e4046

Browse files
committed
dynamic-programming | fibonacci using matrix
1 parent b6b3072 commit 22e4046

File tree

11 files changed

+439
-24
lines changed

11 files changed

+439
-24
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ Implementation using trees is better as we can apply union by rank and path comp
212212
In case of linked list find takes O(n) time and union also takes O(n) time and create takes O(1) time
213213

214214

215+
## Back Tracking
216+
217+
- Permutations means ordered combinations. Eg. My juice is made by a combination of 10 fruits (order does not matter), but in case of permutations the order does matter. Basically changing the order can change the outcome means permutations. Changing the order has no effect on outcome is combination
218+
- Backtracking uses recursion where each call in the stack has its stored values and backtracking makes
219+
use of those values to do decision making at a particular level in the recursion tree
220+
221+
## Pattern Matching
222+
223+
- For KMP to find all occurences of a pattern in a string, whenever the match is found, we assign the
224+
pointer of pattern variable value that is there in the prefix suffix array at an index less then the
225+
current value of the pointer and we start comparing again.
226+
- Boyer-Moore algorithm is much more efficient than KMP at places where pattern to be searched has characters
227+
as different as possible. If the characters are same, then in worst case it will give a time complexity of
228+
O(mn) where it will end up comparing most of the characters, therefore, in case characters are mostly same
229+
use KMP
230+
215231
# Topic0: Programming Questions
216232

217233
## Note:
@@ -439,6 +455,7 @@ In case of linked list find takes O(n) time and union also takes O(n) time and c
439455
- [Find the sum of digits for all numbers from 1 to N for a given N](/dynamic-programming/question30.c)
440456
- [Given a string of digits, find the length of the longest substring of a string, such that the length of the substring is '2k' digits and sum of left k digits is equal to the sum of right k digits](/dynamic-programming/question31.c)
441457
- [Given a rod of length 'n' inches and an array of prices that contains prices of all pieces of size smaller than n, find the max value obtainable by cutting the rod and selling the pieces](/dynamic-programming/question32.c)
458+
- [Fibonacci series](/dynamic-programming/question33.c)
442459

443460
### Graphs
444461

@@ -453,7 +470,17 @@ In case of linked list find takes O(n) time and union also takes O(n) time and c
453470

454471

455472
### Pattern Matching
473+
456474
- [Given a text and a pattern, find all occurences of a pattern in a given text.](/pattern-matching/question1.c)
475+
- [Implement KMP algorithm to find all occurences of a pattern in a given text](/pattern-matching/question2.c)
476+
- [Boyer-Moore algorithm for string finding patterns](/pattern-matching/question3.c)
477+
- [Rabin-Karp for string finding patterns](/pattern-matching/question4.c)
478+
479+
### Back Tracking
480+
481+
- [Generate all permutations of a given string](/back-tracking/question1.c)
482+
- [Program to generate all strings of n bits](/back-tracking/question2.c)
483+
- [N-queens problem](/back-tracking/question3.c)
457484

458485
### MISC
459486

back-tracking/question1.c

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Generate all permutations of a given string
3+
4+
Backtracking is a concept wherein at each point we backtrack to see what was done and was is left
5+
6+
METHOD:
7+
Idea is to keep swapping at each level to achieve this
8+
9+
*/
10+
// C program to print all permutations with duplicates allowed
11+
#include <stdio.h>
12+
#include <string.h>
13+
#include <string.h>
14+
15+
int hash[256];
16+
17+
void initHash(){
18+
int i;
19+
for(i=0;i<256;i++){
20+
hash[i] = 0;
21+
}
22+
}
23+
24+
void outputResult(char *result){
25+
int i, length = strlen(result);
26+
27+
for(i=0;i<length;i++){
28+
printf("%c ", result[i]);
29+
}
30+
printf("\n");
31+
}
32+
33+
34+
void permute(char *str, int size, char *result, int level){
35+
36+
if(level == size){
37+
outputResult(result);
38+
return;
39+
}
40+
41+
int i;
42+
for(i=0; i<size; i++){
43+
if(hash[str[i]] == 0){
44+
continue;
45+
}
46+
result[level] = str[i];
47+
hash[str[i]]--;
48+
permute(str, size, result, level + 1);
49+
hash[str[i]]++;
50+
}
51+
}
52+
53+
int main(){
54+
char str[] = "ABC";
55+
int length = strlen(str);
56+
57+
char result[length];
58+
59+
int i;
60+
61+
initHash();
62+
for(i=0;i<length;i++){
63+
hash[str[i]]++;
64+
}
65+
66+
permute(str,length,result,0);
67+
68+
return 0;
69+
}

back-tracking/question2.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Program to generate all strings of n bits
3+
4+
Here we use recursion
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
void display(int *arr, int n){
11+
int i;
12+
for(i=0;i<n;i++){
13+
printf("%d ", arr[i]);
14+
}
15+
printf("\n");
16+
}
17+
18+
void generate(int start, int end, int *arr, int size){
19+
if(start == end){
20+
display(arr,size);
21+
return;
22+
}
23+
arr[start] = 0;
24+
generate(start + 1,end, arr, size);
25+
arr[start] = 1;
26+
generate(start + 1,end, arr, size);
27+
}
28+
29+
int main(){
30+
31+
printf("enter the value of n:\n");
32+
int n;
33+
scanf("%d",&n);
34+
35+
int arr[n];
36+
37+
generate(0,n,arr,n);
38+
39+
return 0;
40+
}

back-tracking/question3.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
N-queens problem
3+
There is a NXN chessboard. You have to write a program to place 4 queens in such a way that
4+
they do not attack each other.
5+
6+
Naive Approach:
7+
We can place the first queen in n^2 ways,
8+
second in n^2-1 ,third in -2 and so on.
9+
10+
Lets say n=4
11+
12+
Then in worst case we will have n^2cn total combinations of placing the queens and it will take us n^2 time
13+
to validate each combination to see if thats the right position for the queens to not kill each other.
14+
15+
Time complexity: O(n^2cn)n^2
16+
17+
METHOD2:
18+
Here we can restrict the queen per row. Therefore every queen will have 4 ways to select a cell
19+
4 X 4 X 4 X 4 for all the 4 queens
20+
Hence total ways n^n
21+
22+
Time complexity O(n^n)n^2 //to validate what all ways are correct
23+
24+
METHOD3: We can restrict the queens by row and column both. First queen will then have 4 ways, second will
25+
have 3 ways as 1 column is also gone, third will have 2 and so on.
26+
27+
4 X 3 X 2 X 1.
28+
29+
Time complexity: O(n!)n^2
30+
31+
METHOD4:
32+
Here we can use backtracking at each step to see if we are heading in the right direction.
33+
34+
Lets say first queen is placed at first cell (and we are going in ascending order), second will be placed in
35+
next row in 2,3 or 4 column. In second column it cannot be placed, so we place it in third.
36+
37+
Now third queen cannot be placed in first or third and cannot be placed in 2nd or 4th as it will be killed by
38+
second queen, hence we backtrack and change position of q2, and move it to 4th place. This way q3 can now
39+
be placed in second column.
40+
41+
Now llarly for 4th queen it cannot be placed anywhere so we backtrack to change position of q3, which also cannot
42+
be placed anywhere else, we backtrack to change q2, for which all positions are over, so we backtrack to change
43+
position for q1 until we find the right match for all.
44+
45+
Time complexity in this case cannot be measured as we dont know how many times will the backtracking take place
46+
but it is definately less then O(n!)n^2
47+
48+
*/
49+
#include <stdio.h>
50+
#include <stdlib.h>
51+
52+
void init(int n, int arr[n][n]){
53+
int i,j;
54+
for(i=0;i<n;i++){
55+
for(j=0;j<n;j++){
56+
arr[i][j] = 0; //0 means cell is empty
57+
}
58+
}
59+
}
60+
61+
int main(){
62+
int n;
63+
printf("enter the value of n\n");
64+
scanf("%d",&n);
65+
66+
int arr[n][n];
67+
68+
init(n,arr);
69+
70+
findNQueen()
71+
72+
return 0;
73+
}
74+

back-tracking/question4.c

Whitespace-only changes.

dynamic-programming/question33.c

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void display(int f[2][2]){
5+
int i,j;
6+
printf("-------------------\n");
7+
for(i=0;i<2;i++){
8+
for(j=0;j<2;j++){
9+
printf("%d ", f[i][j]);
10+
}
11+
printf("\n");
12+
}
13+
printf("----------------\n");
14+
}
15+
16+
void mul(int a[2][2], int b[2][2]){
17+
int p = a[0][0]*b[0][0] + a[0][1]*b[1][0];
18+
int q = a[0][0]*b[0][1] + a[0][1]*b[1][1];
19+
int r = a[1][0]*b[0][0] + a[1][1]*b[1][0];
20+
int s = a[1][0]*b[0][1] + a[1][1]*b[1][1];
21+
22+
a[0][0] = p;
23+
a[0][1] = q;
24+
a[1][0] = r;
25+
a[1][1] = s;
26+
}
27+
28+
29+
void ipow(int f[2][2], int power){
30+
31+
if(power <= 1){
32+
return;
33+
}
34+
35+
int base[2][2] = {
36+
{1,1},
37+
{1,0}
38+
};
39+
40+
mul(f,f);
41+
42+
ipow(f,power/2);
43+
44+
if(power%2 != 0){
45+
mul(f,base);
46+
}
47+
48+
}
49+
50+
void ipowIterative(int f[2][2],int power){
51+
52+
int base[2][2] = {
53+
{1,1},
54+
{1,0}
55+
};
56+
57+
while(power > 1){
58+
mul(f,f);
59+
if(power%2 !=0){
60+
mul(f,base);
61+
}
62+
power = power/2;
63+
}
64+
65+
}
66+
67+
int fib(int n){
68+
int f[2][2] = {
69+
{1,1},
70+
{1,0}
71+
};
72+
printf("calling pow with power %d\n", n-1);
73+
ipowIterative(f,n-1);
74+
75+
return f[0][0];
76+
}
77+
78+
int main(){
79+
80+
int n = 11;
81+
82+
printf("answer is %d\n", fib(n));
83+
84+
return 0;
85+
}

misc/question3.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Additive sequence
3+
*/
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
int isAdditive(char *str){
9+
int length = strlen(str);
10+
11+
int isAdditive = 1;
12+
13+
int i;
14+
15+
16+
17+
printf("%d\n", str[length-1]-'0');
18+
// for(i=2;i<length;i++){
19+
// printf("comparing %d with %d and %d ", str[i]-'0',str[i-1]-'0',str[i-2]-'0');
20+
// if((str[i]-'0') != ((str[i-1]-'0') + (str[i-2] - '0'))){
21+
// return 0;
22+
// }
23+
// }
24+
25+
return isAdditive;
26+
}
27+
28+
int main(){
29+
30+
int cases;
31+
scanf("%d", &cases);
32+
33+
char str[200];
34+
35+
int i;
36+
for(i=0;i<cases;i++){
37+
scanf("%s",str);
38+
if(isAdditive(str)){
39+
printf("%d\n", 1);
40+
}else{
41+
printf("%d\n", 0);
42+
}
43+
}
44+
45+
return 0;
46+
}

nextquestions.md

+2
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@ TODO:
108108
- breadth and depth first traversal in a tree
109109
- Graphs question6 union and find method to be done
110110
- Graphs question8 to be done along with backedges
111+
- Try Boyer-Moore and Rabin-karp in pattern matchin question3 and 4
112+
- Back tracking - program to print all permutations question1 (some problem)
111113

0 commit comments

Comments
 (0)