Design and Analysis of Algorithms.

ALGEBRAIC PROBLEM
Definition:
 The first design technique we present is called algebraic
transformation.
 Assume we have an input I that is a member of set S1 and a
function f(I) that describes what must be computed.
 Usually the output f(I) also a member of S1.
 Though a method may exist for computing f(I) using operations on
elements in S1, this method may be inefficient.
 The algebraic transformation technique suggests that we alter the
input into another form to produce a member of set S2.
 The set S2 contains exact the same elements as S1 except it
assumes a different representations for there.
Why would we transform the input into another form?
 Because it may be easier to compute the function f for elements of
S2 than for elements of S1.
 Once the answer in S2 in computed, an inverse transformation is
performed to yield the result in set S1.
Example:
Example
 Let S1 be the set of n-degree polynomials (n>0) with
integer coefficients represented by a list of their coefficients ;
 The set S2 consists of exactly the same set of polynomials
but is represented by their values 2n+1 points; that is the
2n+1 pairs (xi, A (xi)) 1<i<2n+1, would represent the
polynomial A.
 The function f to be computed is the one that determines the
product of two polynomials A(x) and B(x) assuming the set
S1 representation to start with . Rather than forming the
product directly using the conventional method.
Backtracking:
The general method—8 queens problem—Sum of subsets—Graph
coloring—Hamiltonian cycle—Knapsack problem.
BACKTRACKING
 It is one of the most general algorithm design techniques.
 Many problems which deal with searching for a set of solutions or
for a optimal solution satisfying some constraints can be solved
using the backtracking formulation.
 To apply backtracking method, tne desired solution must be
expressible as an n-tuple (x1…xn) where xi is chosen from some
finite set Si.
 The problem is to find a vector, which maximizes or minimizes a
criterion function P(x1….xn).
 The major advantage of this method is, once we know that a partial
vector (x1,…xi) will not lead to an optimal solution that
(mi+1………..mn) possible test vectors may be ignored entirely.
 Many problems solved using backtracking require that all the
solutions satisfy a complex set of constraints.
 These constraints are classified as:
i) Explicit constraints.
ii) Implicit constraints.
1) Explicit constraints:
Explicit constraints are rules that restrict each Xi to take values
only from a given set.
Some examples are,
Xi  0 or Si = {all non-negative real nos.}
Xi =0 or 1 or Si={0,1}.
Li  Xi  Ui or Si= {a: Li  a  Ui}
 All tupules that satisfy the explicit constraint define a possible
solution space for I.
2) Implicit constraints:
The implicit constraint determines which of the tuples in the
solution space I can actually satisfy the criterion functions.
Algorithm:
Algorithm IBacktracking (n)
// This schema describes the backtracking procedure .All solutions are
generated in X[1:n]
//and printed as soon as they are determined.
{
k=1;
While (k  0) do
{
if (there remains all untried
X[k]  T (X[1],[2],…..X[k-1]) and Bk (X[1],…..X[k])) is true ) then
{
if(X[1],……X[k] )is the path to the answer node)
Then write(X[1:k]);
k=k+1;
//consider the next step.
}
else k=k-1;
//consider backtracking to the previous set.
}
}
 All solutions are generated in X[1:n] and printed as soon as they are
determined.
 T(X[1]…..X[k-1]) is all possible values of X[k] gives that
X[1],……..X[k-1] have already been chosen.
 Bk(X[1]………X[k]) is a boundary function which determines the
elements of X[k] which satisfies the implicit constraint.
 Certain problems which are solved using backtracking method are,
1. Sum of subsets.
2. Graph coloring.
3. Hamiltonian cycle.
4. N-Queens problem.
BRANCH AND BOUND
Definition
Branch and Bound is a systematic method for solving optimization
problems branch and bound is a rather general optimization technique
that applies where the greedy method dynamic programming fail.
There are Two Types
 LC Branch and Bound Solution(Least Cost)
 FIFO Branch and Bound Solution(First in First Out)
Branch and Bound Example:
 This method using the formula
 (C+ w[i]<m)
 The E-node is expanded and its two children, nodes, 2 and 3,
generated.
 The cost c(2)=-38, c(3)=-32,and u(3)=-27.
 Both nodes are put onto the list of live node
