0% found this document useful (0 votes)
4 views27 pages

17 Problemreduction

Uploaded by

aashirvad245
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views27 pages

17 Problemreduction

Uploaded by

aashirvad245
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

UNIT 3: Transform and Conquer

Transform and Conquer:


Problem reduction
Problem Reduction
• The problem can be transformed to an easier
problem to solve, i.e., transformed into an
entirely different problem.

(very powerful idea, extensively used in complexity theory)


Example 1:
Computing the Least Common Multiple of two
positive integers m and n : LCM(m, n)

• lcm(m,n) = the smallest integer that is divisible by


both m and n.
Example: lcm(12, 15) = 60
lcm(50, 45) = 450

Middle school method:


Given the prime factorizations of m and n, lcm(m, n) can be
computed as the product of all the common prime factors of m and
n times the product of m 's prime factors that are not in n, n times n
's prime factors that are not in m.
Given the prime factorizations of m and n, lcm(m, n) can be computed as
the product of all the common prime factors of m and n times the product
of m 's prime factors that are not in n, n times n 's prime factors that are
not in m.

Example:
12 = 2 . 2 . 3
15 = 3. 5
lcm(12, 15) = (3) . 2 . 2 . 5 = 60

Example:
24 = 2 . 2 . 2 . 3
60 = 2 . 2 . 3 . 5
lcm(24, 60) = (2 . 2 . 3) . 2 . 5 = 120

Drawback: inefficient and requires a list of consecutive primes


Efficient algorithm using problem reduction:

where gcd(m, n) can be computed very efficiently by


Euclid's algorithm.

https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/
Example 2:
Counting paths between two vertices in a graph

Note:

The number of different paths of length k > 0 from


the ith vertex to the jth vertex of a graph (undirected
or directed) is equal to the (i, j)th element of Ak
where A is the adjacency matrix of the graph.
Solution by problem reduction:

solved with an algorithm for computing an


appropriate power of its adjacency matrix
Example 3:
Solving optimization problems

Note:

If a problem asks to find a maximum of some


function, it is said to be a maximization problem;
if it asks to find a function's minimum, it is called a
minimization problem.
Example:
To find a minimum of some function f (x) and you
know an algorithm for maximizing the function. How
can you take advantage of the latter?

Solution by problem reduction:

(to minimize a function, maximize its negative instead and, to


get a correct minimal value of the function itself, change the
sign of the answer)
Similarly, a maximization problem can be reduced
to an equivalent minimization problem.

(to maximize a function, minimize its negative instead and, to


get a correct maximal value of the function itself, change the
sign of the answer)
Example 4:
Linear Programming

Note:

Many problems of optimal decision making can be


reduced to an instance of the linear programming
problem, which is a problem of optimizing a linear
function of several variables subject to constraints in
the form of linear equations and linear inequalities.
Example:
Consider a university endowment that needs to invest $100
million. This sum must be split between three types of
investments: stocks, bonds, and cash. The endowment
managers expect an annual return of 10%,7%, and 3% for
their stock, bond, and cash investments, respectively.
• Since stocks are more risky than bonds, the endowment
rules require the amount invested in stocks to be no more
than one third of the moneys invested in bonds.
• In addition, at least 25% of the total amount invested in
stocks and bonds must be invested in cash.
How should the managers invest the money to maximize the
return?
Solution by problem reduction – linear programming:

Mathematical model:
Let x, y, and z be the amounts (in millions of dollars) invested
in stocks, bonds, and cash, respectively. Then the optimization
problem is:
Applications of Linear Programming (LP):

LP is flexible to model a wide variety of important


applications, such as
• airline crew scheduling,
• Transportation and communication network
planning,
• oil exploration and refining,
• Industrial production optimization
Example 5:
Reduction to graph problems

• variety of puzzles and games can be reduced to graph


problems
• vertices of a graph typically represent possible states of the
problem in question while edges indicate permitted
transitions among such states.
• One of the graph's vertices represents an initial state, while
another represents a goal state of the problem. (state-
space graph)
• transformation reduces the problem to the question about a
path from the initial-state vertex to a goal-state vertex
Example: River crossing puzzle

A peasant finds himself on a river bank with a wolf, a


goat, and a head of cabbage. He needs to transport
all three to the other side of the river in his boat.
However, the boat has room only for the peasant
himself and one other item (either the wolf, the goat,
or the cabbage). In his absence, the wolf would eat
the goat, and the goat would eat the cabbage.

Find a way for the peasant to solve his problem or


prove that it has no solution.
P, w, g, c stand for
the peasant, the
wolf, the goat, and
the cabbage,
respectively;

the two bars II


denote the river
Let’s check our understanding
Puzzle
You are given a list of numbers for which you need to
construct a min-heap.
How would you use an algorithm for constructing a
max-heap to construct a min-heap?

Solution:
Inspired by Heapsort!!

Idea: Build min-heap in-place using array


representing the max-heap
Puzzle
Consider a chocolate manufacturing company that produces only
two types of chocolate – A and B. Both the chocolates require Milk
and Choco only. To manufacture each unit of A and B, the following
quantities are required:
• Each unit of A requires 1 unit of Milk and 3 units of Choco
• Each unit of B requires 1 unit of Milk and 2 units of Choco

The company kitchen has a total of 5 units of Milk and 12 units of


Choco. On each sale, the company makes a profit of
• Rs 6 per unit A sold
• Rs 5 per unit B sold.

Now, the company wishes to maximize its profit. How many units
of A and B should it produce respectively?
Profit: Maximize Z = 6X+5Y

Subject to:
X+Y ≤ 5
3X+2Y ≤ 12
X≥0 & Y≥0
Puzzle
• You have 11, 8 and 5 litre milk container.
• Each container has no markings except for that which gives
you it's total volume.
• You have a 12 litre container full of milk.
• You must use the containers in such away as to exactly
measure out 4 litre of milk.

How is this done?


Puzzle
Jealous husbands:
There are n (n ≥ 2) married couples who need to cross a river. They have a
boat that can hold no more than two people at a time. To complicate
matters, all the husbands are jealous and will not agree on any crossing
procedure that would put a wife on the same bank of the river with
another woman's husband without the wife's husband being there too,
even if there are other people on the same bank.

Can they cross the river under such constraints?


• Solve the problem for n = 2.
• Solve the problem for n = 3 (classical version of the problem)

Indicate how many river crossings they will take


N=2
W1W2
W1
H1H2
H1
H1W1

Solution to two-couple case:


Choose a couple to move first
1. move both wives across
2. move the other wife back
3. move both husbands across
4. move the other husband back
5. move the other couple
N=3
Solution to three-couple case:
choose a couple h-i:w-i to move
1. move w-i and another wife across
2. move the other wife back
3. move both remaining wives across
4. move one of the other wives back
5. move h-i and another husband across
6. move the other couple back
Next session…
Dynamic Programming

You might also like