0% found this document useful (0 votes)
5 views11 pages

Dynamic Programming

The document contains three separate algorithms: dynamic programming for finding the maximum sum path in a grid with optional skips, Huffman coding for generating and printing character codes based on frequency, and the Rabin-Karp algorithm for substring search using hashing. Each algorithm is implemented in C, with functions for handling specific tasks such as node creation, priority queue operations, and pattern matching. The main function for each algorithm initializes input and calls the respective function to execute the algorithm.

Uploaded by

Athish Vishnu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Dynamic Programming

The document contains three separate algorithms: dynamic programming for finding the maximum sum path in a grid with optional skips, Huffman coding for generating and printing character codes based on frequency, and the Rabin-Karp algorithm for substring search using hashing. Each algorithm is implemented in C, with functions for handling specific tasks such as node creation, priority queue operations, and pattern matching. The main function for each algorithm initializes input and calls the respective function to execute the algorithm.

Uploaded by

Athish Vishnu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Dynamic programming

#include <stdio.h>
#include <string.h>

#define INF 1000000000


#define MAX 105

int dp[MAX][MAX][MAX];
int a[MAX][MAX];

int max(int a, int b)


{
return (a > b) ? a : b;
}

int f(int n, int m, int k, int i, int j)


{
if (i >= n || j >= m)
return -INF;
if (i == n - 1 && j == m - 1)
{
if (k >= 0)
return a[i][j];
else
return -INF;
}
if (dp[i][j][k] != -1)
return dp[i][j][k];
int ans = -INF;

int moveRight = a[i][j] + f(n, m, k, i, j + 1);


int moveDown = a[i][j] + f(n, m, k, i + 1, j);
ans = max(ans, moveRight);
ans = max(ans, moveDown);

if (k > 0)
{
int skipRight = f(n, m, k - 1, i, j + 1);
int skipDown = f(n, m, k - 1, i + 1, j);
ans = max(ans, skipRight);
ans = max(ans, skipDown);
}

return dp[i][j][k] = ans;


}

int main() {
int n, m, k;
scanf("%d %d", &n, &m);
scanf("%d", &k);

for (int i = 0; i < n; i++)


{
for (int j = 0; j < m; j++)
{
scanf("%d", &a[i][j]);
}
}

memset(dp, -1, sizeof(dp));

int result = f(n, m, k, 0, 0);


printf("%d\n", result);

return 0;
}

Huffman coding
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node


{

char c;
int freq;
struct node* left;
struct node* right;
} Node;

Node* pq[MAX_SIZE];
int pq_size = 0;

Node* createNode(char c, int freq)


{

Node* newNode = (Node*)malloc(sizeof(Node));


newNode->c = c;
newNode->freq = freq;
newNode->left = newNode->right = NULL;
return newNode;
}

void insertPQ(Node* n)
{
pq[pq_size++] = n;
for (int i = pq_size - 1; i > 0; i--)
{

if (pq[i]->freq < pq[i-1]->freq)


{

Node* temp = pq[i];


pq[i] = pq[i-1];
pq[i-1] = temp;
}

Node* pollPQ()
{

if (pq_size == 0) return NULL;


Node* n = pq[0];
for (int i = 1; i < pq_size; i++)
{

pq[i-1] = pq[i];
}

pq_size--;
return n;
}

void printCodes(Node* root, char* code, int depth)


{

if (root == NULL) return;

if (root->c != '$')
{

code[depth] = '\0';
printf("%c %s %d\n", root->c, code, root->freq);
}

if (root->left != NULL)
{

code[depth] = '0';
printCodes(root->left, code, depth + 1);
}

if (root->right != NULL)
{

code[depth] = '1';
printCodes(root->right, code, depth + 1);
}

}
void levelOrder(Node* root)
{

if (root == NULL) return;

Node* queue[MAX_SIZE];
int front = 0, rear = 0;
queue[rear++] = root;

while (front < rear)


{

int size = rear - front;


for (int i = 0; i < size; i++)
{

Node* current = queue[front++];


printf("%c ", current->c);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}

printf("\n");
}

int main() {
char s[1000];
printf("Enter a string:
"); fgets(s, sizeof(s),
stdin); s[strcspn(s, "\n")]
= '\0';

int freq[MAX_SIZE] = {0};

for (int i = 0; i < strlen(s); i++)


{

freq[(unsigned char)s[i]]++;
}

for (int i = 0; i < MAX_SIZE; i++)


{

if (freq[i] > 0) {
insertPQ(createNode((char)i, freq[i]));
}
}

while (pq_size > 1)


{

Node* left = pollPQ();


Node* right = pollPQ();
Node* newNode = createNode('$', left->freq + right->freq);
newNode->left = left;
newNode->right = right;
insertPQ(newNode);
}

Node* root = pollPQ();

char code[100]; printf("\


nHuffman Codes:\n");
printCodes(root, code, 0);

return 0;
}

Rabin karp
#include <stdio.h>
#include <string.h>

#define MAX 1000

void rk(char *p, char *t, int q)


{
int m = strlen(p);
int n = strlen(t);
int i, j, ph = 0, th = 0, h = 1;
int d = m + n;

for (i = 0; i < m - 1; i++)


h = (h * d) % q;

for (i = 0; i < m; i++)


{

ph = (p[i] + d * ph) % q;
th = (t[i] + d * th) % q;
}

for (i = 0; i <= n - m; i++)


{
if (ph == th) {
for (j = 0; j < m; j++)
{
if (t[i + j] != p[j])
break;
}
if (j == m)
printf("Pattern found at index: %d\n", i);
}
if (i < n - m) {
th = (d * (th - t[i] * h)
+ t[i + m]) % q; if (th
< 0)
th = (th + q);
}

int main()
{
char t[MAX], p[MAX];

printf("Enter TEXT: "); scanf("%s", t);

printf("Enter PATTERN: "); scanf("%s", p);


int q = 101; rk(p, t, q);

return 0;
}

You might also like