FIFO Branch and Bound Solution:
 The children node are generated left to right.
 Nodes and 3 are generated and added to the queue(in the order).
 The value Upper remains unchanged.
 i+1 using the FIFO method.
Graph coloring
What is Graph coloring?
 Associating a color or a color label with the vertices, edges or
faces of a graph whilst fulfilling a certain is called graph coloring.
Three Different ways to color graph…
 Vertex coloring
 Edge coloring
 Face coloring
Vertex coloring:
To color the vertices in the graph in such a way that two adjacent
vertices do not have the same color.
Adjacent vertices:
Two vertices are adjacent if they are connected through an edge
Edge coloring:
To color the edges in the graph in such a way that two adjacent edges do
not have the same color.
Adjacent vertices:
Two edges are adjacent if they are connected through an edge
Face coloring:
To color the faces in the planar graph in such a way that two adjacent
faces do not have the same color.
Adjacent vertices:
Two faces are adjacent if they share a common edge.
Example…
Program to implement 0/1 knapsack problem using greedy method
#include<iostream.h>
int main()
{
int array[2][100], n, w, i, curw, used[100], maxi = -1, totalprofit = 0;
cout << "Enter number of objects: ";
cin >> n;
cout << "Enter the weight of the knapsack: ";
cin >> w;
second row is to store profits */
for (i = 0; i < n; i++)
{
cin >> array[0][i] >> array[1][i];
}
for (i = 0; i < n; i++)
{
used[i] = 0;
}
curw = w;
while (curw >= 0)
{
maxi = -1;
//loop to find max profit object
for (i = 0; i < n; i++)
{
if ((used[i] == 0) && ((maxi == -1) || (((float) array[1][i]
/ (float) array[0][i]) > ((float) array[1][maxi]
/ (float) array[0][maxi]))))
{
maxi = i;
}
}
used[maxi] = 1;
curw -= array[0][maxi];
totalprofit += array[1][maxi];
if (curw >= 0)
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< array[0][maxi] << " Profit: " << array[1][maxi]
<< " completely in the bag, Space left: " << curw;
}
else
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< (array[0][maxi] + curw) << " Profit: "
<< (array[1][maxi] / array[0][maxi]) * (array[0][maxi]
+ curw) << " partially in the bag, Space left: 0"
<< " Weight added is: " << curw + array[0][maxi];
totalprofit -= array[1][maxi];
totalprofit += ((array[1][maxi] / array[0][maxi]) *
(array[0][maxi]
+ curw));
}
}
cout << "\nBags filled with objects worth: " << totalprofit;
return 0;
}
Output:
Enter number of objects: 5
Enter the weight of the knapsack: 3
1
2
3
4
5
6
7
8
9
1
Added object 1 weight: 1 profit: 2 completely in the bag, space left:
2
Added object 1 weight: 1 profit: 2 partially in the bag, space left: 0
weight added is: 2
Bags filled with objects worth:4
KNAPSACK PROBLEM
KNAPSACK
The knapsack problem or rucksack problem is a problem in
combinatorial optimization. It derives its name from the following
maximization problem of the best choice of essentials that can fit into
one bag to be carried on a trip. Given a set of items, each with a weight
and a value, determine the number of each item to include in a collection
so that the total weight is less than a given limit and the total value is as
large as possible.
The Original Knapsack Problem (1)
Problem Definition
Want to carry essential items in one bag
Given a set of items, each has
A cost (i.e., 12kg)
A value (i.e., 4$)
Goal
To determine the # of each item to include in a collection so that
The total cost is less than some given cost
And the total value is as large as possible
The Original Knapsack Problem (2)
Three Types
0/1 Knapsack Problem
restricts the number of each kind of item to zero or one
Bounded Knapsack Problem
restricts the number of each item to a specific value
Unbounded Knapsack Problem
places no bounds on the number of each item
Complexity Analysis
The general knapsack problem is known to be NP-hard
No polynomial-time algorithm is known for this problem
Here, we use greedy heuristics which cannot guarantee the optimal
solution
0/1 Knapsack Problem (1)
Problem: John wishes to take n items on a trip
The weight of item i is wi & items are all different (0/1 Knapsack
Problem)
The items are to be carried in a knapsack whose weight capacity is c
When sum of item weights ≤ c, all n items can be carried in the
knapsack
When sum of item weights > c, some items must be left behind
Which items should be taken/left?
0/1 Knapsack Problem (2)
John assigns a profit pi to item i
All weights and profits are positive numbers
John wants to select a subset of the n items to take
 The weight of the subset should not exceed the capacity of
