0% found this document useful (0 votes)
31 views52 pages

PST Unit 1

5445

Uploaded by

AMBA DIGITAL
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)
31 views52 pages

PST Unit 1

5445

Uploaded by

AMBA DIGITAL
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/ 52

PROBLEM SOLVING

TECHNIQUES
CHAPTER -1
INTRODUCTION
• Computer is an electronic device which is used
to perform calculations with great speed.
• Computer System
A computer system is made up of two major
components :Hardware & Software.
HARDWARE
The physical components of the computer
system is called Hardware.
Eg: Keyboard, Mouse, Monitor etc
SOFTWARE
The logical components of the computer
system is known as software.
Eg: The Programs like C,C++,Java etc
Computer Hardware
• The hardware components of the computer
system consists of 5 major parts.
• Input devices- It is used to accept the data
entered by the user. Eg:- Keyboard,
Mouse, Scanner
etc
• Central Processing Unit(CPU)- CPU is
known as the heart or brain of the computer.
Its function is to control and co-ordinate all the
operations in the computer system.
Parts of CPU
• Control Unit – It controls and co-ordinates
all the operations in the CPU.
• Arithmetic & Logic Unit – This part of the
CPU is used to perform the arithmetic & logic
operations like +,-,*,/,% etc
• Memory Unit – It is used to store information.
Types of Memory

•  Primary Memory – It is the Main Memory


of the computer.
Eg: RAM – Random Access Memory
- It is Volatile Memory
-Read & Write Operations are possible
ROM – Read Only memory
- It is Non – Volatile Memory
- Only Read operation is possible.
 Secondary Memory – It is also known as
Auxiliary
Memory.
- They are not directly accessed by the CPU.

Eg: Magnetic Disk, Pendrive, CD,


Floppy Disk, Hard disk etc.
• Output Device :- It is used to display the
results after processing.
• Eg: Printer, Monitor Etc.
PROGRAM DEVELOPMENT LIFE CYLCE/
Steps in Problem Solving
* Algorithm
• Algorithm is a step by step procedure to solve
a given problem in finite number of steps.
or
• The steps taken to solve a problem is called
as algorithm. In computer programming term,
algorithm is useful for you
to solve the problem in the most efficient way.
*Advantages of Algorithm
• It is easy to understand since it is written in
normal English language.
• It consists of finite number of steps.
• Debugging is very easy since it is a step by
step instruction.
• It is independent of any programming
language so that the user need not know the
syntax of a programming language.
*Disadvantage
• Developing algorithms for large and complex
problems would be time consuming.
• Complex logic through algorithm would be
difficult to understand.
Standard Notations of Algorithm
• Name: It specifies the problem.
• Step Number : Each instruction is identified
by a number called step number.
• INPUT/OUTPUT: It describes the inputs &
outputs.
• Beginning/Termination :It indicates the
beginning and ending of the algorithm with a
START/STOP statement.
Write an algorithm to prepare a cup of tea.
• Step 1:- START
• Step 2:- Take a vessel
• Step 3:- Pour Water
• Step 4:- On the stove
• Step 5:- Place the vessel on the stove.
• Step 6:- Allow it to boil.
• Step 7:- Put tea powder
• Step 8:- Pour sufficient milk
• Step 9:- Add sugar
• Step 10:- STOP
Write an algorithm to add two numbers
• Step 1:- START
• Step 2:- INPUT A and B
• Step 3:- CALCULATE SUM=A+B
• Step 4:- OUTPUT SUM
• Step 5:- STOP
Write an algorithm to find the average of 4
numbers
• Step 1:- START
• Step 2:- INPUT m1,m2,m3 and m4
• Step 3:- CALCULATE
TOTAL = m1+m2+m3+m4
• Step 4:- CALCULATE AVERAGE =TOTAL/4
• Step 5:- OUTPUT AVERAGE
• Step 6:-STOP
Write an algorithm to find the biggest of 2
numbers
• Step 1:- START
• Step 2:- INPUT a and b
• Step 3:- IF a >b then PRINT Biggest is a
• Step 4:-else PRINT biggest is b
• Step 5:- STOP
*Write an algorithm to find the biggest of 3
numbers(let three numbers are a,b,c)
• Step 1:- START
• Step 2:- INPUT a ,b and c
• Step 3:- If (a >b) and (a>c) then PRINT
Biggest is a
• Step 4:- else if (b>c) then PRINT Biggest is b ,
else print biggest is c
• Step 5:- STOP
Applications of Algorithm
•Internet
•e-Commerce
•Weather Forecasting
•Linear Programming – optimum solution for a given
problem.
•Shortest Path Algorithm – shortest path
between two nodes.
•Speech Recognition
•Image Processing
*Qualities of Good Algorithm
• They are simple & Powerful.
• They are easy to understand.
• They can be easily modified.
• They can be documented to be used by others
who do not have a detailed knowledge pf
inner working.
• They are independent of programming
languages.
*Analysis of algorithms
• Analysis of algorithms is the determination of
the amount of time ,space & resources
required to execute it.
Or
• The analysis of algorithms is the process of
finding the computational complexity of
algorithms – the amount of time, storage, or
other resources needed to execute them.
*Complexity of an Algorithm
• Complexity of an algorithm is a measure of the
amount of time/space required by an
algorithm for an input of given size(n).
• Usually, the efficiency or running time of an
algorithm is stated as a function related to the
time complexity & space complexity.
• Space Complexity of an algorithm is the total
amount of space (computer memory) required
by the algorithm to complete its execution.
• Time complexity of an algorithm is the total
amount of time required by an algorithm to
complete its execution..
Types of algorithm analysis −
• Priori Analysis – Checking the algorithm
before it is implemented.
• Posteriori Analysis - Checking the algorithm
after it is implemented.
• Worst-case − The maximum number of steps
taken on input data of n elements.
• Best-case − The minimum number of steps
taken on data of n elements.
• Average case − An average number of steps
taken on input data of n elements..
*Asymptotic Notations
• It is the mathematical way of representing the time
complexity.
• Execution time of an algorithm depends on the
instruction set, processor speed, I/O speed. Hence,
we estimate the efficiency of an algorithm
• Following asymptotic notations are used to calculate
the running time complexity of an algorithm.
• O − Big Oh – upper bound
• Ω − Omega – lower bound
• θ − Theta- average bound
• Any function can be represented in either upper
bound, lower bound or in average bound.
(Big Oh) : Asymptotic Upper Bound

