Algo Assignment4
Algo Assignment4
Decision variables:
Subjected to constrains:
b)
x1 + y1 + s1 = 63
x2+ y2 + s3 = 50
2x2 + y2 + s4 = 80
c)
Step 1:
Basic x1 x2 y1 y2 s1 s2 s3 s4 s5 RHS
x1 1 0 0 0 1 0 0 0 0 63
x2 0 1 0 0 0 1 0 0 0 50
y1 2 1 1 0 0 0 1 0 0 100
y2 0 2 0 1 0 0 0 1 0 80
s5 -1 -1 0 0 0 0 0 0 1 -70
Step2: entering the variable: x1 (coefficient is negative and has the smallest index) minimum ratio
test would be
s1: 63/1 = 63
S2: 100/2 = 50
S5 : -70/-1 = 70
Basic x1 x2 y1 y2 s1 s2 s3 s4 s5 RHS
x1 1 0 0 0 1 0 0 0 0 63
x2 0 1 0 0 0 1 0 0 0 50
y2 0 2 0 1 0 0 0 1 0 80
s5 -1 -1 0 0 0 0 0 0 1 -70
2. Describe how to extend the Rabin-Karp method to the problem of searching a text string for an
occurrence of any one of a given set of k patterns. Start by assuming that all k patterns have the
same length. Then generalize your solution to allow the patterns to have different lengths.
Ans:
The Rabin-Karp algorithm is primarily used for string searching and string matching. It's
particularly efficient when dealing with multiple patterns. Initially designed for single pattern searching, it
employs hashing to quickly compare substrings of the text to the patterns.
Preprocessing:
> Compute the hash value for each pattern in the set of k patterns. Store these hash values in a data
structure, such as a hash table or a set.
Searching:
> Compute the hash value for all substrings in the text string with lengths between the minimum and
maximum pattern lengths. To do this, you'll need to use the rolling hash function for each substring
length. If any of these hash values match any of the precomputed hash values, perform a character-by-
character comparison to confirm a match. If there is a match, you have found one of the patterns in the
text. Slide the window one character to the right and compute the new hash values for all substring
lengths. If any of the new hash values match any of the precomputed hash values, perform a character-by-
character comparison to confirm a match. Repeat this process until you've checked the entire text string.
3. The set-partition problem takes as input a set S of numbers. The question is whether the
numbers can be partitioned into two sets A and A = S − A such that
Ans: To show that the set-partition problem is NP-complete, we need to demonstrate two things: The
problem is in NP. The problem is NP-hard, so that every problem in NP can be reduced to it in
polynomial time. Here are steps to show problem A in NP-Complete.
> reduction from a known NP-complete problem (Subset sum) to the set-partition problem. Here the
subset sum problem is given a set of integers S and target sum T, is there a subset of S whose elements
sum up to T. for this given instance of the subset sum problem set S and target sum T, we construct an
instant of the set-partition problem: Given a set X of integers and target number t, find a subset Y⊆ X
such that the members of Y add up to exactly t. let s be the sum of numbers of X. Feed X’ = X∪ {s - 2t}
into SET-PARTITION. Accept if and only if SET-PARTITION accepts. By this given reduction works in
polynomial time. Hence this is NP-complete.
4. You have one computer and a set of n tasks {a1,a2,...,an} requiring time on the computer. Each
task aj requires tj time units on the computer (its processing time), yields a profit of p j, and has a
deadline dj. The computer can process only one task at a time, and task a j must run without
interruption for tj consecutive time units. If task aj completes by its deadline dj, you receive a profit
pj. If instead task aj completes after its deadline, you receive no profit. As an optimization problem,
given the processing times, profits, and deadlines for a set of n tasks, you wish to find a schedule
that completes all the tasks and returns the greatest amount of profit. The processing times, profits,
and deadlines are all nonnegative numbers.
Ans: Here they given set of n tasks, each with processing time tj, profit of pj, and deadline dj, and a
single machine, the decision problem is to determine if there exists a schedule that allows all tasks to be
completed by their respective deadlines while maximizing the total profit.
Let us take a set of tasks {a1, a2, ....., an} where each task has: processing time:tj (a<= j <=n), profit: pj
(1<=j<=n) deadline: dj (1<=j<=n). Single machine capable of processing one task at a time. If there exists
a schedule that completes all tasks by their deadlines while maximizing the total profit its a descision
problem.
Ans: Proving NP – completeness: we can reduce it to the NP-complete problem by 3-Partition. In the 3-
partition problem, we are given a set of 3n positive integers and must determine if they can be partitioned
into n groups of three such that the sum of integers in each group is the same.
Explanation: - Create n tasks, each with processing time tj = p = sum of integers in one group of 3.
> set the profit for each task to be pj = 1. Set the deadline for each task to be dj = n. Now, if there exists a
valid schedule for these tasks that completes all of them by their deadlines, it means we can partition the
integers in 3-partition into n groups of three with equal sums. Therefore, the decision problem is NP-
complete.
(c) Give a polynomial-time algorithm for the decision problem, assuming that all processing times
are integers from 1 to n. (Hint: Use dynamic programming.)
Ans: Polynomial- Time Algorithm for Decision Problem: Assume all processing times are integers from
1 to n, we can use dynamic problem to solve the decision problem.
− Create a 2D array dp, where dp[i][j] represents whether it's possible to select a subset of the first i
tasks such that the total processing time is exactly j.
− Initialize dp[0][0] to True (it's possible to select no tasks with a processing time of 0).
− Iterate through the tasks one by one. For each task aj with processing time tj and profit pj, update
dp[i][j] as follows:
− After processing all tasks, check if dp[n][n] is True. If it is, there exists a schedule that completes
all tasks by their deadlines, and you output YES. Otherwise, output NO. This algorithm runs in
O(n^2) time since the 2D dp array has dimensions n x n, and each entry can be computed in
constant time.
(d) Give a polynomial-time algorithm for the optimization problem, assuming that all process- ing
times are integers from 1 to n.
Ans: Polynomial-Time Algorithm for Optimization Problem: To solve the optimization problem, we can
modify the algorithm from part (c) slightly. Instead of using a 2D array to store whether it's possible to
select a subset of tasks, we can use a 2D array to store the maximum profit achievable up to a certain
time.
− Create a 2D array dp, where dp[i][j] represents the maximum profit achievable using the first i
tasks and j time units.
− Initialize dp[0][0] to 0 (no profit is earned with 0 time units).
− Iterate through the tasks one by one. For each task aj with processing time tj, profit pj, and
deadline dj, update dp[i][j] as follows:
5. The bottleneck traveling salesman problem (BTSP) is to find a tour that visits each city exactly
once and minimizes the maximum distance traveled between any two adjacent cities on the tour. A
solution to the bottleneck traveling-salesperson problem is the Hamiltonian cycle that minimizes
the cost of the most costly edge in the cycle. Assuming the cost function satisfies the triangle
inequality, it shows that there is a polynomial-time approximation algorithm with approximation
ratio 3 for this problem. (Hint: Consider a bottleneck spanning tree T of an undirected graph G
which is a spanning tree of G whose largest edge weight is minimum over all spanning trees of G,
which can be found in linear-time. Show recursively how to visit all the nodes in a bottleneck
spanning tree, exactly once by taking a full walk of the tree and skipping nodes, but without
skipping more than two consecutive intermediate nodes – we can always do this and obtain a graph
with a Hamiltonian cycle. Show that the costliest edge in a bottleneck spanning tree has a cost
bounded from above by the cost of the costliest edge in a bottleneck Hamiltonian cycle.)
Ans: In the Bottleneck Travelling-salesman problem, we want to find a Hamiltonian cycle with the
minimum cost of the most expensive edge. So, we have a cost function that satisfies the triangle
inequality.
Algorithm:
− The Hamiltonian cycle obtained in the previous step is twice the weight of the minimum spanning
tree, which provides a 2-approximation.
− The triangle inequality guarantees that any Hamiltonian cycle’s cost is bounded by the cost of the
most expensive edge in the graph.
− The cost of the most expensive edge in the Minimum spanning tree (“e”) is the most significant
contributor to the Hamiltonian cycles' cost. The final approximation ratio is at most 3 (2* 1.5),
where 1.5 is a bound on the ratio of the Hamiltonian cycles’ cost to the most expensive edge.
The above algorithm constructs a Hamiltonian cycle with an approximation of at most 3, with
polynomial-time complexity. This satisfies the requirements for the Bottleneck Travelling-Salesman
Problem.