You are on page 1of 10

ASSIGNMENT NUMBER: 8

Name :Mridul KANTI SIKDER


Roll no:21CS8002
Subject :Design analysis and algorithm

Part 1

#include <stdio.h>
#include <stdbool.h>

bool realizable(int A[], int n, int T) {

// Calculate the sum of all elements in the array A

int sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];

// Check if the parity of sum and T is the same


if ((sum % 2 != 0 && T % 2 == 0) || (sum % 2 == 0 && T % 2
!= 0)) {
return false;
// Parity mismatch, T cannot be realized
}

// Calculate the range of possible values between -sum and


sum

int S = sum;
int dp[n][2 * S + 1];
// Dynamic programming table
// Initialize the table with false values
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= 2 * S; j++)
{
dp[i][j] = false;
}
}

// Base case: The first element in the array A

dp[0][A[0] + S] = true;

dp[0][-A[0] + S] = true;

// Fill in the dynamic programming table

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


{

for (int j = -S; j <= S; j++)


{
if (dp[i - 1][j + S])
{
dp[i][j + A[i] + S] = true;
dp[i][j - A[i] + S] = true;
}
}
}

// Check if T can be realized


if (T + S >= 0 && T + S <= 2 * S && dp[n - 1][T + S])
{
return true; // T is realizable
} else {
return false; // T cannot be realized
}
}

int main()
{
int A[] = {7, 12, 1, 9, 5};
int n = 5;
int T = 8;

if (realizable(A, n, T))
{
printf("T is realizable.\n");
} else {
printf("T is not realizable.\n");
}

return 0;
}

Output:

PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) { gcc lab8_part1.c -o lab8_part1


} ; if ($?) { .\lab8_part1 }

T is realizable.

PART 2

#include <stdio.h>
#include <stdbool.h>

void printRealization(int A[], bool dp[][2 * 10000 + 1], int


n, int T, int currentSum, int index) {
if (index == 0) {
printf("%d", A[index]);
return;
}
if (dp[index - 1][currentSum - A[index] + 10000]) {
printRealization(A, dp, n, T, currentSum - A[index],
index - 1);
printf("+%d", A[index]);
} else if (dp[index - 1][currentSum + A[index] + 10000]) {
printRealization(A, dp, n, T, currentSum + A[index],
index - 1);
printf("-%d", A[index]);
}
}

bool realizableAndPrint(int A[], int n, int T) {


int sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
}

if ((sum % 2 != 0 && T % 2 == 0) || (sum % 2 == 0 && T % 2


!= 0)) {
return false; // Parity mismatch, T cannot be realized
}

int S = sum;
bool dp[n][2 * 10000 + 1];

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


for (int j = 0; j <= 2 * 10000; j++) {
dp[i][j] = false;
}
}

dp[0][A[0] + 10000] = true;


dp[0][-A[0] + 10000] = true;

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


for (int j = -S; j <= S; j++) {
if (dp[i - 1][j + 10000]) {
dp[i][j + A[i] + 10000] = true;
dp[i][j - A[i] + 10000] = true;
}
}
}

if (T + 10000 >= 0 && T + 10000 <= 2 * 10000 && dp[n -


1][T + 10000]) {
// T is realizable, print one way of realizing it
printf("One way to realize T: ");
printRealization(A, dp, n, T, T, n - 1);
printf(" = %d\n", T);
return true;
} else {
return false; // T cannot be realized
}
}

int main() {
int A[] = {7, 12, 1, 9, 5};
int n = 5;
int T = 8;

if (realizableAndPrint(A, n, T)) {
printf("T is realizable.\n");
} else {
printf("T is not realizable.\n");
}

return 0;
}
Output:
PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) {
gcc daa8_par
cd "c:\Users\HP\daa lab\" ; if ($?) { gcc daa8_part2.c -o
daa8_part2 } ; if ($?) { .\daa8_part2 }

One way to realize T: 7-12-1+9+5 = 8

T is realizable.

Part:3
#include <stdio.h>
#include <stdbool.h>

void printAllRealizations(int A[], bool dp[][2 * 10000 + 1],


int n, int T, int currentSum, int index, char* expression) {
if (index == 0) {
if (currentSum == A[0]) {
printf("%s+%d\n", expression, A[0]);
}
if (currentSum == -A[0]) {
printf("%s-%d\n", expression, A[0]);
}
return;
}

char newExpression[100]; // A buffer for the new


expression
if (currentSum - A[index] >= -10000 && dp[index -
1][currentSum - A[index] + 10000]) {
sprintf(newExpression, "%s+%d", expression, A[index]);
printAllRealizations(A, dp, n, T, currentSum -
A[index], index - 1, newExpression);
}
if (currentSum + A[index] >= -10000 && dp[index -
1][currentSum + A[index] + 10000]) {
sprintf(newExpression, "%s-%d", expression, A[index]);
printAllRealizations(A, dp, n, T, currentSum +
A[index], index - 1, newExpression);
}
}

void showAllRealizations(int A[], int n, int T) {


int sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
}

if ((sum % 2 != 0 && T % 2 == 0) || (sum % 2 == 0 && T % 2


!= 0)) {
printf("T is not realizable due to parity
mismatch.\n");
return;
}

int S = sum;
bool dp[n][2 * 10000 + 1];

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


for (int j = 0; j <= 2 * 10000; j++) {
dp[i][j] = false;
}
}

dp[0][A[0] + 10000] = true;


dp[0][-A[0] + 10000] = true;

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


for (int j = -S; j <= S; j++) {
if (dp[i - 1][j + 10000]) {
dp[i][j + A[i] + 10000] = true;
dp[i][j - A[i] + 10000] = true;
}
}
}

if (T + 10000 >= 0 && T + 10000 <= 2 * 10000 && dp[n -


1][T + 10000]) {
printf("All realizations of T:\n");
char expression[100] = "";
printAllRealizations(A, dp, n, T, T, n - 1,
expression);
} else {
printf("T is not realizable.\n");
}
}

int main() {
int A[] = {7, 12, 1, 9, 5};
int n = 5;
int T = 8;

showAllRealizations(A, n, T);

return 0;
}

Output:

cd "c:\Users\HP\daa lab\" ; if ($?) { gcc


daa8_part2.c -o daa8_part2 } ; if ($?) { .\daa8_part2 }
One way to realize T: 7-12-1+9+5 = 8
T is realizable.

PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) {


gcc daa8_part3.c -o daa8_part3 } ; if ($?) { .\daa8_part3 }

All realizations of T:
+5+9-1-12+7
-5+9-1+12-7

You might also like