• Big Oh notation is used to describe how the


size of the input data affects an algorithm’s
running time.
• This method is used to find the upper bound
of an algorithm’s running time or growth
rate of function.(worst case)
Therefore f(n) =O(n)
Example
• If I have a function f(n) = 2n+3
then 2n+3 = -------? Which should be greater than
2n+3.
Therefore lets take 2n+3 <=10n
let n=1
therefore 5<=10 ----true n>=1

According Big O Notation f(n)= O(g(n))


Therefore f(n) =O(n) which is closer to the function.
Omega Notation
• This notation is used to find the lower bound
behaviour of a function.
• Lower bound behaviour means the algorithm
will take the least execution time.(best case)
Definition
• As per the definition, the function
f(n) = (g(n)) if and only if there exists a positive
constants c and n0 such that
f(n)>=c*g(n) for all n>=n0
Example- f(n)>=c*g(n) for all n>=n0
• If I have a function f(n) = 2n+3
then 2n+3 = -------? Which should be less than 2n+3.
Therefore lets take 2n+3 >=1*n
let n=1
therefore 5>=1 ----true for all n>=1
It is in the format that
f(n)=2n+3
c1
ng(n)

According omega Notation f(n)= (g(n))


Therefore f(n) = (n) which is closer to the function.
Theta Notation

The theta notation can be used when the function


