0% found this document useful (0 votes)
21 views42 pages

CH 1

Uploaded by

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

CH 1

Uploaded by

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

Chapter 1

BY
Manoj Kumar Kar
Algorithm specification:

• Pseudo code conventions.


• Recursive Algorithms
Pseducode Conventions
1. Comments.
2. Blocks.
3. Identifiers.
4. Assignments.
5. Logical Values.
6. Arrays.
7. Looping.
8. Conditional Statement.
9. Input and Output
10. Procedure
Psedu code Conventions
1. Comments:
• Comments begin with // and continue until the
end of the line.
2. Blocks:
• Blocks are indicated with matching braces {}.
• A compound statement ( A collection of simple
statements) can be represented as a block.
• The body of a procedure also forms a block.
• Statements are delimited by ;
3. Identifier:
• An identifier begins with a letter.
• The data types of variables are not explicitly
declared.
4. Assignments:
• Assignment of values to variables is done
using the assignment statement
(variable):=(expression
5. Logical Values:
• There are two Boolean values true and false. In order
to produce these values, the logical operators and, or,
and not and the relational
operators<,<,=,/,>,and > are provided
6. Arrays:
• Elements of multidimensional arrays are accessed
using [ and ]. For example if A is a two dimensional
array, the (i,j)th element of the
array is denoted as -A[i,j] Array indices start at zero
7. Looping:
• The following looping statements are employed.
• While, for and Repeat untill
while (condition)do
{
(statement1)
(statement n)
}
• The general form of a for loop is
• for variable:= value 1 to value2 step step do
{
(statement1)
(statement n)
}
• A repeat-until-statement is constructed as
follows:
repeat
{statement1)
{statement n)
until{condition)
8. Conditional Statements:
• A conditional statement has the following
forms:
• if {condition) then {statement)
• if (condition) then {statement1)
else(statement2)
9. Input and Output:
• Input and output are done using the
instructions read and write. No format is used
to specify the size of input or output
quantities.
10. Procedure:
• There is only one type of procedure Algorithm
• An algorithm consists of
1. Heading
2. Body
Heading
• The heading takes the form
Algorithm_ Name((parameter list))
where Name is the name of the procedure
({parameter list)) is a listing of the procedure parameters.
Body:
• The body has one or more (simple or
compound) statements enclosed within braces
{ and }.
Example
• Write an Algorithm to find and return the maximum of
n given numbers:
1. Algorithm Max(A, n)
2. // A is an array of size n.
3.{
4. Result:=A[l];
5. for i :=2 to n do
6. if A[i] >Result then Result:=A[i\\;
7. Return Result;
8. }
Selection sort:
1. for i :=1 to n do
2. {
3. Examine a[i] to a[n] and suppose
4. the smallest element is at a[j];
5. Interchange a[i] and a[j];
6. }
Selection Sort
1. Algorithm Selection Sort n(a),
2. // Sort the array a[\\ :n] into non decreasing order.
3.{
4. for i :=1to n do
5. {
6. j =i;
7. for k :=i+ 1to n do
8. if (a[k]<a[j] )then j=k
9. t :=a[i]; a[i]:=a[j]; a[j]-t;
10. }
11. }
Recursive Algorithm
• A recursive function is a function that is
defined in terms of itself.
• Similarly, an algorithm is said to be recursive if
the same algorithm is invoked in the body.
• Algorithm A is said to be indirect recursive if it
calls another algorithm which in turn calls A.
ALGORITHM SPECIFICATION
• [Towers of Hanoi] The Towers of Hanoi puzzle is fashioned
after the ancient Tower of Brahma ritual. According to
legend, at the time the world was created, there was a diamond
tower (labelled A) with 64 golden disks. The disks were of decreasing
size and were stacked on tin: tower in decreasing order of size
bottom to top. Besides this tower. There were two other diamond
towers (labelled B and C).Since the time of creation, Brahman priests
have been attempting to move the disks from
tower A to tower B using tower C for intermediate storage. As the
disks are very heavy, they can be moved only one at a time. In
addition, at no time can a disk be on top of a smaller disk. According
to legend, the world will come to an end when the priests have
completed their task.
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);
9. TowersOfHanoi(n-1, z, y, x);
10. }
11. }
Performance Analysis
• Does it do what we want it to do?
• Does it work correctly according to the
original specifications of the task?
• Is there documentation that describe show to
use it and how it works?
• Are procedures created in such a way that
they perform logical sub-functions?
• Is the code readable?
Performance Analysis
• The performance of an algorithm can be
measured by its complexity such as
1. Space Complexity.
2. Time Complexity:
Space Complexity
• The space complexity of an algorithm is the amount of
memory it needs to run to completion.
• Example: Iterative function for sum
1. Algorithm Sum( a, n)
2 {
3. s:=0.0;
4. for i :=1 to n do
5. s :=s+ a[i];
6. Return s;
7. }
Space Complexity
• A fixed part that is independent of the
characteristics (e.g., number, size)of the inputs
and outputs.
• This part typically includes the instruction
space(i.e., space for the code), space for
simple variables and fixed-size component
variables(also called aggregate),space for
constants, and so on.
Space Complexity
• 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 this depends on
instance characteristics and the recursion
stack space (insofar as this space depends on
the instance characteristics)
• The space requirement S(P) of any algorithm P
may therefore be written as
S(P)= c + Sp (instance characteristic)
Where C is a constant
Sp is an instance characteristic.
Example 1
1. Algorithm abc (a, b, c)
2. {
3. return a + b + b * c+ (a + b -c)/(a+ b) + 4.0;
4. }
• For the above Algorithm , the problem instance is characterized
by
the specific values of a, b, and c. Making the assumption that one
word is adequate to store the values of each of a, b, c, and the
result, we see that the space needed by a, b, c is independent of
the instance characteristics.
Hence
• Sp(instance characteristics=)0.
Example 2
1. Algorithm Sum( a, n)
2. {
3. s:=0.0;
4. for i :=1 to n do
5. s :=s+ a[i];
6. returns;
7. }
• The problem instances for the above Algorithm are
characterized by n, the number of elements to be
summed.
• The space needed by n is one word, since it is of type
integer.
• The space needed by a is the space needed by variables of
type array of floating point numbers.
• This is at least n words, since a must be larger enough to
hold the n elements to be summed. So, we
obtain
Ssum(n)>= (n+3) (n for a[]. One each for n, i and s).
Time Complexity
• The time complexity of an algorithm is the amount
of computer time it needs to run to completion.
• The time T(P) taken by a program P is the sum of
the compile time and the run (or execution)time.
• T(P)= Compile Time + Execution Time
• The compile time does not depend on the
instance characteristic.
• Also, we may assume that a compiled program
will be run several times without recompilation.
• Consequently we concern ourselves with just the run time of a
program. This run time is denoted by Tp (instance characteristic).
• Tp(n) = CA ADD(n)+ CBSUB(n) CM MUL(n)+ CD
DIV(n) + ...
• Where n denotes the instance characteristics a, and CA, CB CM, CD
respectively, denote the time needed for an addition,
subtraction, multiplication, division, and so on, and ADD, SUB,
MUL, DIV, and so on, are functions whose values are the
numbers of additions ,subtractions,
multiplications, divisions, and so on, that performed when the
code for P is used on an instance with characteristic n.
• Obtaining such an exact formula is in itself an impossible task, since
the time needed for an addition, subtraction, multiplication and
division so on, often depends on the numbers being added,
subtracted, multiplied, and so on.
• The value of Tp(n) for any given n can be obtained only
experimentally the program is typed, compiled, and run on a
particular machine.
• The execution time is physically clocked, and Tp(n) obtained. Even
with this experimental approach, one could face difficulties.
• In a multiuser system, the execution time depends on such factors
as system load, the number of other programs running on the
computer at the time program P is run, the characteristic of these
other programs and so on.
• Given the minimal utility of determining the exact number of
additions, subtractions, and so on, that are needed to solve a
problem instance with characteristics given by n, we might as
well lump all the operations together (provided that the time
required by each is relatively independent of the instance
characteristics) and obtain a count for the total number of
operations. We can go one step further and count only the
number of program steps.
• A program step is loosely defined as a syntactically or
semantically meaningful segment of a program that has an
execution time that is independent of the instance
characteristic.
• For example the entire statement
• Return a + b+ b*c+(a+ b-c)/(a+ b) + 4.0;
• Is considered as one step as its execution time
is independent of the instance characteristics.
• depends on the kind of statement. For exampThe
number of steps any program statement is assigned
le,
• Comments count as zero steps
• An assignment statement which does not involve
any calls to other algorithms is counted as one step
• In an iterative statement such as the for, while, and
repeat-until statements we consider the step counts
only for the control part of the statement.
Asymptotic Notation(O, Ω, Ɵ)

