Design and Analysis of Algorithm
Design and Analysis of Algorithm
Sum of n no’s
Abstract solution:
Sum of n elements by adding up elements one at a time.
Abstract solution:
Find the Largest number among a list of elements by considering one element at a
time.
Little more detailed solution:
1. Treat first element as largest element
2. Examine a[2] to a[n] and suppose the largest element is at a [ i ];
3. Assign a[i] to largest;
https://www.freshersnow.com/
4. Return largest;
Linear search
Abstract solution:
From the given list of elements compare one by one from first element to last
element for the required element
https://www.freshersnow.com/
Selection Sort
Abstract solution:
From those elements that are currently unsorted, find the smallest and place it
next in the sorted list.
Bubble Sort
Abstract solution:
Comparing adjacent elements of an array and exchange them if they are not in order. In the
process, the largest element bubbles up to the last position of the array first and then the
second largest element bubbles up to the second last position and so on.
for i := 1 to n do
{
Compare a[1] to a [n-i] pair-wise (each element with its next)
and interchange a[j] and a[j+1] whenever a[j] is greater than a[j+1]
}
https://www.freshersnow.com/
{
if ( a[j] > a[j+1] )
{
// swap a[j] and a[j+1]
temp := a[j]; a[j] := a[j+1];a[j + 1] := temp;
}
}
}
}
Insertion sort
Abstract solution:
Insertion sort works by sorting the first two elements and then inserting the third element in its
proper place so that all three are sorted. Repeating this process, k+1 st element is inserted in its
proper place with respect to the first k elements (which are already sorted) to make all k+1
elements sorted. Finally, 'n' th element is inserted in its proper place with respect to the first n-1
elements so all n elements are sorted.
https://www.freshersnow.com/
Pseudo-code conventions
1. Comments begin with // and continue until the end of line.
2. Blocks are indicated with matching braces:{ and }. A compound statement (i.e., a
collection of simple statements) can be represented as a block. The body of a
procedure also forms a block. Statements are delimited by;.
3. An identifier begins with a letter. The data types of variables are not explicitly
declared. The types will be clear from the context. Whether a variable is global or
local to a procedure will also be evident from the context. We assume simple data
types such as integer, float, char, Boolean, and so on. Compound data types can
be formed with records. Here is an example:
Node = record
{ datatype_1 data_1;
::::::::::::::::::::
Datatype_n data_n;
node *link;
}
In this example, link is a pointer to the record type node. Individual data items of
a record can be accessed with and period. For instance if p points to a record of
type node, p data_1 stands for the value of the first field in the record. On the
other hand, if q is a record of type node, q. dat_1 will denote its first field.
4. Assignment of values to variables is done using the assignment statement
(variable) :=(expression);
5. There are two Boolean values true and false. In order to produce these values,
the logical operators and, or, and not and the relational operators
<, ≤, =, ≠ , ≥ , and > are provided.
6. Elements of multidimensional arrays are accessed using [ and ]. For example, if A
is a two dimensional array, the (i,j) the element of the array is denoted as A[i,j].
Array indices start at zero.
7. The following looping statements are employed: for, while, and repeat- until.
The while loop takes the following form:
While (condition) do
{
(statement 1)
::::::::::::::
(statement n)
}
https://www.freshersnow.com/
As long as (condition) is true, the statements get executed. When (condition)
becomes false, the value of (condition) is evaluated at the top of loop.
The general form of a for loop is
for variable := value 1 to value 2 step step do
{
(statement 1)
::::::::::::::
(statement n)
}
Here value1, value2, and step are arithmetic expressions. A variable of type
integer or real or a numerical constant is a simple form of an arithmetic
expression. The clause “step step” is optional and taken as +1 if it does not occur.
Step could either be positive or negative variable is tested for termination at the
start of each iteration. The for loop can be implemented as a while loop as
follows:
Variable := value1;
fin :=value2;
incr:=step;
while ((variable –fin) * step ≤,0) do
{
(statement 1)
:::::::::::::::
(statement n)
}
A repeat-until statement is constructed as follows :
repeat
(statement 1)
:::::::::::::::
(statement n)
Until (condition)
The statements are executed as long as (condition) is false. The value of
(condition) is computed after executing the statements.
The instruction break; can be used within any of the above looping instructions to
force exit. In case of nested loops, break; results in the exit of the innermost loop
that it is a part of. A return statement within any of the above also will result in
exiting the loops. A return statement results in the exit of the function itself.
8. A conditional statement has the following forms:
if (condition) then (statement)
if (condition) then (statement 1) else (statement 2)
Here (condition) is a boolean expression and (statement),(statement 1),
https://www.freshersnow.com/
and (statement 2) are arbitrary statements (simple or compound).
We also employ the following case statement:
Case
{
:(condition 1 ) : (statement 1)
:::::::::::::::
:(condition n ) : (statement 1)
:else : (statement n+1)
}
Here (statement 1) and (statement 2), etc. could be either simple statements or
compound statements. A case statement is interpreted as follows. If (condition 1)
is true, (statement 1) gets executed and the case statements is exited. If
(statement 1) is false,(condition 2) is evaluated. If (condition 2) is true,
(statement 2) gets executed and the case statement exited, and so on. If none of
the conditions (condition 1),…,(condition n) are true, (statement n+1) is executed
and the case statement is exited. The else clause is optional.
9. Input and output are done using the instructions read and write, No format is
used to specify the size of input or output quantities.
10. There is only one type of procedure: Algorithm. An algorithm consists of a
heading and a body. The heading takes the form.
Algorithm Name ((parameter list))
Where Name is the name of the procedure and ((parameter list)) is a listing of the
procedure parameters. The body has one or more (simple or compound)
statements enclosed within braces { and }. An algorithm may or may not return
any values. Simple variables to procedures are passed by value. Arrays and
records are passed by reference. An array name or a record name is treated as a
pointer to the respective data type.
As an example. The following algorithm finds and returns the maximum of n
given numbers:
Algorithm Max (A, n)
// A is an array of size n.
{
Result := A[1];
for i: = 2 to n do
If A[i] > Result then Result := A[i];
Return Result;
}
https://www.freshersnow.com/
RECURSION
Definition: It is the process of repeating items in a self similar way.
Example 1:
DEFINITION(Computer Science): This is the method of defining a function in which the function
being defined is applied within its own definition.
In simple terms if a function makes a call to itself then it is known as Recursion.
Example3: Observe the recursive definition for a function Suml(n) to compute
Sum of first n natural numbers.
If n=1 then
return 1
else
return n + Sum(n-1)
https://www.freshersnow.com/
Steps to be followed to write a Recursive function
2.RECURSIVE STEP: Contains recursive definition for all other cases(other than base case)to reduce
them towards the base case
Note: if any one of the two or both, wrongly defined makes the program infinitely Recursive.
Algorithm RecursiveSum(a,k)
{
if (k<=0) then
Return 0.0;
else
Return (RecursiveSum(a,k-1) + a[k]);
}
https://www.freshersnow.com/
RecursiveSelectionSort (a,1);
https://www.freshersnow.com/
Recursive solution for the InsertionSort
First call of recursion is:
RecursiveInsertionSort(a,n);
Algorithm RecursiveInsertionSort(a,k)
{
if (k > n)
Return; // Do nothing
else
{
RecursiveInsertionSort(a,k-1);
//Insert 'k'th element into k-1 sorted list
value := a[k]; j := k - 1;
done := false;
repeat
if a[j] > value
{
a[j + 1] := a[j]; j := j - 1;
if (j < 0) done := true;
}
else done := true;
until done;
a[j + 1] := value;
}
}
Algorithm bin(int n)
{
if (n=1 or n=0)
{
write n;
https://www.freshersnow.com/
return;
}
else
bin=n/2;
write n mod 2;
}
Rules:
(1) Can only move one disk at a time.
(2) A larger disk can never be placed on top of a smaller disk.
(3) May use third post for temporary storage.
https://www.freshersnow.com/
{
TowersOfHanoi (numberOfDisks -1, x,z, y);
move (start, end);
TowersOfHanoi (numberOfDisks -1, z, y, x);
}
}
Performance Analysis
Space Complexity:
Algorithm abc (a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}
The Space needed by each of these algorithms is seen to be the sum of the following
component.
1. A fixed part that is independent of the characteristics (eg: number, size)of the inputs and
outputs. The part typically includes the instruction space (ie. Space for the code), space for
simple variable and fixed-size component variables (also called aggregate) space for constants,
and so on.
2. A variable part that consists of the space needed by component variables whose size is
dependent on the particular problem instance being solved, the space needed by referenced
variables (to the extent that is depends on instance characteristics), and the recursion stack
space.
The space requirement s(p) of any algorithm p may therefore be written as,
S(P) = c+ Sp(Instance characteristics)
Where ‘c’ is a constant.
Example 2:
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do
s= s+a[I];
return s;
}
The problem instances for this algorithm are characterized by n, the number of elements to
be summed. The space needed d by ‘n’ is one word, since it is of type integer.
The space needed by ‘a’ a is the space needed by variables of type array of floating point
numbers.
This is at least ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be
summed.
So, we obtain Sum(n)>=(n+s)
[ n for a[ ],one each for n, I a& s]
Alogorithm RSum(a,n)
{
https://www.freshersnow.com/
If(n<= 0) then
return 0.0;
Else
return RSum(a,n-1)+a[n];
}
Recursive function for Sum
Time Complexity:
A Program step is loosely defined as a syntactically or semantically meaningful segment of a
program that has an execution time that is independent of the instance characteristics.
For Ex: Return a+b+b*c+(a+b-c)/(a+b)+4.0;
Algorithm RSum(a,n)
{
Count :=count+1; // For the if conditional
if (n < 0)then
{
count:=count+1//For the return
return 0.0;
}
else
https://www.freshersnow.com/
{
count :=count+1 // For the addition, function invocation and return
return RSum(a,n-1)+a[n];
}
}
Algorithm Recursive sum with count statements added
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
{
count :=count +1; // For ‘for i’
for j :=1 to n do
{
count :=count+1; // For ‘for j’
c[i, j] := a[i, j]+b[i, j];
count :=count +1; //For the assignment
}
count := count +1 ;// For loop initialization and last time of ‘for j’
}
count := count+1 ; // For loop initialization and last time of ‘for i’
}
Algorithm for Matrix addition with counting statements
https://www.freshersnow.com/
Statement s/e Frequency total steps
n=0 n>0 n=0 n>0
1.Algorithm RSum(a,n) 0 - - 1. 0
2.{
3. if(n< 0) then 1 1 1 1 1
4. return 0.0: 1 1 0 1 0
5. else return
6.RSum (a,n-1) + a[n]; 1+x 0 1 0 1+x
7. } 0 --- --- 0 0
Total 2 2+x
x = tRSum (n-1)
https://www.freshersnow.com/
Statement s/e frequency Total steps
Algorithm Linear search (a, req_ele)
{
Flag=0
for i := 1 to n do
{
If (a[i] = req_ele) then
{
Flag=1;
Return i;
}
}
If (flag) then
Write “element found at i position”
Else
Write “element not found
}
Total
https://www.freshersnow.com/
Kinds of Analysis of Algorithms
• The Priori Analysis is aimed at analyzing the algorithm before it is implemented(based on the
algorithm) on any computer. It will give the approximate amount of resources required to solve the
problem before execution. In case of priori analysis, we ignore the machine and platform dependent
factors. It is always better if we analyze the algorithm at the earlier stage of the software life cycle.
Priori analysis require the knowledge of
– Mathematical equations
– Determination of the problem size
– Order of magnitude of any algorithm
Asymptotic notations(O,Ω,Ө)
Step count is to compare time complexity of two programs that compute same function and also to
predict the growth in run time as instance characteristics changes. Determining exact step count is
difficult and not necessary also. Since the values are not exact quantities we need only comparative
statements like c1 n2 ≤ tp(n) ≤ c2n2.
For ex: consider two programs with complexities c1n2 + c2 n and c3 n respectively. For small values
of n, complexity depend upon values of c1, c2 and c3. But there will also be an n beyond which
complexity of c3 n is better than that of c1 n2 + c2 n.This value of n is called break-even point. If this
point is zero, c3n is always faster (or at least as fast).
https://www.freshersnow.com/
Function Name
1 Constant
log n Logarithmic
N Linear
n log n n log n
n2 Quadratic
n3 Cubic
2n Exponential
n! Factorial
Definition [Big ‘oh’] The function f(n)=O(g(n)) iff there exist positive constants c and no such that
f(n)≤c*g(n) for all n, n ≥ no.
Ex1: f(n) = 2n + 8, and g(n) = n2. Can we find a constant c, so that 2n + 8 <= n2? The number 4 works
here, giving us 16 <= 16.
For any number c greater than 4, this will still work. Since we're trying to generalize this for large
values of n, and small values (1, 2, 3) aren't that important, we can say that f(n) is generally faster than
g(n); that is, f(n) is bound by g(n), and will always be less than it.
Ex7:3n+2≠O(1) as3n+2 not less than or equal to c for any constant c and all n≥n0
Ex 8: 10n2+4n+2≠O(n)
Definition[Omega] The function f(n) =Ώ (g(n) (read as “f of n is omega of g of n'') iff there exist
positive constants c and n0 such that f(n) ≥c *g(n) for all n, n≥ no.
https://www.freshersnow.com/
Ex1: The function 3n+2=Ω(n) as 3n+2≥3n for n≥1
(the inequality holds for n≥0,but the definition of Ω requires an n0>0).
3n+2=Ω(n) as 3n+2≥3n for n≥1.
Definition [Theta] The function f(n) = Θ (g(n) ) (read as “f of n is theta of g of n'') iff there exist
positive constants C1,C2, and n0 such that c1g(n)≤ f(n) ≤ c2g(n) for all n, n≥n0
Pb 1: 6n+4= Θ(___) as 6n+4≥________for all n ≥_______and 6n+4 ≤ _______ for all n ≥_______
Ex 2 : 3n + 3 = Ө(n)
Ex 3 : 10n2 + 4n +2 = Ө(n2)
Ex 4: 6* 2n + n2 = Θ (2n)
Ex5:10*log n+4= Θ (log n)
Ex6:3n+2≠ Θ(1)
Ex7:3n + 3 = Ө(n)
Ex 8: 10n2 +4n+2 ≠ Θ (n)
https://www.freshersnow.com/
Theorem: If f(n) = am nm ++................+a3n3+a2n2+a1 n + ao and am > 0, then f(n)= Ω(nm)
proof : Left as an exercise.
Definition [Little ‘oh’] The Function f (n) =o(g(n)) (read as ‘f of n is little oh of g of n’)iff
lim f(n) =0
n→∞ g(n)
Example:
Definition [Little omega] The function f(n)= ω(g(n)) (read as “ f of n is little omega of g of n”) iff
lim g(n) = 0
n→∞ f(n)
https://www.freshersnow.com/
Divide and Conquer
Divide and Conquer for finding Counterfeit coin (exactly one coin):
Algorithm CounterfeitCoin(Bunch, numberofcoins)
{
if (numberofcoins = 2) {
weigh the two coins;
if one of them weighs less that is counterfeit coin;
else, no counterfeit coin in the Bunch
}
else {
Divide Bunch into two halves Bunch1 and Bunch2;
if Bunch1 weighs less than Bunch 2, call CounterfeitCoin(Bunch1, numberofcoins / 2);
else, call CounterfeitCoin(Bunch2, numberofcoins / 2);
}
}
Divide and Conquer algorithm design works on the principle of dividing the given problem into
smaller sub problems which are similar to the original problem. The sub problems are ideally of
the same size.
The Divide and Conquer strategy can be viewed as one which has three steps. The first step is
called Divide which is nothing but dividing the given problems into smaller sub problems which
are identical to the original problem and also these sub problems are of the same size. The
second step is called Conquer where in we solve these sub problems recursively. The third step
https://www.freshersnow.com/
is called Combine where in we combine the solutions of the sub problems to get the solution for
the original problem.
Algorithm BinSrch(a,i,l,x) {
// Given an array a[I:l] of elements in non decreasing order, 1≤ i ≤ l,
// determine whether x is //present and if so, return j such that x =a[j]; else return 0.
if (l=i) then { // if Small(P)
if (x=a(i)) then return i; else return 0;
}
else {
// Reduce P into a smaller subproblem.
mid :=[(i + l)/2 ];
if (x=a[mid]) then return mid;
else if (x<a[mid] then
return BinSrch (a,i, mid -1,x);
else return BinSrch (a, mid +1,l,x);
}
}
Iterative Binary Search (non-recursive)
https://www.freshersnow.com/
if ( x<a[mid]) then high :=mid – 1;
else if (x>a[mid]) then low :=mid + 1;
else return mid;
}
return 0;
}
Theorem:-If n is in the range [2k_1, 2k), then BinSearch makes at most k element comparisons
for a successful search and either k —1 or k comparisons for an unsuccessful search. (In other
words the time for a successful search is 0 (log n) and for an unsuccessful search is (log n).
Proof: Consider the binary decision tree describing the action of BinSearch on n elements. All
successful searches end at a circular node whereas all unsuccessful searches end at a square node.
If 2 k-1 ≤ n <2k, then all circular nodes are at levels 1, 2,... , k whereas all square nodes are at levels k
and k + 1 (note that the root is at level 1). The number of comparisons needed to terminate at a
circular node on level i is i whereas the number of element comparisons needed to terminate at a
square node at level i is only i — 1. The theorem follows.
Merge Sort
Merge sort is yet another sorting algorithm which works on the Divide and Conquer design
principle.
• Merge sort works by dividing the given array into two sub arrays of equal size
• The sub arrays are sorted independently using recursion
• The sorted sub arrays are then merged to get the solution for the original array.
https://www.freshersnow.com/
The breaking of the given input array into two sub arrays of equal size is part of the Divide step.
The recursive calls to sort the sub arrays are part of the Conquer step. The merging of the sub
arrays to get the solution for the original array is part of the Combine step.
The basic operation in Merge sort is comparison and swapping. Merge Sort Algorithm calls it
self recursively. Merge Sort divides the array into sub arrays based on the position of the
elements whereas Quick Sort divides the array into sub arrays based on the value of the
elements. Merge Sort requires an auxiliary array to do the merging (Combine step). The
merging of two sub arrays, which are already sorted, into an auxiliary array can be done in O(n)
where n is the total number of elements in both the sub arrays. This is possible because both the
sub arrays are sorted.
https://www.freshersnow.com/
for k:=j to high do
{ b[i] : =a[k]; i:=i+1; }
else for k :=h to mid do
{ b[i] := a[k]; i:= i+1; }
for k: =low to high do a[k] :=b[k];
}
Complexity of Merge Sort is O(n log n) and binary search is O(log n).
Binary Search:
Let T(n) the time used to search n elements. As we need to search only one of the halves, the
Recurrence relation is : T(n) = T(n/2) + c
In the same way: T(n/2) = T(n/4) + c, so T(n) = T(n/4) + 2c.
Going in this way ...
T(n) = T(n/2m) + mc, and
QuickSort:
Quick sort is one of the most powerful sorting algorithms. It works on the Divide and Conquer
design principle. Quick sort works by finding an element, called the pivot, in the given input
array and partitions the array into three sub arrays such that the left sub array contains all
elements which are less than or equal to the pivot. The middle sub array contains the pivot. The
https://www.freshersnow.com/
right sub array contains all elements which are greater than the pivot. Now, the two sub arrays,
namely the left sub array and the right sub array are sorted recursively.
The partitioning of the given input array is part of the Divide step. The recursive calls to sort
the sub arrays are part of the Conquer step. Since the sorted sub arrays are already in the right
place there is no Combine step for the Quick sort.
14. repeat
15. j : = j – 1;
16. until (a[j] ≤ v);
https://www.freshersnow.com/
18. } until (I ≥ j);
1. Algorithm Interchange ( a , i, j)
2. //Exchange a[i] with a [j]
3. {
4. p : = a[i];
5. a [i] : = a[j]; a[j] : p;
6. }
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i (p)
65 70 75 80 85 60 55 50 45 +∞ 2 9
65 45 75 80 85 60 55 50 70 +∞ 3 8
65 45 50 80 85 60 55 75 70 +∞ 4 7
65 45 50 55 85 60 80 75 70 +∞ 5 6
65 45 50 55 60 85 80 75 70 +∞ 6 5
60 45 50 55 65 85 80 75 70 +∞
https://www.freshersnow.com/
Greedy Algorithms
Greedy algorithms – overview
The Greedy approach helps in constructing a solution for a problem through a sequence of steps
where each step is considered to be a partial solution. This partial solution is extended
progressively to get the complete solution.
In the greedy approach each step chosen has to satisfy the constraints given in the problem. Each
step is chosen such that it is the best alternative among all feasible choices that are available. The
choice of a step once made cannot be changed in subsequent steps.
Suppose, we want to make change for an amount ‘A’ using fewest no of currency notes. Assume
the available denominations are Rs 1, 2, 5, 10, 20, 50, 100, 500, 1000.
To make a change for A=Rs 28, with the minimum number of notes, one would first choose a
note of denomination Rs 20, 5, 2 and 1.
Denomination table
for Rs 28 for Rs 783 for Rs 3799
1000 X 0 0 1000 X 1000 X
500 X 0 0 500 X 500 X
100 X0 0 100 X 100 X
50 X 0 0 50 X 50 X
20 X 1 20 20 X 20 X
10 X 0 0 10 X 10 X
5X1 5 5X 5X
2X1 2 2X 2X
1X1 1 1X 1X
Total 28 Total Total
https://www.freshersnow.com/
SA := SA + denom_value[i];
}
else {
i++;
}
}
Print denom _select
}
In Greedy method the problems have 'n' inputs called as candidate set, from which a subset is
selected to form a solution for the given problem. Any subset that satisfies the given constraints
is called a feasible solution. We need to find a feasible solution that maximizes or minimizes an
objective function and such solution is called an optimal solution.
In the above ex currency notes denomination set { 1000 ….1000 ,500….500, 100….100,
50…50, 20…20,10…10,5…5,2..2,1…1}is candidate set.
In the above ex constraint is our solution make the exact target amount of cash. Hence, any
feasible solution i.e. sum of selected notes should be equal to target amount.
In the above ex objective function is our solution should consist of the fewest number of
currency notes. Hence, any optimal solution which is one of the feasible solutions that optimizes
the objective function. There can be more than one optimal solution.
1) Select: it selects an input from array a[ ] (candidate set) and puts in the variable x.
2) Feasible: it is a Boolean function which checks whether the selected input meets the
constraints or not.
3) Union: if the selected input i.e. 'x' makes the solution feasible, then x is included in the
solution and objective function get updated.
https://www.freshersnow.com/
Characteristics of Greedy:
2) They take decisions on the basis of information at hand without worrying about the effect
these decisions may have in the future.
Knapsack problem:
A thief robbing a store finds n items, the items each worth vi rupees and weights wi grams,
where vi and wi are positive numbers. He wants to take as valuable load as possible but he
can carry at most w grams in his knapsack(bag). Which item should he take?
1) 0-1 knapsack problem: Here the items may not be broken into smaller pieces, so thief may
decide either to take an item or to leave to it(binary choice). It cannot be efficiently solved by
greedy algorithm
2) Fractional (General) Knapsack problem: Here thief can take the fraction of items, meaning
that the items can be broken into smaller pieces so that thief may be able to carry a fraction x i of
item i. This can be solved easily by greedy.
Maximize ∑ Pi xi …………………………………..(1)
Subject to ∑ Wi xi ≤ m………………………………………..(2)
The profit and weights are positive numbers. A feasible solution is any set
(x1,x2,………………..xn) satisfying (2) and(3). An optimal solution is feasible solution for which
(1) is maximized.
n=3, m=20 , (P1, P2, P3) =(25, 24, 15) & (w1, w2,w3) = (18,15,10)
Note that knapsack problem calls for select a subset of the objects hence fits the subset paradigm.
https://www.freshersnow.com/
1) We can try to fill the knapsack by including the object with largest profit(greedy approach to
the profit) .If an object under consideration does not fit, then a fraction of it is included to fit the
knapsack. Object 1 has the largest profit value.P1=25. So it is placed into the knapsack first. Then
x1=1 and a profit of 25 is earned. Only 2 units of knapsack capacity are left. Objects 2 has the
next largest profit P2=24. But W2 =15 & it does not fit into the knapsack. Using x2 =2/15 fills the
knapsack exactly with the part of the object 2.
The method used to obtain this solution is termed a greedy method at each step, we chose to
introduce that object which would increase the objective function value the most.
(x1 , x2 , x3) ∑ wi xi ∑ pi xi
(1 , 2/15 , 0) 20 28.2
This is not an optimal solution.
2)We apply greedy approach by choosing value per unit weight is as high as possible
Item(n) Value(p1,p2,p3) Weight(w1,w2,w3) Val/weight
1 25 18 1.388
2 24 15 1.6
3 15 10 1.5
Here p2/w2 > p3/w3 > p1/w1. Now the items are arranged into non increasing order of p i/wi.
Second item is the most valuable item. We chose item 2 first. Item 3 is second most valuable
item. But we cannot choose the entire item3 as the weight of item 3 exceeds the capacity of
knapsack. We can take ½ of the third item. Therefore the solution is x1 = 0, x2 = 1, x3 = ½ and
maximum profit is ∑ pixi = 0*25 + 1*24 + ½ * 15 = 31.5
If the items are already arranged in non increasing order of pi/wi , then the function
greedy knapsack obtains solution corresponding to this strategy.
https://www.freshersnow.com/
if (i <= n) then x[i] := U / w[i];
}
If the items are already sorted into decreasing order of vi/wi, then time complexity is
O(n)
Therefore Time complexity including sort is O(n log n)
A tree is defined to be an undirected, acyclic and connected graph (or more simply, a
graph in which there is only one path connecting each pair of vertices).
Application of MST
1) practical application of a MST would be in the design of a network. For instance, a group
of individuals, who are separated by varying distances, wish to be connected together in a
telephone network. MST can be used to determine the least costly paths with no cycles in
this network, thereby connecting everyone at a minimum cost.
2) Another useful application of MST would be finding airline routes MST can be applied to
optimize airline routes by finding the least costly paths with no cycles
https://www.freshersnow.com/
28
1 1
2 10 2
10 14 16
16
14
6 3 6 7 3
24 7
25 18 25 5 12
12
22 4
5
22 4
(a) (b)
Prim's algorithm finds a minimum spanning tree for a connected weighted graph. This means
it finds a subset of the edges that forms a tree that includes every vertex, where the total
weight of all the edges in the tree is minimized.
Steps
Ex:
https://www.freshersnow.com/
Minimum spanning tree using Prim’s algorithm can be formed as follows.
Algorithm Prim(E,cost,n,t)
https://www.freshersnow.com/
mincost := mincost + cost[j,near [j]];
near[j]:=0;
for k:=1 to n do // Update near[ ]
if((near[k]≠0) and (cost[k,near[k]] > cost[k,j]))then
near[k]:=j;
}
return mincost;
}
Kruskal’s Algorithm
Kruskal's algorithm is another algorithm that finds a minimum spanning tree for a connected
weighted graph. If the graph is not connected, then it finds a minimum spanning forest (a
minimum spanning tree for each connected component).
Kruskal's Algorithm builds the MST in forest. Initially, each vertex is in its own tree in
forest. Then, algorithm considers each edge in turn, order by increasing weight. If an edge (u,
v) connects two different trees, then (u, v) is added to the set of edges of the MST, and two
trees connected by an edge (u, v) are merged into a single tree on the other hand, if an edge
(u, v) connects two vertices in the same tree, then edge (u, v) is discarded. The resultant may
not be a tree in all stages. But can be completed into a tree at the end.
t = EMPTY;
while ((t has fewer than n-1 edges) && (E != EMPTY))
{
choose an edge(v, w) from E of lowest cost;
delete (v, w) from E;
if (v, w) does not create a cycle in t
add (v, w) to t;
else
discard (v, w);
}
To check whether there exist a cycle, place all vertices in the same connected component of t
into a set. Then two vertices v and w are connected in t then they are in the same set.
https://www.freshersnow.com/
Example:
Minimum spanning tree using Kruskal’s algorithm can be formed as given below.
Kruskal’s Algorithm
Float kruskal (int E[ ][ ], float cost[ ][ ], int n, int t[ ][2])
{
int parent[w];
consider heap out of edge cost;
for (i=1; i<=n; i++)
parent[i] = -1; //Each vertex in different set
i=0;
mincost = 0;
while((i<n-1) && (heap not empty))
https://www.freshersnow.com/
{
Delete a minimum cost edge (u,v) from the heap and re heapify;
j = Find(u); k = Find(v); // Find the set
if (j != k)
{
i++;
t[i][1] = u;
t[i][2] = v;
mincost += cost[u][v];
Union(j, k);
}
if (i != n-1)
printf(“No spanning tree \n”);
else
return(mincost);
}
}
45
1 2 3
15
35 30
10 20 20
4 5 6
15 3
Path Length
1) 1.4 10
2) 1.4.5 25
3) 1.4.5.2 45
4) 1.3 45
https://www.freshersnow.com/
Greedy algorithm to generate shortest paths
Boston
5
San Francisco Chicago 1500 250
4
2 800 1000 New York
3 6
300 Denver 1400 900
1000
1700
1
8
Los Angeles 1000 7
(a) Digraph
https://www.freshersnow.com/
1 2 3 4 5 6 7 8
1. 0
2. 300 0
3. 100 800 0
4. 1200 0
5. 1500 0 250
6. 1000 0 900 1400
7. 0 1000
8. 1700 0
(a) Length – adjacency matrix
https://www.freshersnow.com/
Dynamic Programming (DP)
Dynamic Programming is a design principle which is used to solve problems with
overlapping sub problems.
It is used when the solution to a problem can be viewed as the result of a sequence of
decisions. It avoids duplicate calculation in many cases by keeping a table of known
results and fills up as sub instances are solved.
In Dynamic Programming We usually start with the smallest and hence the simplest sub-
instances. By combining their solutions, we obtain the answers to sub-instances of
increasing size, until finally we arrive at the solution of the original instance.
It follows a bottom-up technique by which it start with smaller and hence simplest sub
instances. Combine their solutions to get answer to sub instances of bigger size until we
arrive at solution for original instance.
Divide and conquer is a top-down method. When a problem is solved by divide and
conquer, we immediately attack the complete instance, which we then divide into smaller
and smaller sub-instances as the algorithm progresses. The difference between Dynamic
Programming and Divide and Conquer is that the sub problems in Divide and Conquer
are considered to be disjoint and distinct where as in Dynamic Programming they are
overlapping.
Principle of Optimality
An optimal sequence of decisions has the property that whatever the initial state and
decisions are, the remaining decisions must constitute an optimal decision sequence with
regard to the state resulting from the first decision.
In other words, principle of optimality is satisfied when an optimal solution is found for a
problem then optimal solutions are also found for its sub-problems also.
General Method
The Idea of Developing a DP Algorithm
https://www.freshersnow.com/
Step 3: Bottom-up computation: Compute the value of an optimal solution in a bottom-up
fashion by using a table structure.
Step 4: Construction of optimal solution: Construct an optimal solution from computed
information.
Steps 3 and 4 may often be combined.
Remarks on the Dynamic Programming Approach
Steps 1-3 form the basis of a dynamic-programming solution to a problem. Step 4 can be
omitted if only the value of an optimal solution is required.
cost(i, i) = 0,
cost(i, j) is the length of the edge <i, j> if <i, j> E and
The graph allows edges with negative cost value, but negative valued cycles are not allowed.
All pairs shortest path problem is to find a matrix A such that A[i][j] is the length of the
shortest path from i to j.
Consider a shortest path from i to j, i≠j. The path originates at i goes through possibly many
vertices and terminates at j. Assume that there is no cycles. If there is a cycle we can remove
them without increase in the cost because there is no cycle with negative cost.
Algorithm makes n passes over A. Let A0, A1, .. An represent the matrix on each pass.
Let Ak-1[i,j] represent the smallest path from i to j passing through no intermediate vertex
greater than k-1. This will be the result after k-1 iterations. Hence kth iteration explores
whether k lies on optimal path. A shortest path from i to j passes through no vertex greater
than k, either it goes through k or does not.
If it does not, then no intermediate vertex has index greater than k-1. Then Ak[i,j] = Ak-1[i,j]
https://www.freshersnow.com/
Cost adjacency matrix for the graph is as given below
Algorithm
https://www.freshersnow.com/
// n vertices; A[i, j] is the cost of a shortest path from vertex
// i to vertex j. cost[i, i] = 0.0, for 1≤ i≤n
for (int i=1; i<=n; i++)
A[i][j] = cost[i][j];
Maximize ∑ pi xi
The decisions on the xi are made in the order xn,xn-1 …x1. Following a decision on xn, we may
be in one of two possible states the capacity remaining in the knapsack is m and no profit has
accrued or the capacity remaining is m-wn and profit of pn has accrued). It is clear that the
remaining decisions xn-1,…..,x1 must be optimal w.r.t the problem state resulting from the
decision xn. Otherwise xn,…..x1 will not be optimal.
fn(m)=max{fn-1(m),fn-1(m-wn)+pn}
in general,
fi(y)=max{fn-1(y),fn-1(m-wi)+pi}
https://www.freshersnow.com/
Where Si is the set of all pairs for fi including (0,0) and fi is completely defined by the
pairs (pi,wi)
Si+1 may obtained by merging Si and S1 i. This merge corresponding to taking the make of
two functions fi-1(x) and fi-1(x-wi)+pi in the object function of 0/1 Knapsack problem
So, if one of Si-1 and S1i has a pair (pj, wj) and the other has a pair (pk,wk) and pj ≤ pkwhile
wj≥wk. Then the pair (pj ,wj) is discarded this rule is called purging or dominance rule when
generating Si all the pairs (p,w)with w>m may also be purged
If we try to remove ith item then profit of ith item is reduced from total profit and weight of ith
item is reduced from total weight which belongs to the profit and weight of (i-1)th item
Initially we start with S0= {0, 0} i.e profit =0 & weight=0 (bag is empty)
Addition
Then, S0 1 is obtained by adding P1, W1 to all pairs in S0. For general i, S I 1 is obtained by
adding Pi, Wi to S i-1 The following recurrence relation defines S I 1.
Take any two sets Si and Si 1 consisting of pairs(Pj , Wj) and (Pk ,Wk). The purging rule states
that if Pj≤ Pk and Wj ≥ Wk then (Pj, Wj) will be deleted. In other words, remove those pairs
that achieve less profit using up more weight.
In other words, the given profit, weight pairs are: (1,2) (2,3) and (4,3)
Addition:
Merging operation:
Si = Si-1 + Si-1 1
https://www.freshersnow.com/
= {(0,0),(1,2)}+{2,3)}
S12 = {(2,3),(3,5)}
S2 = S1 U S12 = {(0,0)(1,2)}U{(2,3),(3,5)}
S2 = {(0,0),(1,2),(2,3),(3,5)}
= {(0,0),(1,2),(2,3),(3,5)}+{(4,3)}
S1 3 = {(4,3),(5,5),(6,6),(7,8)}
= {(0,0),(1,2),(2,3),(3,5)}U{(4,3),5,5),(6,6),(7,8)}
={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6),(7,8)}
Tuple (7,8) is Discarded because the weight (8) exceeds max capacity of Knapsack m=6 so,
S3 ={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6)
This purging rule has to be applied for every Si. In this example, no purging was necessary in
S1 and S2.
S3 = {(0,0),(1,2),(2,3),(4,3),(5,5),(6,6)}
S3 = {(0,0),(1,2),(4,3),(5,5),(6,6)}
The searching process is done starting with the last tuple (P1, W1) of Sn. A set of 0/1 values
for xi ‘s such that ∑ pi xi=P1 and ∑ wi xi=W1 . This can be determined by carrying out a
search through the si s.
If (P1,W1) Sn-1 , we can set xn=0. Otherwise (if (P1, W1) not in S n-1), we set Xn to 1.
Then, we will continue this search recursively through all Sn-1, Sn-2, …., S0 with either (P1,
W1) in the case xn = 0 or (P1-pn,W1-wn) in the case xn=1.
In the above example, last tuple in S3 is (6,6) but (6, 6) S2. Hence x3=1.
https://www.freshersnow.com/
(2, 3) S2 but (2,3) S1 So, x2=1.
Therefore the optimal sol is {x1=0,x2=1,x3=1} meaning weight 2 and weight 3 are taken and
weight 1 is ignored.
https://www.freshersnow.com/
Use eq. (1) to get g (i, S) for |S| =1. Then find g(i, S) with |S|=2 and so on.
APPLICATION:
1. Suppose we have to route a postal van to pick up mail from the mail boxes located at
‘n’ different sites.
2. An n+1 vertex graph can be used to represent the situation.
3. One vertex represents the post office from which the postal van starts and return.
4. Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
5. The route taken by the postal van is a tour and we are finding a tour of minimum
length.
6. Every tour consists of an edge <1,k> for some k V-{} and a path from vertex k to
vertex 1.
7. The path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
8. the function which is used to find the path is
g(1,V-{1}) = min{ cij + g(j,S-{j})}
1. Find g(i,) =ci1, 1≤ i <n, hence we can use equation(2) to obtain g(i,s) for all s to size
1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3. Then s=2, and we have to proceed until |s| <n-1.
4. for example consider the graph.
https://www.freshersnow.com/
starting position
From eq g(i,s) =min{cij +g(j,s-{j}) we obtain g(i,S) for all S of size 1,2…
When |s|<n-1, the values of I and S for which g(i,S) is needed such that
i 1, 1 s and i s.
|s| =0
i =1 to n. g(i, )=ci1,1≤ i≤ n
g(1,) = c11 => 0; g(2,) = c21 => 5; g(3,) = c31 => 6; g(4,) = c41 => 8
|s| =1
i =2 to 4
| s| =2
i 1, 1 s and i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
= min{13+18,12+13} = min{31,25} = 25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
|s| = 3
https://www.freshersnow.com/
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
optimal cost is 35
if the following are Two Binary Search Trees (B S T ) For given set S. here assumption is
each word has same probability no unsuccessful search
for
for
do int
do
whil
e
if whil
ee
int
if
(a) (b)
We require 4 comparisons to find out an identifier in worst case in fig(a).But in fig (b) only 3
comparisons on avg the two trees need 12/5 ,11/5 comparisons
https://www.freshersnow.com/
In General, we can consider different words with different frequencies (probabilities) and un
successful searches also
Let Given set of words are {a1, a2, a3, …….an} With a1< a2 <….. an
∑p(i) + ∑q(i) =1
1≤i≤n 0≤i≤n
To obtain a cost function for BST add external nodes in the place of every empty sub
true. These nodes called external nodes.
for
for
do int
do wh
ile
int
if wh
ile
if
If BST represents ‘n’ identifiers then there will be exately ‘n ‘ internal nodes and n+ 1
external nodes every internal node represent a point where successful search may terminate
and every external node represents a point where an unsuccessful search may terminates
If a successful search terminates at an internal node at level L, then L interations are required.
So expected cost for the internal node for ai is P(i) * Level (ai)
For unsuccessful search terminate at external node the words which are not in BST can be
partioned into n+1 Equalnce classes. Equvalence class E0 represents least among all the
identifers in BST& The equalence classes ‘En’ represents greatest among all Elements in BST
Hence search terment at level Ei-1
1≤i≤n 0≤i≤n
https://www.freshersnow.com/
problem:
Let P(i)=q(i)=1/7, for all i in the BST The following word set(a1,a2,a3)= (do,it,while)
do
Whil if
e
if do Whil if
e
Whil
do e
(a) (b) (c)
do
While
do While
if
if
(d) (e)
= 15/7
= 13/7
Tree b is optimal
cost(Tree a)=2.05
https://www.freshersnow.com/
cost (Tree c)= 1*0.5 + 2*0.1 + +3*0.05+3*0.15+3*0.11+2*0.05 +1 * 0.05
=1.75
ak a2
a1 a3
E0…….Ek-1 Ek ………..En
1≤i≤k-1 0≤i≤k-1
l=i+1
https://www.freshersnow.com/
c(0,n) = min∑ { c (0,k-1) + c(k,n) + w(0,k-1) + w(k,n) + p(k) }
1≤k≤n
In General,
i≤k≤j
The above Eq can be solved for c (0,n) by first computing all c(i,j) such that j-1 = 1
Next we compute all c(i,j) of such that j-i=2 then c(i,j) with j-i=3 and so on.we record
all the values of r(i,j) of each tree then optimal BST can be computed from these r (i,j)
w(i,i) = q(i)
https://www.freshersnow.com/
VERTEX 1 2 3 4 5 6 7 8 9 10 11 12
COST 16 17 9 18 15 7 5 7 4 2 5 0
DISTANCE 2/3 7 6 8 8 10 10 10 12 12 12 12
https://www.freshersnow.com/
Backtracking
• Problem which deals with searching for a set of Solutions or which ask for an Optimal Solution
satisfying Some Constraints can be solved using Backtracking.
• The desired solution is expressible as an N-Tuple (X1, X2, _ _ _ _ _ _, Xn) where Xi are chosen from
some Finite Set Si. The problem to be solved calls for finding one vector that maximizes or
minimizes a criterion function P(X1 , X2, _ _ _ _ _ _, Xn) sometimes it seeks all vectors that satisfies
P.
• Let mi is the size of set Si then there are m = m1 . m2 . m3 . ………. mn n – Tuples that are possible
candidate sets.
• Brute force approach evaluates all possible m-Tuples
• Backtracking yields same problem with less than m trails
o The basic idea of Backtracking is to build up the Solution vector one component at a
time and
o to use modified criterion function Pi(X1, X2, _ _ _ _ _ _, Xi)(Bounding Function) to test
whether the vector being formed has any chance of success.
o The major advantage of this method is, If it is realized that the partial vector (X1, X2, _ _ _
_ _ _, Xi) can in no way lead to an optimal solution, then Mi+1, _ _ _ _ Mn possible vectors
can be ignored entirely.
• Many Problems solved using Backtracking require that all the solutions satisfy some complex set
of Constraints
o These are classified as
§ Explicit
§ Implicit
o Explicit constraints are rules that restrict each Xi to take on values only from a given set
o Implicit Constraints describe the way in which the Xi must relate to each other
Examples
In 8-Queens Problem the solution space contains 88 tuples 8-Tuples
Explicit Constraints
Si = {1,2,3,4,5,6,7,8}
Implicit Constraints
No two Xi’s can be the same (All Queens are in different Columns)
No two queens can be on the same Diagonal
• Solution Space is facilitated by Tree Organization called State Space Tree
https://www.freshersnow.com/
State Space tree for the 4-Queens Problem
1
3 8
4 6 9
5 7 1
0
Method 1:
As soon as a new child “C” of the current E Node “R” is generated, this child will become the
new E-Node. Then R will become E-node again when the sub-tree C has been Fully Explored.
(This is the DFS Strategy)
Method 2:
The E-Node will remain E-node until it is Dead.
In the both of the methods, Bounding Functions are used to kill Live Nodes with out generating all of
their Children.
Depth First Generation (Method 1) with Bounding Function is called Backtracking.
https://www.freshersnow.com/
Example of a backtrack solution to the 4-queens problem
Portion of the tree of State Space Tree for 4-Queens problem that is generated during back-tracking
1. Algorithm Backtrack(k)
2. // This schema describes the backtracking process using
3. // recursion. On entering, the first k-1 values
4. //x[1]x[2],….x[k-1] of the solution vector
5. //x [1: n] have been assigned. x [] and n are global.
6. {
7. for (each x[k] €T(x[1],…..,x[k-1])do
8. {
9. if (Bk(x[1],x[2],….,x[k])≠0)then
10. {
11. if(x[1],x[2],….,x[k] is a path to an answer node)
12. then write (x[1:k]);
13. if (k<n) then Backtrack(k+1);
14. }
15. }
16. }
https://www.freshersnow.com/
1. Algorithm IBacktrack(n)
2. // This schema describes the backtracking process.
3. // All solutions are generated in x{1: n} and printed
4. // as soon as they are determined.
5. {
6. k := 1;
7. while (k≠ 0) do
8. {
9. if (there remains an untried x[k] €T (x[1],x[2],…..,
10. x[k-1] and Bk(x[1],….,x[k])is true)then
11. {
12. if(x[1],….,x[k] is a path to an answer node)
13. then write(x[1:k]);
14. k :=k+1; // Consider the next set.
15. }
16. else k:= k-1;// Backtrack to the previous set.
17. }
18. }
In the above board the Diagonal (3,1) and (7,5) is normal Diagonal
Where as (1,7) and (4,4) is opposite Diagonal
https://www.freshersnow.com/
In Opposite Diagonals i+j = k+l
1. Algorithm place(k,i)
2. // Returns true if a queen can be placed in kth row and
3. //ith column. Otherwise it returns false. x[]is a
4. //global array whose first (k-1) values have been set.
5. //Abs(r) returns the absolute value of r.
6. {
7. for j :=1 to k-1 do
8. if((x[j] =i) // Two in the same column
9. or (Abs(x[j]-i) = Abs(j-k)))
10. // or in the same diagonal
11. then return false;
12. return true;
13. }
1. Algorithm N Queen(k,n)
2. // Using backtracking, this procedure prints all
3. //possible placement of n queens on an n x n
4. //chessboard so that they are nonattacking.
5. {
6. for i:=1 to n do
7. {
8. if place(k, i) then
9. {
10. x[k] := i;
11. if (k = n] then write (x[1 ; n]);
12. else NQueens (k+1,n);
13. }
14. }
15. }
https://www.freshersnow.com/
Sum of subsets problem:
• Suppose we are given n Distinct Positive numbers (Called Weights) and we desire to find all
the combinations of these numbers whose sums are m. This is called Sum of Sub Sets
Problem.
o Ex: n=4, (w1, w2, w3, w4) = (11, 13, 24, 7) & m = 31 then the possible subsets are
(11, 13, 7) & (24, 7)
• We could formulate this problem using either Fixed or variable sized Tuple
• In Fixed Sized Tuple representation the solution subset is represented by n-Tuple where the
elements in this solution vector are either 0 or 1. For Above example in fixed size Tuple is
(1,1,0,1) or (0,0,1,1)
• In Variable Size representation we represent our solution set with the index of the element
in the actual set
o For our above example our solution subsets are (1,2,4) or (3,4)
• We Consider the Backtrack solution using fixed Size representation.
https://www.freshersnow.com/
1. Algorithm SumOfSub(s, k ,r)
2. //Find all subsets of w[1 :n]that sum to m. The values of x[j],
3. //1≤j<k, have already been determined. s = ∑ k-1 w[j]* x[j]
j=1
4. //and r=∑nj=k w[j]’s are in nondecreasing order. n
5. //It is assumed that w[1]≤ m and=∑ni=1 w[i] ≥m
6. {
7. // Generate left child. Note: s+w[k]≤m since Bk-1 is true.
8. x[k]:=1
9. if (s+w[k]=m) then write (1:k); // Subset found
10. // There is no recursive call here as w[j] > 0,1≤j≤n.
11. else if (s+w[k]+w[k+1]≤m)
12. then Sum Of Sub(s+w[k],k+1,r-w[k]);
13. //Generate right child and evaluate Bk
14. if((s+r-w[k]≥m) and (s+w[k+1]≤m)) then
15. {
16. x[k]:=0;
17. Sum Of Sub(s, k+1,r-w[k]);
18. }
19. }
Recursive backtracking algorithm for sum of subset problem
Graph Coloring
• Let G be a Graph and m be a +ve integer
• Nodes of G must be colored with ‘m’ Colors such that no two adjacent nodes have the same
color yet only ‘m’ colors are used.
• This is termed “m-color ability Decision” Problem
• This Problem asks for the smallest integer m for which the graph G can be colored
• This Integer is referred to as the Chromatic number of the Graph
• Ex:
• For a Planar Graph the maximum number for the chromatic number is 4.
1. Algorithm mColoring(k)
2. // This algorithm was formed using the recursive backtracking
3. // schema. The graph is represented by its Boolean adjacency
4. // matrix G[1 :n, 1:n]. All assignments of 1,2,…, m to the
5. // vertices of the graph such that adjacent vertices are
6. // assigned distinct integers are printed. k is the index
7. // of the next vertex to color.
8. {
9. repeat
10. {// generate all legal assignments for x[k].
11. Next value (k);// Assign to x(k) a legal color.
12. if (x[k]=0) then return; // No new color possible
13. if(k=n) then // At most m colors have been
14. // used to color the n vertices.
15. write (x[k+1];
16. else mColoring(k+1)
17. } until ( false);
18. }
Finding all – m coloring of a graph
https://www.freshersnow.com/
• The NextValue(k) will assign the next possible color to the vertex k. This function will checks
the newly assigned color is not as the color of the adjacent vertex(which were already
assigned)
• mColoring(k) calls NextValue(k) and checks whether all vertices were colored(k=n or not), if
not it will calls mColoring(k+1)
Hamiltonian Cycle
• Let graph G=(V,E) be a connected graph with n vertices. Hamiltonian Cycle is a round trip
path along n edges of G that visits every vertex once and returns to its starting position.
• The backtracking solution vector(x1,x2 ……. xn) is defined so that xi represents the ith visited
vertex of the proposed cycle. Now all we need is determine how to compute the set of
possible vertices for xk if x1,x2 ……. xk-1 have already been chosen. If k =1 then x1 can be any
of the n vertices.
• To avoid printing the same cycle n times, we require that x1 =1.
• If 1<k<n, then xk can be any vertex v that is distinct from x1, x2 ……. xk-1 and v is connected by
an edge to xk-1. The vertex Xn can only be the one remaining vertex and it must be connected
to both xn-1 and x1.
https://www.freshersnow.com/
1. Algorithm Hamiltonian(k)
2. // This algorithm uses the recursive formulation of
3. // backtracking to find all the Hamiltonian cycles
4. //of a graph. The graph is stored as an adjacency
5. //matrix G[1: n,1:n]. All cycles begin at node 1.
6. {
7. repeat
8. { // Generate values for x[k]
9. Next value(k);// Assign a legal next value to x[k]
10. if(x[k]=0) then return;
11. if (k=n) then write (x[1:n]);
12. else Hamiltonian(k+1);
13. } until (false);
14. }
https://www.freshersnow.com/
Basic concepts
Decision Problems:
Example-1:
o Consider graph G = (V, E), find if there is a path from vertex ‘X’ to vertex ‘Y’ whose
length is at most 10.
Observation:
The above example problems are called decision problems, because the problem has
solution (answer), i.e. is either YES or NO.
Some decision problems can be solved efficiently, using time polynomial to the size of
the input of that problem
Generally ‘P’ is used to denote the set of all these polynomial-time solvable problems
Example:
Find the shortest path from vertex ‘X’ to vertex ‘Y’ for a graph G.
o First apply Dijkstra's algorithm and then give the correct answer
One more categorization of decision problems is to observe if the problem can be verified
in time polynomial toward the input size.
Example:
For the same graph G, if there is a path from ‘X’ to ‘Y’ with length is less than 10, then:
1. Find the sequence of vertices without repetition in any path from ‘X’ to ‘Y’ with
length is less than 10.
https://www.freshersnow.com/
Examples:
3. Given a set of numbers, can be divide them into two groups such that their sum is the
same (equal)?
For some problems, directly we cannot provide solution in polynomial time. Such
problems can be represented by ‘NP’.
o ‘P’ is polynomial-time
Examples:
Polynomial problems: searching, sorting and string editing – these problems need the
time O(log n), O(n log n) and O(mn) respectively.
NP-complete problems:
Example:
The decision versions of the Travelling sales person, graph coloring problem and
Hamiltonian cycle problem are NP-complete problems.
https://www.freshersnow.com/
Optimization problems whose decision versions are NP-complete are called NP-hard.
All NP-complete problems are NP-hard but some NP-hard problems are not known to be
NP-complete.
Deterministic algorithm:
The class ‘P’ consists of all problems that can be solved in polynomial time, O(Nk), by
deterministic computers.
A deterministic algorithm generates the same output for a problem instance every time
the algorithm is run
search(element, A, i)
if (A[i] == x)
then return i;
else
Let us say, input A = {7, 5, 3, 8, 9, 3} and x = 9 then the search begins from 7, then 5,
then 3 and so on until a successful search or element not found.
The complexity of the algorithm is O(n) since the input is not sorted.
Non-deterministic computer:
A nondeterministic computer has more than one output state and it “chooses” the correct
one, is known as “nondeterministic choice”.
https://www.freshersnow.com/
o Stage 1: Non-deterministic (“Guessing”) Stage: An arbitrary string ‘S’ is
generated that can be thought of as a candidate solution to the given instance ‘x’.
o Stage-I: a method makes a guess about the possible solution for problem A.
o Stage-II: a method checks if the guessed solution is really a solution for problem
A.
STAGE-1:
NonDeterministicSearch(x, A)
https://www.freshersnow.com/
i := NDS(x, A, 1); // calling function NDS()
return i;
STAGE-2:
NDS(x, A, i)
if (A[i] == x) then
return i;
else
Example-2:
Nondeterministic algorithm to sort the given element of an array A[1..n] is as shown below:
Algorithm NDSORT(A, B)
for i := 1 to n do
if (B[j]!= 0) then
failure();
endfor
https://www.freshersnow.com/
endfor
write(B);
success();
Note:
Many optimization problems can be stated as decision problems with the property that:
o The decision problem can be solved in polynomial time if and only if the
optimization problem can be solved in polynomial time.
Time complexity of a nondeterministic algorithm is O(f(n)), for all input size ‘n’ that
result in successful completion and the time required is at most cf(n) where ‘c’ and ‘n0’
are positive constants
Example-1:
Algorithm NDKnapsack( p, w, n, m, r, x)
for i := 1 to n do
failure();
else
https://www.freshersnow.com/
success();
The time complexity of this algorithm appears to be O(n)+O(m), where, ‘m’ is the length of
the input.
Hence, the algorithm’s time complexity is polynomial in the size of the input.
Example-2:
A formula is in conjunctive normal form (CNF) if it is of the form ci where, clause ci = lij
A formula is in disjunctive normal form (DNF) if it is of the form ci where, clause ci = lij
For example, (x3 ) (x1 ) is in CNF, then the DNF is (x1 x2) (x3 )
The satisfiability (SAT) problem is to search out whether or not a formula is true for a few
assignment of truth values to the variables.
Algorithm NDSAT(E, n)
success();
else
failure();
https://www.freshersnow.com/
}
NP-complete problems are the “hardest” problems in NP, if any NP-complete problem
can be solved in polynomial time, then all NP-complete problems can be solved in
polynomial time and in fact every problem in NP can be solved in polynomial.
Reducibility:
NP-Hard problems:
NP-Complete problems:
https://www.freshersnow.com/
NP-Hard problems are not NP-complete.
The decision problem HALTING returns TRUE, if, for a given input ‘I’ and a given
deterministic algorithm ‘A’; the algorithm ‘A’ terminates, otherwise it loops forever. There is no
algorithm of any type to solve HALTING.
o Construct an algorithm ‘A’ whose input is the encoding of a boolean circuit ‘B’
o ‘A’ tries all possible (2n, where ‘n’ is the number of input lines to the circuit)
combinations to simulate the circuit, and looks to see if the output is 1 for any of
the combinations. If it is, then ‘A’ halts. If not, then ‘A’ goes into an infinite loop
It is clear from the construction that ‘A’ halts exactly when ‘B’ is satisfiable, and loops
forever exactly when ‘B’ is not satisfiable.
https://www.freshersnow.com/
o The code in algorithm A for the step to generate the 2n combinations is O(n) long;
and if you assume that the number of gates in the boolean circuit is some constant
‘k’ times the number of inputs, then the code to simulate those gates is also O(n)
long
Note: We only need to consider the length of the code in algorithm ‘A’ and not it’s
running time, in order to have polynomial time reducibility.
We know that the HALTING problem is undecidable, i.e., there is no algorithm to solve
HALTING. By using this fact, we can prove by contradiction that HALTING is not in NP.
Let us say a polynomial time verification algorithm for HALTING, given an input string
‘x’ and an algorithm ‘AHNP’ such that A(x, y) = 1, with ‘x’ being an encoding of an
instance of HALTING that returns TRUE and ‘y’ being a certificate that is polynomial
length in ‘x’
Given such an AHNP(x, y), we can look at the input string xhalt whenever AHNP(xhalt,
y)=1 and conclude that xhalt includes the representation of an algorithm Ahalt that will halt
The algorithm AHNP can be modified to recognize encodings of all algorithms ‘A’ that do
terminate. But this contradicts the assumption that HALTING is undecidable.
Cook’s theorem
The Cook's theorem (Cook–Levin theorem) states that the boolean satisfiability
problem is NP-complete, i.e., any problem in NP can be reduced in polynomial time by
a deterministic Turing machine.
Proof:
https://www.freshersnow.com/
Let us say a NP decision problem D. And polynomial function P and a Turing machine M for
given any instance I (of length ‘n’) of D, together with a candidate certificate c, will check in
time no greater than P(n).
Let us consider the machine M with q states (0, 1, 2, .., q-1) and a tape alphabet (a1, a2, ..,
as)
Initially, the tape is inscribed with the problem instance on the squares 1, 2, .., n and the
putative certificate on the squares –m, .., -2, -1
Assume that, the machine halts scanning square 0, and that the symbol in this square at
that stage will be a1 if and only if the candidate certificate is a true certificate
Here, m <= P(n); since with a problem instance of length ‘n’ the computation is
completed in at most P(n) steps; during this process, the Turing machine head cannot
move more than P(n) steps to the left of its starting point
1. For i = 0, 1, .., P(n) and j = 0, 1, 2, .., q-1 the proposition Qij says that after ‘i‘
computation steps, M is in state ‘j’.
2. For i = 0, 1, .., P(n), j = -P(n), …, P(n) and k =1, 2, ..,s; the proposition Sijk says that
after ‘i’ computation steps, square ‘j’ of the tape contains the symbol ak.
3. For i = 0, 1, .., P(n), j = -P(n), …, P(n), the proposition Tij says that after ‘i’ computation
steps, the machine M is scanning square ‘j’ of the tape.
1. At each computation step, M is in at least one state. For each i = 0, 1, .., P(n) we have
the clause Qi0 V Qi1 V … Qi(q-1) giving (P(n)+1)q = O(P(n)) literals altogether.
2. At each computation step, M is in at most one state. For each i = 0, 1, .., P(n) and for
each pair j, k of distinct states, we have the clause (Qij ^ Qik), giving a total of q*(q-
1)*(P(n) + 1) = O(P(n)) literals altogether.
3. At each step, each tape square contains at least one alphabet symbol. For each i = 0, 1, ..,
P(n), and -P(n) j P(n) we have the clause Sij1 v Sij2 v … Sijs; giving
(P(n)+1)*(2P(n)+1)*s = O(P(n)2) literals altogether.
4. In every step, every tape square has at most one alphabet symbol. For every i = 0, 1, ..,
P(n), and -P(n) j P(n), and every distinct pair ak,al of symbols we have the clause (Sijk ^
Sijl); giving a total of (P(n)+1)*(2P(n)+1)*s*(s-1) = O(P(n)2) literals altogether.
https://www.freshersnow.com/
5. In every step, the tape is scanning at least one square. For each i = 0, 1, .., P(n), we have
the clause Ti(-P(n)) v Ti(1-P(n)) v … Ti(P(n)-1) v TiP(n); giving (P(n)+1)*(2P(n)+1)=O(P(n)2)
literals altogether.
6. In every step, the tape is scanning at most one square. For each i = 0, 1, .., P(n) and each
distinct pair j, k of tape squares from -P(n) to P(n), we have the clause (Tij ^ Tik); giving a
total of 2P(n)*(2P(n)+1)*(P(n)+1)=O(P(n)3) literals.
7. Initially, the machine is in state 1 scanning square 1. This is expressed by the two clauses
Q01 and T01 giving just two literals.
8. After the first step, at every step the configuration is determined the preceding step by the
functions T, U, and D. For every i = 0, 1, .., P(n), -P(n) j P(n), k = 0, 1, .., q-1 and l = 1,
2, .., s we have the clauses
The fourth of these clauses ensures that the contents of any tape square ther
These clauses contribute a total of (12s + 3)(P(n) + 1)(2P(n) + 1)q = O(P(n)2) literals.
9. Initially, the string ai1 ai2 … ain defining the problem instance ‘I’ is inscribed on squares
1, 2, …, n of the tape. This is expressed by the ‘n’ clauses S01i1, S02i2, …, S0nin a total of ‘n’
literals.
10. By the P(n)th step, the machine has reached the halt state, and is then scanning square0,
which contains the symbol a1. This is expressed by the three clauses QP(n)0, SP(n)01 and
TP(n)0 giving another 3 literals.
Altogether the number of literals involved in these clauses is O(P(n)3). Thus, the procedure for
setting up these clauses, given the original machine M and the instance ‘I’ of problem D, can be
accomplished in polynomial time.
We must now show that we have succeeded in converting the problem D into SAT.
https://www.freshersnow.com/
This means that there is a certificate ‘c’ such that when M is run with inputs ‘c’ and ‘I’ it
will halt scanning symbol a1 on square0
This means that there is some sequence of symbols that can be placed initially on squares
-P(n), -P(n-1), .., -1 of the tape so that all the clauses above are satisfied
Conversely, suppose ‘I’ is a negative instance of D. In that case there is no certificate for ‘I’,
which means that whatever symbols are placed on squares -P(n), -P(n-1), .., -1 of the tape, when
the computation halts the machine will not be scanning a1 on square0. This means that the set of
clauses above is not satisfiable, and hence constitutes a negative instance of SAT.
Thus from the instance ‘I’ of problem D is constructed in polynomial time, a set of
clauses which constitute a positive instance of SAT if and only ‘I’ is a positive instance
of D.
And since D was an arbitrary NP problem it follows that any NP problem can be
converted to SAT in polynomial time.
https://www.freshersnow.com/