Docsity

Algorithm Design: Merge Sort and Dynamic Programming, Exercises of Designs and Groups

Partial preview of the text

Download Algorithm Design: Merge Sort and Dynamic Programming and more Exercises Designs and Groups in PDF only on Docsity!

DESIGN AND

ANALYSIS OF

ALGORITHM

LECTURE NOTES

ON

DESIGN AND ANALYSIS OF ALGORITHMS

Prepared by

Dr. Subasish Mohapatra

Department of Computer Science and Application

College of Engineering and Technology, Bhubaneswar

Biju Patnaik University of Technology, Odisha

Module-I
Lecture 1 - Introduction to Design and analysis of algorithms 
MOTIVATION

The advancement in science and technology enhance the performance of processor, which proportionally affect the characteristics of computer system, such as security, scalability and reusability. Important problems such as sorting, searching, string processing, graph problems, Combinational problems, numerical problems are basic motivations for designing algorithm.

DESIGN GOAL

The Basic objective of solving problem with multiple constraints such as problem size performance and cost in terms of space and time. The goal is to design fast, efficient and effective solution to a problem domain. Some problems are easy to solve and some are hard. Quite cleverness is required to design solution with fast and better approach. Designing new system need a new technology and background of the new technology is the enhancement of existing algorithm. The study of algorithm is to design efficient algorithm not only limited in reducing cost and time but to enhance scalability, reliability and availability.

The main concern of the course ensures:

i) Correctness of solution ii) Decomposition of application into small and clear units which can be maintained precisely iii) Improving the performance of application

INTRODUCTION TO ANALYSIS OF ALGORITHM

A lay man perceives that a computer perform anything and everything. It is very difficult to ensure that it is not really the computer but the man behind computer who does the whole thing.

For example users just enter their queries and can get information as he/she desire. A common man rarely understands that a man made procedure called search has done the entire task and the only support provided by the computer is the execution speed and organized storage information.

‘Algorithm’ is defined after the name of Abu Ja’ far Muhammad ibn Musa Al-Khwarizmi , Ninth century , al-jabr means “restoring ” referring to the process of moving a subtracted quantity to other side of an equation ; al-muqabala is “comparing” and refers to subtracting equal quantities from both sides of an equation.

Definition of Algorithm

 An algorithm is a set of rules for carrying out calculation either by hand or on a machine.  An algorithm is a sequence of computational steps that transform the input into the output.  An algorithm is a sequence of operations performed on data that have to be organized in the data structures.  A finite set of instruction that specify a sequence of operations to be carried out in order to solve a specific problem or class of problems is called an algorithm.  An algorithm is an abstraction of a program to be executed on a physical machine (model computation).

 An algorithm is defined as set of instructions to perform a specific task within finite no. of steps.  Algorithm is defined as a step by step procedure to perform a specific task within finite number of steps.  It can be defined as a sequence of definite and effective instructions, while terminates with the production of correct output from the given input.

Example of algorithm

Let us assume can common example of ATM withdrawal system .To present the scenario of a person under goes the following steps.

1. SWAP the card
2. Enter the PIN
3. Put the amount
4. Withdraw amount
5. Check balance
6. Cancel/clear.

If we go through these above 6 steps without considering the statement of the problem, we should assume that this is the algorithm for clearing the toilet. As of several ambiguities arises there while comprehending every step. The step 1 may imply toothbrush, paintbrush, toilet brush etc. Such an ambiguous doesn’t an instruction an algorithmic step. Thus every step should be made unambiguous step is called ‘ definite instruction’. Even if the step 2 is rewritten as apply the toothpaste, to eliminate ambiguities yet the conflicts such as, where to apply the toothpaste and where the source of the toothpaste is, need to be resolved. Therefore, the act of applying the toothpaste is not mentioned. Although unambiguous, such unrealizable steps can’t be included as algorithmic instruction as they are not effective.

The definiteness and effectiveness of an instruction implies the successful termination of that instruction. Hence that above two may not be sufficient to guarantee the termination of the algorithm. Therefore, while designing an algorithm care should be taken to provide a proper termination for algorithm.

CHARACTERISTICS OF AN ALGORITHM

