• No results found

Algorithm has method the can be used by a computer for the solution of a problem .the following is -definition of algorithm

N/A
N/A
Protected

Academic year: 2021

Share "Algorithm has method the can be used by a computer for the solution of a problem .the following is -definition of algorithm"

Copied!
6
0
0

Loading.... (view fulltext now)

Full text

(1)

INTRODUCTION

The word algorithm comes from the name of a Persian mathematician. Algorithm has method the can be used by a computer for the solution of a problem .the following is -definition of algorithm.

“An algorithm is finite set of instructions that if followed accomplish a particular task”.

The following are the characteristics of an algorithm.

1. Input: an algorithm should accept zero or more quantities that are externally supplied.

2. Output: an algorithm must produce at least one quantity as output.

3. Definiteness: each instruction in an algorithm is clear and not complex.

4. Finiteness: the algorithm should terminate after finite number of steps.

5. Effectiveness: every instruction with in the algorithm must be very basic.

Note: In general each step of algorithm must require one or more operations. The algorithms which satisfy the definite and effective properties are known as computational procedure. Generally there are four distinct areas in the study of algorithms.

1. How to device algorithms – how to create or generate an algorithm - It is an art.

2. How to validate algorithm – a carefully developed algorithm-should compute the correct answer for all possible legal inputs.

3. How to analyze algorithm −Algorithm analysis or performance- Analysis refers how an algorithm utilizes the system resource that is processor time (clock) and storage (memory).

4. How to test a program −Testing program consists of two phases a) Debugging

b) Profiling

a) Debugging means executing programs on sample data sets

to determine whether fault results occur and if so correct them.

b) Profiling means executing a correct program on data sets and measuring the time and space it takes to generate the result.

How to write an algorithm

We can describe an algorithm in many ways, to write an algorithm we can use natural language English or graphic symbols. Suppose if we select English, make sure that the instructions are definite. The graphic representation of algorithm is known as the flow charts.

The following notations we use to write the algorithms

1. Comments begin with ‘//’ and they continue until end of line.

2. The instructions within algorithm are ended with; and block of instructions are enclosed within braces { and }.

3. An identifier begins with a letter. The data types of the variables not explicitly declared. A compound data type can be formed with record.

(2)

Eg: node=record {

Datatype1 data1;

Datatype2 data2;

……….. ……

……….. ……

}

4. Assignment of values to variables is done by using the assignment statement <Variable> :=< expression> Or Variable ← value

5. To produce the Boolean values, we may use logical operators AND,OR and NOT and relational operators <,=,>,≤,≥,≠.

6. Elements of an array is represented by using the [ ].For example A may be two dimensional array, i, j are the indexes then it can be written as A[i, j].

7. The loop statements are represented as follows While <condition> do

{

Statements

…………

}

For variable: = value1 to value2 step do {

Statements

………….

}

Here step is optional and take as ‘+1’ if it does not occur and it could be positive or negative.

8. The conditional statements are represented as follows a) If <condition> then

Statement

If <condition> then Statement-1 Else

Statement-2

b) CASE

{

:< condition 1>: Statement 1 ………….. ………..

:< condition-n>: Statement n : Else statement

}

9. Input and output statements are specified with Read and Write instructions.

No format specifies are required.

10. The procedures are begin with Algorithm keyword Eg: Algorithm Name (parameter list)

{

Statements

…………

(3)

…………

}

Analysis of Algorithms:

The analysis of the program does not mean that simply working of program but to check for all possible situations the program works correctly or not. The analysis involves the efficiency of the program. Here program efficiency means,

1. The program may require or utilize less amount of memory space.

2. The program should execute in less time, that utilizing less amount of processor clock.

So we understand that Time and space factors determine the program efficiency. This is also known as Time and Space complexity. The following is the definition. “The space complexity of an algorithm is the amount of memory it needs to run to completion”.

“The time complexity is the amount of computer time needs to run to completion”.

The performance evaluation can be divided into two phases.

1. Priory estimates-Which known as performance analysis

2. Posteriori testing-Which is known as performance measurement.

Space Complexity:

The space needed by the algorithm is the sum of following things,

1. A fixed part that is independent of characteristics, of inputs, outputs. This part contains instruction space.

2. A variable part that consists of the space needed by the component variables whose size is dependent on problem instance to be solved.

Finally the space requirement S(p) of a program ‘p’ may written as

S(p)=C+Sp (Instance characteristics) Where c is constant.

Time Complexity:

The time complexity T(p) of a program ‘p’ is sum of compile time and run time(execution time) the compile time does not depend upon instance characteristics and once a compiled program will be run several times without recompilation. So we consider run time as a complexity of a program. We can measure the time complexity of a program in number of ways.

1. Time is physically clocked-It is not a effective method.

2. Program step count-It is also known as frequency count. We follow this method.

a) Let us consider x=x+1 this statement will execute exactly once. Hence its frequency count is one 0(1)

b) If(a<5)

{ This is also executing at once so its frequency count is 0(1).

b=10 }

(4)

c) a=1 while (a<5)

{

printf (“hai”);

a++;

}

In the above a=1 statement is executed once; While (a<5) is executed 5 times. printf( ) and a++ statements are also executed 5 times each. So its frequency count is 1+5+5+5=16.

