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

Algo Vc Lecture3

Uploaded by

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

Algo Vc Lecture3

Uploaded by

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

Lecture 3.

Algorithms and its


comparisons

1
Recap

 An algorithm is a step by step process to solve a problem.


 No of Inputs, outputs, completeness, accuracy , correctness and finite are the
main features of algorithms
 Pseudo code can be used expert person by following the syntax of any
programming language
 An algorithm is platform Independent
 Algorithms notation are used to design an algorithm

2
Expressing Algorithms

 English description
More easily More
expressed  Pseudocode precise

 High-level
programming
language
Analysis of algorithms

 Why analyze algorithms?


– evaluate algorithm performance
– compare different algorithms
 Analyze what about them?
– running time, memory usage, solution quality
– worst-case and “typical” case
 Computational complexity
– Classifying problems according to difficulty level
– algorithms provide upper bound scenario

4
Analysis of algorithms (Cont !!!)

– to show problem is hard or complete


– It requires at least a given amount of resources
– Transform problems to establish “equivalent” difficulty

5
What is the "best" Algorithm

 You can considered an algorithm best on the base answers of following questions.
– How fast does it run?
Refer to processing time
– How "complicated" is the algorithm
Time Complexity
Space Complexity
– How well is the algorithm documented
Written pseudo code should be clear , complete and well documented
– Can the machine used have influence on the results
Yes
Some good algorithms can not perform well on slow machines.

6
Important Point to note

 Programs depend on the operating systems, machine, compiler/interpreter used, etc.


 Analysis of algorithms compare algorithms and not programs.

 Performance of Algorithms should be measured before its implementation as program in any

language.
 Algorithms should be checked on different machines.

7
Example-1.

 Consider following four pseudo codes (in C)


 Purpose of all pseudo codes is same.

 We are analysing them on the base do time and space trade-off factors

8
PesuedoCode-1.
main()
{
Int a,b,c;
a=2;
b=3;
c=a+b; Facts:
printf(“%d”, c);
} 1.Three variables ( 6
bytes)
2.Three assignment
process
3.One Calculation
4.One output
statement

9
PesuedoCode-2.
main()
{
int a,b;
a=2;
b=3;
printf(“%d”, a+b); Facts:
}
1.Two variables ( 4
bytes)
2.Two assignment
process
3.One Calculation
4.One output
statement

10
PesuedoCode-3.
main()
{
int a,b,c;
scanf(“%d%d”, &a, &b);
c=a+b;
printf(“%d”, c); Facts:
}
1.Three variables ( 6
bytes)
2.One input
statement
3.One Assignment
4.One Calculation
5.One output
statement

11
PesuedoCode-4.
main()
{
int a,b;
scanf(“%d%d”, &a, &b);

}
printf(“%d”, a+b);
Facts:
1.Two variables ( 4
bytes)
2.One input
statement
3.One Calculation
4.One output
statement

12
Comparison of Pseudo codes

Facts Pseudo Pseudo Pseudo Pseudo


1 2 3 4
Variable 3 (6 2(4 3 (6 2
s bytes) bytes) bytes) (4bytes)
No of 3 2 1 0
Assignm
ents
No of 1 1 1 1
Calculati
on
Input 0 0 1 1
13 Stateme
nts
Which one Pseudo code is best and
how

 With respect to space trade of Pseudo code 2 and Pseudo code 4 are candidate for best
because in these pseudo codes 2 variables are used.
 But when focus on time tradoff/complexity then Pseudo 2 code will best
 Why not Pseudo code 4 is best though, you are entering dynamic data (through scanf )
– Because Some instruction access by microprocessor and some are executed by IO circuit of computer system.
– Switching between IO and Microprocessor takes extra time

14
Example-2. Pseudo code about
finding swapping the position of two
digits number
main()
{
int N,a,b;
N=54;
a= N/10;
b=N%10 Facts:
N = b*10+a

}
printf(“%d”, N); 1.variables ?
2.input statement ?
3.Calculation ?
4.output
statement ?
5.Assignments ?

15
Example-2. Pseudo code about
finding swapping the position of two
digits number (Cont!!!)

Facts:
1.variables ? 3 (6 bytes)
2.input statement ? 0
3.Calculation ? 3
4.output statement ? 1
5.Assignments ? 3

16
Example-2. Pseudo code about
finding swapping the position of two
digits number (Cont!!!) (Second
Pseudo code)
main()
{
int N=54;
N = (N%10)*10 + (N/10)

}
printf(“%d”, N);
Facts:
1.variables ?
2.input statement ?
3.Calculation ?
4.output
statement ?
5.Assignments ?

17
Example-2. Pseudo code about
finding swapping the position of two
digits number (Cont!!!)

Facts:
1.variables ? 1 (2 bytes)
2.input statement ? 0
3.Calculation ? 1
4.output statement ? 1
5.Assignments ? 1

18
Home Work

 Writeat least three pseudo codes to find the largest of three numbers and
analyzed these pseudo codes on base of space and time trade off factors.

19
Summary

 Analgorithm or pseudo code will be considered best if it full fill the time and space
trade off/ complexities.
 Performance of algorithm should be measured and not of implemented program.
 During writing algorithm or pseudo code, you have to focus over the extra use of data
structure and switching of instructions form CPU to IO circuits and vise versa

20
In Next Lecturer

 In
next lecture, we will talk the time and space complexities of algorithms
by taking some examples.

21

You might also like