0% found this document useful (0 votes)
20 views18 pages

Daa 812

The document outlines various practical exercises related to algorithms for a B. Tech course at Parul University. It includes practicals on topics such as minimum time to rot oranges, edit distance between strings, minimum path sum in a grid, and unique paths in a grid. Each practical provides a clear aim, code implementation, and expected output.
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)
20 views18 pages

Daa 812

The document outlines various practical exercises related to algorithms for a B. Tech course at Parul University. It includes practicals on topics such as minimum time to rot oranges, edit distance between strings, minimum path sum in a grid, and unique paths in a grid. Each practical provides a clear aim, code implementation, and expected output.
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/ 18

PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY

SUBJECT: Design and Analysis Algorithm Laboratory


SUBJECT CODE:
303105306
B. TECH 3rd YEAR
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

PRACTICAL – 8
AIM: Given a grid of dimension N x M where each cell in the grid can have
values 0, 1, or 2 which has the following meaning:
0: Empty cell
1: Cells have fresh oranges 2: Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A
rotten orange at index [i,j] can rot other fresh oranges at indexes [i-1,j], [i+1,j],
[i,j-1], [i,j+1] (up, down, left and right) in unit time’

CODE:
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a cell in the grid


struct Cell {
int x, y;
};

// Direction vectors for adjacent cells (up, down, left, right)


int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

// Function to check if the cell is valid


int isValid(int x, int y, int N, int M) {
return (x >= 0 && x < N && y >= 0 && y < M);
}

ENROLMENT NO: 2203031280003 Page | 23


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

// Function to calculate the minimum time to rot all the oranges


int minTimeToRotOranges(int grid[][100], int N, int M) {
struct Cell queue[N * M];
int front = 0, rear = 0;
int freshOranges = 0, time = 0;

// Add all the initially rotten oranges to the queue


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (grid[i][j] == 2) {
queue[rear++] = (struct Cell){i, j};
}
else if (grid[i][j] == 1) {
freshOranges++;
}
}
}

// BFS process to rot adjacent fresh oranges


while (front < rear && freshOranges > 0) {
int size = rear - front;
time++;
while (size--) {
struct Cell curr = queue[front++];
for (int k = 0; k < 4; k++) {

ENROLMENT NO: 2203031280003 Page | 24


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

int newX = curr.x + dx[k];


int newY = curr.y + dy[k];
if (isValid(newX, newY, N, M) && grid[newX][newY] == 1) {
// Rot the fresh orange
grid[newX][newY] = 2;
freshOranges--;
queue[rear++] = (struct Cell){newX, newY};
}
}
}
}

// If there are still fresh oranges, return -1 (not possible to rot all)
if (freshOranges > 0) {
return -1;
}
return time;
}

int main() {
int N = 5, M = 5;
int grid[100][100] = {
{1, 1, 0, 0, 0},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 1},

ENROLMENT NO: 2203031280003 Page | 25


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

{0, 0, 0, 1, 0},
{0, 0, 1, 1, 2}
};

int result = minTimeToRotOranges(grid, N, M);


if (result == -1) {
printf("All oranges cannot be rotted.\n");
} else {
printf("Minimum time to rot all oranges: %d\n", result);
}

return 0;
}

OUTPUT:

ENROLMENT NO: 2203031280003 Page | 26


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

PRACTICAL – 9
AIM: Given two strings str1 and str2 and below operations that can be
performed on str1. Find minimum number of edits (operations) required to
convert ‘str1’ into ‘str2’. Insert Remove Replace, All of the above operations
are of equal cost.

CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to find the minimum of three numbers
int min(int x, int y, int z) {
return x < y ? (x < z ? x : z) : (y < z ? y : z);
}

ENROLMENT NO: 2203031280003 Page | 27


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

// Function to compute the minimum edit distance between two strings


int editDistance(char* str1, char* str2, int m, int n) {
// Create a DP table to store results of subproblems
int dp[m + 1][n + 1];
// Fill the DP table
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
// If the first string is empty, we need to insert all characters of str2
if (i == 0) {
dp[i][j] = j;
}
// If the second string is empty, we need to remove all characters of str1
else if (j == 0) {
dp[i][j] = i;
}
// If the characters are the same, no operation is needed, take value
from diagonal
else if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
// If characters are different, consider all operations and take the
minimum
else {
dp[i][j] = 1 + min(dp[i - 1][j], // Remove
dp[i][j - 1], // Insert

ENROLMENT NO: 2203031280003 Page | 28


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

dp[i - 1][j - 1] // Replace


);
}
}
}
// The final answer is in dp[m][n]
return dp[m][n];
}
int main() {
char str1[100], str2[100];
// Input two strings
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// Get the lengths of the two strings
int m = strlen(str1);
int n = strlen(str2);
// Call the function to find the minimum edit distance
printf("Minimum number of edits required: %d\n", editDistance(str1, str2,
m, n));

return 0;
}