d) Let us consider for (i=1; i<=n; i++) {

Printf(“Hai”);

}

In the above i=1, is executed once, i<=n is executed (n+1) times including fail test case and i++, printf( ) statements are executed ‘n’ times. So the frequency count is 1+

(n+1)+n+n=3n+2.

A PRIORI Analysis of Recursive Functions:

The priori analyses of recursive functions are different from the iterative functions. We know that in iterative functions we calculate the total frequency count and then estimate the complexity by using the mathematical functions such as big ’oh’.

In case of recursive functions first we formulate the recurrence relation, which define the behavior of the function. After formulating the recurrence relation we find the solution to it and then estimate its complexity. To frame the recurrence relation, we use unknown time function T(n), Where n is used to measure the size of the arguments. We then get the recurrence relation for T(n) in terms of T(k) for various values of k.

e.g:1 the following is the recurrence function for the factorial.

function fact(n) {

if (n==1) then fact=1 else

fact=n * fact(n-1) }

In the factorial function the time complexity for if (n=1) and fact=1 is O(1) and time complexity for fact=n*fact(n-) is O(1)+T(n-1). Here T(n-1) is time complexity for fact(n- 1). Let c, d are some constants.

T(n)=c+T(n-1) =c+c+T(n-1-1) =2c+T(n-2)

=3c+T(n-3)

(5)

For Kth step

T(k)=c.k+T(n-k) if n>k But finally k=n-1

T(n) = c(n-1)+T(n-n+1) = (n-1)c+T(1) = (n-1)c+d =O(n)

Eg:2 The following is the function for Towers of Hanoi function towers(N,S,D,I)

{

if (N=0) then exit ( )

else

towers (N-1, S, D, I) //Transfer N-1 disks from Peg S to Peg I by using Peg D as intermediate Transfer disk S to D

towers (n-1, I, S, D) //Transfer N-1 disks from Peg I to Peg D using Peg S as intermediate.

}

The recurrence relation derived for towers of Hanoi as. Let T(N) is the minimum numbers of transfers to solve the puzzle with N disks.

Let N=0 No disks are transfer

N>0 two recursive calls required to transfer n-1 disks from source to destination.

Thus the recurrence relation is T(N)=0 if N=0 T(n)=2T(n-1)+1 if N>0

‘+1’ represents final transfer (single transfer) from peg S to peg D.

Now the recurrence relation is T(N)=2T(N-1)+1

=2(2T (n-2) +1)+1 =22T(n-2) +2+1

=22(2T (n-3) +1)+2+1 =23T(n-3) +22+2+1

For kth step

T(n)=2kT(n-k)+2(k-1)+2(k-2)+22+2+1

Finally k=n then

T(n)=2nT(0)+2n-1+2n-2+…..+22+2+1 =2n.0+2n-1

=0+2n-1 =O(2n)

(6)

Different approaches to design an algorithm:

We know that algorithms are used to manipulate the data contained in data structure.

So the algorithms can perform operations on the stored data. Generally each and every algorithm is designed in modular fashion. That is each and every algorithm is divided into small peaces called modules and then writes the algorithm for every module independently. The following are advantages of modularization.

1. It is easy to understand an algorithm.

2. It is easy to design and implement the algorithm 3. Modules are designed independently

There are two main approaches in modularization a) To-down approach

b) Bottom-up approach

(Diagram Material)

To-Down approach: In Top-down approach, initially we consider an algorithm into a single unit and then divide this into two or more modules. If necessary these modules are further divided into sub modules and soon until we reach the desired level of complexity.

Bottom-up approach: It is reverse to the top down approach in which we goes into the deep up to basic or elementary operations and then integrate(combined) to form entire algorithm.

Advantages of Top-down approach:

1. It is appreciated for ease of documenting modules.

2. It is easy to generate test cases.

3. It is easy to implement the code.

4. In this approach we can easily remove the errors.

Disadvantages:

Here modules are analyzed in isolation-that is without considering their communication with other modules

Advantages of Bottom-up approach:

1. This approach allows for information hiding.

2. It provide abstract interface between modules.

3. It exactly defines the boundaries between the modules.

Disadvantages:

It is difficult to design an algorithm in bottom-up strategy.

References

Related documents

Project Management, Configuration Services, Deployment Services, Asset Tagging, Apple One-to-One Services Solution, Remote Consulting, and Full-Time Resources are custom

Based on quality management principles Customer focused Leadership Involvement of people Process Approach System Approach Continual Improvement. Factual Approach to Decision

Ranging widely from research to academic studies to community education, these grants support the mission of Otterbein, enhance faculty achievement and student learning,

The distribution data in Figure 13 demonstrates that at least a year in advance, with production modules rather than champion modules, SSI accomplishments far exceed the 2003 DOE

This conclusion is further supported by the following observations: (i) constitutive expression of stdE and stdF in a Dam + background represses SPI-1 expression (Figure 5); (ii)

Making sacramental wine requires special attention and care, starting with qvevri washing and marani hygiene and ending with fermentation, aging and storage. During

Bayes rule, Dempster-Shafer rule and Full Bayes Updating rule imply consequentialism but do not imply dynamic consistency if the capacity is not necessarily additive.. CKL

Indaco noun - masculine Examples maglione indaco indigo sweater color indaco color indigo. L'indaco prende il nome da una pianta