22mic0017 VL2023240501807 Ast01
22mic0017 VL2023240501807 Ast01
22mic0017 VL2023240501807 Ast01
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
CSI 2003
Advanced Algorithms
DIGITAL ASSIGNMENT -1
Submitted to - LAKSHMANAN K
Submitted by
Vedang Mishrra
22MIC0017
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char seq1[MAX_LEN], seq2[MAX_LEN];
int m, n;
m = strlen(seq1);
n = strlen(seq2);
int lcs_table[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
lcs_table[i][j] = 0;
} else if (seq1[i - 1] == seq2[j - 1]) {
lcs_table[i][j] = lcs_table[i - 1][j - 1] + 1;
} else {
lcs_table[i][j] =
(lcs_table[i - 1][j] > lcs_table[i][j - 1]) ? lcs_table[i
- 1][j] : lcs_table[i][j - 1];
}
}
}
printf("LCS Table:\n");
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
printf("%d ", lcs_table[i][j]);
}
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
printf("\n");
}
lcs_str[lcs_len] = '\0';
printf("Longest Common Subsequence: %s\n", lcs_str);
return 0;
}
OUTPUT:
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
CODE:
#include <stdio.h>
void fibonacci_recursive(int n) {
if (n <= 1) {
printf("%d ", n);
} else {
fibonacci_recursive(n - 1);
printf("%d ", n);
}
}
int fibonacci_top_down(int n, int *memo) {
if (n <= 1) {
return n;
} else if (memo[n] != -1) {
return memo[n];
} else {
memo[n] = fibonacci_top_down(n - 1, memo) + fibonacci_top_down(n - 2,
memo);
return memo[n];
}
}
void fibonacci_bottom_up(int n) {
int fib[n + 1];
fib[0] = 0;
fib[1] = 1;
int main() {
int n, choice;
while (1) {
printf("\nMenu:\n");
printf("1. Fibonacci using Recursion\n");
printf("2. Fibonacci using Top-Down DP (Memoization)\n");
printf("3. Fibonacci using Bottom-Up (DP)\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:{
printf("Enter the nth term: ");
scanf("%d", &n);
printf("Fibonacci sequence: ");
fibonacci_recursive(n);
printf("\n");
break;
}
case 2:{
printf("Enter the nth term: ");
scanf("%d", &n);
return 0;
}
OUTPUT:
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
void subset_sum_util(int set[], int subset[], int n, int target, int sum, int
k, int start) {
if (sum == target) {
print_subset(subset, k);
return;
}
for (int i = start; i < n && sum + set[i] <= target; i++) {
subset[k] = set[i];
subset_sum_util(set, subset, n, target, sum + set[i], k + 1, i + 1);
}
}
int main() {
int n, target;
int set[MAX_SIZE];
printf("Enter the elements of the set: ");
for (int i = 0; i < n; i++) {
scanf("%d", &set[i]);
}
return 0;
}
OUTPUT:
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
QUESTION -4: Write a program to solve the 8-queen problem using
backtracking.
CODE:
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
1 2 3 4 5 6 7 8
1 - - - Q - - - -
2 - - - - - - - Q
3 Q - - - - - - -
4 - - Q - - - - -
5 - - - - - Q - -
6 - Q - - - - - -
7 - - - - - - Q -
8 - - - - Q - - -
Solution 84:
1 2 3 4 5 6 7 8
1 - - - - - - Q -
2 - - Q - - - - -
3 Q - - - - - - -
4 - - - - - Q - -
5 - - - - - - - Q
6 - - - - Q - - -
7 - Q - - - - - -
8 - - - Q - - - -
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
QUESTION 5: Write a program to solve the graph coloring problem
with 3 colors. Get the input (i.e, graph in the form adjacency matrix)
from the user.
CODE:
#include <stdio.h>
#include <stdbool.h>
return false;
}
printf("Graph Coloring:\n");
for (int i = 0; i < num_vertices; i++) {
printf("Vertex %d is colored with Color %d\n", i + 1, coloring[i]);
}
}
int main() {
int num_vertices;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &num_vertices);
int graph[MAX_VERTICES][MAX_VERTICES];
int num_colors = 3;
graph_coloring(graph, num_colors, num_vertices);
return 0;
}
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
OUTPUT:
22MIC0017
VEDANG MISHRRA
CSI2003-ADVANCED ALGORITHMS LAB
THANKYOU