• Definition of Big O notation:


• [Big "oh"] The function f(n) = 0(g(n)) (read as F
of n is big 0h of g of n") iff (if and only if) there
exist positive constants c and n0
• Such that f(n) <=c* g(n) for all n, n >= n0
Examples

• The function 3n+2 = 0(n)as 3n+2 < 4n for all n


> =2.
• 3n + 3 = O(n) as 3n + 3 < 4n for all n >= 3
• 1OOn+ 6 = 0(n)as 1OOn+6 < 101n for all n > 6.
• 10n2+4n +2 = 0(n2)as 10n2+4n+2 <= 11n2 For
all n>=5
• 3n + 2 != 0(1) as 3n +2 is not less than or equal
to c for any constant c and for all n >= n0
• 0(n)is called linear notation.
• 0(n2)is called quadratic notation
• 0(n3)is called cubic Notation.
• 0(2n) is called exponential notation.
Definition of Ω notation
• The function F(n) = Ω (g(n)) (read as F of n
is omega of g of n) iff there exist positive
constants c and n0 such that
f(n) >= c* g(n) for all n, n>= n0
Example
• 3n + 2 = Ω (n) as 3n + 2 > = 3n for n >= 1
• 3n+3 = Ω (n) as 3n+3 >=3n for n > =1
• 100n+6 = Ω(n) as 100n+6 > 1OOn for n>=1
• 10 n2+ 4n + 2 = Ω(n2 ) as 10 n2 + 4n + 2>= n2
for n>=1
Definition of Ɵ Notation
• The function F(n) = Ɵ (g(n)) (read as F of n
is theta of g of n) iff there exist positive
constants c1, c2 and n0 such that
c1g(n)<= f(n)<= c2g(n) for all n, n>= n0
Example
• 3n + 2 = Ɵ (n) as 3n + 2 > = 3n for all n >= 2
and 3n + 2 > = 4n for all n>=2
In this case c1 = 3, c2 = 4 and n0 = 2
• 3n+3 = Ɵ(n)
• 10 n2+ 4n + 2 = Ɵ(n2 )

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy