0% found this document useful (0 votes)
9 views21 pages

Daa Ass2

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)
9 views21 pages

Daa Ass2

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/ 21

DAA LAB

ASSESSMENT 2

RAMAN HARITASH

22BCE2138

QUES1:

Write a program to apply LC Branch and Bound to find out the maximum profit gain by the thief
using the following information for the 0/1 knap sack problem.
N=4 , M=15 Profit =( 10,10,12,18) weight = (2, 4, 6, 9)
For example:

Tes
Input Result
t

1 4 Profit =38
15 solution = {1,1,0,1}
10 10 12 18
2 4 6 9

CODE:

#include <stdio.h>

#include <stdlib.h>

#define N 4

Typedef struct {

Int profit;

Int weight;

Float profit_per_unit;

} Item;
Int compare(const void *a, const void *b) {

Item *item1 = (Item *)a;

Item *item2 = (Item *)b;

Return (item2->profit_per_unit – item1->profit_per_unit) > 0 ? 1 : -1;

Float bound(int u, int weight, int profit, Item items[], int M) {

If (weight >= M)

Return 0;

Float bound = profit;

Int j = u + 1;

Int totweight = weight;

While (j < N && totweight + items[j].weight <= M) {

Totweight += items[j].weight;

Bound += items[j].profit;

J++;

Int k = j;

If (k < N)

Bound += (M – totweight) * items[k].profit_per_unit;

Return bound;

Void knapsack_branch_and_bound(int M, int profits[], int weights[]) {

Item items[N];

For (int I = 0; I < N; i++) {

Items[i].profit = profits[i];

Items[i].weight = weights[i];

Items[i].profit_per_unit = (float)profits[i] / weights[i];

}
Qsort(items, N, sizeof(Item), compare);

Int max_profit = 0;

Int max_profit_solution[N] = {0};

Int u = -1;

Int weight = 0;

Int profit = 0;

Int solution[N] = {0};

Void knapsack_recursive(int u, int weight, int profit, int solution[]) {

If (weight <= M && profit > max_profit) {

Max_profit = profit;

For (int I = 0; I < N; i++)

Max_profit_solution[i] = solution[i];

If (bound(u, weight, profit, items, M) > max_profit) {

If (u < N – 1) {

Solution[u + 1] = 1;

Knapsack_recursive(u + 1, weight + items[u + 1].weight, profit + items[u + 1].profit, solution);

Solution[u + 1] = 0;

Knapsack_recursive(u + 1, weight, profit, solution);

Knapsack_recursive(u, weight, profit, solution);


Printf(“Profit =%d\n”, max_profit);

Printf(“solution = {“);

For (int I = 0; I < N; i++) {

Printf(“%d”, max_profit_solution[i]);

If (I < N – 1)

Printf(“,”);

Printf(“}\n”);

Int main() {

Int profits[N] = {10, 10, 12, 18};

Int weights[N] = {2, 4, 6, 9};

Int M = 15;

Knapsack_branch_and_bound(M, profits, weights);

Return 0;

OUTPUT:
QUES 2:

Apply Branch and Bound technique to solve the Job Selection Problem by writing a program
using the following information
Number of Jobs =4

Jobs 1 2 3 4

Penalty 5 10 6 3

Deadlin 1 3 2 1
e

Time 1 2 1 1

For example:

Tes
Result
t

1 Cost = 16
Upper Bound = 8
Jobs Completed within deadline are J2 and J3

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#include <stdbool.h>
Void copy_bool_array(bool dest[], bool src[], int size) {

For (int I = 0; I < size; i++) {

Dest[i] = src[i];

Void print_selected_jobs(

Bool optimal_selection[],

Int n_jobs

){

Bool first = true;

For(int I = 0; I < n_jobs; i++) {

If (optimal_selection[i]) {

If (first) {

Printf(“Jobs Completed within deadline are J%d”, I + 1);

First = false;

} else {

Printf(“ and J%d”, I + 1);

If (first) {

Printf(“No jobs can be completed”);

Putchar(‘\n’);

Void find_optimal_selection(
Int penalties[],

Int deadlines[],

Int times[],

Int n_jobs,

Int current_job,

Int prev_upper_bound,

Int prev_cost,

Int prev_elapsed_time,

Bool prev_selection[],

Int *min_upper_bound_ptr,

Int *min_penalty_ptr,

Bool optimal_selection[]

){

Int elapsed_time = prev_elapsed_time + times[current_job];

If (elapsed_time > deadlines[current_job]) {

Return;

Int upper_bound = prev_upper_bound – penalties[current_job];

If (upper_bound < *min_upper_bound_ptr) *min_upper_bound_ptr = upper_bound;

Bool *selection = malloc(n_jobs * sizeof(bool));

Copy_bool_array(selection, prev_selection, n_jobs);

Selection[current_job] = true;

Int cost = prev_cost – penalties[current_job];

If (cost < *min_penalty_ptr) {

*min_penalty_ptr = cost;

Copy_bool_array(optimal_selection, selection, n_jobs);


}

For (int I = 0; I < n_jobs; i++) {

If (!selection[i]) {

Find_optimal_selection(

Penalties,

Deadlines,

Times,

N_jobs,

I,

Upper_bound,

Cost,

Elapsed_time,

Selection,

Min_upper_bound_ptr,

Min_penalty_ptr,

Optimal_selection

);

Free(selection);

Int calculate_array_sum(int a[], int size) {

Int sum = 0;

For(int I = 0; I < size; i++) {

Sum += a[i];

}
Return sum;

Int main() {

Int n_jobs = 4;

Int penalties[] = {5, 10, 6, 3};

Int deadlines[] = {1, 3, 2, 1};

Int times[] = {1, 2, 1, 1};

Bool initial_selection[] = {false, false, false, false};

Int sum_of_penalties = calculate_array_sum(penalties, n_jobs);

Int min_upper_bound = sum_of_penalties;

Int min_penalty = INT_MAX;

Bool optimal_selection[] = {false, false, false, false};

For(int I = 0; I < n_jobs; i++) {

Find_optimal_selection(

Penalties,

Deadlines,

Times,

N_jobs,

I,

Sum_of_penalties,

Sum_of_penalties,

0,

Initial_selection,

&min_upper_bound,

&min_penalty,

Optimal_selection

);

}
Printf(“Cost = %d\n”, sum_of_penalties – min_penalty);

Printf(“Upper Bound = %d\n”, min_upper_bound);

print_selected_jobs(optimal_selection, n_jobs);

OUTPUT :
QUES3:

Write a program to apply KMP String Matching algorithm to verify the given pattern is present
in the string or not. If present display its occurrences

For example:

Tes
Input Result
t

1 b a c b a b a b a b a c a a b Pattern is found in the


a b a b a c a string
Number of Shifts needed is 6

CODE:

#include <stdio.h>

#include <string.h>

// Function to compute the prefix array for the pattern

void computeLPSArray(char *pattern, int M, int *lps) {

int len = 0; // length of the previous longest prefix suffix

lps[0] = 0; // lps[0] is always 0

int i = 1;

while (i < M) {

if (pattern[i] == pattern[len]) {

len++;

lps[i] = len;
i++;

} else {

if (len != 0) {

len = lps[len - 1];

} else {

lps[i] = 0;

i++;

// Function to perform pattern matching using KMP algorithm

void KMPSearch(char *pattern, char *text) {

int M = strlen(pattern);

int N = strlen(text);

int lps[M]; // Preprocess the pattern to compute the LPS array

computeLPSArray(pattern, M, lps);

int i = 0; // index for text[]

int j = 0; // index for pattern[]

int shifts = 0; // Count of shifts needed

while (i < N) {

if (pattern[j] == text[i]) {

j++;

i++;
}

if (j == M) {

printf("Pattern is found in the string\n");

printf("Number of Shifts needed is %d\n", i - j);

return;

} else if (i < N && pattern[j] != text[i]) {

if (j != 0) {

j = lps[j - 1];

shifts++;

} else {

i = i + 1;

shifts++;

printf("Pattern is not found in the string\n");

int main() {

char text[] = "bacbabababacaababaca";

char pattern[] = "ababac";

KMPSearch(pattern, text);

return 0;

}
OUTPUT:
QUES4:

Write a program to apply the Rabin Karp String Matching algorithm to check whether the given
pattern is present in the String or not
Note : Use the Robin Karp Finger Print function to verify the pattern is present in the
string or not
For example:

Tes
Input Result
t

1 c c a c c a a e d b a The given pattern is present in the


d b a String

CODE:

#include <stdio.h>

#include <string.h>

#define alphabet_size 256

void rabin_karp_string_matching(char *pattern, char *text) {

int pattern_length = strlen(pattern);

int text_length = strlen(text);

int prime = 101;

int i, j;

int pattern_hash = 0;

int text_hash = 0;
int h = 1;

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

h = (h * alphabet_size) % prime;

for (i = 0; i < pattern_length; i++) {

pattern_hash = (alphabet_size * pattern_hash + pattern[i]) % prime;

text_hash = (alphabet_size * text_hash + text[i]) % prime;

for (i = 0; i <= text_length - pattern_length; i++) {

if (pattern_hash == text_hash) {

for (j = 0; j < pattern_length; j++) {

if (text[i + j] != pattern[j])

break;

if (j == pattern_length)

printf("The given pattern is present in the String\n");

if (i < text_length - pattern_length) {

text_hash = (alphabet_size * (text_hash - text[i] * h) + text[i + pattern_length]) % prime;

if (text_hash < 0)

text_hash = (text_hash + prime);

}
int main() {

char text[] = "ccaccaaedba";

char pattern[] = "dba";

rabin_karp_string_matching(pattern, text);

return 0;

OUTPUT:

You might also like