DAA Assignment 2 PDF
DAA Assignment 2 PDF
Code
#include <stdio.h>
int main() {
int n, target;
int arr[n];
printf("Enter the elements in sorted order:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
2. Apply Binary Search on 2D NxM array (A) having numbers stored in non-
decreasing order under row-major scanning.
Code
#include <stdio.h>
if (midValue == target) {
printf("Element found at position (%d, %d)\n", row, col);
return 1;
}
int main() {
int rows, cols, target;
int matrix[rows][cols];
printf("Enter elements of the matrix in non-decreasing row-major order:\n");
return 0;
}
Code
#include <stdio.h>
// Function to find the bitonic point using binary search
int findBitonicPoint(int arr[], int low, int high) {
if (low <= high) {
int mid = low + (high - low) / 2;
return -1;
}
int main() {
int n;
int arr[n];
printf("Enter the bitonic sequence: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
if (bitonicIndex != -1) {
printf("The bitonic point is %d at index %d\n", arr[bitonicIndex],
bitonicIndex);
} else {
printf("No bitonic point found.\n");
}
return 0;
}
4. Apply Merge Sort to count inversion pairs in an array. Two elements a[i] and
a[j] form an inversion pair if a[i] > a[j] and i < j. Example: The sequence 2, 4, 1, 3,
5 has three inversions (2, 1), (4, 1), (4, 3).
Code
#include <stdio.h>
int mergeAndCount(int arr[], int temp[], int left, int mid, int right) {
int i = left; // Left subarray index
int j = mid + 1; // Right subarray index
int k = left; // Merged array index
int invCount = 0;
return invCount;
}
int mergeSortAndCount(int arr[], int temp[], int left, int right) {
int invCount = 0;
return invCount;
}
int main() {
int n;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
return 0;
}
return maxValue;
}
int main() {
int n, W;
Item items[n];
return 0;
}
Code
#include <stdio.h>
#include <limits.h>
return result;
}
int main() {
int n;
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
if (n < 2) {
printf("Array must have at least two elements.\n");
return 0;
}
return 0;
}
Code
#include <stdio.h>
// Partition function (Lomuto partition scheme)
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
return i + 1;
}
if (pivotIndex == k) {
return arr[pivotIndex];
} else if (pivotIndex > k) {
return quickSelect(arr, low, pivotIndex - 1, k);
} else {
return quickSelect(arr, pivotIndex + 1, high, k);
}
}
r e t u r n - 1 ;
}
if (medianIndex < n - 1) {
printf("Right Neighbor: %d\n", quickSelect(arr, 0, n - 1, medianIndex + 1));
} else {
printf("Right Neighbor: None\n");
}
}
int main() {
int n;
int arr[n];
findMedianNeighbors(arr, n);
return 0;
}
8. Given an array p[ ] which represents the chain of matrices such that the i-th
matrix Ai is of dimension p[i-1] x p[i]. We need to write a function that should
return the optimal parenthesizing expression resulting in a minimum
multiplication cost to multiply the chain.
Code
#include <stdio.h>
#include <limits.h>
// L is chain length
for (int L = 2; L < n; L++) {
for (int i = 1; i < n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = INT_MAX;
int main() {
int n;
printf("Enter the number of matrices: ");
scanf("%d", &n);
matrixChainOrder(p, n + 1);
return 0;
}
Code
#include <stdio.h>
return dp[n][W];
}
int main() {
int n, W;
return 0;
}
10. Implement the greedy algorithm to solve the problem of the Job Sequencing
with deadlines.
Code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int maxProfit = 0;
int jobCount = 0;
int main() {
int n;
Job jobs[n];
jobSequencing(jobs, n);
return 0;
}
11. Implement a greedy algorithm for finding the single-source shortest paths.
Suggest an algorithm if the given graph contains negative weights and non-
negative weight cycle and implement it.
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
int main() {
int graph[V][V] = {
{0, 10, 0, 0, 5},
{0, 0, 1, 0, 2},
{0, 0, 0, 4, 0},
{7, 0, 6, 0, 0},
{0, 3, 9, 2, 0}
};
int src;
printf("Enter the source vertex: ");
scanf("%d", &src);
dijkstra(graph, src);
return 0;
}
12. Apply Strassen’s Matrix Multiplication strategy for odd dimensional square
matrices.
Code
#include <stdio.h>
#include <stdlib.h>
#define MAX 3 // Assuming the square matrix dimension is odd and MAX x MAX
C[0][0] = M1 + M4 - M5 + M7;
C[0][1] = M3 + M5; C[1][0] = M2
+ M4; C[1][1] = M1 - M2 + M3 +
M6; }
// Driver code
int main() {
int A[MAX][MAX] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int B[MAX][MAX] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int C[MAX][MAX] = {0};
strassenMultiply(A, B, C);
printf("Resultant Matrix:\n");
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
printf("%d ", C[i][j]);
}
p r i n t f ( " \ n " ) ;
}
return 0;
}
13. Given a cost 2D-matrix and a position (m, n), write a function that returns
the minimum cost-path to reach (m, n) from (0, 0).
Code
#include <stdio.h>
#include <limits.h>
#define N 3
return dp[m][n];
}
int main() {
int cost[N][N] = {{1, 2, 3}, {4, 8, 2}, {1, 5, 3}};
int m = 2, n = 2;
return 0;
}
int main() {
int coins[] = {1, 2, 5};
int m = sizeof(coins) / sizeof(coins[0]);
int V;
return 0;
}
15. Implement all pairs of the shortest path algorithms for a graph using Floyd
Warshall’s strategy.
Code
#include <stdio.h>
#define INF 99999
#define V 4
int main() {
int graph[V][V] = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};
floydWarshall(graph);
return 0;
}