Every algorithm should have the following five characteristic features:

a) Input b) Output c) Finiteness d) Definiteness e) Effectiveness f) Precision g) Determination h) Correctness i) Generality

a) Input An algorithm may have one or more inputs. The inputs are taken from a specified set of subjects. The input pattern may be texts, images or any type of files. b) Output

Expectation from an algorithm

Correctness:-

Correct: Algorithms must produce correct result.

Produce an incorrect answer: Even if it fails to give correct results all the time still there is a control on how often it gives wrong result. Eg. Rabin- Miller PrimalityTest (Used in RSA algorithm): It doesn’t give correct answer all the time.1 out of 250 times it gives incorrect result.

Approximation algorithm: Exact solution is not found, but near optimal solution can be found out. (Applied to optimization problem.)

Less resource usage:

Algorithms should use less resources (time and space).

Resource usage:

Here, the time is considered to be the primary measure of efficiency .We are also concerned with how much the respective algorithm involves the computer memory.But mostly time is the resource that is dealt with. And the actual running time depends on a variety of backgrounds: like the speed of the Computer, the language in which the algorithm is implemented, the compiler/interpreter, skill of the programmers etc.

So, mainly the resource usage can be divided into: 1.Memory (space) 2.Time

Time taken by an algorithm?

performance measurement or Apostoriori Analysis: Implementing the algorithm in a machine and then calculating the time taken by the system to execute the program successfully.

Performance Evaluation or Apriori Analysis. Before implementing the algorithm in a system. This is done as follows

How long the algorithm takes :-will be represented as a function of the size of the input.

f(n)→how long it takes if ‘n’ is the size of input.

How fast the function that characterizes the running time grows with the input size.

“Rate of growth of running time”.

The algorithm with less rate of growth of running time is considered better.

How algorithm is a technology?

Algorithms are just like a technology. We all use latest and greatest processors but we need to run implementations of good algorithms on that computer in order to properly take benefits of our money that we spent to have the latest processor. Let’s make this example more concrete by pitting a faster computer(computer A) running a sorting algorithm whose running time on n values grows like n2 against a slower computer (computer B) running assorting algorithm whose running time grows like n lg n. They each must sort an array of 10 million numbers. Suppose that computer A executes 10 billion instructions per second (faster than any single sequential computer at the time of this writing) and computer B executes only 10 million instructions per second, so that computer A is1000 times faster than computer B in raw computing power. To make the difference even more dramatic, suppose that the world’s craftiest programmer codes in machine language for computer A, and the resulting code requires 2n2 instructions to sort n numbers. Suppose further that just an average programmer writes for computer B, using a high- level language with an inefficient compiler, with the resulting code taking 50n lg n instructions.

Computer A (Faster) Computer B(Slower)

Running time grows like n2. Grows inkling.

10 billion instructions per sec. 10million instruction per sec

2n2 instruction. 50 long instruction.

Time taken=

It is more than 5.5hrs it is under 20 mins.

So choosing a good algorithm (algorithm with slower rate of growth) as used by computer B affects a lot.

Let Ci be the cost of ith line. Since comment lines will not incur any cost C3=0. Cost No. Of times Executed

C1n C2 n-

C3=0 n-1 C4n-1 C

C6 )

C7 C8n-

Running time of the algorithm is:

T(n)=C1n+C2(n-1)+0(n-1)+C4(n-1)+C5 +C6( )+C7( )+ C8(n-1)

Best case:

It occurs when Array is sorted.

All devalues are 1.

T(n)=C1n+C2(n-1)+0 (n-1)+C4(n-1)+C5 +C6( )+C7( )+ C8(n-1)

=C1n+C2 (n-1) +0 (n-1) +C4 (n-1) +C5 + C8 (n-1)

= (C1+C2+C4+C5+ C8) n-(C2+C4+C5+ C8)

Which is of the forman+b?

Linear function of n. so, linear growth.

Worst case: It occurs when Array is reverse sorted, and tj =j

T(n)=C1n+C2(n-1)+0 (n-1)+C4(n-1)+C5 +C6( )+C7( )+ C8(n-1)

=C1n+C2(n-1)+C4(n-1)+C5 +C6( )+C7( )+ C8(n-1)