the knapsack (constraint)
 Cannot select a fraction of an item (constraint)
 The profit of the subset is the sum of the profits of the
selected items (optimization function)
 The profit of the selected subset should be maximum
(optimization criterion)
Let xi = 1 when item i is selected and xi = 0 when item i is not selected
Because this is a 0/1 Knapsack Problem, you can choose the item or not
choose it.
Thank You…
0/1 KNAPSACK PROBLEM:
 To use the branch and bound technique to solve any problem, it is
first necessary to conceive of a state space tree for the problem.
 We have already see two possible state space tree organizations for
the technique of section This difficult is a easily overcome b
replacing the objective function.
 The purpose of this paper is to analyze several algorithm design
paradigms applied to a single problem – the 0/1 Knapsack Problem.
 It is an NP-complete problem and as such an exact solution for a
large input is practically impossible to obtain.
The Knapsack Problem (KP)
 The Knapsack Problem is an example of a combinatorial
optimization problem, which seeks for a best solution from among
many other solutions.
 It is concerned with a knapsack that has positive integer volume (or
capacity) V.
 There are n distinct items that may potentially be placed in the
knapsack. Item i has a positive integer volume Vi and positive
integer benefit Bi.
FOR EXAMPLE:
 If one or more of the Qi is infinite, the KP is unbounded; otherwise,
the KP is bounded [1].
 The bounded KP can be either 0-1 KP or Multiconstraint KP.
 If Qi = 1 for i = 1, 2, …, N, the problem is a 0-1 knapsack problem
In the current paper, we have worked on the bounded 0-1 KP, where
we cannot have more than one copy of an item in the knapsack.
ALGORITHM:
{
b:=cp; c:=cw;
for i:=k+1 to n do
{
if (c + w[i] < m) then
{
c :=c + w[i]; b:=b-p[i];
}
}
return b;
}
LC (LEAST COST) BRANCH AND BOUND SOLUTION
 [LCBB] consider the knapsack instance n=4, (p1, p2, p3 p4,)= (10,
10, 20, 18),(w1,w2, w3, w4)= (2,4,6,9), and m=15.
 Let us trace the working of an LC branch-and-bound search using
c(.) and u(.) as defined previously.
 We continue to use the fixed tuple size formulation.
 The search begins with the root as the E-node.
BRANCH AND BOUND
 We can backtrack if we know the best possible solution in current
sub tree is worse than current best solution obtained so far.
 Estimates for improvement given current ordering
