Design and Analysis of Algorithms Lab - 3
Design and Analysis of Algorithms Lab - 3
(BCSE204P)
Faculty: - Dr Malini S
AIM:
ALGORITHM:
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)
return best_solution_cost
C/C++ CODE:
OUTPUT:
VERIFICATION STATUS