expt 5
expt 5
Aim:- Write C program to implement the following using Branch and Bound Algorithm.
a) 0/1 Knapsack
THEORY:
● The Branch and Bound algorithm are used to solve optimization problems, particularly
combinatorial optimization problems.
● It involves dividing the problem into smaller subproblems and constructing a search tree.
● The algorithm starts with an initial solution and initializes upper and lower bounds.
● The search process begins by exploring the search tree using a depth-first search (DFS) or
breadth-first search (BFS) strategy.
● At each node of the search tree, the algorithm evaluates the bounds to determine if further
exploration is warranted.
● If the lower bound of a node is higher than the current best solution, the subtree rooted at that
node is pruned.
● If the upper bound of a node is lower than the current best solution, the algorithm backtracks to
the parent node.
● At each node, a decision is made to include or exclude a particular element or constraint from the
solution.
● The current solution and the bounds are updated accordingly.
● The algorithm continues exploring the search tree until all nodes have been visited or until the
best solution is found.
● The final solution obtained is the optimal solution to the optimization problem.
RollNo.211105036 |Page
a) 0/1 KNAPSACK
Date:09/05/2023
Problem Statement
Solve the 0/1 knapsack problem for the knapsack instance n = 4 and m = 35
(w1 . . . . w4) = (5,7,4,2) (p1 . . . . . p4) = (30,28,20,24)
Algorithm
Algorithm UBound(cp,cw,k,m)
//w[i] and p[i] are respectively the weight and profit of the ith object .
{
B:= cp ;
C:= cw;
For i:= k+1 to n do
{
If(c+w[i] ≤ m) then
{
c:=c + w[i] ;
b:=b-p[i];
}
}
return b;
}
Set currentsol[level] = 0;
Bnb(level +1,w,p)
}
Time and Space Complexity
Time Complexity = O(2n + n2)
Space complexity = O(n)
Where , n is the number of Elements .
RollNo.211105036 |Page
Code:
/*code for 0/1 knapsack bnb problem*/
#include <stdio.h>
#define N 100
void fill();
int uppbound(int level, int w, int p);
void sort();
void bnb(int level, int w, int p);
struct ITEM
{
int w, p;
};
int main()
{
printf("Enter the knapsack capacity: "), scanf("%d",&m);
printf("Enter the total no. of items: "), scanf("%d",&n);
printf("Enter the profit and weight for each item:\n");
for (i = 0; i < n; i++)
printf("Item %d: ",i+1), scanf("%d",&items[i].p),
scanf("%d",&items[i].w);
cw = cp = bestp = 0;
fill();
sort();
bnb(0, cw, cp);
return 0;
}
void fill()
{
for (i = 0; i < n; i++)
currentsoln[i] = bestsoln[i] = 0;
}
RollNo.211105036 |Page
break;
}
}
return ub;
}
void sort()
{
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n - i - 1; j++)
{
if ((items[j].p / items[j].w) < (items[j + 1].p / items[j + 1].w))
{
int tempp = items[j].p, tempw = items[j].w;
items[j].p = items[j + 1].p, items[j].w = items[j + 1].w;
items[j + 1].p = tempp, items[j + 1].w = tempw;
}
}
}
}
currentsoln[level] = 1;
bnb(level + 1, w + items[level].w, p + items[level].p);
currentsoln[level] = 0;
bnb(level + 1, w, p);
}
OUTPUT :
RollNo.211105036 |Page
Knapsack Solution is:
Max Profit = 74
Items Included: 1 1 1 0
--------------------------------
Process exited after 28.13 seconds with return value 0
Press any key to continue . . .
Conclusion:
a) 0/1 Knapsack
RollNo.211105036 |Page
Experiment No . 06: INTERNET ALGORITHMS Date:12/05/2023
Theory
• Internet algorithms are specifically designed algorithms used in various aspects of the internet to
solve specific problems efficiently.
• These algorithms address challenges related to data transmission, routing, network optimization,
information retrieval, and security, among others.
• Internet algorithms often consider factors such as latency, bandwidth, scalability, fault tolerance,
and resource constraints to ensure efficient and reliable operations.
RollNo.211105036 |Page
• The Boyer-Moore algorithm achieves a best-case time complexity of O(n/m) and an average-
case time complexity of O(n + m), making it efficient for practical use.
Huffman Coding:
• Huffman coding is a compression algorithm used to encode data efficiently by assigning shorter
codes to frequently occurring characters and longer codes to less frequent characters.
• The algorithm builds a binary tree (Huffman tree) based on the frequency of characters in the
input data.
• The characters with higher frequency are assigned shorter codes, and those with lower frequency
are assigned longer codes, ensuring prefix-free codes.
• Huffman coding achieves compression by representing the input data using the generated
Huffman codes, reducing the overall number of bits required for storage or transmission.
• The time complexity of the Huffman coding algorithm is O(n log n), where n is the number of
unique characters in the input.
RollNo.211105036 |Page
a) KMP PATTERN MATCHING
Date:12/05/2023
Problem Statement
Write a C program to implement KMP pattern matching for
T = aabbbaababbbabab and P = bbaba
Algorithm
Algorithm KMPMatch(T,P):
Input: Strings T (text) with n characters and P (pattern) with m characters
Output: Starting index of the first substring of T matching P, or an indication
that P is not a substring of T
f ← KMPFailureFunction(P) // construct the failure function f for P
i←0
j←0
while i<n do
if P[j] = T[i] then
if j = m − 1 then
return i − m + 1 // a match!
i←i+1
j←j+1
else if j > 0 // no match, but we have advanced in P then
j ← f(j − 1) // j indexes just after prefix of P that must match
else
i←i+1
return “There is no substring of T matching P.”
Algorithm KMPFailureFunction(P):
Input: String P (pattern) with m characters
Output: The failure function f for P, which maps j to the length of the longest
prefix of P that is a suffix of P[1..j]
i←1
j←0
f(0) ← 0
while i<m do
RollNo.211105036 |Page
if P[j] = P[i] then
// we have matched j + 1 characters
f(i) ← j + 1
i←i+1
j←j+1
else if j > 0 then
// j indexes just after a prefix of P that must match
j ← f(j − 1)
else
// we have no match here
f(i) ← 0
i←i+1
CODE:
#include <stdio.h>
#include <string.h>
#define N 256
int f[N],n,m;
OUTPUT:
C:\Users\Nidhi Shanbhag \ Documents\kmp.exe
Pattern bbaba found at index 10 in aabbbaababbbabab
--------------------------------
Process exited after 0.2755 seconds with return value 0
Press any key to continue . . .
RollNo.211105036 |Page
b) BOYER MOORE PATTERN MATCHING
Date:16/05/2023
Problem Statement
Write a C program to implement Boyer Moore pattern matching for
T = 1123114234112113 and P= 4112113
Algorithm
Algorithm BMMatch(T,P):
Input: Strings T (text) with n characters and P (pattern) with m characters
Output: Starting index of the first substring of T matching P, or an indication
that P is not a substring of T
compute function last
i←m−1
j←m−1
repeat
if P[j] = T[i] then
if j = 0 then
return i // a match!
else
i←i−1
j←j−1
else
i ← i + m − min(j, 1 + last(T[i])) // jump step
j←m−1
until i>n − 1
return “There is no substring of T matching P.”
CODE:
#include <stdio.h>
#include <string.h>
#define charnum 256
int i;
int lastc[charnum];
RollNo.211105036 |Page
lastc[i] = -1;
for ( i = 0; i < m; i++)
lastc[(int)p[i]] = i;
}
OUTPUT:
C:\Users\Nidhi Shanbhag \Documents\bm.exe
String 1 is: 1123114234112113
String 2 is: 4112113
Pattern 4112113 found at index 9 in 1123114234112113
--------------------------------
Process exited after 0.2885 seconds with return value 0
Press any key to continue . . .
RollNo.211105036 |Page
c) HUFFMAN CODING (Text Compression)
Date:19/05/2023
Problem Statement
Write a C program to implement Huffman Encoding for
“j is the position of the partitioning element “
Algorithm
Algorithm Huffman(C):
Input: A set, C, of d characters, each with a given weight, f(c)
Output: A coding tree, T, for C, with minimum total path weight
Initialize a priority queue Q.
for each character c in C do
Create a single-node binary tree T storing c.
Insert T into Q with key f(c).
while Q.size() > 1 do
f1 ← Q.minKey()
T1 ← Q.removeMin()
f2 ← Q.minKey()
T2 ← Q.removeMin()
Create a new binary tree T with left subtree T1 and right subtree T2.
Insert T into Q with key f1 + f2.
return tree Q.removeMin()
CODE:
#include <stdio.h>
#include <stdlib.h>
#define N 50
int i;
struct minheap
RollNo.211105036 |Page
{
int size, cap;
struct minheapnode **array;
};
struct minheapnode
{
char item;
int freq;
struct minheapnode *left, *right;
};
return temp;
}
return minh;
}
if (smallest != current_index)
{
swap(&minh->array[smallest], &minh->array[current_index]);
heapify(minh, smallest);
}
}
heapify(minh, 0);
return temp;
}
minh->size = size;
buildminheap(minh);
return minh;
}
while (!checksize(minh))
{
left = getminnode(minh);
right = getminnode(minh);
RollNo.211105036 |Page
top = newnode('$', left->freq + right->freq);
insert(minh, top);
}
return getminnode(minh);
}
RollNo.211105036 |Page
OUTPUT:
C:\Users\Nidhi Shanbhag \Documents\huffman.exe
Huffman Codes
n 000
s 0010
p 0011
f 01000
a 01001
m 01010
r 010110
g 010111
e 011
i 100
t 101
110
o 1110
j 111100
l 111101
h 11111
--------------------------------
Process exited after 0.3133 seconds with return value 0
Press any key to continue . . .
RollNo.211105036 |Page
d)LCS (Text Similarity)
Date:23/05/2023
Problem Statement
Write a C program to implement LCS for X= KLOKMKNKLOK and Y=KLLKNKLLKNYY
Algorithm
Algorithm LCS(X, Y ):
Input: Strings X and Y with n and m elements, respectively
Output: For i = 0,...,n − 1, j = 0,...,m − 1, the length L[i, j] of a longest
common subsequence of X[0..i] and Y [0..j]
for i ← −1 to n − 1 do
L[i, −1] ← 0
for j ← 0 to m − 1 do
L[−1, j] ← 0
for i ← 0 to n − 1 do
for j ← 0 to m − 1 do
if X[i] = Y [j] then
L[i, j] ← L[i − 1, j − 1] + 1
else
L[i, j] ← max{L[i − 1, j] , L[i, j − 1]}
return array L
CODE:
#include <stdio.h>
#include <string.h>
#define N 256
int L[N][N],i,j;
int main()
{
char x[] = "KLOKMKNKLOK", y[] = "KLLKNKLLKNYY";
int m = strlen(y), n = strlen(x), ss[strlen(y)];
lcs(x, y, n, m), sequence(ss, m, n);
printf("The string 1: %s\nThe string 2: %s\n",x,y);
printf("Length of the longest common subsequence is: %d\nThe longest
subsequence is: ", L[m][n]), show(y, ss, m);
return 0;
}
RollNo.211105036 |Page
OUTPUT:
C:\Users\ Nidhi Shanbhag \Documents\lcs.exe
The string 1 is KLOKMKNKLOK
The string 2 is KLLKNKLLKNYY
Length of the longest common subsequence is: 7
The longest subsequence is: KLKNKLK
--------------------------------
Process exited after 0.6355 seconds with return value 0
Press any key to continue . . .
CONCLUSION:
RollNo.211105036 |Page