Design and Analysis of Algorithms
Design and Analysis of Algorithms
UNIT I
Algorithms: Definitions and notations: standard notations - asymptotic notations – worst case, best case
and average case analysis; big oh, small oh, omega and theta notations; Analysis of Sorting and Searching:
Heap, shell, radix, insertion, selection and bubble sort; sequential, binary and Fibonacci search. Recursive
algorithms, analysis of non-recursive and recursive algorithms, solving recurrence equations, analyzing
control structures.
2 Marks
1. What is an algorithm? (UQ April 2012 & APRIL 2013)
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a
required output for any legitimate input in finite amount of time.
Problem
Algorithm
4. What is algorithmic?
The study of algorithms is called algorithmic. It is more than a branch of computer science. It is
the core of computer science and is said to be relevant to most of science, business and technology.
5. What is the formula used in Euclid’s algorithm for finding the greatest common divisor of two
numbers?
Euclid‟s algorithm is based on repeatedly applying the equality
Gcd(m,n)=gcd(n,m mod n) until m mod n is equal to 0, since gcd(m,0)=m.
6. What are the three diffe rent algorithms used to find the gcd of two numbe rs?
The three algorithms used to find the gcd of two numbers are .
Euclid‟s algorithm
Consecutive integer checking algorithm
Middle school procedure
7. What are the fundamental steps involved in algorithmic proble m solving?
The fundamental steps are
1. Understanding the problem.
2. Ascertain the capabilities of computational device
3. Choose between exact and approximate problem solving.
4. Decide on appropriate data structures.
5. Algorithm design techniques
6. Methods for specifying the algorithm
7. Proving an algorithms correctness
8. Analyzing an algorithm.
9. Coding an algorithm
8. What is an algorithm design technique?
An algorithm design technique is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing.
13. What are the steps involved in the analysis frame work?
The various steps are as follows
Measuring the input‟s size.
Units for measuring running time.
Orders of growth.
Worst case, best case and average case efficiencies
Cop is the time of execution of an algorithm‟s basic operation on a particular computer and C(n) is the
number of times this operation needs to be executed for the particular algorithm.
ALGORITHM MaxElement(A[0..n-1])
//Determines the value of the largest element in a given
array //Input:An array A[0..n-1] of real numbers
//Output: The value of the largest element in A
maxval a[0]
for I 1 to n-1 do
if A[I] > maxval
maxval A[I]
return maxval
26. Write the general plan for analyzing the efficiency for non-recursive algorithms.
The various steps include.
Decide on a parameter indicating input‟s size.
Identify the algorithms basic operation. .
Check whether the number of times the basic operation is executed depends on size of
input. If it depends on some additional property the worst, average and best-case
efficiencies have to be investigated separately.
Set up a sum expressing the number of times the algorithm‟s basic operation is executed.
Using standard formulas and rules of manipulation find a closed- form formula for the count or at least
establish its order of growth.
27. Give a non-recursive algorithm for element uniqueness problem.
ALGORITHM UniqueElements(A[0..n-1])
//Checks whether all the elements in a given array are
distinct //Input: An array A[0..n-1]
//Output Returns „true‟ if all elements in A are distinct and „false‟
//otherwise
for I to n-2 do
for j I+1 to n-1 do
if A[I] = A[j] return
false return true
29. Write a non-recursive algorithm for finding the numbe r of binary digits for a positive decimal
integer.
ALGORITHM Binary(n)
// Input A positive decimal integer n
// Output The number of binary digits in n‟s binary representation
//
// count 1 while n>1 do
// count count + 1 n n/2
// return count
ALGORITHM F(n)
// Computes n! Recursively
// Input A non- negative integer n
// Output The value of n!
if n=0 return 1
else return F(n-1) * n
31. What is the recurrence relation to find out the numbe r of multiplications and the initial condition
for finding the n-th factorial number?
The recurrence relation and initial condition for the number of multiplications
is M(n)=M(n-1)+1 for n>0
M(0)=0
32. Write the general plan for analyzing the efficiency for recursive algorithms.
The various steps include
Decide on a parameter indicating input‟s size.
Identify the algorithms basic operation. .
Check whether the number of times the basic operation is executed
Depends on size of input. If it depends on some additional property the worst, average and best-case efficiencies
have to be investigated separately.
Set up a recurrence relation with the appropriate initial condition, for the number of times the basic operation is
executed.
Solve the recurrence or at least ascertain the orders of growth of its solution.
ALGORITHM
To move n>1 disks from peg1 to peg3, with peg2 as auxiliary, first move recursively n-1 disks from peg1 to peg2 with
peg3 as auxiliary.
.Then move the largest disk directly from peg1 to peg3.
Finally move recursively n-1 disks from peg2 to peg3 with peg1 as auxiliary.
If n=1 simply move the single disk from source peg to destination peg.
34. Write a recursive algorithm to find the number of binary digits in the binary representation of an
integer.
ALGORITHM BinRec(n)
// Input A positive decimal integer n
// Output The number of binary digits in n‟s binary representation
if n=1 return 1
else return BinRec(n/2)+1
ALGORITHM SelectionSort(A[0..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for I 0 to n-2 do
min I
for j I+1 to n-1 do
if A[j] < A[min] min j
swap A[I] and A[min]
37. What is bubble sort?
Another brute force approach to sort a problem is to compare adjacent elements of the list and
exchange them if they are out of order, so we end up “bubbling up” the largest element to the last position in
the list. The next pass bubbles up the second largest element, and so on until n-1 passes, the list is sorted. Pass
I can be represented as follows
For i = 1 to n
For j = 1 to n
If (a[i]>a[j])
{
t= a[i];
a[i]=a[j];
a[j]=t;
}
ALGORITHM BubbleSort(A[0..n-1])
//The algorithm sorts array A[0..n-1] by bubble sort
//Input: An array A[0..n-1] of orderable elements
//Output: Arrar A[0..n-1] sorted in ascending order
For i = 1 to n
For j = 1 to n
If (a[i]>a[j])
{
t= a[i];
a[i]=a[j];
a[j]=t;
Sequential search simply compares successive elements of a list with a given search key until either a
match is encountered or the list is exhausted without finding a match. The enhancement in this version is to
append the search key to the end of the list , then the search for the key will have to be successful & so we can
eliminate a check for the list‟s end on each iteration.
If „q‟ is always chosen such that „aq‟ is the middle element(that is, q=[(n+1)/2), then the resulting
search algorithm is known as binary search.
Omega
The function f(n) =W (g(n)) iff there exist positive constant C and no such
that f(n) C * g(n) for all n, n ³ n0.
Theta
The function f(n) =q (g(n)) iff there exist positive constant C1, C2, and no such
that C1 g(n)£ f(n) £ C2 g(n) for all n, n ³ n0.
Little 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 ³n0.
Little Omega.
The function f(n) =W (g(n)) iff there exist positive constant C and no such
that f(n) C * g(n) for all n, n ³ n0.
A search is an algorithm for finding an item with specified properties among a collection of items.
There are 3 types of search
1 Linear search
2 Binary search
3 Fibonacci search
1. Space Complexity:
The space complexity of an algorithm is the amount of money it needs to run to
compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time it needs to run to
compilation.
11 Marks
1. Describe briefly about Algorithm. (or) Describe the rules for writing algorithm. (or)
Describe Analysis of Algorithm (or) Describe Analysis of Control Structures. Explain
Standard Notation (UQ APRIL’13)
Informal Definition:
An Algorithm is any well-defined computational procedure that takes some value or set of
values as Input and produces a set of values or some value as output. Thus algorithm is a sequence of
computational steps that transforms the i/p into the o/p.
Formal Definition:
An Algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In
addition, all algorithms should satisfy the following criteria.
3. Pseudo-code Method:
In this method, we should typically describe algorithms as program, which resembles
language like Pascal & algol.
Pseudo-Code Conventions:
1. Comments begin with // and continue until the end of line.
2. Blocks are indicated with matching braces {and}.
3. An identifier begins with a letter. The data types of variables are not explicitly declared.
4. Compound data types can be formed with records. Here is an example,
Node. Record
{
data type – 1 data-1;
.
.
.
data type – n data – n;
node * link;
}
Here link is a pointer to the record type node. Individual data items of a record can be accessed
with and period.
.
.
.
<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
Case statement:
Case
{ : <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
9. Input and output are done using the instructions read & write.
10. There is only one type of procedure:
Algorithm, the heading takes the form,
Algorithm Name (Parameter lists)
As an example, the following algorithm fields & returns the maximum of „n‟ given numbers:
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local variables.
Next we present 2 examples to illustrate the process of translation problem into an algorithm.
Most of the time, average-case analysis are performed under the more or less realistic assumption
that all instances of any given size are equally likely.
For sorting problems, it is simple to assume also that all the elements to be sorted are distinct.
Suppose we have „n‟ distinct elements to sort by insertion and all n! permutation of these elements
are equally likely.
To determine the time taken on a average by the algorithm ,we could add the times required to sort
each of the possible permutations ,and then divide by n! the answer thus obtained.
An alternative approach, easier in this case is to analyze directly the time required by the
algorithm, reasoning probabilistically as we proceed.
For any I,2 I n, consider the sub array, T[1….i].
The partial rank of T[I] is defined as the position it would occupy if the sub array were sorted.
For Example, the partial rank of T[4] in [3,6,2,5,1,7,4] in 3 because T[1….4] once sorted is
[2,3,5,6].
Clearly the partial rank of T[I] does not depend on the order of the element in
Sub array T[1…I-1].
Analysis
Best case:
This analysis constrains on the input, other than size. Resulting in the fasters possible run time
Worst case:
This analysis constrains on the input, other than size. Resulting in the fasters possible run time
Average case:
This type of analysis results in average running time over every type of input.
Complexity:
Complexity refers to the rate at which the storage time grows as a function of the problem size
Asymptotic analysis:
Expressing the complexity in term of its relationship to know function. This type
analysis is called asymptotic analysis.
Asymptotic notation:
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.
Omega: the function f(n)=Ω(g(n)) iff there exist positive constants c and no such that f(n) ≥ c*g(n) for all n, n
≥ no.
Theta: the function f(n)=ө(g(n)) iff there exist positive constants c1,c2 and no such that c1 g(n) ≤ f(n) ≤ c2
g(n) for all n, n ≥ no.
f(n)=0(g(n)), if f(n) (g(n)) n n0, and c is a positive constant and n0 is break even point i.e f(n)
grows no faster than g(n). The g(n) is the upper bound.
0(g(n)) = { f(n): there exist +ve const c and n0 such that 0 f(n) cg(n) for all n n0}
c(g(n))
f(n)
n0
0(1) Constant
0(log n) Logarithmic function
0(n) linear in n
0(n2) quadratic
o(n3) cubic
0(2n) exponential
0(n log n) logarithmic
Example 4.1:
1. f(n)=3n+2
f(n) cg(n)
3n + 2 c(n)
3n + 2 cn here c = 4
if n=1 => 3 + 2 4 (i.e) 5 4
if n=2 => 3 X 2 + 2 4 X 2 (i.e) 8 8
2. 10n2 + 4n + 2
f(n) cg(n)
10n2 + 4n + 2 = c(n2)
= 11(n2) { c=11}
Where g(n) is only a lower bound of f(n) an c is a positive constant and n0 is a break even point.
(g(n)={f(n): There exist +ve constant c and n0 such that 0 cg(n) f(n) for all n n0}
Example 4.2:
f(n)=3n+2
f(n)cg(n)
3n+23(n);
3n+2=(n)
(1)
f(n)
c(g(n))
n0 Big – omega notation: f (n) (g (n))
iff c1g(n)c2g(n) for all n(nn0) C1,c2 positive constant and n0 is break even point.
(g(n))={ f(n): there exist +ve constant c1,c2 and n0 such that 0c1g(n)c2g(n) for all
C2(g(n))
f(n)
c1(g(n))
n0
Example 4.3:
f(n)=3n+2
c1g(n)f(n) c2g(n)
so c1=3, c2=4, n0 = 2
(g(n)={f(n) : for any +ve const c>0, there exist a const n0>0 such that 0f(n)<cg(n) for all n n0}
n g(n) == 0.
Little-omega notation ()
This is used to describe the best-case analysis of algorithms and concerned with small values of n.
f(n)=(g(n))
n g(n) == 0.
(g(n))={f(n): for any +ve const c>0 there exist a const n0 > 0 such that 0cg(n)<f(n) for all nn0}
a. 100n + 6 n
b. 6 x 2n + n2 2n
c. 3n3 + 2n2 + n + 1 n3
Difference between big-oh to small-oh
Proof:
Real numbers a1,b1,a2,and b2
Since t1(n)0(g(n))
C3=max{c1,c2}
Consider nmax{n1,n2}
t1(n)+t2(n)C1g1(n)+c2g2(n)
C3g1(n)+c3g2(n)=c3[g1(n)+g2(n)]
C3 2max{g1(n),g2(n)}
hence, t1(n)+t2(n)0(max{g1(n),g2(n)})
t1(n)0(g1(n)) t1(n)+t2(n)0(max{g1(n),g2(n)})
t2(n)0(g2(n))
Basic efficiency classes
Basic asymptotic efficiency classes
Recursion:
-Recursion is a process by which a function calls itself repeatedly until some specified condition has been
satisfied.
Recursion can be used for repetitive computations in which each action is stated in terms of previous
result. There are two conditions that must be satisfied by any recursive procedure.
1. Each time a function calls itself it should get nearer to the solution.
2. There must be a decision criterion for stopping the process.
In making the decision about whether to write an algorithm in recursive or non-recursive form, it is always
advisable to consider a tree structure for the problem. If the structure is simple then use non-recursive form. If
the tree appears quite bushy, with little duplication of tasks, then recursion is suitable.
The recursion algorithm for finding the factorial of a number is given below,
Algorithm : factorial-recursion
Input : n, the number whose factorial is to be found.
Output : f, the factorial of n
Method : if(n=0)
f=1
else
f=factorial(n-1) * n
if end
algorithm ends.
3. Restore the most recently saved parameters, local variable and return address and goto the latest return
address.
1. Many programming languages do not support recursion; hence, recursive mathematical function is
implemented using iterative methods.
2. Even though mathematical functions can be easily implemented using recursion it is always at the cost
of execution time and memory space. For example, the recursion tree for generating 6 numbers in a
Fibonacci series generation is given in fig 2.5. A Fibonacci series is of the form 0,1,1,2,3,5,8,13,…etc,
where the third number is the sum of preceding two numbers and so on. It can be noticed from the fig
2.5 that, f(n-2) is computed twice, f(n-3) is computed thrice, f(n-4) is computed 5 times.
3. A recursive procedure can be called from within or outside itself and to ensure its proper functioning it
has to save in some order the return addresses so that, a return to the proper location will result when
the return to a calling statement is made.
4. The recursive programs needs considerably more storage and will take more time.
Mathematical functions such as factorial and Fibonacci series generation can be easily implemented
using recursion than iteration.
In iterative techniques looping of statement is very much necessary.
Recursion is a top down approach to problem solving. It divides the problem into pieces or selects out
one key step, postponing the rest.
Iteration is more of a bottom up approach. It begins with what is known and from this constructs the
solution step by step. The iterative function obviously uses time that is O(n) where as recursive function has
an exponential time complexity.
It is always true that recursion can be replaced by iteration and stacks. It is also true that stack can be
replaced by a recursive program with no stack.
.
.
.
Now we are left with the tasks of moving the disks from tower C to B.
To do this, we have tower A and B available.
The fact, that towers B has a disk on it can be ignored as the disks larger than the
disks being moved from tower C and so any disk scan be placed on top of it.
Algorithm:
1. Algorithm TowersofHanoi(n,x,y,z)
2. //Move the top „n‟ disks from tower x to tower y.
3. {
.
.
.
4.if(n>=1) then
5. {
6. TowersofHanoi(n-1,x,z,y);
7. Write(“move top disk from tower “ X ,”to top of tower “ ,Y);
8. Towersofhanoi(n-1,z,y,x);
9. }
10. }
2. Permutation Generator:
Given a set of n>=1elements, the problem is to print all possible permutations of this set.
For example, if the set is {a,b,c} ,then the set of permutation is,
{ (a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)}
It is easy to see that given „n‟ elements there are n! different permutations.
A simple algorithm can be obtained by looking at the case of 4 statement(a,b,c,d)
The Answer can be constructed by writing
Algorithm:
Algorithm perm(a,k,n)
{
if(k=n) then write (a[1:n]); // output permutation
else //a[k:n] has more than one permutation
// Generate this recursively.
for I:=k to n do
{
t:=a[k];
a[k]:=a[I];
a[I]:=t;
perm(a,k+1,n);
//all permutation of a[k+1:n]
t:=a[k];
a[k]:=a[I];
a[I]:=t;
}
}
Performance Analysis:
1. Space Complexity:
The space complexity of an algorithm is the amount of money it needs to run to
compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time it needs to run to
compilation.
Space Complexity:
Space Complexity Example:
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 tyepe array of floating point
numbers.
This is atleast „n‟ words, since „a‟ must be large enough to hold the „n‟ elements to be
summed.
So,we obtain Ssum(n)>=(n+s)
[ n for a[],one each for n,I a& s]
Time Complexity:
The time T(p) taken by a program P is the sum of the compile time and the run
time(execution time)
The compile time does not depend on the instance characteristics. Also we may assume that a
compiled program will be run several times without recompilation .This rum time is denoted by
tp(instance characteristics).
The number of steps any problem statement is assigned depends on the kind of statement.
Interactive statement such as for, while & repeat-until Control part of the statement.
1. We introduce a variable, count into the program statement to increment count with initial value
0.Statement to increment count by the appropriate amount are introduced into the program.
This is done so that each time a statement in the original program is executes count is
incremented by the step count of that statement.
Algorithm:
Algorithm sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
If the count is zero to start with, then it will be 2n+3 on termination. So each invocation of sum
execute a total of 2n+3 steps.
First determine the number of steps per execution (s/e) of the statement and the
total number of times (ie., frequency) each statement is executed.
By combining these two quantities, the total contribution of all statements, the
step count for the entire algorithm is obtained.
Statement S/e Frequency Total
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for I=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
Let us denote C(n) the number of times this comparison is executed. Which is repeated per each value of
the loop‟s variable i within the bounds between 1 and n-1.
n-1
C(n) = 1
i=1
n-1
Example
Consider the element uniqueness problem. Check whether all the elements in a given array are distinct.
The following straightforward algorithm can solve this problem.
ALGORITHM: Unique elements (A [0...n-1])
//check whether all the elements in a given array are distinct.
//input: an array A [0…n-1]
//output: returns “true” if all the elements in A are
//distinct and “false” otherwise
for i = 0 to n-2 do
for j = i+1 to n-1 do
if A[i]=A[j]
return false
return true.
For each value of the loop‟s variable j between its limits i+1 and n-1; and this is repeated for each
value of the outer loop, for each value of the loop‟s variable i between its limits 0 and n-2. Accordingly, we
get
n-2 n-1 n-2 n-2
= (n-1) - i = (n-1) 1 -
( n-2)(n-1) (n-1)n
if n 1 return n
else return F (n-1) + F (n-2)
f(5)
f(4) f(3)
f(1) f(0)
Figure 6.1: Tree of recursive calls for computing the Fibonacci number for n=5
The indispensable last step when analyzing an algorithm is often to solve a recurrence equation.
With a little experience and intention, most recurrence can be solved by intelligent guesswork.
However, there exists a powerful technique that can be used to solve certain classes of recurrence
almost automatically.
This is a main topic of this section the technique of the characteristic equation.
1. Intelligent guess work:
0 if n=0
T(n) = 3T(n ÷ 2)+n otherwise
n 1 2 4 8 16 32
T(n) 1 5 19 65 211 665
n T(n)
1 1
2 3 * 1 +2
22 32 * 1 + 3 * 2 + 22
23 33 * 1 + 32 * 2 + 3 * 22 + 23
24 34 * 1 + 33 * 2 + 32 * 22 + 3 * 23 + 24
25 35 * 1 + 34 * 2 + 33 * 22 + 32 * 23 + 3 * 24 + 25
= ∑ 3k-i 2i
= 3k ∑ (2/3)i
EG : 2
0 n=0
tn = 5 n=1
3tn-1 + 4tn-2, otherwise
Characteristics Polynomial, x2 – 3x – 4 = 0
(x – 4)(x + 1) = 0
Roots r1 = 4, r2 = -1
Eqn 1 C1 = -C2
C2 = -1 , C1 = 1
fn = 1. 4n + (-1) . (-1)n
fn = 4n + 1n
Homogenous Recurrences :
* We begin our study of the technique of the characteristic equation with the resolution of homogenous
linear recurrences with constant co-efficient, i.e the recurrences of the form,
a0tn + a1tn-1 + ….. + aktn-k = 0
* The values of ti on „K‟ values of i (Usually 0 ≤ i ≤ k-1 (or) 0 ≤ i ≤ k) are needed to determine the
sequence.
* Consider for instance our non familiar recurrence for the Fibonacci sequence,
fn = f n-1 + f n-2
* This recurrence easily fits the mould of equation after obvious rewriting.
fn – f n-1 – f n-2 = 0
n if n=0 or n=1
fn =
f n-1 + f n-2 otherwise
1 ±√ (1 + 4)
= ----------------
2
1 ± √5
= ----------
2
1+√5 1 - √5
r1 = --------- and r2 = ---------
2 2
when n=0, f0 = C1 + C2 = 0
when n=1, f1 = C1r1 + C2r2 = 1
C1 + C2 = 0 (1)
C1r1 + C2r2 = 1 (2)
C1 = -C2
Substitute C1 in equation(2)
-C2r1 + C2r2 = 1
C2[r2 – r1] = 1
Substitute r1 and r2 values
1 - √5 1 - √5
C2 --------- - --------- =1
2 2
1 – √5 – 1 – √5
C2 --------------------- =1
2
-C2 * 2√5
-------------- = 1
2
– √5C2 = 1
C1 = 1/√5 C2 = -1/√5
Thus,
n n
1 1 + √5 -1 1 - √5
fn = ---- --------- + ---- --------
√5 2 √5 2
n n
1 1 + √5 1 – √5
= ---- --------- - ---------
√5 2 2
3. Inhomogeneous recurrence :
* The solution of a linear recurrence with constant co-efficient becomes more difficult
when the recurrence is not homogeneous, that is when the linear combination is not
equal to zero.
* Consider the following recurrence
a0tn + a1 t n-1 + … + ak t n-k = bn p(n)
* The left hand side is the same as before,(homogeneous) but on the right-hand side
we have bnp(n), where,
b is a constant
p(n) is a polynomial in „n‟ of degree „d‟.
Example(1) :
substitute t1 in eqn(3),
Example: (3)
Consider the recurrence,
1 if n=0
tn =
4t n-1 – 2n otherwise
tn – 4t n-1 = -2n (A)
In this case , c=2, p(n) = -1, degree =0
(x-4)(x-2) = 0
-2C2 = 4t0 - 6
2C2 = 6 – 4t0
C2 = 3 – 2t0
3 – 2(1) = 1
C2 = 1
tn = O(2n)
Example : (4)
0 if n=0
tn =
2t n-1 + n +2n otherwise
tn – 2t n-1 = n + 2n (A)
There are two polynomials.
t2 – 2t1 = 2 + 4
t2 – 2t1 = 6
t2 = 6 + 2t1
t2 = 6 + 2.3
t2 = 6 + 6
t2 = 12
4. Change of variables:
* It is sometimes possible to solve more complicated recurrences by making a
change of variable.
* In the following example, we write T(n) for the term of a general recurrences,
and ti for the term of a new recurrence obtained from the first by a change of variable.
Example: (1)
Consider the recurrence,
1 , if n=1
T(n) =
3T(n/2) + n , if „n‟ is a power of 2, n>1
1
T(n) = 3T(n/2) + n
We use the fact that, T(2i) = ti & thus T(n) = tlogn when n= 2i to obtain,
T(n) = C1. 3 log2n + C2. 2log2n
T(n) = C1 . nlog23 + C2.n [i = logn]
When „n‟ is a power of 2, which is sufficient to conclude that,
Example: (2)
In this eqn,
b = 4, P(n) = 1, degree = 0
The characteristic polynomial,
(x – 4)(x – 4) = 0
EXAMPLE : 3
In this case,
b = 2, P(n) = i, degree = 1
(x – 2)(x – 2)2 = 0
tn = O(n.log22n)
Example: 4
T(n) = 2 ,n=1
5T(n/4) + Cn2 , n>1
In this case,
b = 16, P(n) = 1, degree = 0
EXAMPLE: 5
2 ,n=1
T(n) =
T(n/2) + Cn , n > 1
T(n) = T(n/2) + Cn
= T(2i/2) + C. 2i
= T(2 i-1) + C. 2i
ti = t i-1 + C. 2i
ti – t i-1 = C. 2i
tn = O(n)
EXAMPLE: 6
1 , n =1
T(n) =
3T(n/2) + n; n is a power of 2
ti = T(2i) = 3T(2i/2) + 2i
= 3T(2 i-1) + 2i
ti = 3t i-1 + 2i
So, b = 2, P(n) =1, degree = 0
(x – 3)(x – 2) = 0
tn = O(n log23)
EXAMPLE: 7
ti = T(2i) = 2T(2i/2) + 2i . i
= 2T(2 i-1) +i. 2i
= 2t i-1 + i.2i
ti – 2ti-1 = i. 2i
General solution,
tn = C1.r1i + C2.i.r2i + C3. i2. r3i
= C1.2i + C2. i.2i + C3. i2.2i
= C1. n+C2.n.log2n + C3.i2.n
= C1.n + C2.n.log2n + C3(2 log2n).n
tn = O(n. 2 log2n)
A heap is a complete binary tree with the property that the value at each node is at least as large as the
value at its children.
An array [25, 13, 17, 5, 8, 3] is represented as heap above. The maximum element in an array is
always a root node.
The indices of its parent, left child and right child can be computed as
PARENT (i) = floor (i/2)
LEFT (i) = 2i
RIGHT (i) = 2i + 1
All the tree levels are completely filled except possibly for the lowest level, which is filled from the
left up to a point.
h
The levels above the lowest level from a complete binary tree of height h-1 contain 2 –1 node.
A heap of height h has
h+1
Maximum no. of elements =2 nodes.(When lowest level is completely filled)
h
Minimum no. of nodes=2 nodes.
Height of a node:
It is defined as no. of edges on a simple downward path from a node to leaf.
Height of a tree:
It is defined as no. of edges on a simple downward path from a root node to leaf.
To insert an element into the heap, one adds it "at the bottom" of the heap and then compares it with
its parent, grandparent, great grandparent and so on, until it is less than or equal to one of these values.
Example:
a[1] a[2] a[3] a[4] a[5] a[6] a[7]
40 80 35 90 45 50 70
Step1: Take the first element of an array.a[1]=40.
rd
Step 3: Take the 3 element .a[3]=35.Add this element to the right side of the root node.
th
Step 4: Take the 4 element .a[4]=90.Add this element to the left side of the node 40
th
Step 5: Take the 5 element .a[5]=45.Add this element to the right side of the node 80.
th
Step 6: Take the 6 element .a[6]=50.Add this element to the left side of the node 35.
th
Step 7: Take the 7 element .a[7]=70.Add this element to the right side of the node 50.
The figure shows one example of how insert ( ) would insert a new value into an existing heap. To do
heap sort, first add the first element in the tree. Then add the second element to the tree. If the tree does not
satisfy the heap property then adjust the nodes in the tree. Then insert the next element and adjust the nodes.
Repeat the above steps till the insertion of last element. Finally we obtain the sorted elements in heap tree
representation.
Procedures for Heap Sort:
1. Build a Heap(Heapify)
2. Adjust
Procedure Heapify( ):
Create a heap from n elements.
Maintain the heap property. That‟s why we are using adjust ( ) procedure to arrange the elements.
Procedure Adjust ( );
Adjust takes as input the array a[ ] and integer I and n. It regards a[1..n] as a complete binary tree. If
the sub trees rooted at 2I and 2I+1 are max heaps, then adjust will rearrange elements of a[ ] such that the tree
rooted at I is also a max heap. The maximum elements from the max heap a[1..n] can be deleted by deleting
the root of the corresponding complete binary tree. The last element of the array, i.e. a[n], is copied to the
root, and finally we call Adjust(a,1,n-1).
Algorithm Heapsort(a,n)
{
Heapify(a,n);
//Transform the array into heap.
//Interc hange the new ma with the element at the end of the
array. for i=n to 2 step –1 do
{
t:=a[i];
a[i]:=a[1];
a[1]:=t;
Adjust(a,1,i-1);
}
}
Algorithm Heapify(a,n)
{
//Readjust the elements in a[1:n] to form a
heap. for i:= n/2 to 1 step –1 do
Adjust(a,I,n);
}
Algorithm Adjust(a,i,n)
{
//The complete binary trees with roots 2i and 2i+1 are
//combined with node i to form a heap rooted at i.
//No node has an address greater than n or less than
1. j=2i;
item=a[i]; while
(j<=n) do
{
if ((j<n) and (a[j]< a[j+1]))
then j=j+1;
//compare left and right child and let j be the larger
child if ( item >= a[i]) then break;
// A position for item is
found a[j/2]=a[j];
j=2j;
}
a[j/2]=item;
}
Analysis:
Heapify() requires n operations.->o(n)
Adjust() requires o(log n) operations for each invocation.Adjust is called n times by Heapsort( ).So
time is o(n log n).
Heapsort = O(n) +O(n log n) =O(n).
Algorithm HeapSort( ):
T(n)=O(n)
Algorithm Heapify( ):
T(n)=O(n)
Algorithm Adjust( ):
T(n)=O(n log n)
Shell sort, named after its inventor, Donald Shell, was one of the first algorithms to break the quadratic time
barrier.
It works by comparing elements that are distant; the distance between comparisons decreases as the algorithm
runs until the last phase, in which adjacent elements are compared. For this reason, Shell sort is sometimes
referred to as diminishing increment sort.
Shell sort uses a sequence, h1, h2, . . . , ht, called the increment sequence. Any increment sequence will do as long as
h1 = 1, but obviously some choices are better than others.
All elements spaced hk apart are sorted. The file is then said to be hk-sorted.
For example, Figure shows an array after several phases of Shell sort.
Shellsort routine using Shell's increments (better increments are possible)
PROGRAM
void shell(int a[10],int n)
{
int k,i,j,temp;
for(k=n/2;k>0;k=k/2)
for(i=k;i<n;i++)
{
temp=a[i];
for(j=i;(j>=k)&&(a[j-k]>temp);j=j-k)
{
a[j]=a[j-k];
}
a[j]=temp;
}
}
TIME & SPACE COMPLEXITY
Worst Case : O(n2) , Best Case : O(n log n), Average case: O(n1.5)
Advantages:
Fastest algorithm for sorting smaller elements.
Requires less memory
11. Write and analyze about radix or bucket sort (UQ APRIL12)
Radix sort is otherwise called as bucket sort
Let A be the array of „n‟ nos.
Our aim is to sort the nos in ascending order
IMPLEMENTATION:
FRONT[0] = REAR[0] = -1
FRONT[9] = REAR[9] = -1
Delete the elements from the queue and store it in an array.
PROGRAM:
for (k = 0; k < 3; k++)
{
for (i = 0; i < count; i++)
{
min = array[i] % 10; /* To find minimum lsd */
t = i;
for (j = i + 1; j < count; j++)
{
if (min > (array[j] % 10))
{
min = array[j] % 10;
t = j;
}
}
temp = array1[t];
array1[t] = array1[i];
array1[i] = temp;
temp = array[t];
array[t] = array[i];
array[i] = temp;
}
for (j = 0; j < count; j++) /*to find MSB */
array[j] = array[j] / 10;
}
The average case for radix sort is O(m+n), the worst case is also O(m+n).
Analysis:
Running time depends on the stable sort.
Each digit is in the range of 1 to k.
Each pass over n takes θ (n+k) time.
There are d no. of passes.
Total time for radix sort is θ(dn+kd) where d is constant and k is θ (n)
The radix sort is linear in time.
12.Explain about insertion sort with example.
Sorting techniques:
o
File is a collection of data items.
o
Each data item in a file is called record.
o
The records are numbered by R[1],R[2],R[3]……… and so on up to R[n]
o
The key is associated with each record, using the key only we can identify the particular
record.
o
Sorting a group of nos or sequences of nos or data items means rearranging them in either
ascending rder or descending order.
INSERTION SORTING:
Let A be the array of „n‟ nos.
Our aim is to sort the numbers in ascending order.
Scan the array from A[1] to A[n-1] and find the smallest element A[R] where R=1,2,3……(N-1) and
insert into the proper position previously sorted sub-array, A[1],A[2]…..A[R-1]
If R=1, the sorted sub-array is empty, so A[1] is sorted itself.
If R=2, A[2] is inserted into the previously sorted sub-array A[1], i.e., A[2] is inserted either before
A[1] or after A[1]
If R=3, A[3] is inserted into the previously sorted sub-array A[1],A[2] i.e., A[3] is inserted either
before A[1] or after A[2] or in between A[1] and A[2].
We can repeat the process for(n-1) times, and finally we get the sorted array.
Eg.,
A[1] A[2] A[3] A[4] A[5] A[6]
[56] 91 35 42 68 78
[56 91] 35 42 68 78
[35 56 91] 42 68 78
[35 42 56 91] 68 78
[35 42 56 68 91] 78
[35 42 56 68 78 91]
PROGRAM
void insertion(int a[10])
{
int i,j,temp;
for(i=0;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while ((j>=0)&&(a[j]>temp))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
Analysis:
Best-Case:
The while- loop in line 5 executed only once for each j. This happens if given array A is already sorted.
T(n) = an + b =
O(n) T(n)=O(n)
It is a linear function of
n. Worst-Case:
The worst-case occurs, when line 5 executed j times for each j. This can happens if array A starts out
in reverse order .
2 2
T(n) = an + bc + c = O(n )
2
T(n)=O(n )
It is a quadratic function of n.
Extra Memory
This algorithm does not require extra memory.
11. Explain about selection sort? (UQ APRIL13 & Nov’12)
Let A be the array of n numbers.
Our aim is to sort the nos in ascending order.
First find the smallest element position in the array(say i) then interchange zeroth position and ith
position element.
Find the second smallest element position in the array(say j),then interchange first position and j th
position element.
We can repeat the process up to (n-1) times. Finally we will be getting sorted array
PROGRAM:
for(i=1;i<=n-1;i++)
{
min=a[i];
k=i;
for(j=i+1;j<=n;j++)
{
if(a[j]<min)
{
min=a[j];
k=j;
}
a[k]=a[i];
}
a[i]=min;
}
The algorithm, the search for the record with the next smallest key is called a pass. There are n-1 such passes
required in order to perform the sort. This is because each pass places one record into its proper location.
During the first pass, in which the record with the smallest key is found, n-1 records are compared.
In general, for the ith pass of the sort, n-i comparisons are required. The total number of comparisons is
herefore, the sum
Therefore, the no. of comparisons is proportional to n2. i.e., O(n2).
Analysis:
The number of moves with this technique is always of the order O(n).So the time complexity is O(n).
Scan-1:
1. Take the first no and compare with the second no. if it is small, don‟t interchange, otherwise
interchange the elements.
2. Take the second no and compare with the third no. if it is small, don‟t interchange, otherwise
interchange the elements.
3. This process is continued till (n-1)th element is compared with nth element.
4. At end of the scan-1, the highest element is placed on the nth position.
Scan-2:
1. Take the first no and compare with the second no. if it is small, don‟t interchange, otherwise
interchange the elements.
2. Take the second no and compare with the third no. if it is small, don‟t interchange, otherwise
interchange the elements.
3. This process is continued till (n-2)th element is compared with (n-1)th element.
4. At end of the scan-2, the second highest element is placed on the (n-1)th position.
Scan-(n-1):
1. Take the first no and compare with the second no. if it is small, don‟t interchange, otherwise
interchange the elements.
PROGRAM:
void bubble(int a[10])
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
TIME AND SPACE COMPLEXITY OF BUBBLE SORTING:
The best case involves performing one pass which requires n-1 comparisons. Consequently, the
best
case is O(n). The worst case performance of the bubble sort is comparisons and exchanges.
the average case is more difficult to analyze than the other cases. It can be shown that the average case
analysis is O(n2). the average no. of passes is approximately . for n=10 the average number of
casses is 6. The average no. of comparisons and exchanges are both O(n2).
Analysis:
The number of comparisons made
2
1 + 2 + 3 + . . . + (n - 1) = n(n - 1)/2 = O(n )
In this algorithm the number of comparison is irrespective of data set i.e., input whether best or worst.
2
Time Complexity T(n) = O(n )
It does not require extra memory.
14. Write in detail about searching or Explain about Sequential(Linear), Binary and Fibonacci search
(UQ APRIL13 &
Nov’10) (UQ: Apr/May'14) ( (11)
Let us assume that we have a sequential file and we wish to retrieve an element matching with key „k‟,
then, we have to search the entire file from the beginning till the end to check whether the element matching k
is present in the file or not.
There are a number of complex searching algorithms to serve the purpose of searching. The linear search and
binary search methods are relatively straight forward methods of searching.
The simplest search technique is the linear or sequential search. In this technique, we start at a
beginning of a list or table and search for the required record until the desired record is found or
the list is exhausted. This technique is suitable for a table or a linked list or an array and it can be
applied to an nordered list but the efficiency is increased if it is an ordered list.
For any search the total work is reflected by the number of comparisons of keys that makes in
the earch.
The number of comparisons depends on where the target (value to be searched) key appears.
If the desired target key is at the first position of the list, only one comparison is required.
If the record is at second position, two comparisons are required. If it is the last position of the
list, n comparisons are compulsory.
If the search is unsuccessful, it makes n comparisons as the target will be compared with all the
entries of the list.
ALGORITHM
// n -number of elements
// x -search element
for i=1 to n do
if(x==a[i])
return i;
}
return 0;
For example:
12 7 3 8 5
The target element is 8.
Comparisons 1:
A[0] 12 8 checks the target key with the array element A[0].
A[1] 7
A[2] 3
A[3] 8
A[4] 5
The two elements are not equal so the target checks with the next value.
Comparisons 2:
A[0] 12
A[1] 7 8 checks the target key with the array element A[1].
A[2] 3
A[3] 8
A[4] 5
The two elements are not equal so the target checks with the next value.
Comparisons 3:
A[0] 12
A[1] 7
8 checks the target key with the array element
A[2] 3 A[2].
A[3] 8
A[4] 5
The two elements are not equal so the target checks with the next value.
Comparisons 4:
A[0] 12
A[1] 7
A[2] 3
8 checks the target key with the array element
A[3] 8 A[3].
A[4] 5
The two elements are equal. So the checking is finished. The result is displayed.
Worst Case:
th
The worst case occurs when an item is not found or the search item is the last (n ) element. For both
situations we must examine all n elements of the array.So the complexity of the sequential search is n. i.e.,
O(n). The execution time is proportional to n.i.e) the algorithm is exec uted in linear time.
Best Case:
The best case occurs when the search item is at the first location itself. So the time complexity is O(1).
Average Case:
The average case occurs if the search element is at the middle location. So we must examine n/2
elements of the array. So the time complexity is O(n/2).
ii.BINARY SEARCH
The main objective of binary search is to search a particular element which is present in the list of
elements. The list of elements should be in a increasing or non-decreasing or sorted order.
For searching lists with more values linear search is insufficient, binary search helps in searching larger
lists. To search a particular item the approximate middle entry of the table is located, and its key values
examined.
1. If the search value is higher than the middle value, then the search is made with the elements after the
middle element.
2. If the search value is smaller than the middle element, then the search is made with the elements
before the middle value.
ALGORITHM
// a - array of integers
// n - number of elements
// x - search element
low=1;
high=n;
while(low<=high)
{ mid=(low+high)/2;
if(a[mid]==x)
return x;
else if(x<a[mid])
low=1;
high=mid-1;
else if(x >a[mid])
low=mid+1;
high=n; }
return 0;
}
EXAMPLE:
Let us apply the algorithm with an example. Suppose array A [ ] contains elements the following elements.
8 10 15 20 28 30 33 Let us
search for the element 15.
Mid= (0+6)/2 = 3.
8 10 15 20 28 30 33
Is 15 = = A[3] ? No
15 < A[3], repeat the steps with low =0 and high = mid-1=2
Mid=(0+2)/2 = 1.
8 10 15
Is 15 = = A [1]? No
15 > A [1], repeat the steps with low = mid +1 =2 and high = 2
Mid=(2+2)/2 = 2.
8 10 15
Mid=high=2
Is 15 = = A[2] ? Yes
Return (2).
Let us search for an element that is not in the list. Search Element=9
Is low > high? NO
Mid=(0+6)/2 = 3.
8 10 15 20 28 30 33
Is 9 = = A[3] ? No
9 < A[3], repeat the steps with low =0 and high = mid-1=2
Mid=(0+2)/2 = 1.
8 10 15
Is 9 = = A[1] ? No
9 < A[1], repeat the steps with low = 0 and high = mid -1 = 0
Mid=(0+0)/2 = 0.
[0]
Low = mid = high =0
Is 9 = = A[0]? No
When low value becomes greater than high value algorithm returns that the element 9 is not present n
the list.
.iii)Fibonacci search
The Fibonacci search technique is a method of searching a sorted array using a divide and conquer
algorithm that narrows down possible locations with the aid of Fibonacci numbers. This method is faster
than raditional binary search algorithms, with a complexity of O(log(x)).
ALGORITHM:
Step 1. Start
Step 2. Read the value of n
Step 3. for i=1 to n increment in steps of 1
Read the value of ith element into array
Step 4. Read the element(x) to be searched
Step 5. search<--fibonacci(a,n,p,q,r,key)
Step 6. if search equal to 0 goto step 7 otherwise goto step 8
Step 7.print unsuccessful search
Step 8. print successful search
Step 9. stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[50],i,key,loc,p,q,r,m,fk;
clrscr();
printf("\nenter the value of n");
scanf("%d",&n);
printf("enter the number");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter the number to be searched");
scanf("%d",&key);
fk=fib(n+1);
p=fib(fk);
q=fib(p);
r=fib(q) ;
m=(n+1)-(p+q);
if(key>a[p])
p=p+m;
loc=rfibsearch(a,n,p,q,r,key);
if(loc==0)
printf("key is not found");
else
printf("The number is found in %d position",loc);
getch();
}
int fib(int m)
{
int a,b,c;
a=0;
b=1;
c=a+b;
while(c<m)
{
a=b;
b=c;
c=a+b;
}
return b;
}
int rfibsearch(int a[],int n,int p,int q,int r,int key)
{
int t;
if(p<1||p>n)
return 0;
else if(key==a[p])
return p;
else if(key<a[p])
{
if(r==0)
return 0;
else
{
p=p-r;
t=q;
q=r;
r=t-r;
return rfibsearch(a,n,p,q,r,key);
}
}
else
{
if(q==1)
return 0;
else
{
p=p+r;
q=q-r;
r=r-q;
return rfibsearch(a,n,p,q,r,key);
}
}
}
OUTPUT:
Enter the value of n : 6
Enter the number :
20
15
28
31
56
18
Enter the number to be searched : 15
The number is found in 2 position
Beginning with an array containing Fj – 1 elements, the length of the subset is boundedto Fj-1-1 elements.
To earch through a range with a length of Fn-1 at the beginning we have to make n comparisons in the worst
ase. Fn = (1/sqrt(5))*((1+sqrt(5))/2)n, that‟s approximately c*1,618n (with a constant c). For N+1 = *1,618n
elements we need n comparisons, i.e., the maximum number of comparisons is O (ld N).
Pondicherry University Questions
2 Marks
11 Marks
1. Explain about analysis of algorithms & analysis of control structures. (UQ APRIL‟13)
2. Write about solving recurrences or Discuss about recursive and non recursive algorithms.(UQ APRIL12)
(UQ:Apr/May'14)
3. Write and analyze about heap sort (UQ Nov‟12)
4. Write in detail about radix sort (UQ APRIL12)
5. Write and analyze selection sort(UQ APRIL13 & Nov‟12)
6. Write and analyze about bubble sort (UQ APRIL 13,NOV‟14))
7. Write in detail about searching or Explain about Sequential and Fibonacci search (UQ APRIL13 &
Nov‟10) (UQ: Apr/May'14)
UNIT II
Divide and Conquer Method: General Method –binary search –maximum and minimum –merge sort -
quick sort –Strassen‟s Matrix multiplication–knapsack. problem –Greedyminimum spanning tree algorithms –
single source shortest path algorithm –scheduling, optimal storage on tapes, optimal merge patterns.
2 Marks
1.Define the divide and conquer method. (UQ APRIL’13 & APRIL’12)
Given a function to compute-and-conquer strategyon suggests„n‟ splitting inputs the inputs including k‟ sub
problems. The sub problems mu sub solutions into a solution of the whole. If the sub problems are still relatively
large, then the divide -and conquer strategy can possibly be reapplied.
24. Write the general algorithm for Greedy method control abstraction.
Algorithm Greedy (a, n)
{
Solution = 0;
For i=1 to n do
{
X=select(a);
If fesible(solution,x) then
Solution=union(Solution,x);
}
Return solution;
}
General method:
Small(P) is a Boolean-valued function that determines whether the i/p size is small enough that the answer
can be computed without splitting.
If this so, the function „S‟ is invoked.
Otherwise, the problem P is divided into smaller sub problems.
These sub problems P1, P2 …Pk are solved by
Combine is a function that determines the so
If the sizetheof sizes„p‟isofn theand „k‟ sub problem computing time of D And C is described by the recurrence
relation.
Example:
1) Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n.
We have,
T(n) = 2T(n/2)+n
= 2[2T(n/2/2)+n/2]+n
= [4T(n/4)+n]+n
= 4T(n/4)+2n
= 4[2T(n/4/2)+n/4]+2n
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+n+2n
= 8T(n/8)+3n
*
*
*
T(n) =2^log n T(n/2^log n) + n log n
Corresponding to the choice of i=log n
Thus, T(n) = 2^log n T(n/2^log n) + n log n
= n. T(n/n) + n log n
= n. T(1) + n log n [since, log 1=0, 2^0=1]
= 2n + n log n
2. Write algorithm and explain binary search (UQ APRIL’13& APRIL’12)
The main objective of binary search is to search a particular element which is present in the list of elements. The
list of elements should be in a increasing or non-decreasing or sorted order.
For searching lists with more values linear search is insufficient, binary search helps in searching larger lists. To
search a particular item the approximate middle entry of the table is located, and its key values examined.
1. If the search value is higher than the middle value, then the search is made with the elements after
the middle element.
2. If the search value is smaller than the middle element, then the search is made with the elements
before the middle value.
ALGORITHM
high=n; }
return 0;
}
EXAMPLE:
Let us apply the algorithm with an example. Suppose array A [ ] contains elements the following elements.
8 10 15 20 28 30 33 Let us search
for the element 15.
Mid= (0+6)/2 = 3.
8 10 15 20 28 30 33
Is 15 = = A[3] ? No
15 < A[3], repeat the steps with low =0 and high = mid-1=2
8 10 15
Is 15 = = A [1]? No
15 > A [1], repeat the steps with low = mid +1 =2 and high = 2
Mid=(2+2)/2 = 2.
8 10 15
Is 15 = = A[2] ? Yes
Return (2).
Let us search for an element that is not in the list. Search Element=9
Mid=(0+6)/2 = 3.
8 10 15 20 28 30 33
Is 9 = = A[3] ? No
9 < A[3], repeat the steps with low =0 and high = mid-1=2
Mid=(0+2)/2 = 1.
8 10 15
Is 9 = = A[1] ? No
9 < A[1], repeat the steps with low = 0 and high = mid -1 = 0
Mid=(0+0)/2 = 0.
8
[0]
Low = mid = high =0
Is 9 = = A[0]? No
When low value becomes greater than high value algorithm returns that the element 9 is not present n the
list.
Proof:
We assume that all statements work as expected and that comparisons such as x>a[mid] are appropriately
carried out.
Let us consider another simple problem that can be solved by the divide-and-conquer technique.
The problem is to find the maximum and minimum items in a set of „n‟ elements
In analyzing the time complexity of this algorithm, we once again concentrate on the no. of element
comparisons.
More importantly, when the elements in a[1:n] are polynomials, vectors, very large numbers, or strings of
character, the cost of an element comparison is much higher than the cost of the other operations.
Hence, the time is determined mainly by the total cost of the element comparison.
Straight MaxMin requires 2(n-1) element comparison in the best, average & worst cases.
An immediate improvement is possible by realizing that the comparison a[I]<min is necessary only
when a[I]>max is false.
Hence we can replace the contents of the for loop by,
If(a[I]>max) then max:=a[I];
Else if (a[I]<min) then min:=a[I];
Now the best case occurs when the elements are in increasing order.
The no. of element comparison is (n-1).
The worst case occurs when the elements are in decreasing order.
The no. of elements comparison is 2(n-1)
Let P=(n, a[I] ,……,a[j]) denote an arbitra Here „n‟ is the no. of elements in the list (
minimum of the list.
If the list has more than 2 elements, P has to be divided into smaller instances.
For example , we might divide „P‟ into -the [n/2],a[[n/2]+1],…..,a[n])
After having divided „P‟ into 2 smallerivelyinvoking thesub same divide-and-conquer algorithm.
As another example divide-and-conquer, we investigate a sorting algorithm that has the nice property
that is the worst case its complexity is O(n log n)
This algorithm is called merge sort
It uses divide and conquer rule for its operation.
Merging is a process of combining 2 sorted sub tables and produce a single sorted table.
This can be achieved by finding the record with the smallest key occurring either of the table and lace
into a new table.
IMPLEMENTATION:
1. Merge 2 sorted sub tables into a single table and store into a temporary array TEMP
PROGRAM:
Worst Case : O(nlog n) , Best Case : O(n log n), Average case: O(n log n)
Advantages:
Better cache performance
It is stable sort
Simple to understand than heap sort
Limitations:
Require extra memory space
If the time for the merging operations is proportional t described by the recurrence relation.
T(n)={an=1,‟ n/2)+cn n>1,‟c‟ a constant.
When „n‟ is a power of 2, n= 2^k, we can solve t
T(n) =2(2T(n/4) +cn/2) +cn
= 4T(n/4)+2cn
= 4(2T(n/8)+cn/4)+2cn
*
*
= 2^k T(1)+kCn.
= an + cn log n.
It is easy to see that if s^k<n<=2^k+1, then T(n)<=T(2^k+1). Therefore,
T(n)=O(n log n)
The divide-and-conquer approach can be used to arrive at an efficient sorting method different from
merge sort.
Quick sort is otherwise called as partition exchange sorting.
It uses divide and conquer method.
Let A be the array of „n‟ nos.
1.The value of I is incremenr=ted till a[i]<=pivot and the value of j is decremented till a[j]>pivot, this process is
repeated until i<j
2.If a[i]>pivot and a[j]<pivot, and also if i<j then swap a[i] and a[j]
3.If i>j then swap a[j] and a[pivot]
`
PROGRAM
void quick(int a[10],int low,int high)
{
int m,i;
if(low<high)
{
m=partition(a,low,high);
quick(a,low,m-1);
quick(a,m+1,high);
}
}
int partition(int a[10],int low,int high)
{
int pivot=a[low],i=low,j=high;
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
{
j--;
}
if(i<=j)
swap(a,&i,&j);
}
swap(a,&low,&j);
return j;
}
void swap(int a[10],int*i,int*j)
{
int temp;
temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
}
TIME AND SPACE COMPLEXITY OF QUICK SORTING:
Worst Case : O(n2) , Best Case : O(n log n), Average case: O(nlog n)
Advantages:
o Faster than other algorithms.
o Better cache performance & high speed.
Limitations
Requires more memory space
6. Explainstrasson’smultiplicationmatrix(UQ Nov’12&APRIL’12)
Let A and B be the 2 n*n Matrix. The product matrix C=AB is calculated by using the formula,
C11=A11B11+A12B21
C12=A11B12+A12B22
C21=A21B11+A22B21
C22=A21B12+A22B22
For EX:
2222 11 11
4*4= 2222 1111
2222 * 111 1
2222 111 1
The Divide and conquer method
22 2 2 1 1 1 1 44 4 4
22 22 * 1 1 1 1 = 44 44
22 2 2 1 1 1 1 44 4 4
22 2 2 1 1 1 1 44 4 4
To compute AB using the equation we need to perform 8 multiplication of n/2*n/2 matrix and from 4
addition of n/2*n/2 matrix.
Ci,j are computed using the formula in equation----- 4
As can be sum P, q, R, S, T, U, and V can be computed using 7 Matrix multiplication and 10 addition or
subtraction.
The Cij are required addition 8 addition or subtraction.
Example
4 4 4 4
*
4 4 4 4
P=(4*4)+(4+4)=64
Q=(4+4)4=32
R=4(4-4)=0
S=4(4-4)=0
T=(4+4)4=32
U=(4-4)(4+4)=0
V=(4-4)(4+4)=0
C11=(64+0-32+0)=32
C12=0+32=32
C21=32+0=32
C22=64+0-32+0=32
32 32
Since n/2n/2 &matrix can be can be added in Cn for some constant C, The overall computing time T(n) of the
resulting divide and conquer algorithm is given by the sequence.
That is T(n)=O(n^3)
* Matrix multiplication are more expensive then the matrix addition O(n^3).We can attempt to reformulate the
equation for Cij so as to have fewer multiplication and possibly more addition .
Stressen has discovered a way to compute the Cij of equation (2) using only 7 multiplication and 18 addition
or subtraction.
Strassen‟s formula are
P= (A11+A12)(B11+B12)
Q= (A12+A22)B11
R= A11(B12-B22)
S= A22(B21-B11)
T= (A11+A12)B22
U= (A21-A11)(B11+B12)
V= (A12-A22)(B21+B22)
C11=P+S-T+V
C12=R+t
C21=Q+T
C22=P+R-Q+V
7. Explain greedy method
DEFINITION:
A problem with N inputs will have some constraints .any subsets that satisfy these constraints are called a
feasible solution.
A feasible solution that either maximize can minimize a given objectives function is called an optimal
solution.
Feasible is a Boolean value function that determines whether X can be included into the solution vector.
The function Union combines X with The solution and updates the objective function.
The function Greedy describes the essential way that a greedy algor ithm will once a particular problem is
chosen ends the function subset, feasible & union are properly implemented.
Example
Suppose we have in a country the following coins are available :
Dollars(100 cents)
Quarters(25 cents)
Dimes( 10 cents)
Nickel(5 Cents)
Pennies(1 cent)
Our aim is paying a given amount to a customer using the smallest possible number of coins.
For example if we must pay 276 cents possible solution then,
1 doll+7 q+ 1 pen 9 coins
2 doll +3Q +1 pen 6 coins
2 doll+7dim+1 nic +1 pen 11 coins.
8. Explain knapsack problem (UQ:NOV’14)
We are given n objects and knapsack or bag with capacity M object I has a weight Wi where I varies from 1
to N.
The problem is we have to fill the bag with the help of N objects and the resulting profit has to be maximum.
Formally the problem can be stated as
There are so many ways to solve this problem, which will give many feasible solut ion for which we have to
find the optimal solution.
But in this algorithm, it will generate only one solution which is going to be feasible as well as optimal.
First, we find the profit & weight rates of each and every object and sort it according to the descending order
of the ratios.
Select an object with highest p/w ratio and check whether its height is lesser than the capacity of the bag.
If so place 1 unit of the first object and decrement .the capacity of the bag by the weight of the object
you have placed.
Repeat the above steps until the capacity of the bag becomes less than the weight of the object you have
selected .in this case place a fraction of the object and come out of the loop.
Whenever you selected.
ALGORITHM:
Example:
Capacity=20
N=3 ,M=20
Wi=18,15,10
Pi=25,24,15
Pi/Wi=25/18=1.36,24/15=1.6,15/10=1.5
Descending Order Pi/Wi 1.6 1.5 1.36
Pi = 24 15 25
Wi = 15 10 18
Xi = 1 0.5 0
PiXi=1*24+0.5*15 31.5
The optimal solution is 31.5
The problem is the number of jobs, their profit and deadlines will be given and we have to find a sequence of
job, which will be completed within its deadlines, and it should yield a maximum profit.
Points To re member:
To complete a job, one has to process the job or a action for one unit of time.
Only one machine is available for processing jobs.
A feasible solution for this problem is a subset of j of jobs such that each job in this subject can be
completed by this deadline.
If we select a job at that time ,
Since one job can be processed in a single m/c. The other job has to be in its waiting state until the job is completed and the
machine becomes free.
So the waiting time and the processing time should be less than or equal to the dead line of the job.
ALGORITHM:
Algorithm JS(d,j,n)
//The job are ordered such that p[1]>p[2]…>p[n]
//j[i] is the ith job in the optimal solution
// Also at terminal d [ J[ i]<=d[ J {i+1],1<i<k
{
d[0]= J[0]=0;
J[1]=1;
K=1;
For I =1 to n do
if (d[J[r]]<d[I]) then
{
for q=k to (r+1) step –1 do J temp=I;
i=k;
else
exit;
}
return k;
}
Example :
1. n=5 (P1,P2,P5)=(20,15,10,5,1)…
(d1,d2….d3)=(2,2,1,3,3)
(1) (1) 20
(2) (2) 15
(3) (3) 10
(4) (4) 5
(5) (5) 1
(1,2) (2,1) 35
(1,3) (3,1) 30
(1,4) (4,1) 25
(1,5) (5,1) 21
(2,3) (3,2) 25
(2,4) (2,4) 20
(2,5) (2,5) 16
(1,2,3) (3,2,1) 45
(1,2,4) (1,2,4) 40
………etc to find all solutions
The Solution 13 is optimal
Prims Algorithm is one of the way to compute a minimum spanning tree which uses a greedy technique.This
algorithm begins with a set U initialized to { 1 }. It then grows a spanning tree, one edge at a time. At each step it
finds a shortest edge (u,v) such that cost of(u,v) is the smallest among all the edges, whwre u is in minimum
spanning tree and V is not in Minimum Spanning Tree.
ALGOTITHM:
void Prims(Table T)
{
vertex V,W;
/* Table initialization */
for(i=0;i<NumVertex;i++)
{
T[i].known=False;
T[i].dist=Infinity;
T[i].path=0;
}
for(; ;)
{
Let V be the start vertex with smallest distance
T[V].dist=0;
T[v].known=Treu;
for each W adjacent to V
if(!T[w].known)
{
T[w].dist=Min(T[W].dist,CVW);
T[W].path=V;
}
}
EXAMPLE:
Here a is taken as source vertex.
Then the distance of its adjacent vertex is as follows:T[b].dist=Min(T[b].dist,Cab)
= Min( ∞,2)
=2
T[d].dist=Min(T[d].dist,Cad)
= Min( ∞,1)
=1
T[c].dist=Min(T[c].dist,Cac)
= Min( ∞,3)
=3
Next vertex d with its minimum distance is marked as visited and the distance of its unknown adjacent vertex is
updated.
T[b].dist=Min(T[b].dist,Cdb)
= Min( 2,2)
=2
T[c].dist=Min(T[c].dist,Cdc)
= Min(3,1)
=1
Next vertex c with its minimum distance is marked as visited and the distance of its unknown adjacent vertex is
updated.
Finally vertex b which is not visited is marked
KRUSKAL ALGORITHM: Explain about kruskal’s method for creating a spanning tree
Kruskal algorithm ueses a greedy technique to compute a minimum spanning tree. This algorithm selects the
edges in the order of smallest weight and accept an edge if it does not cause a cycle.The algorithm terminates if
enough edges are accepted.
ALGORITHM
Voidd Kruskal(graph G)
{
int EdgesAccepted=0;
while(EdgesAccepted<Numvertx -1)
{
USet=Find(U,S);
VSet=Find(V,S);
if(USet!=VSet)
{
EdgesAccepted++;
SetUnion(S,Uset,VSet);
}
}
}
EXAMPLE:
DIJIKSTRA’S ALGORITHM: Explain about Dijikstra's algorithm: (Apr 12, Nov 12, Nov 13)
The general method to solve the single source shortest path problem is known as Dijikstra‟s algorithm. This is
applied to weighted graph
This algorithm is the prime example of Greedy technique, which generally solve a problem in stages by doing what
appears to be the best thing in each stage. This algorithm proceeds in stages, just like the unweighted shortest path
algorithm. At each stage, it selects a vertex V, which has the smallest dv among all the unknown vertices, and
declares that as the shortest path from S to V and mark it to be known. We should set dw = dv + Cvw, if the new
value for dw would be an improvement.
ALGORITHM:
void Dijikstra( Graph G, Table T)
{
vertex V,W;
for(i=0;iNNumvertex;i++)
{
T[i].known=false;
T[i].dist=Infinity;
T[i].path = Not aVetrex;
}
T[start].dist=0;
for( ; ; )
{
V = smallest unknown distance vertes;
if( V = = Not A vertex)
break;
T[V].known=true;
for each W adjacent to V
if(!T[W].known)
{
T[W].dist = Min(T[W].dist,T[V].dist+Cvw)
T[W].path=V;
}
}
}
EXAMPLE:
Here d is the next minimum distance vertex . The adjacent vertex to d is c, therefore the distance of c is updated as
follows:
Next vertex is b.
Since the the adjacent vertex d is already visited , select the next minimum vertex c and marked it as visited.
I d(i) MRT
1, 2 , 3 5+15+!8 38
1, 3 , 2 5+8+18 31
2, 3 , 1 10+13+18 41
2, 1 , 3 10+15+18 43
3, 1 , 2 3+8+18 29(min)
3 , 2 ,1 3+13+18 34
The 5th order 3,2,1 takes minimum MRT 29.So that if the programs are stored in the sequence 3,2,1
then it is an optimal solution.
Analysis:
The greedy method simply requires to store the programs in non decreasing order of their lengths. This
ordering can be carried out in O(n log n) time using an efficient sorting algorithm(heap sort).
Example 2 :
x1 x2 x3 x4 x5
20 30 10 5 30
Merge x3+x4 10+5=15=>z1 Merge z1+x1
15+20=35=>z2 Merge x2+x5 30+30=60=>z3
Merge z2+z3 35+60=95
Total no. of record moves = 205moves
2 Way Merge Pattern:
1.It is represented by binary merge trees. 2.Leaf
nodes are drawn as squares. 3.Nodes are called
as external nodes.
4.Remaining nodes are drawn as circles.(ie) internal nodes
5. Each internal node has exactly 2 children. Internal node represents the file obtained by its 2 children.
95
35
60
15
20 30 30
5 10
In the above tree, square represents a single sorted file and circle represents the merging of 2 files.
2 Marks
11 Marks
1. Write algorithm and explain binary search (UQ APRIL‟13 & APRIL‟12)
2. Explain Maximum and Minimum (UQ APRIL‟13,NOV‟14)
3. Explain merge sort (UQ APRIL‟12,APRIL/MAY‟14)
4. Explain strasson‟s matrix
5. Explain minimum spanning,Apr/May‟14)
6. Explain optimal storage on tapes (UQ APRIL‟12)
7. Explain quick sort (UQ:APRIL/MAY‟14)
8. ExplaiN knapsacK problem(UQ:NOV‟14)
.
UNIT III
Dynamic Programming: General method – multi- stage graphs – all pair shortest path algorithm – 0/1
Knapsack and Traveling salesman problem – chained matrix multiplication. Basic Search and Traversal
technique: Techniques for binary trees and graphs – AND/OR graphs – biconnected components – topological
sorting.
2 Marks
1. Write the difference between the Greedy method and Dynamic programming. Greedy method
Dynamic programming
1. Only one sequence of decision is generated.
1. Many number of decisions are generated.
2. It does not guarantee to give an optimal solution always.
2. It definitely gives an optimal solution always.
17. Write about multistage graph with example (UQ APRIL’13 & Nov’12,APRIL/MAY’14)
A multistage graph G = (V,E) is a directed graph in which the vertices are portioned into K > = 2 disjoint sets
Vi, 1 <= i<= k. In addition, if <u,v> is an edge in E, then u < = Vi and V Vi+1 for some i, 1<= i< k.
Let G(V,E) be a directed graph with n vertices , „E‟ is the set of edges& V is the set of n vertices. Each
edge has an associated non-negative length. Calculate the length of the shortest path between each pair of
nodes i.eShortest path between every vertex to all other vertices. The all pairs shortest path problem is to
determine a matrix A such that A(i,j) is the length of a shortest path from i to j.
The principle of optimality: If k is the node on the shortest path from i to j then the part of the path from
i to k and the part from k to j must also be optimal, that is shorter.
11 Marks
It is an algorithm design method that can be used when the solution to a problem can be viewed as the
result of a sequence of decisions.
The idea of dynamic programming is thus quit simple: avoid calculating the same thing twice, usually
by keeping a table of known result that fills up a sub instances are solved.
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.
Dynamic programming on the other hand is a bottom- up technique. 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 instances.
The essential difference between the greedy method and dynamic programming is t hat the greedy
method only one decision sequence is ever generated. In dynamic programming, many decision
sequences may be generated. However, sequences containing sub-optimal sub-sequences cannot be
optimal and so will not be generated.
Because of principle of optimality, decision sequences containing subsequences that are suboptimal
are not considered. Although the total number of different decision sequences is exponential in the
n
number of decisions(if there are d choices for each of the n decisions to be made then there are d
possible decision sequences),Dynamic programming algorithms often have a polynomial complexity.
1. A multistage graph G = (V,E) is a directed graph in which the vertices are portioned into K > = 2 disjoint
sets Vi, 1 <= i<= k.
2. In addition, if <u,v> is an edge in E, then u < = Vi and V Vi+1 for some i, 1<= i< k.
3. If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk] = 1.
4. Let „s‟ and „t‟ be the source and destination respectively.
5. The cost of a path from source (s) to destination (t) is the sum of the costs of the edger on the path.
6. The MULTISTAGE GRAPH problem is to find a minimum cost path from„s‟ to„t‟.
7. Each set Vi defines a stage in the graph. Every path from „s‟ to „t‟ starts in stage-1, goes to stage-2 then to
stage-3, then to stage-4, and so on, and terminates in stage-k.
8. This MULISTAGE GRAPH problem can be solved in 2 ways.
a) Forward Method.
b) Backward Method.
FORWARD METHOD
Assume that there are „k‟ stages in a graph. In this FORWARD approach, we will find out the cost of each
th st
and every node starting from the „k‟ stage to the 1 stage. We will find out the path (i.e.) minimum
cost path from source to the destination (i.e.) [ Stage-1 to Stage-k ].
PROCEDURE:
Maintain a cost matrix cost (n) which stores the distance from any vertex to the destination.
If a vertex is having more than one path, then we have to choose the minimum distance path and the
intermediate vertex which gives the minimum distance path will be stored in the distance array „D‟.
In this way we will find out the minimum cost path from each and every vertex.
Finally cost (1) will give the shortest distance from source to destination.
For finding the path, start from vertex-1 then the distance array D(1) will give the minimum cost
neighbor vertex which in turn give the next nearest vertex and proceed in this way till we reach the
Destination.
For a „k‟ stage graph, there will be „k‟ vertex in the path.
In the above graph V1……V5 represent the stages. This 5 stage graph can be solved by using forward
approach as follows,
ALGORITHM:
TRACE OUT:
n=12k=5
cost (12)=0 d (12)=0
for j = 11 to 1
When j=11
cost (11)=5 d (11)=12
When j=10
cost (10)=2 d (10)=12
When j=9
cost ( 9)=4 d ( 9)=12
When j=8
Note:
If there is more than one path from vertex j to next stage vertices,then we find the closest vertex by the
following formula:
When j=6
cost(6) = min (c (6,9) + cost(9),c (6,10) +cost(10))
= min(6+4 , 5 +2)
= min(10,7)
=7
cost(6) = 7 d(6) = 10
When j=5
cost(5) = min (c (5,7) + cost(7),c (5,8) +cost(8))
= min(11+5 , 8 +7)
= min(16,15)
= 15
cost(5) = 15 d(5) = 18
When j=4
cost(4) = min (c (4,8) + cost(8))
= min(11+7)
= 18
cost(4) = 18 d(4) = 8
When j=3
cost(3) = min (c (3,6) + cost(6),c (3,7) +cost(7))
= min(2+7 , 7 +5)
= min(9,12)
=9
cost(3) = 9 d(3) = 6
When j=2
cost(2) = min (c (2,6) + cost(6),c (2,7) +cost(7) ,c (2,8) +cost(8))
= min(4+7 , 2+5 , 1+7 )
= min(11,7,8)
=7
cost(2) = 7 d(2) = 7
When j=1
cost(1)= min (c (1,2)+cost(2) ,c (1,3)+cost(3) ,c (1,4)+cost(4) c(1,5)+cost(5))
p[1]=1 p[5]=12
for j=2 to 4
When j=2 p[2]=d[p[1]]=d[1]=2
When j=3 p[3]=d[p[2]]=d[2]=7
When j=4 p[4]=d[p[3]]=d[7]=10
The path through which you have to find the shortest distance from vertex 1 to vertex 12.
(ie)
p[1] p[2] p[3] p[4] p[5]
D ( 1) = 2
D ( 2) = 7
D ( 7) = 10
D (10) = 12
9 2 3 2
ANALYSIS:
The time complexity of this forward method is O (V + E)
BACKWARD METHOD
If there one „K‟ stages in a graph using back ward approach. we will find out the cost of each & every
st th
vertex starting from 1 stage to the k stage.
We will find out the minimum cost path from destination to source (i.e.)[from stage k to stage 1]
PROCEDURE:
1. It is similar to forward approach, but differs only in two or three ways.
2. Maintain a cost matrix to store the cost of every vertices and a distance matrix to store the minimum
distance vertex.
3. Find out the cost of each and every vertex starting from vertex 1 up to vertex k.
4. To find out the path star from vertex „k‟, then the distance array D (k) will give the minimum cost
neighbor vertex which in turn gives the next nearest neighbor vertex and proceed till we reach the
destination.
ALGORITHM :
TRACE OUT:
n=12k=5
bcost (1)=0 d (1)=0
for j = 2 to 12
When j=2
bcost(2) = 9 d(2)=1
When j=3
bcost(3) = 7 d(3)=1
When j=4
bcost(4) = 3 d(4)=1
When j=5
bcost(5) = 2 d(5)=1
When j=6
bcost(6) =min(c (2,6) + bcost(2),c (3,6) + bcost(3))
=min(13,9)
bcost(6) = 9 d(6)=3
When j=7
bcost(7) =min(c (3,7) + bcost(3),c (5,7) + bcost(5) ,c (2,7) + bcost(2))
=min(14,13,11)
bcost(7) = 11 d(7)=2
When j=8
bcost(8) =min(c (2,8) + bcost(2),c (4,8) + bcost(4) ,c (5,8) +bcost(5))
=min(10,14,10)
bcost(8) = 10 d(8)=2
When j=9
bcost(9) =min(c (6,9) + bcost(6),c (7,9) + bcost(7))
=min(15,15)
bcost(9) = 15 d(9)=6
When j=10
bcost(10)=min(c(6,10)+bcost(6),c(7,10)+bcost(7)),c(8,10)+bcost(8))
=min(14,14,15)
bcost(10)= 14 d(10)=6
When j=11
bcost(11) =c (8,11) + bcost(8))
bcost(11) = 16 d(11)=8
When j=12
bcost(12)=min(c(9,12)+bcost(9),c(10,12)+bcost(10),c(11,12)+bcost(11))
=min(19,16,21)
bcost(12) = 16 d(12)=10
p[1]=1 p[5]=12
for j=4 to 2
When j=4 p[4]=d[p[5]]=d[12]=10
When j=3 p[3]=d[p[4]]=d[10]=6
When j=2 p[2]=d[p[3]]=d[6]=3
The path through which you have to find the shortest distance from vertex 1 to vertex 12.
p[5]=12p[4]= =10 p[3]= =6 p[2]= =3 p[1]=1
So the minimum cost path is,
7 2 5 2
1 3 6 10 12
The cost is 16.
DIJIKSTRA’S ALGORITHM: Explain about Dijikstra's algorithm: (Apr 12, Nov 12, Nov 13)
The general method to solve the single source shortest path problem is known as Dijikstra’s algorithm. This is
applied to weighted graph
This algorithm is the prime example of Greedy technique, which generally solve a problem in stages by doing
what appears to be the best thing in each stage. This algorithm proceeds in stages, just like the unweighted
shortest path algorithm. At each stage, it selects a vertex V, which has the smallest dv among all the unknown
vertices, and declares that as the shortest path from S to V and mark it to be known. We should set dw = dv +
Cvw, if the new value for dw would be an improvement.
ALGORITHM:
voidDijikstra( Graph G, Table T)
{
vertex V,W;
for(i=0;iNNumvertex;i++)
{
T[i].known=false;
T[i].dist=Infinity;
T[i].path = Not aVetrex;
}
T[start].dist=0;
for( ; ; )
{
V = smallest unknown distance vertes;
if( V = = Not A vertex)
break;
T[V].known=true;
for each W adjacent to V
if(!T[W].known)
{
T[W].dist = Min(T[W].dist,T[V].dist+Cvw)
T[W].path=V;
}
}
}
EXAMPLE:
Here d is the next minimum distance vertex . The adjacent vertex to d is c, therefore the distance of c is
updated as follows:
Next vertex is b.
Since the the adjacent vertex d is already visited , select the next minimum vertex c and marked it as visited.
Let G(V,E) be a directed graph with edge cost cij is defined such that cij>0 for all i and j and cij = ,if
<i,j> E.
o Let V = n and assume n>1.
The traveling salesman problem is to find a tour of minimum cost.
A tour of G is a directed cycle that include every vertex in V.
The cost of the tour is the sum of cost of the edges on the tour.
The tour is the shortest path that starts and ends at the same vertex (ie) 1.
Application:
1. Suppose we have to route a postal va n to pick up mail from the mail boxes located at „n‟ different
sites.
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.
9. g(i,s) be the length of a shortest path starting at vertex i, going through all vertices in S, and
terminating at vertex 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set „s‟.
10
15
15
20 8 9 13
8
12
7
Cost matrix
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
Starting position
STEP 1:
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
STEP 2:
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
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})}
min{8+15,9+18}
min{23,27}
=23
STEP 3:
12+8 =20
10+8 =18
8+5 =13
13+5=18
STEP 4:
g{4,} =c41 = 8
g{3,} =c31 = 6
g{2,} =c21 = 5
a
s = 0.
i =1 to n.
s =1
i =2 to 4
= 9+6 =15
g(2,{4})
= c24 + g(4,)
= 13+5 =18
= 12+8 =20
= 8+5 =13
= 9+6 =15
s =2
i 1, 1 s and i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
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})}
min{8+15,9+18}
min{23,27}
=23
s =3
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35 optimal cost is 35
the shortest path is,
So the optimal tour is 1 2 4 3 1
5. Explain chained matrix multiplication (UQ NOV’12 & NOV’10)
If we have a matrix A of size pq and B matrix of size qr. the product of these two matrixes C is given by,
Matrix multiplication is associative, so if we want to calculate the product of more than 2 matrixes m=
m1m2……. mn.
For example,
A = 13 5
B = 5 89
C = 89 3
D = 3 34
M = (((A.B).C).D)
A.B C
= (13 * 5 * 89) * (89 * 3)
A.B.C. D
= (13 * 89 * 3) * (3 *
34) A.B.C.D
= 13 * 3 * 34
(ic) = 13 * 5 * 89 + 13 * 89 * 3 + 13 * 3 * 34
= 10,582 no. of multiplications one required for that sequence.
nd
2 Sequence,
M = (A * B) * (C * D)
= 13 * 5 * 89 + 89 * 3 * 34 + 13 * 89 * 34
= 54201 no. of Multiplication
rd
3 Sequence,
M = (A.(BC)) . D
= 5 * 89 * 3 + 13 * 5 * 3 + 13 * 3 *34
= 2856
For comparing all these sequence, (A(BC)).D sequences less no. of multiplication.
For finding the no. of multiplication directly we are going to the Dynamic programming method.
STRAIGHT FORWARD METHOD:
Our aim is to find the total no. of scalar multiplication required to compute the matrix product.
Let (M1,M2,……Mi) and Mi+1,Mi+2,……Mn be the chain of matrix to be calculated using the dynamic
programming method.
In dynamic programming we always start with the smallest instances and continue till we reach the required
size.
We build the table diagonal by diagonal; diagonal s contains the elements mij such that j-1 =s.
S =0,1,……n-1
i=1,2,……n-1.
A=>135
B=>589
C=>893
D=>334
d[0]=13
d[1]=5
d[2]=89
d[3]=3
d[4]=34
if s=0,
m(1,1)=0
m(2,2)=0
m(3,3)=0
m(4,4)=0
if s=1,
if s=2,
mi,i+s =min(mik+mk+1,i+s+di-1dkdi+s)
=(0+1335+(13*5*3),5785+0+(13*89*3))
=min(1530,9256)
=1530
=(0+9078+(5*89*34),1335+0+(5*3*34))
=min(24208,1845)
=1845
=min(4055,54201,2856)
=2856
1 2 3 4
3 0 9078 s=1
4 0 s=0
ALGORITHM:
Procedure cmatrix(n,d[0..n])
For s=0 to n-1 do
{
if(s==0)
m(i,j) =0
if(s==1)
fori=1 to n-1 do
m(i,i+1) =d(i-1)*d(i)*d(i+1)
else
{
m=
fori=1 to n-s do
for k=i to i+s do
min=[m(i,k) +m(k+1,i+s)+d(i-1)*d(k)*d(i+s)]
m(i,i+s) =min
This problem is similar to ordinary knapsack problem but we may not take a fraction of an object.
We are given „ N „ object with weight Wi and profits Pi where I varies from l to N and also a knapsack
with capacity „ M „.
The problem is, we have to fill the bag with the help of „ N „ objects and the resulting profit has to be
maximum.
n
Formally, the problem can be started as, maximize Xi Pi
i=l
n
subject to Xi Wi L
M i=l
Where Xi are constraints on the solution Xi{0,1}. (u) Xi is required to be 0 or 1. if the object is
selected then the unit in 1. If the object is rejected than the unit is 0. That‟s why it is called as 0/1,
knapsack problem.
To solve the problem by dynamic programming we up a table T[1…N, 0…M] (ic) the size is N. where
„N‟ is the no. of objects and column starts with „O‟ to capacity (ic) „M‟.
In the table T[i,j] will be the maximum valve of the objects i varies from 1 to n and j varies from O to
M.
If i=l and j <w(i) then T(i,j) =o, (ic) o pre is filled in the table.
If i=l and j w (i) then T (i,j) = p(i), the cell is filled with the profit p[i], since only one object can be
selected to the maximum.
If i>l and j <w(i) then T(i,l) = T (i- l,j) the cell is filled the profit of previous object since it is not
possible with the current object.
If i>l and j w(i) then T (i,j) = max T(i-1,j), Vi +T(i- l,j-w(i)),. since only „l‟ unit can be selected to the
maximum.
If is the current profit + profit of the previous object to fill the remaining capacity of the bag.
After the table is generated it will give details the profit.
Start with the last position of i and j, T[i,j], if T[i,j] = T[i- l,j] then no object of „i‟ is required so move
up to T[i- l,j].
After moved, we have to check if, T[i,j]=T[i- l,j-w(i)]+ p[I], if it is equal then one unit of object „i‟ is
selected and move up to the position T[i- l,j-w(i)]
Repeat the same process until we reach T[i,o], then there will be nothing to fill the bag stop the
process.
Time is 0(nw) is necessary to construct the table T.
Consider a Example,
M = 6,
N=3
W1 = 2, W2 = 3, W3 = 4
P1 = 1, P2 =2, P3 = 5
i 1 to N
j 0 to 6
o<2 T =0
i=l, j=2
2 o,= T1,2 = l.
i=l, j=3
3>2,= T1,3 = l.
i=l, j=4
4>2,= T1,4 = l.
i=l, j=5
5>2,= T1,5 = l.
i=l, j=6
6>2,= T1,6 = l.
i=2, j=1
l<3= T(2,1)
= T(i- l) T 2,1 =0
V2 V3
V4 V5 V6 V7
V1
V1
V2 V3
V2 V3
V1 V8
(a) V4 V5 V8
(b) (c)
Thus the sequence
so generated is V1,V2, V8, V3,V4, V5,V6, V7. Here we need a queue instead of a stack to
implement it.
We add unvisited vertices adjacent to the one just visited at the rear and read at from to find the next vertex
to visit.
2. VISITED(v) 1
3. Initialise Q to be empty //Q is a queue//
4. loop
5. for all vertices w adjacent to v do
6. if VISITED(w) = 0 //add w to queue//
7. then [call ADDQ(w, Q); VISITED(w) 1] //mark w as VISITED//
8. end
9. if Q is empty then return
10. call DELETEQ(v,Q)
11. forever
12. end BFS
COMPUTING TIME
1. Each vertex visited gets into the queue exactly once, so the loop forever is iterated at most n times.
2. If an adjacency matrix is used, then the for loop takes O(n) time for each vertex visited. The total time is,
therefore, O(n2).
3. In case adjacency lists are used the for loop as a total cost of d1+……..+dn = O(e) where di =
degree(vi). Again, all vertices visited. Together with all edges incident to from a connected
component of G.
9. Explain biconnected components (UQ:APRIL/MAY’14) Articulation
Point:
A vertex „v‟ in a connected graph „G‟ is an articulation point, iff the deletion of vertex v together with
all vertices incident to v disconnects the graph into 2 or more
non empty components.
Biconnected Graph:
A graph „G‟ is biconnected, iff it contains no articulation
points. Real Time Example:
„G‟ is a graph representing a communication network, vertices represent communication station,
edges represent communication lines.
Deletion of anyone of the vertex does not produce any biconnected component
The graph han no articulation point
Two biconnected component can have one vertex in common and this vertex is an articulation point
If G has P articulation point and b biconnected components, the graph contains (b-p) new edges in G.
Algorithm for connecting biconnected components using articulation points for each articulation point a do
Depth First search provides a linear time algorithm, to find all articulation pointers in a connected graph.
First, starting at any vertex , we perform a depth first search and number the nodes as they are visited.
IDENTIFICATION OF ARTICULATION POINT USING LOWEST DEPTH FIRST NUMBER L9u)
Before every vertex V in the depth first spanning tree we compute the L(U) that is reachable from V by taking
Zero or more tree edges and then possibly one back edge.
L(1) = dfn(1)=1
=1
.
.
.
.
.
Algorithm Bicomp(u,v)
If(dfn[w]<dfn[u]) then
If(L(W)>=dfn(u))then
Repeat;
Write(x,y);
Until false;
Bicomp(w,u)
L(u)=min(L(u),L(w));
ALGORITHM:
Void Toposort(Graph G)
{
Queue Q;
Vertex V,W;
Q=Create Queue(NumVErtex);
Makeempty(Q);
for each vertex V
if(indegree[v]== 0)
Enqueue(V,Q);
while(!ISEmpty(Q))
{
V=dequeue(Q);
TopNum[V]=++counter;
for each W adjacent to V
if(--Indegree[W]==0)
Enqueue(W,Q);
}
DisposeQueue(Q);
}
EXAMPLE:
In depth first traversal, the processing proceeds along a path from the root through one child to the
most distant
descendent of that first child before processing a second child. In other words, in the
depth first
traversal, all the descendants of a child are processed before going to the next child.
In a breadth-first traversal, the processing proceeds horizontally form the root to all its children,
then to
its children’s children, and so forth until all nodes have been processed. In other words, in
breadth
traversal, each level is completely processed before the next level is started.
Depth-First Traversal
There are basically three ways of binary tree traversals. They are :
1. Pre Order Traversal
2. In Order Traversal
3. Post Order Traversal
In C, each node is defined as a structure of the following form: struct node
{
int info;
struct node *lchild;
struct node *rchild;
}
typedefstruct node NODE;
Binary Tree Traversals( Recursive procedure ) 1. Inorder
Traversal
Steps 1.Traverse left subtree in inorder
2.Process root node
3.Traverse right subtree in inorder
Algorithm
Begin
If ( not empty (T) ) then
Begin
Inorder_traversal ( left subtree ( T ) )
Print ( info ( T ) ) / * process node */
Inorder_traversal ( right subtree ( T ) )
End
End
C Coding
void inorder_traversal ( NODE * T)
{
if( T ! = NULL)
{
inorder_traversal(T->lchild);
printf(“%d \t “, T->info);
inorder_traversal(T->rchild);
}
}
Algorithm
Algorithm preorder traversal (Bin-Tree T)
Begin
If ( not empty (T) ) then
Begin
Print ( info ( T ) ) / * process node * /
Preoder traversal (left subtree ( T ) )
Inorder traversal ( right subtree ( T ) )
End
End
C function
void preorder_traversal ( NODE * T)
if( T ! = NULL)
{
printf(“%d \t “, T->info);
inorder_traversal(T->lchild);
inorder_traversal(T->rchild);
}
Postorder Traversal
Begin
If ( not empty (T) ) then
Begin
Postorder_traversal ( left subtree ( T ) )
Postorder_traversal ( right subtree( T))
Print ( Info ( T ) ) / * process node */
End
End
C function
The graph A represents problem A that can be solved by solving either both sub node B amd C or D or
E.
Arc represents AND .
We prodeces the dummy variable A and AIIare OR
A and AI are AND (arc)
Nodes with no descendents is terminal.
Terminals can be divided into 2 :
Solvables (represent by square symbol)
Non solvables (represent by circle symbol)
Breaking down a problem into many sub problem is problem reduction. Two different problems may generate
a same problem. Graph is not a tree
Problem:
Consider a directed graph . The problem to be solved is P1. To do this, one can solve node P2,P3,P7 as P1 in
an OR node. The cost incurred is either 2,2,8(P2,P3,P7). To solve P2, both P4 and P5 have to solved as P2 in
AND node. Total cost to do this is 2. To solve P3 we can solve either P3 or P6 . The minimum cost is 1. Node
P7 is set free.
Therefore optimal way is to solve P1, is tio solve P6 first and then P3 and finally P1
Cost = 3
Pondicherry University Questions
2 Marks
11 Marks
Backtracking: The general method – 8-queens problem – sum of subsets – graph coloring – Hamiltonian cycle
– Knapsack problem.
2 Marks
1. What are the require ments that are needed for performing Backtracking? (UQ NOV’10)
To solve any problem using backtracking, it requires that all the solutions satisfy a complex set of
constraints. They are:
i. Explicit constraints.
ii. Implicit constraints.
They are rules that restrict each xi to take on values only from a give set. They depend on the
particular instance I of the problem being solved. All tuples that satisfy the explicit constraints define a
possible solution space.
They are rules that determine which of the tuples in the solution space of I satisfy the criteria function.
It describes the way in which the xi must relate to each other.
The tree organization of the solution space is referred to as state space tree.
All the paths from the root of the organization tree to all the nodes is called as state space of the
problem
Answer states are the solution states s for which the path from the root to s defines a tuple that is a
member of the set of solutions of the problem.
The tree organizations those are independent of the problem instance being solved are called as static
tree.
9. Define a live node.
A node which has been generated and all of whose children have not yet been generated is called as a
live node.
E – Node (or) node being expanded. Any live node whose children are currently being generated is
called as an E – node.
11. Define a dead node.
Dead node is defined as a generated node, which is to be expanded further all of whose children have
been generated.
12. What are the factors that influence the efficiency of the backtracking algorithm?
The efficiency of the backtracking algorithm depends on the following four factors. They are:
i. The time needed to generate the next xk
ii. The number of xk satisfying the explicit constraints.
iii. The time for the bounding functions Bk
iv. The number of xk satisfying the Bk.
The problem is to place eight queens on a 8 x 8 chessboard so that no two queen “attack” each other
that is, so that no two of them are on the same row, column or on the diagonal.
Given n distinct positive numbers usually called as weights, the problem calls for finding all the
combinations of these numbers whose sums are m.
Let G be a graph and m be a given positive integer. We want to discover whether the nodes of G can
be colored in such a way that no two adjacent nodes have the same color yet only m colors are used.
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.
This 8 queens problem is to place n-queens in an „N*N‟ matrix in such a way that no two queens
attack each otherwise no two queens should be in the same row, column, diagonal.
Solution:
th
The solution vector X (X1…Xn) represents a solution in which Xi is the column of the row where I
queen is placed.
First, we have to check no two queens are in same row.
Second, we have to check no two queens are in same column.
th
The function, which is used to check these two conditions, is [I, X (j)], which gives position of the I
queen, where I represents the row and X (j) represents the column position.
Third, we have to check no two queens are in it diagonal.
Consider two dimensional array A[1:n,1:n] in which we observe that every element on the same
diagonal that runs from upper left to lower right has the same value.
Also, every element on the same diagonal that runs from lower right to upper left has the same value.
Suppose two queens are in same position (i,j) and (k,l) then two queens lie on the same diagonal , if
and only if |j- l|=|I-k|.
Refer Example 4 queen in class notes
STEPS TO GENERATE THE SOLUTION:
We start with the empty board and the placed queen 1 in the first poosible position i.e(1,1) row 1 ,
column 1.
Queen 2 cannot be placed in (2,1) and (2,2) so acceptable position is(2,3) that is row 3 , column 3
This position proves to be dead end, because there is no acceptable position for queen 3 . So the
algorithm backtracks and put queen 2 in next possible position at (2,4) that is row 2, column 4.
The queen 3 is placed at (3,2) that is row 3 , column 2 this lead dead end.
the algorithm backtracks queen 1 and move it to (1,2) that is row 1, column 2.
Queen 2 cannot be placed in (2,1), (2,2), (2,3) so acceptable position is (2,4) that is row 2, column 4.
Queen 3 is placed at (3,1) that is row 3, column 1.
Queen 4 cannot be placed in (4,1),(4,2) . So acceptable position is (4,3)
Algorithm:
t(n)=def n!
We are given „n‟ positive numbers called weights and we have to find all combinations of
these numbers whose sum is M. this is called sum of subsets problem.
If we consider backtracking procedure using fixed tuple strategy , the elements X(i) of the solut
ion vector is either 1 or 0 depending on if the weight W(i) is included or not.
If the state space tree of the solution, for a node at level I, the left child corresponds to X(i)=1
and right to X(i)=0.
GENERATION OF STATE SPACE TREE:
Algorithm:
Algorithm sumofsubset(s,k,r)
{
//generate the left child. notes+w(k)<=M since Bk-1 is true.
X{k]=1;
If (S+W[k]=m) then write(X[1:k]); // there is no recursive call here as W[j]>0,1<=j<=n.
Else if (S+W[k]+W[k+1]<=m) then sum of sub (S+W[k], k+1,r- W[k]);
//generate right child and evaluate Bk.
If ((S+ r- W[k]>=m)and(S+ W[k+1]<=m)) then
{
X{k]=0;
sum of sub (S, k+1, r- W[k]);
}
}
o(2N/2N)
Let G=(V,E) be a connected graph with „n‟ vertices. A HAMILTONIAN CYCLE is a round trip path
along „n‟ edges of G which every vertex once and returns to its starting position.
If the Hamiltonian cycle begins at some vertex V1 belongs to G and the vertex are visited in the order
of V1,V2…….Vn+1,then the edges are in E,1<=I<=n and the Vi are distinct except V1 and Vn+1
which are equal.
Refer Example in class notes
Procedure:
1. Define a solution vector X(Xi……..Xn) where Xi represents the I th visited vertex of the proposed
cycle.
6. When these two conditions are satisfied the current vertex is included in the cycle, else the next vertex
is tried.
7. When the nth vertex is visited we have to check, is there any path from nth vertex to first 8 vertex. if
no path, the go back one step and after the previous visited node.
8. Repeat the above steps to generate possible Hamiltonian cycle.
}
Repeat
}
Algorithm Nextvalue (k)
{
Repeat
{
X [k]=(X [k]+1) mod (n+1); //next vertex
If (X [k]=0) then return;
If (G [X [k-1], X [k]] 0) then
{
For j=1 to k-1 do if (X [j]=X [k]) then
break; // Check for distinction.
If (j=k) then //if true then the vertex is distinct.
If ((k<n) or ((k=n) and G [X [n], X [1]] 0)) then return;
}
} Until (false);
}
6. Explain graph coloring (UQ APRIL’12,APRIL/MAY’14,NOV’14)
Let „G‟ be a graph and „m‟ be a given positive integer. If the nodes of „G‟ can be colored in such a
way that no two adjacent nodes have the same color. Yet only „M‟ colors are used. So it‟s called M-
color ability decision problem.
The graph G can be colored using the smallest integer „m‟. This integer is referred to as chromatic
number of the graph.
A graph is said to be planar iff it can be drawn on plane in such a way that no two edges cross each
other.
Procedure for Graph coloring:
The basic idea behind the solution is that once a vertex is assigned a color then all the vertices which are
connected to that are refrained from using the same color.
We have some set of color C, then initially all colors are available to all the vertices.
We start assigning color o vertices, the number of available colors to the remaining vertices would also start
reducing depending on the existence of edges between vertices.
Refer Example in class notes
Algorithm:
Algorithm mColoring(k)
// the graph is represented by its Boolean adjacency matrix G[1:n,1:n] .All assignments //of 1,2,……….,m to
the vertices of the graph such that adjacent vertices are assigned //distinct integers are printed. ‟k‟ is the index
of the next vertex to color.
{
repeat
{
// generate all legal assignment for X[k].
Nextvalue(k); // Assign to X[k] a legal color.
If (X[k]=0) then return; // No new color possible.
If (k=n) then // Almost „m‟ colors have been used to color the „n‟ vertices
Write(x[1:n]);
Else mcoloring(k+1);
}until(false);
}
Algorithm Nextvalue(k)
// X[1],……X[k-1] have been assigned integer values in the range[1,m] such that //adjacent values have
distinct integers. A value for X[k] is determined in the //range[0,m].X[k] is assigned the next highest numbers
color while maintaining //distinctness form the adjacent vertices of vertex K. If no such color exists, then X[k]
is 0.
{
repeat
{
X[k] = (X[k]+1)mod(m+1); // next highest color.
If(X[k]=0) then return; //All colors have been used.
For j=1 to n do
{
// Check if this color is distinct from adjacent color.
If((G[k,j] 0)and(X[k] = X[j]))
// If (k,j) is an edge and if adjacent vertices have the same color.
Then break;
}
n
The time spent by Nextvalue to determine the children is (mn) and Total time is = (m n).
State Space Tree:
7. Explain Knapsack Proble m using Backtracking (UQ APRIL’13 & NOV’12)
The problem is similar to the zero-one (0/1) knapsack optimization problem is dynamic programming
algorithm.
We are given „n‟ positive weights Wi and ‟n‟ positive profits Pi, and a positive number „m‟ that is
the knapsack capacity, the is problem calls for choosing a subset of the weights such that,
Xi Constitute Zero-one valued Vector.
The Solution space is the same as that for the sum of subset‟s problem.
Bounding functions are needed to help kill some live nodes without expanding them. A good bounding
function for this problem is obtained by using an upper bound on the value of the best feasible solution
obtainable by expanding the given live node.
The profits and weights are assigned in descending order depend upon the ratio.
Algorithm Bknap(k,cp,cw)
// „m‟ is the size of the knapsack; „n‟ no.of weights & profits. W[]&P[] are the //weights & weights. P[I]/W[I]
P[I+1]/W[I+1].
//fw Final weights of knapsack.//fp
final max.profit.
//x[k] = 0 if W[k] is not the knapsack,else X[k]=1.
{
// Generate left child.
If((W+W[k] m) then
{
Y[k] =1;
If(k<n) then Bnap(k+1,cp+P[k],Cw +W[k])
If((Cp + p[w] >fp) and (k=n)) then
{
fp = cp + P[k];
fw = Cw+W[k];
for j=1 to k do X[j] = Y[j];
}
}
Algorithm Bound(cp,cw,k) //
cp current profit total. //cw
current weight total.
//k the index of the last removed item.
//m the knapsack size.
{
b=cp;
c=cw;
for I =- k+1 to n do
{
c= c+w[I];
if (c<m) then b=b+p[I];
else return b+ (1-(c-m)/W[I]) * P[I];
}
return b;
}
2 Marks
1 What are the requirements that are needed for performing Backtracking? (UQ NOV‟10) (Qn.No.1)
2. State Sum of Subsets problem. (UQ APRIL‟13 & APRIL‟12,NOV‟14) ( Qn.No.14)
3. State m – colorability decision problem (UQ NOV‟10) ( Qn.No.15)
4. Define chromatic number of the graph. (UQ APRIL‟13) ( Qn.No.16)
5. Explain Hamiltonian cycles? (UQ NOV‟12 & APRIL‟12,APRIL/MAY‟14) ( Qn.No.18)
6. Define Backtracking (UQ APRIL‟12,APRIL/MAY‟14) ( Qn.No.19)
7. Define state space tree. (UQ:NOV‟14) ( Qn.No.4)
11 Marks
Branch and Bound Method: Least Cost (LC) search–the 15-puzzle problem–control abstractions for LC-Search
– Bounding – FIFO Branch-and-Bound - 0/1 Knapsack problem – traveling salesman problem. Introduction to
NP-Hard and NP-Completeness.
2 Marks
2. What are the searching techniques that are commonly used in Branch-and-Bound Method?
The searching techniques that are commonly used in Branch-and-Bound method are:
i. FIFO
ii. LIFO
iii. LC
iv. Heuristic search
15 Define SATISFIABILITY.
Let x1, x2, x3….,xn denotes Boolean variables.
Let xi denotes the relation of xi.
A literal is either a variable or its negation.
A formula in the prepositional calculus is an expression that can be constructed using literals and the
operators and Λ or v.
A clause is a formula with at least one positive literal. The satiability problem is to determine if a formula is
true for some assignment of truth values to the variables.
16. What are the two classes of non polynomial time problems?
• NP? hard
• NP?complete
18. Give the mathematical equation for 0/1knapsack problem. (UQ APR’11)
A simple way to find the upper bound ‗ub‗ is to add ‗v‗, the total value of the items already selected, the
product of the remaining capacity of the knapsack W-w and the best per unit payoff among the remaining
items, which is vi+1/wi+1 ub = v + (W-w)( vi+1/wi+1)
The design technique known as branch and bound is very similar to backtracking (seen in unit 4) in that it
searches a tree model of the solution space and is applicable to a wide variety of discrete combinatorial
problems.
Each node in the combinatorial tree generated in the last Unit defines a problem state. All paths from the
root to other nodes define the state space of the problem.
Solution states are those problemstates‗s‗for which the path from the rootto‗s‗defines a tuple in
thesolution space. The leaf nodes in the combinatorial tree are the solution states.
Answer states are those solutionstates‗s‗for which the path from the root to’s’defines a tuple that is
amember of the set of solutions (i.e., it satisfies the implicit constraints) of the problem.
The tree organization of the solution space is referred to as the state space tree.
A node which has been generated and all of whose children have not yet been generated is called a
livenode.
The live node whose children are currently being generated is called the E-node (node being expanded).
A dead node is a generated node, which is not to be expanded further or all of whose children have been
generated.
Bounding functions are used to kill live nodes without generating all their children.
Depth first node generation with bounding function is called backtracking. State generation methods in
which the E-node remains the E-node until it is dead lead to branch-and-bound method.
The term branch-and-bound refers to all state space search methods in which all children of the E-node are
generated before any other live node can become the E-node.
In branch-and-bound terminology breadth first search(BFS)- like state space search will be called FIFO
(First In First Output) search as the list of live nodes is a first - in-first -out list(or queue).
A D-search (depth search) state space search will be called LIFO (Last In First Out) search, as the list of
live nodes is a list- in-first-out list (or stack). Bounding functions are used to help avoid the generation of sub
trees that do not contain an answer node. The branch-and-bound algorithms search a tree model of the
solution space to get the solution.
listnode = record
{
listnode *next, *parent; Float cost;
}
Algorithm LC Search(t)
\\ Search t for an answe r node
{
If *t is an answer node then output *t and return; E
= t ; \\ E- node
Initialize the list of live nodes to be empty;
Repeat
{
For each child x of E do
{
If x is an answer node the output the
path from x to t and return
Add(x) ; \\ x is a new live node.
(x->parent):=E; \\Pointer for path to root.
}
If there are no more live nodes then
{
Write(―No answer node‖); return;
}
E=Least(); } until (false);
}
Algorithm explanation:-
Example
15 Puzzle Problems
the 15 puzzle consists of 15 numbered tiles on a square frame with a capacity of 16 tiles
Initial arrangement of the tiles are given, the objective of the problem is to transform this arrangement
into the goal arrangement through a series of legal moves.
The legal moves are ones in which a tile adjacent to the empty spot is moved to Empty Spot
From the initial arrangement, four moves are possible
We can move any one of the tiles numbered 2,3,5,6 to the empty spot
These arrangements are called states of the puzzle
To speed up the search, we can associate the cost c(x) with each node x in the state space tree.
C‗(x)=f(x)+g‗(x) where f(x) – length of the path from root to node x
g‗(x) – an estimate of the length of a shortest path from x to a goal node in the
subtree with root x
(or)
g‗(x) – number of nonblank tiles not in their goal position
A branch -and-bound searches the state space tree using any search mechanism in which all the
children of the E-node are generated before another node becomes the E-node.
We assume that each answer node x has a cost c(x) associated with it and that a minimum-cost answer
node is to be found. Three common search strategies are FIFO, LIFO, and LC.
A cost function (.) such that (x) <=c(x) is used to provide lower bounds on solutions obtainable
from any node x.
If upper is an upper bound, then all live nodes x with (x)>upper may be killed as all answer nodes
reachable from x have cost c(x)>=c‗(x)>upper6
The starting value for upper can be set to infinity.
Each time a new answer node is found, the value of upper can be updated
C‗(x) and upper and c(x) can be computed using formulas
Example
Job Sequencing Proble m
Given n jobs and one processor
Each job i is defined as 3 tuples(Pi,di,ti)
ti - processing time in units
di - deadline
Pi - penalty, if the job I is not completed within the deadline, then penalty Pi is incurred
if x is a root node
c(x) - minimum penalty corresponding to any node in the subtree within root x
(or)
c(x)=min{c(child-1),c(child-2),….c(child-n) ,where x is node in subtree
To use branch and bound technique to solve any problem, it is first necessary to conceive of a state
space tree for the problem.
Branch and bound technique is used to solve minimization problem(eg 15 puzzle, 4 queen, node with
minimum cost is selection criteria)
The branch and bound technique can not be applied directly in the knapsack problem , because the
knapsack problem is a maximization problem(eg maximum profit is the selection criteria)
This difficulty is easily overcome by replacing the objective function PiXi by the function -PiXi
The modified knapsack problem is stated as
Every leaf node in the state space tree representing an assignment for which
INTRODUCTION:
It is algorithmic procedures similar to backtracking in which a new branch is chosen and is there
(bound there) until new branch is choosing for advancing.
This technique is implemented in the traveling salesman problem [TSP] which are asymmetric
(Cij<>Cij) where this technique is an effective procedure.
STEP: Find least cost in a row and negate it with rest of the
elements.
STEP 4: Find least cost in a column and negate it with rest of the elements.
STEP 5: Preserve cost matrix C [which row reduced first and then column reduced] for
th
thei time.
STEP 6: Enlist all edges (i, j) having cost = 0.
STEP 8: Compare all effective cost and pick up the largest l. If two or more have same cost then
arbitrarily choose any one among them.
th th
STEP 9: Delete (i, j) means delete i row and j column change (j, i) value to infinity. (Used to avoid
infinite loop formation) If (i,j) not present, leave it.
STEP 10: Repeat step 1 to step 9 until the resultant cost matrix having order of 2*2 and reduce it. (Both
R.R and C.C)
STEP 11: Use preserved cost matrix Cn, Cn-1… C1
Choose an edge [i, j] having value =0, at the first time for a preserved matrix and leave that
matrix.
4 3
6
MATRIX:
1 2 3 4 5
25 40 31 27
1
5 17 30 25
2
19 15 6 1
3
9 50 24 6
4
22 8 7 10
5
PHASE I
C1 [ROW REDUCTION:
1 2 3 4 5
1 0 15 6 2
2 0 12 25 20
3 18 14 5 0
4 3 44 18 0
15 1 0 3
5
STEP 3: C1 [Column Reduction]
1 2 3 4 5
0 15 3 2
1
2 0 12 25 20
3 18 14 2 0
4 3 44 18 0
15 1 0 3
5
STEP 5:
Preserve the above in C1,
1 2 3 4 5
1 0 15 3 2
2 0 12 22 20
3 18 14 2 0
4 3 44 18 0
5 15 1 0 3
STEP 6:
STEP 7:
Calculation of effective cost
[E.C] (1,2) = 2+1 =3 (2,1) = 12+3
= 15 (3,5) = 2+0 =2 (4,5) = 3+0 =
3 (5,3) = 0+12 = 12 (5,4) = 0+2 =
2
STEP 8:
L having edge (2,1) is the largest.
STEP 9: Delete (2,1) from C1 and make change in it as (1,2) if exists.
Now Cost Matrix =
2 3 4 5
15 3 2
1
3 14 2 0
44 18 0
4
1 0 0
5
PHASE II:
STEP1:C2(R, R)
2 3 4 5
1
13 1 0
3 14 2 0
4 44 18 0
5 1 0 0
STEP 3: C2 (C, R)
2 3 4 5
1 13 1 0
3 13 2 0
4 43 18 0
5 0 0 0
C2 =
2 3 4 5
1
13 1 0
3 13 2 0
4 43 18 0
5 0 0 0
STEP 6:
(1,5) = 1+0 =1
(3,5) = 2+0 =2
(4,5) = 18+0 =18
(5,2) = 0+13 =13
(5,3) = 0+13 =13
(5,4) = 0+1 =1
2 3 4
1 13 1
3 13 2
5 0 0
PHASE III:
STEP 1: C3 (R, R)
2 3 4
1
12 0
3 11 0
0 0 5
STEP 3: C3 (C, R)
2 3 4
1 12 0
3
11 0
5
0 0
STEP 5: preserve the above in C2
STEP 8: Here we are having two edges (1,4) and (5,3) with cost = 12. Hence arbitrarily choose (1,4)
STEP 9: Delete (i,j) (1,4) and make change in it (4,1) = if exists.
Now cost matrix is
2 3
2 11
3 0 0
C4 (RR)=
2 3
3 0
5 0 0
C4 (C, R) =
2 3
3 0
5 0 0
Therefore,C4 = 2 3
3 0
0 0 5
C4 2 3
3
0
5
0 0
C3 2 3 4
1
12 0
3
5 11 0
0 0
C2 =
2 3 4 5
1 13 1 0
3 13 2 0
4 43 18 0
5 0 0 0
C1 =
1 2 3 4 5
0 15 3 2
2
0 12 22 20
3
4 18 14 2 0
5 3 44 18 0
15 1 0 0
STEP 12:
i) Use C4 =
2 3
3 0
5 0 0
Here (3,2) =0
Hence, T (3,2)
Use C3 =
2 3 4
1
3 12 0
5 11 0
0 0
Here (1,4) =0
Hence, T (3,2), (1,4)
Use C2=
2 3 4 5
1 13 1 0
3 13 2 0
43 18 0
4
0 0 0
5
Here (1,5) not possible because already chosen index i (i=j) (3,5)
not possible as already chosen index.
(4,5) 0
Hence, T (3,2), (1,4), (4,5)
Use C1 =
1 2 3 4 5
1
0 15 3 2
2
0 12 22 20
3
4 18 14 2 0
5 3 44 18 0
15 1 0 0
Final result:3—2—1—
4—5—3
Cost is 15+15+31+6+7=64
The field of complexity theory deals with how fast can one solve a certain type of problem.We assume
problem in solvable.
Basic Concepts:
Algorithm contains operations whose outcome are uniquely defined.
Definition of non deterministic algorithm:
Algorithm contains operations whose outcomes are not uniquely defined but are limited to specified
set of possibilities.
To specify non deterministic algorithm., 3 functions are needed.
1. Choice
2. Failure
3. Success
Choice(S)
Arbitrarily chosen one of the elements of set S
Failuer()
It signals an unsuccessful operation
Success()
It signals a successful completion.
Decision Problem:
Any problem for which the answer is either 0 or 1 is called a decision problem.
Example of 0/1 knapsack Problem:
The Knapsack decision problem is to determine whether there is 0/1 assignment of values to xi,
1<=i<=n such that £pi xi>=r and £Wi xi<= m
Where r is a given number
Pi Wi are non negative numbers
Optimization Problem:
Any problem that involves the identification of an optimal ( either maximum or minimum ) value of a given
cost function is known as optimization problem.
Polynomial Transformation
Problem Transformation: some algorithms which take a decision problem X (or rather ANY instance of
theproblem of type X), and output a corresponding instance of the decision problem of type Y, in such a way
that if the input has answer True, then the output (of type Y) is also True and vice versa. [REMINDER:
Problem
≡ Input; & Solution ≡ Algorithm that takes any input and produces correct output.]
For example, you can write a problem transformation algorithm from 3-SAT problem to 3D-Matching
problem
Note that the problem transformations are directed.
When a problem transformation algorithm is polynomial- time we call it a polynomial transformation.
Existence of a polynomial transformation algorithm has a great significance for the complexity issues.
Suppose you have (1) a poly- transformation Axy exists from a (source) problem X to another (target)
problem Y, and (2) Y has a poly algorithm P y , then you can solve any instance of the source problem X
polynomially, by the following method.
Just transform any instance of X into another instance of Y first using Axy, and then use Y‗s poly-algorithm Py.
Both of these steps are polynomial, and the output (T/F) from Y‗s algorithm is valid for the source instance (of X)
as well. Hence, the True/False answer for the original instance of P y (Axy (X)) will be obtained in poly- time. This
constitutes an indirect poly-algorithm for X, thus making X also belonging to the P-class.
Note, |Axy(X)| is polynomial with respect to |X|.
It is easy to see that there are NP hard problems that are not NP complete
NP complete is a subset of NP , the set of all decision problems, whose solution can be verified in polynomial
time.
Cook’s theorem.
Cook modeled all NP-problems (an infinite set) to an abstract Turing machine. Then he developed a poly-
transformation from this machine (i.e., all NP-class problems) to a particular decision problem, namely, the
Boolean Satisfiability (SAT) problem.
Significance of Cook’s theorem: if one can find a poly-algorithm for SAT, then by using Cook‗s poly-
transformation one can solve all NP-class problems in poly-time (consequently, P-class = NP-class would be
proved).
SAT is the historically first identified NP-hard problem!
Further significance of Cook’s theorem: if you find a poly-transformation from SAT to another problem
Z,then Z becomes another NP-hard problem. That is, if anyone finds a poly algorithm for Z, then by using
your poly-transformation from SAT-to-Z, anyone will be able to solve any SAT problem- instance in poly-
time, and hence would be able to solve all NP-class problems in poly- time (by Cook‗s theorem).
These problems, which have a chain of poly-transformation from SAT, are called NP-hard problems.
If an NP-hard problem also belongs to the NP-class it is called an NP-complete problem, and the group of
such problems are called NP-complete problems.
Circuit Satisfiability
Given a Boolean circuit, if there is an assignment of Boolean values to the input make the output
true.
Satisfiability
Given a Boolean formula, does these exixt a truth assignment to the variable to make the expression
true.
The goal of verification algorithm is verify a ―yes‖ answer to a decision problem‘s input
A verification algorithm takes a problem instances x and answer ―yes‖. If there exists a certificate y
such that answer for x with certificate y is ―yes‖
Example for NP complete problem:
1. Travelsalesman Problem:
For each 2 cities an integer cost is given to travel from one of the cities to the another.
The salesman wants to make a minimum cost circuit visiting each city exactly once.
2.Circuit Satisfiability:
Take a Boolean circuit with a single output node an ask whether there is an assignment of values to the
circuit, input . So that the output is 1.
Logic Gates are examples
3.Class Scheduling problem:
N teachers with certain hour restrictions M classes to be scheduled.
We can Schedule all classes
Make sure that no 2 teachers teach the same class at same time
No teacher is scheduled to teach 2 classes at once.
Backtracking:
Effective for decision problem
Systematically traverse through possible paths to locate solution or dead ends.
A the end of the path algorithm left with(x,y) pair, x is remaining subproblem and y is set of choices to
get x.
Branch and Bound
Effective for optimization problem.
Extended to backtracking
Instead of stopping once a single solution is found, continuous searching until the best solution is found.
Pondicherry University Questions
2 Marks
1 Define Branch-and-Bound method. (UQ APRIL‗13 & APRIL‗12) (Qn.No.1)
2. What are NP- hard and Np-complete problems? (UQ NOV‗12) (Qn.No.3)
3. Explain Least Cost (LC) Search (UQ APRIL‗13) (Qn.No.7) 4 .FIFO
Branch and Bound (UQ NOV‗12) (Qn.No.8)
5. Differentiate backtracking and branch-and-bound. Techniques (UQ APR‗11) (Qn.No.9)
6. Give the mathematical equation for 0/1knapsack problem. (UQ APR‗11) (Qn.No.18)
7. What is bounding function? (UQ:APRIL/MAY‗14) (Qn.No.19)
8. what is non deterministic algorithm (UQ:APRIL/MAY‗14,NOV‗14) ) ( Qn.No.20)
9. What is the use of ranking function (UQ:NOV‗14) (Qn.No.21)
11 Marks
1. Explain Branch and bound method (UQ APRIL‗12 & APRIL‗13,APRIL/MAY‗14) (Qn.No.1)
2. Explain Least Cost (LC) Search with 15 puzzle example(UQ APRIL‗13) (Qn.No.2)
3. Explain Knapsack problem (UQ NOV‗10,NOV‗14) (Qn.No.4)
4. Explain travelling salesman problem (UQ NOV‗12, NOV‗10 & APRIL‗12) (Qn.No.5)
5. Explain NP hard and NP-completeness (UQ APRIL‗13, NOV‗12 & APRIL‗12 ,APRIL/MAY‗14,NOV‗14)
(Qn.No.6)
15 PUZZLE GRAPH