OUTPUT:

ENROLMENT NO: 2203031280003 Page | 29


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

PRACTICAL – 10
AIM: Minimum Path Sum” says that given a n x m grid consisting of non-
negative integers and we need to find a path from top-left to bottom right,
which minimizes the sum of all numbers along the path.

CODE:
#include <stdio.h>
#include <limits.h>
ENROLMENT NO: 2203031280003 Page | 30
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

// Function to find the minimum of two numbers


int min(int x, int y) {
return (x < y) ? x : y;
}
// Function to compute the minimum path sum
int minPathSum(int grid[100][100], int n, int m) {
// Create a DP table to store results of subproblems
int dp[n][m];
// Initialize the top-left corner
dp[0][0] = grid[0][0];
// Initialize the first row (can only move from left)
for (int i = 1; i < m; i++) {
dp[0][i] = dp[0][i - 1] + grid[0][i];
}
// Initialize the first column (can only move from above)
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// Fill the rest of the DP table
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
// The value at dp[i][j] is the grid value plus the minimum of coming
from the left or from above
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
}

ENROLMENT NO: 2203031280003 Page | 31


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

}
// The answer is at the bottom-right corner of the dp table
return dp[n - 1][m - 1];
}
int main() {
int n, m;
printf("Enter the number of rows (n): ");
scanf("%d", &n);
printf("Enter the number of columns (m): ");
scanf("%d", &m);
int grid[100][100];
printf("Enter the grid values:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &grid[i][j]);
}
}
int result = minPathSum(grid, n, m);
printf("The minimum path sum is: %d\n", result);
return 0;
}

OUTPUT:

ENROLMENT NO: 2203031280003 Page | 32


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

ENROLMENT NO: 2203031280003 Page | 33


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

PRACTICAL – 11
AIM: Given string num representing a non-negative integer num, and an
integer k, return the smallest possible integer after removing k digits from
num.

CODE:
#include <stdio.h>
#include <string.h>
// Function to return the smallest possible integer after removing k digits
void removeKDigits(char* num, int k) {
int n = strlen(num);
char stack[n]; // Stack to store the resulting number
int top = 0; // Index for the stack
// Process each digit of the number
for (int i = 0; i < n; i++) {
// Remove digits from the stack if the current digit is smaller than the top
of the stack
while (top > 0 && stack[top - 1] > num[i] && k > 0) {
top--; // Pop the top of the stack
k--; // Decrease the number of digits to remove
}
stack[top++] = num[i]; // Push the current digit onto the stack
}
// If there are still digits to remove, remove from the end
top -= k;

ENROLMENT NO: 2203031280003 Page | 34


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

// Remove leading zeros from the result


int index = 0;
while (index < top && stack[index] == '0') {
index++;
}
// Print the final result
if (index == top) {
printf("0\n"); // If all digits are removed or the result is empty, return "0"
} else {
for (int i = index; i < top; i++) {
printf("%c", stack[i]);
}
printf("\n");
}
}
int main() {
char num[100];
int k;
// Input the number as a string and the number of digits to remove
printf("Enter the number: ");
scanf("%s", num);
printf("Enter the number of digits to remove: ");
scanf("%d", &k);
// Call the function to remove k digits
removeKDigits(num, k);

ENROLMENT NO: 2203031280003 Page | 35


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

return 0;
}

OUTPUT:

ENROLMENT NO: 2203031280003 Page | 36


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

PRACTICAL – 12
AIM: There is a robot on an m x n grid. The robot is initially located at the top-
left corner (i.e., grid[0][0]).The robot tries to move to the bottom-right corner
(i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any
point in time.Given the two integers m and n, return the number of possible
unique paths that the robot can take to reach the bottom-right corner.

CODE:
#include <stdio.h>
// Function to return the number of unique paths in an m x n grid
int uniquePaths(int m, int n) {
// Create a 2D dp array to store the number of paths for each cell
int dp[m][n];
// Initialize the first row and the first column
for (int i = 0; i < m; i++) {
dp[i][0] = 1; // Only one way to move in the first column (down only)
}
for (int j = 0; j < n; j++) {
dp[0][j] = 1; // Only one way to move in the first row (right only)
}
// Fill the dp array

ENROLMENT NO: 2203031280003 Page | 37


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

for (int i = 1; i < m; i++) {


for (int j = 1; j < n; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1]; // Sum of paths from the top and left
}
}
// The bottom-right corner contains the number of unique paths
return dp[m-1][n-1];
}
int main() {
int m, n;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int result = uniquePaths(m, n);
printf("Number of unique paths: %d\n", result);
return 0;
}

OUTPUT:

ENROLMENT NO: 2203031280003 Page | 38


PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR

ENROLMENT NO: 2203031280003 Page | 39

You might also like