f(n) can be bounded both from lower bound
and upper bound.
Definition: The function f(n)= g(n),
if and only if there exists a positive
constants c1,c2 and n0 such that
c1*g(n)<=f(n)<=c2*g(n)
Example
• F(n)=2n+3
1*n <= 2n+3 <= 10*n
This is in the form c1*g(n)<=f(n)<=c2*g(n)
Therefore we can write
F(n)= (n) and this is the Average bound of a
function f(n).
Growth of functions in algorithm
• Growth of functions are used to estimate the
number of steps an algorithm uses as its
input grows. (Running time)
• It depends on the time complexity.
*Standard Mathematical Notations
& Functions
• Monotonicity
- The monotonicity of a function gives an idea about
the behavior of the function. (whether it is increasing
or decreasing)
• - A function is said to be monotonically increasing if
its values are only rising and never falling.
• eg:- f(x) = 2x+3
• - A function is said to be monotonically
decreasing if its values are only falling and never
rising.
• eg:- f(x) = -x3
Floor & Ceil Functions
• The floor() function rounds down to the
nearest integer.
• Eg: floor(2.5)= 2 , floor(2.9)= 2
• The ceil() function rounds up to the
nearest integer.
• Eg: ceil(2.5)= 3 , ceil(2.9)= 3
Remainder Function(Mod)
• REMAINDER returns the remainder of n2
divided by n1 .
• Eg: 5 (mod 2) = 1
30 (mod 5) = 0
Absolute Value Function
• Returns the absolute value of a number. The
absolute value of a number is the number
without its sign.
Eg: ABS(-99)= 99
abs(-33.24) =33.24
Summation Symbol(Σ)
• The symbol Σ (sigma) is used to denote a sum
of multiple terms.
• For example, the sum of first n whole
numbers can be represented in the following
manner:
Factorial Function
• n! - Denotes the product of the positive
integers from 1 to n.
• Eg: 4! = 1*2*3*4
4!=24
5!= 1*2*3*4*5
5!=120
Permutation
• A Permutation of a set is the arrangement of
the elements of the set in some order.
• Eg: A set contains a, b, c. Then the various
permutation combinations can be
abc, acb, bca, cab, bac, cba
Exponents
• Exponent defines the number of times a
number is multiplied by itself.
• For example, in the expression 24, 4 is the
exponent
• Ie, 2*2*2*2 =16
Chapter -2
Fundamental algorithms
*1.Swaping(Exchanging of values of two
variables)
• Step 1: Start
• Step 2: Declare the three variables A,B,Temp
• Step 3:INPUT A,B and Temp
• Step 4: Let Temp=A
• Step 5: A=B
• Step 6: B=Temp
• Step 7: OUTPUT A and B
• Step 8: Stop
2. Counting (Count the number of digits in
an integer)
• Step 1: Start
• Step 2: Declare the variables n and i
• Step 3: INPUT n and initialize i=0
• Step 4: While (n is not equal to 0)
• Step 5:n=n/10
• Step 6:i=i+1
• Step 7: While end
• Step 8: Print i
• Step 9:Stop
*3. Summation of numbers(Sum of N
Natural numbers)
• Step 1: Start
• Step 2: Declare the variables n, i and sum
• Step 3: INPUT n and initialize sum=0,i=1
• Step 4:while i less than or equal to n do the following n
ie,(i<=n)
• Step 5: Sum =Sum+i
• Step 6:Increment the i ,i=i+1
• Step 7: while End
• Step 7:Print Sum
• Step 8:Stop
*4. Factorial of a number
• Step 1: Start
• Step 2: Declare Variable n, fact, i
• Step 3: INPUT n
• Step 4: Initialize the variables fact=1 and i=1
• Step 5: Repeat Until i<=n
5.1:- fact=fact*i
5.2 :- i=i+1
• Step 6: Print fact
• Step 7: Stop
*5.Fibonacci Series(0 1 1 2 3 5 8 13…)
• Step 1: - Start
• Step 2 : - Declare the variables f1,f2,f3,n
• Step 3:- INPUT n (no. of terms in the series)
• Step 4:-Initialize f1=0 , f2=1
• Step 5:- DISPLAY f1,f2
• Step 6:- Calculate f3=f1+f2
• Step 7:- DISPLAY f3
• Step 8:- Set f1=f2, f2=f3
• Step 9:- REPEAT steps 6- 8 for n times
• Step 10:- Stop
*6. Reverse of a number
• Step 1:- Start
• Step 2:- Declare a variable n, rev =0 and rem
• Step 3:- Read the number n;
• Step 4:- While n not equal 0
• Step 5:- rem=n%10;
• Step 6:- rev=rev * 10 + rem;
• Step 7:- n=n/10;
• Step 8:- End While
• Step 9 :- Print rev

Character to number conversion
• Step 1: Start
• Step 2: Declare n , i, value, length.
• Step 3: Initialize value=0
• Step 4: Input character value of n.
• Step 5: Initialize length = length of n
• Step 6: Loop over the characters in ‘n’ from
left to right.
for i= 1 to length
• Step 7: value = value *10 + (ASCII of i th
character - ASCII of 0)
• Step 8:Print value
• Step 9: Stop

You might also like