Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1 Ans:

a) If we formulate the above specifications:

Decision variables:

x1 – tons of maple syrup box car1

x2 – tons of maple syrup box car2

y1 – tons of copper wire box car1

y2 – tons of copper wire box car2

Objective function of Maximum profit:

Maximize Z = 6000 (x1+ x2) + 7000 (y1 + y2)

Subjected to constrains:

> Weight constraint for box car1: x1 + y1 ≤ 63

> Volume constraint for box car1: 2x1 + y1 ≤ 100

> Weight constraint for box car2: x2 + y2 ≤ 50

> Volume constraint for box car2: 2x2 + y2 ≤ 80

> Total maple syrup weight constraint: x1 +x2 ≥ 70

> Non ngativity constraints x1, x2, y1, y2 ≥ 0

b)

Slack forms of above linear program are:

Objective function: Maximize Z = 6000(x1+x2) + 7000(y1+y2)

x1 + y1 + s1 = 63

2x1 +y1 + s2 = 100

x2+ y2 + s3 = 50

2x2 + y2 + s4 = 80

- x1 - x2 + s5 = -70 (by minimum weight condition).

x1, x2, y1, y2, s1, s2, s3, s4, s5 ≥ 0

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

S3: its not feasible its infinite.

S4: its not feasible its infinite.

S5 : -70/-1 = 70

Existing variables: s2 (smallest ratio).

Step3: do pivot operation. And Divide R3/2 then we get

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 1 0.5 0.5 0 0 0 0.5 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.

Let’s assume all k patterns are the same length

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

Show that the set-partition problem is NP-complete.

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.

− Their exist a non-deterministic polynomial time algorithm that solves A. A belongs NP


− Any NP-Complete problem A’ can be reduced to A.
− The reduction of A’ to be done in polynomial time.
− The problem A as solution if and only if A’ has a solution.

> 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.

(a) State this problem as a decision problem.

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.

(b) Show that the decision problem is NP-complete.

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.

High level algorithm:

− 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:

> dp[i][j] = dp[i-1][j] or dp[i-1][j-tj] (if j >= tj)

− 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.

Here's the modified algorithm:

− 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:

dp[i][j] = max(dp[i-1][j], dp[i-1][j-tj] + pj) if j >= tj and i <= dj

dp[i][j] = dp[i-1][j] if j >= tj and i > dj


− After processing all tasks, the maximum profit achievable while meeting all deadlines is given by
dp[n][n].
− This modified dynamic programming algorithm also runs in O(n^2) time

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:

− Build a minimum spanning tree of the graph, which is polynomial time.


− find the maximum-weight edge in the minimum spanning tree and note it as “e”.
− create a doubled minimum spanning tree by duplicating all the edges in the minimum spanning
tree for “e”.
− form a Eulerian tour to create a Hamiltonian cycle, ensuring that you don’t skip more than two
consecutive intermediate nodes.
− Modify the Eulerian tour to create a Hamiltonian cycle, ensuring that you don't skip more than
two consecutive intermediate nodes.

Analyse the approximation ratio:

− 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.

You might also like