Daa Ass2
Daa Ass2
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) {
If (weight >= M)
Return 0;
Int j = u + 1;
Totweight += items[j].weight;
Bound += items[j].profit;
J++;
Int k = j;
If (k < N)
Return bound;
Item items[N];
Items[i].profit = profits[i];
Items[i].weight = weights[i];
}
Qsort(items, N, sizeof(Item), compare);
Int max_profit = 0;
Int u = -1;
Int weight = 0;
Int profit = 0;
Max_profit = profit;
Max_profit_solution[i] = solution[i];
If (u < N – 1) {
Solution[u + 1] = 1;
Solution[u + 1] = 0;
Printf(“solution = {“);
Printf(“%d”, max_profit_solution[i]);
If (I < N – 1)
Printf(“,”);
Printf(“}\n”);
Int main() {
Int M = 15;
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) {
Dest[i] = src[i];
Void print_selected_jobs(
Bool optimal_selection[],
Int n_jobs
){
If (optimal_selection[i]) {
If (first) {
First = false;
} else {
If (first) {
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[]
){
Return;
Selection[current_job] = true;
*min_penalty_ptr = cost;
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 sum = 0;
Sum += a[i];
}
Return sum;
Int main() {
Int n_jobs = 4;
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);
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
CODE:
#include <stdio.h>
#include <string.h>
int i = 1;
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
} else {
lps[i] = 0;
i++;
int M = strlen(pattern);
int N = strlen(text);
computeLPSArray(pattern, M, lps);
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
return;
if (j != 0) {
j = lps[j - 1];
shifts++;
} else {
i = i + 1;
shifts++;
int main() {
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
CODE:
#include <stdio.h>
#include <string.h>
int i, j;
int pattern_hash = 0;
int text_hash = 0;
int h = 1;
h = (h * alphabet_size) % prime;
if (pattern_hash == text_hash) {
if (text[i + j] != pattern[j])
break;
if (j == pattern_length)
if (text_hash < 0)
}
int main() {
rabin_karp_string_matching(pattern, text);
return 0;
OUTPUT: