0% found this document useful (0 votes)
11 views

Design and Analysis of Algorithms Lab - 3

Uploaded by

paul.r212003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Design and Analysis of Algorithms Lab - 3

Uploaded by

paul.r212003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Design and Analysis of Algorithms Lab

(BCSE204P)
Faculty: - Dr Malini S

Digital Assignment III:


KMP and Branch n Bound (min cost)

Date: 14th March, 2024

Rishabh Paul (21BCE2019)


(School of Computer Science and Engineering)
[email protected]
Q1.) KMP String matching Algorithm

AIM:

The Knuth-Morris-Pratt (KMP) algorithm aims to efficiently find all


occurrences of a given pattern string within a larger text string.

ALGORITHM:

Preprocessing (Building the LPS Array):

1. Initialize an array `lps` of size equal to the length of the pattern


string.
2. Set `i = 1` and `j = 0`.
3. While `i` is less than the length of the pattern string:
- If the characters at `i` and `j` positions in the pattern string
match:
- Set `lps[i] = j + 1` and increment both `i` and `j`.
- If they don't match and `j` is not 0:
- Update `j` to `lps[j - 1]`.
- If they don't match and `j` is already 0:
- Set `lps[i] = 0` and increment `i`.
4. After the loop, the `lps` array contains the longest proper prefix
which is also a suffix for each position in the pattern string.

Searching (Using the LPS Array):


1. Initialize `i = 0` (for the text) and `j = 0` (for the pattern).
2. While `i` is less than the length of the text:
- If the characters at `i` and `j` positions in the text and pattern
strings match:
- Increment both `i` and `j`.
- If `j` equals the length of the pattern:
- A match is found at index `i - j`.
- Update `j` to `lps[j - 1]`.
- If there's a mismatch and `j` is not 0:
- Update `j` to `lps[j - 1]`.
- If there's a mismatch and `j` is already 0:
- Increment `i`.
3. Repeat until the end of the text string.
PSEUDOCODE:
KMP_Search(text, pattern):
lps = Build_LPS_Array(pattern)
n = length of text
m = length of pattern
i=0
j=0
while i < n:
if pattern[j] == text[i]:
i=i+1
j=j+1
if j == m:
print "Pattern found at index", i - j
j = lps[j - 1]
else if i < n and pattern[j] != text[i]:
if j != 0:
j = lps[j - 1]
else:
i=i+1

Build_LPS_Array(pattern):
m = length of pattern
lps = array of size m
j=0
i=1
while i < m:
if pattern[i] == pattern[j]:
j=j+1
lps[i] = j
i=i+1
else:
if j != 0:
j = lps[j - 1]
else:
lps[i] = 0
i=i+1
return lps
C/C++ CODE:
It asks to enter the string, so we’ll provide:

• Iamplayinginthefield

• playinginthefield
OUTPUT/RESULT:
Example2:
VERIFICATION STATUS
Q2.) Branch and bound operation and choose job with minimum
cost (row-wise)

AIM
The Branch and Bound algorithm aims to solve combinatorial optimization
problems by systematically searching through the solution space, pruning
branches that are guaranteed to lead to suboptimal solutions. In the context of
choosing jobs with minimum cost row-wise, the aim is to find the optimal
assignment of jobs to minimize the total cost, considering the cost associated
with each job

ALGORITHM
- Initialization:
1. Initialize a priority queue or a stack to store partial solutions.
2. Initialize the current best solution as positive infinity.
3. Start with an empty solution.

- Branching:
1. At each step, select a job to add to the solution.
2. Create branches by considering all possible jobs that can be added next.
3. Calculate the cost of each branch by adding the cost of the selected job to
the current total cost.

- Bounding:
1. Estimate the lower bound on the total cost of each partial solution.
2. Prune branches where the lower bound exceeds the current best solution.
3. Update the current best solution if a complete solution with a lower cost
is found.

- Termination:
1. Continue branching and bounding until all possible solutions have been
explored.
2. Return the best solution found.

PSEUDOCODE:

Branch_And_Bound_Choose_Job_With_Min_Cost(cost_matrix):
n = number of jobs
current_solution = empty list
best_solution_cost = infinity
stack = empty stack
push (stack, (current_solution, 0, 0)) # (solution, total_cost,
current_job_index)

while stack is not empty:


current_solution, total_cost, current_job_index = pop(stack)
if current_job_index == n:
if total_cost < best_solution_cost:
best_solution_cost = total_cost
else:
for job in range(n):
if job not in current_solution:
new_solution = current_solution + [job]
new_cost = total_cost + cost_matrix[current_job_index][job]
if new_cost < best_solution_cost:
push(stack, (new_solution, new_cost, current_job_index + 1))

return best_solution_cost
C/C++ CODE:
OUTPUT:
VERIFICATION STATUS

You might also like