which is of the form an2+bn+c

Quadratic function. So in worst case insertion set grows in n2. Why we concentrate on worst-case running time?

The worst-case running time gives a guaranteed upper bound on the running time for any input.

For some algorithms, the worst case occurs often. For example, when searching, the worst case often occurs when the item being searched for is not present, and searches for absent items may be frequent.

Why not analyze the average case? Because it’s often about as bad as the worst case.

Order of growth:

It is described by the highest degree term of the formula for running time. (Drop lower-order terms. Ignore the constant coefficient in the leading term.)

Example: n2 /2 − 2n = Θ (n2), with c1 = 1/4, c2 = 1/2, and n0 = 8.

step 1: guess the form of solution T(n)=4T(n/2)

 F(n)=4f(n/2)

 F(2n)=4f(n)

 F(n)=n

So, T(n) is order of n2 Guess T(n)=O(n3)

Step 2: verify the induction

Assume T(k)<=ck3 T(n)=4T(n/2)+n

<=4c(n/2)3 +n

<=cn3/2+n

<=cn3-(cn3/2-n)

T(n)<=cn3 as (cn3/2 – n) is always positive So what we assumed was true.

 T(n)=O(n3)

Step 3: solve for constants Cn3/2-n>=

 n>=

 c>=

Now suppose we guess that T(n)=O(n2) which is tight upper bound Assume(k)<=ck

so, we should prove that T(n)<=cn

T(n)=4T(n/2)+n

= 4c(n/2)2+n

=cn2+n

So, T(n) will never be less than cn2. But if we will take the assumption of T(k)=c1 k2-c2k, then we can find that T(n) = O(n2)

2. BY ITERATIVE METHOD:

e.g. T(n)=2T(n/2)+n

=> 2[2T(n/4) + n/2 ]+n

=>22T(n/4)+n+n

=> 22[2T(n/8)+ n/4]+2n

=>23T(n/23) +3n

After k iterations ,T(n)=2kT(n/2k)+kn--------------(1) Sub problem size is 1 after n/2k=1 => k=logn So, after logn iterations ,the sub-problem size will be 1. So, when k=long is put in equation 1 T(n)=nT(1)+nlogn

= nc+nlogn ( say c=T(1))

= O(nlogn)

3.BY RECURSSION TREE METHOD:

In a recursion tree ,each node represents the cost of a single sub-problem somewhere in the set of recursive problems invocations .we sum the cost within each level of the tree to obtain a set of per level cost, and then we sum all the per level cost to determine the total cost of all levels of recursion.

Constructing a recursion tree for the recurrence T(n)=3T(n/4)+cn

4 4

4

4

4

No. Of nodes at depth i=3i

Cost of each node at depth i=c (n/4i)

Cost of each level at depth i=3i c (n/4i)2 = (3/16)icn2 T(n)= i=0∑log4n cn2(3/16)i

T(n)= i=0∑log4n -1 cn2(3/16)i + cost of last level Cost of nodes in last level =3iT(1)

 c3log n (at last level i=log n)

 cnlog 3

T(n)= + c nlog 3

<= cn

 <= cn2*(16/13)+ cnlog 3 =>T(n)=O(n2)

4.BY MASTER METHOD:

The master method solves recurrences of the form

T(n)=aT(n/b)+f(n)

where a>=1 and b>1 are constants and f(n) is a asymptotically positive function. To use the master method, we have to remember 3 cases:

If f(n)=O(nlogba - Ɛ) for some constants Ɛ >0,then T(n)=ϴ(nlogba)

If f(n)=ϴ( nlogba) then T(n)=ϴ(nlogbalogn)

If f(n)=Ὠ(nlogba+Ɛ) for some constant Ɛ>0 ,and if af(n/b)<=cf(n) for some constant c<1 and all sufficiently large n,then T(n)=ϴ(f(n))

2

2

T(n)=2T(n/2)+nlogn

ans: a=2 b=

f(n)=nlogn

using 2nd formula f(n)=ϴ( nlog 2logkn)

=>ϴ(n1 logkn)=nlogn =>K=

T(n)=ϴ( nlog 2 log1n)

=>ϴ(nlog2n)