0% found this document useful (0 votes)
18 views

DAA UNIT 1

This document provides an introduction to algorithms, defining key concepts such as algorithm specification, performance analysis, time complexity, and space complexity. It outlines the characteristics of algorithms, methods for specification including natural language, flowcharts, and pseudo-code, as well as the importance of performance analysis in evaluating algorithm efficiency. Additionally, it discusses the factors influencing time and space complexity, emphasizing the significance of these metrics in algorithm selection and program execution.

Uploaded by

msdchinnu128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

DAA UNIT 1

This document provides an introduction to algorithms, defining key concepts such as algorithm specification, performance analysis, time complexity, and space complexity. It outlines the characteristics of algorithms, methods for specification including natural language, flowcharts, and pseudo-code, as well as the importance of performance analysis in evaluating algorithm efficiency. Additionally, it discusses the factors influencing time and space complexity, emphasizing the significance of these metrics in algorithm selection and program execution.

Uploaded by

msdchinnu128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT-1

Introduction- Algorithm definition, Algorithm Specification, Performance Analysis- Space


complexity, Time complexity, Randomized Algorithms.

Algorithm: An Algorithm is a finite sequence of instructions, each of which has a clear


meaning and can be performed with a finite amount of effort in a finite length of time. No matter
what the input values may be, an algorithm terminates after executing a finite number of
instructions. In addition every algorithm must satisfy the following criteria:

 Input: there are zero or more quantities, which are externally supplied;

 Output: at least one quantity is produced

 Definiteness: each instruction must be clear and unambiguous;

 Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will
terminate after a finite number of steps;

 Effectiveness: every instruction must be sufficiently basic that it can in principle be carried out
by a person using only pencil and paper. It is not enough that each operation be definite, but it
must also be feasible.

We represent algorithm using a pseudo language that is a combination of the constructs of a


programming language together with informal English statements.

Pseudo code for expressing algorithms:


Algorithm Specification: Algorithm can be described in three ways.
1. Natural language like English: When this way is used care should be taken, we should ensure
that each & every statement is definite.
2. Graphic representation called flowchart: This method will work well when the algorithm is
small and simple.
3. Pseudo-code Method: In this method, we should typically describe algorithms as program,
which resembles language like Pascal.
Pseudo-Code Conventions:

1. Comments begin with // and continue until the end ofline.


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 – 1data-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.
5. Assignment of values to variables is done using the assignment statement. := ;
6. There are two Boolean values TRUE and FALSE.  Logical Operators AND, OR, NOT
Relational Operators <=,>,>=, =,!=
7. The following looping statements are employed.
For, while and repeat-until
While Loop:
While < condition > do
{
<St 1>
.
.
.
<St n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<St 1>
.
.
.
<St n>
}
repeat- until:
repeat
<St 1>
.
.
.
<St n>

Until<condition>

8. A conditional statement has the following forms.


If <condition> then <statement>
If <condition> then <statement>
Else <statement1>
Case statement:

Case
{
<cond 1>: <stmt 1>
.
.
.
<con n>: <stmt n>

Else <stmt 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,
Example:

1. Algorithm selection sort(a,n)


2. // Sort the array a[1:n] into non-decreasing order
3. {
4. for I:=1 to n do
5. {
6. j:=I;
7. for k:=i+1 to ndo
8. if (a[k]<a[j])
9. t:=a[I];
10. a[I]:=a[j];
11. a[j]:=t;
12. }
13. }

Performance Analysis:
The performance of a program is the amount of computer memory and time needed to run a
program. We use two approaches to determine the performance of a program. One is analytical,
and the other experimental. In performance analysis we use analytical methods, while in
performance measurement we conduct experiments.

Time Complexity:

The time needed by an algorithm expressed as a function of the size of a problem is called the
time complexity of the algorithm. The time complexity of a program is the amount of computer
time it needs to run to completion.
The limiting behavior of the complexity as size increases is called the asymptotic time
complexity. It is the asymptotic complexity of an algorithm, which ultimately determines the size
of problems that can be solved by the algorithm.

The Running time of a program


When solving a problem we are faced with a choice among algorithms. The basis for this
can be any one of the following:

i. We would like an algorithm that is easy to understand code and debug.


ii. We would like an algorithm that makes efficient use of the computer’s resources,
especially, one that runs as fast as possible.

Measuring the running time of a program

The running time of a program depends on factors such as:

1. The input to the program.


2. The quality of code generated by the compiler used to create the object program.
3. The nature and speed of the instructions on the machine used to execute the program,
4. The time complexity of the algorithm underlying the program
Space Complexity:

The space complexity of a program is the amount of memory it needs to run to


completion. The space need by a program has the following components:
Instruction space: Instruction space is the space needed to store the compiled version
of the program instructions.

Data space:
Data space is the space needed to store all constant and variable values. Data space has
two components:
1. Space needed by constants and simple variables in program.
2. Space needed by dynamically allocated objects such as arrays and class instances.

Environment stack space: The environment stack is used to save information needed to
resume execution of partially completed functions.

Instruction Space: The amount of instructions space that is needed depends on factors
such as:
 The compiler used to complete the program into machine code.
 The compiler options in effect at the time of compilation
 The target computer.

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.

You might also like