2, $40
A down can give $315
, $50
Branch and Bound1.98, $100
B down -> $275
C down -> $225
5, $95
D down -> $125
E down -> $30
A
In
Out
3, $30
B
B
C
C
C
C
LOWER BOUND THEORY
D
E
D
E
D
D
D
D
• Lower bound: an estimate of a number of operations needed to
solve a given problem .
E
E
E
E
E
Examples:
Problem
Weight = 7.12
Value = $190
Lower bound
Weight = 8.98
Value = $235
E
• sorting (comparison-based)
Ω (nlog n)
• searching in a sorted array
Ω (log n)
• n-digit integer multiplication
• multiplication of n-by-n matrices
Ω (n)
Ω (n2)
• We always consider the algorithm with smaller order of
complexity as the better.
• How can we ensure that is there any faster method available
• We need to find a function g(n) which is a lower bound on the time
that algorithm must take, So if f(n) is the time for some algorithm
then we can write f(n)=Ω(g(n) where g(n) is lower bound for f(n)
Or we can say f(n) >= c * g(n) for all n>n0
• Where c & n0 are constants
• For many problems it is easy to calculate the lower bound on n
inputs
• e.g. consider the set of problems to find the maximum of an
ordered set of n integers. Clearly every integer must be examined
at least once. So Ω(n) is a lower bound for that. For matrix
multiplication we have 2n2inputs so the lower bound can be Ω(n2)
Methods of showing lower bounds:
• Trivial lower bounds
•
Sorting
• Information-theoretic arguments (decision trees)
•
Any comparison sorting algorithm (i.e., bubble sort)
•
A convenient model of algorithms involving comparisons in
which (Like sorting):
•
Internal nodes represent comparisons
•
Leaves represent outcomes
Adversary arguments:
•
Merging two sorted lists
• It’s a game between the adversary and the (unknown) algorithm.
• The adversary has the input and the algorithm asks questions to the
adversary about the input.
• The adversary tries to make the algorithm work the hardest by
adjusting the input (consistently).
• It wins the “game” after the lower bound time (lower bound
proven) if it is able to come up with two different inputs.
MODULAR ARITHMETIC
Introduction
Modular arithmetic is a system of arithmetic for integers, where numbers
"wrap around" upon reaching a certain value.
Modular arithmetic is useful in one context it allow the reformulation of
the way addition, subtraction and multiplication are performed.
To find the mod operation
This reformulation is one that parallelism whereas the normal method for
doing arithmetic is serial.
Modular arithmetic allows symbolic mathematical computation.
x mod y=x-y[x/y] , if y#0
X mod 0= x
Examples
1. LCM
2. HCF
3. Largest Number among Three Numbers
4. Largest Number Using Dynamic Memory Location
5. Whether a Number can be express as Sum of Two Prime Numbers
6. To Find Sum of Natural Numbers using Recursion
7. To Calculate Sum of Natural Numbers
8. To Find GCD among two integer numbers
9. To Find G.C.D Using Recursion
1. To compute the GCD of two numbers
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main ( ) {
int a , b;
int r;
clrscr( );
cout<<" To Find the GCD\n";
cout<<" Enter the First number\n";
cin>>a;
cout<<" Enter the second number\n";
cin>>b;
while ( a % b !=0) {
r=a%b;
a=b;
b=r;
}
cout<<"The greatest common divisor of the two numbers entered
is\n"<<r;
getch ( ); }
2. To Find LCM
#include <iostream>
int main() {
int n1, n2, max;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
max = (n1 > n2) ? n1 : n2;
{
if (max%n1 == 0 && max%n2 == 0)
{
cout << "LCM = " << max; break;
} else
++max;
} while (true);
return 0;
}
OUTPUT
Enter two numbers: 12
18
LCM = 36
3. To Find Largest Number Among Three Numbers
#include <iostream>
int main()
{
float n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1>=n2 && n1>=n3) {
cout << "Largest number: " << n1;
}
if(n2>=n1 && n2>=n3)
{
cout << "Largest number: " << n2;
}
if(n3>=n1 && n3>=n2) {
cout << "Largest number: " << n3;
}
return 0;
}
OUTPUT
Enter Three numbers
23
45
12
Largest Number 45
4. To Check Whether a Number can be Express as Sum of Two
Prime Numbers
#include <iostream>
int check_prime(int n);
int main()
{
int n, i, flag=0;
cout << "Enter a positive integer: ";
cin >> n;
for(i=2; i<=n/2; ++i)
{
if (check_prime(i)==0)
{
if ( check_prime(n-i)==0)
{
cout << n << " = " << i << " + " << n-i << endl;
flag=1;
}}
}
if (flag==0)
cout << n << " can't be expressed as sum of two prime numbers.";
return 0;
}
int check_prime(int n)
{
int i, flag=0;
for(i=2;i<=n/2;++i)
{
if(n%i==0) {
flag=1; break; } } return flag; }
OUTPUT
Enter a positive integer: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
5. To Calculate Find Sum of Natural Numbers .
#include <iostream>
int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
OUTPUT
Enter a positive integer: 50
Sum = 1275
6. To Find HCF Using LCM
FORMULA
LCM = (n1*n2)/HCF
#include <iostream>
int main() {
int n1, n2, temp1, temp2, lcm;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
temp1 = n1;
temp2 = n2;
while(temp1 != temp2)
{
if(temp1 > temp2) temp1 -= temp2;
else
temp2 -= temp1;
}
lcm = (n1 * n2) / temp1;
cout << "LCM = " << lcm;
return 0;
}