UNIT \II
C H A P T E R
2
JAlgorithm Fundamentals)
Algorithms atTA I roa qoif Gfys|TóNitA Microwave Oven, Washing
Machine, Air Conditioner fAgjAtGax
APU0A (Problem) 4 2N|Computer-A A-KAILA Computational (14aGF) Problem-4g
gAfrt A0A0 Algorithm \(*|
2.1 UjTCM0AY (Algorithms)
Algorithm A 1A173 aARJA J"m (Unambiguous) fg 1-T 3A (Sequence of
eRLT i1A I Problem- Inpute Output-4R AI SYt ArDetA Algorithm-43
A1IGA 9i +| 1A fog fNA (Set of Rules)-# RaAO PAI
Set of
Input Rules /Output/
Algorithm
2.2 BUiCAHfA-43 ta (Characteristics of Algorithms)
1. (Finiteness) : Algorithm 70 NARRIR Step-4 A S3|
2. Ta0^bOS (Definiteness) :a7GSDt Algorithm-43 Step- HRIfAS A0 AI
3. 3J2-6 Unambiguity): Algorithm-4% A Step R-2|a (Unambiguous) 24|
0. 2a95 (Input) : Algorithm-4 Input 1J (A A-P| ARIP ZS iKA | qeÍs, Input-f
Algorithm tGAI
6. eDbt (Output) Output 7 PI
7. a (Language Independent) : Algorithm l Programming language i A0
English -OILA0 S q Algorithnm ( VNI
[79]
80
is odd or even.
0ddEven(n) //to
check whether remainder
while dividing n by 2
Algorithm: assigned to
the
// x is
1. X := n mod 2 remainder is 1
//if
2. if X = 1 odd, "
"number is
3. | Print
remainder not 1 i.e. 0
is
4. else //if
Print "number is even.
5.
(4RTA 278 Input Variable-5 yo
º Algorithm-4g aIAD qo Function fAA GO 2,
Parameter f (GR0 2I
Algorithm q5 English-4 G0 3I, 9A comment-5 T0RGN ( Ì US ittaI
GVES 2 :[G ARjiT Average 41 PAI
|Algorithm:Average(a, b)
1. avg := (a + b)/2
2. Print avg
2.3 raAg utoTCAST (Recursive Algorithm)
Recursive algorithm 4 4 Y3AS Algorithm, TCo AÌ P| Function q
function-43 (repeat) A AIG PGI| ACA, Q Repeatition 3 s 4
Recursive Algorithm-4g f t E, qt GP0CA0 Algorithm-(
Readability 1yt|Q91, Step-sf Reusability 4KCAS RP (Simplify) qR
Step yle Recursive Algorithm-a NF f N Recursion
Algorithm-4g qft Base Case Recursion
Recursive Algorithm-q a Problem- QA Problem-•b
qq1 (0
DBase case
GwEg: frTA Factorial faef 4|
(OI At1 (4R goet) n-s3
.:. n!= n x(n -
Bactorial 2 n!= n X ( )X (n-2) x.....2y1
1)!
ANIA, n!-44 q5
Sub-problem (n - n! = 1
n = 0|
=0
Case : n!!= 1, R n
:n!= x(n-1)!, q
Base
Recursion
n>0
Algorithm : Factorial_recursive (n)
1. if n = 0
2. return (1)
3. return factorial_recursive (n - 1)
tCA
Factorial recursive(n) algorithm-f6 AI QCAE| q Step 3-a Recursion
QU3
n!- Sub-problem(n - 1)! #HAtAS
Step-2-o 1-return Q|
24 a-f0af BU0TCtta7 (Non-recursive Algorithm)
Non-recursive Algorithm-4A s Iterative Algorithm, T PIA0 Problem-4
G Function-(F Repeat AAt|
GEKS:|G ARR0n-a Factorial f
BAR0 IA, n! =lx2x3 x4x...xn, RCAn AF oRT0 qR 0! = 1|
|Algorithm:Factorial (n) 1/iterative
1. fact := 1 //fact is variable for factorial value
2. for i : 1 to n
3. |fact := fact x i //repeats for n times
4. return fact
SR Factorial (n) Algorithm-4 fact a Variable-g AI 0!-47 Na 3RT 3CACEI gRA
Factorial-4g a
2.5
SutaCAifAVH-F GPb0o-41 AJCA P(Representation of Algorithm
using Flowchart)
Flowchart -$ ATA, | I Process i Algorithm-q l
Flowchart-- a fal 4A14 Step
Table-44
82
Shapes
Flowchart-g Start Ì Stop AitA 2C
1. Oval
(O1A G7S TqS A
2. Input/Output Statement
Parallelogram
Option YT Question (
3. |Diamnond efe, PA0 yes/1o
Diamond-qT vIG GeF yes 3 no Option
Relatiom
Shape-afo1s I Connection T0 AKCT0S| PCA
4. |Arrow
5. Rectangle
te ICÌqpfR
Predefined Process A T C t G7J
6. Predefined Process J S QI
Step YI Task A I 4 t t GJ
7. Comment box -
aP0-bG-4 ICA A erS (Flowchart Representation):
Flowchart
Start
Input n
|Algorithm :0ddEven(n) Store the remainder in
1. X n mod 2 X := n mod 2 x by dividing n by 2
2. if X = 1
3. Print "number is odd."
4. else is no
X= ?
5. |Print "number is even.
Print "Number is Even'
yes
Print "Number is odd"
STOP
2.6
SGU P05 (Pseudo Code)
Pseudo Code Algorithm-4 Step-[aS 4ft fafrG Notation-4A AsTI
(PA Programming Language-4a AS0 Syntax-fer6J B,
Algorithm-4g Pseudo Code-(# AAiIA (OtA Compiler/
a Translate 0 | SA, Pseudo Code q setos ARt 2 4
Interpreter
Program-4 q8A AUS ttAI
Programmer
trER : Pseudo Code (0K t Style faCE DI G3 fég I
Algorithm : GREATER
1. Input m and n /m & n are number;
2. if m> n
3. Print "n is greater"
4. else
Print "n is greater"
5.
a i GREATER Algorithm-bo Step-sf AK 1(RI#5 T(GR0 2o|Q0AAA Comment
3T//'-4T (R0 QCZI A0Statement Step-4 ARG Block of Steps-F | ' s
IRRY CAZIZ| QIA Alogorithm (7IS GUfaGqt Style-4 Pseudo Code RRAI
» MLCU (AG-4 QG (AA s s (Writing Operation) :
Operation Symbol Operation Symbol
Assignment Arithmetic
add
Assignment =
subtract
Comparison
equal to multiply X
divide
not equal to modulus mod
greater than
Logical
less than And and
IV
greater than or equal Or
to Floor
less than or equal to Ceiling
> * (A efs (Writing Condition) : » qfR(NIA fo (Writing Loops) :
1. if condition 1. for v¡r := m to n
2. //steps to repeat
2.
some steps
3, else RTA Var 21 L0op Iterator/Counter, S m
4. |/some steps for-4g fvAN Step-f Repeat ARI
84 n u t a #fDg wnfet (4o) (Semester-I|
3FR (G0S ef (Writing Function):
Parameter- Algorithm-
Function-QI Algorithm (ARA GJ D
First Bracket Step |
P Value Return AU0 QT WTUR GAG VER A06 2IAlgorithm Add (x, y):
1. Z = X +t y
(AG--o Rg ISP ACAS Function Add (x,y)-4 2. return (z)
Algorithm
return z alsI (9TG
QRTA Function Add-•og Return Value o z: 3I return(z)
2.7 BU0TCAM0AT-4 PPO0 (Algorithm Analysis/Efficiency of Algorithm)
(Execute) 205 eCAGK A3RUF (O| OS Running Tia
PNl Algorithm Time-(# ifsa
Algorithm-43 AT Step Operation-41 7RO0A A0CA Running
Space-4 I T T z
R8Time Complexity| KAlgorithm-4g G AGAIGAK AID Memory
KAAAlgorithm OI Eficient o| 9 Complexi
Space Complexity I GPIA Problem aAS GJ
Resource (Time qR Space) JRIS PA o|R Algorithm-4
U RA Time Complexity i Space
obU K Efficient |f fs Input-43 CPA Algorithm
Complexity Q
Conditional Statements | wefe, ffse
Complexity (Time 0 Space) feA fOA ONCA|4g OK
4S execute 24; PAG R | PA, Complexity3
Step-[a AT PIC-0 (PA Step
Best Case Average Case-4 f GA|
Running Time Time complexity-$ Worst case,
*| INI (Time Complexity
time Complexity feN P FR Algorithm-4% Efficiency fbta
N-P) Input C F TAIT
»Worst Case :Worst Case Running Time 21, Algorithm-AI
Step-7i A0 Running Time ( Upper Bound)I Algorithm-•o
g A Time çat AUT AIS
at Algorithm-•5 PAHA0F I 0s|oco (Worst Case-a) execute ATG Y I
Lower Bound 193 I3| o
» Best Case: Best Case-& Algorithm-4% Running Time-4
Algorithm-fog 117 Running Time Step-730|O Input i1 A Running Time
i8a8 a0|Algorithm-542A0KOG R|So(Best Case-4) execute AKA YS|Z
O0SIST t
» Average Case : Average Case-4 Algorithm-4 Running time 1k*
#3, Best Case 0 Worst Case 4 1sM (Rare) RAI39 f a f s (General Case-4)
(IAI Input-4g y Running Time fa P|
etG Average Case- Genera
Case-4 Running Time fA OA0 | A(4, Average Step-sy 3|4A T
Input-AA AAIA FYaNI (Equally Likely) 43 2N aR Probability /31g 3 fag (
Average Running Time (43 3| 84A|
2.8 yjTCYÍAT-4g Cet1lt (Complexity of Algorithm)
AIAIg¢fS Problem G qt9 Algorithm tcrg ATEI #D Efficien"y
A l g o r i t h m - 4
Efficiency AIIA GJ Algorithm-T Complexity fa 1 N|
85
Complexity Algorithm-f6 S08 ATsA art
Algorithm-43
Space-qz afa tA, Complexity- fa1tF Machine Independent- to
Memory
g Algorithm Machine-43 83 ferég Af r fS ttA
Algorithm-43 fR ProgramImplementation-4 (Code-4) fSr fT Mernory Space Tsa
Complexity-* alnput Size 1 Input-44 I(n)-44 Function frta
N-(1) Time Complexity (2) Space Complexity l
Machine q105 Computer tMobile -oA0 Computing Device( |
Input Size-T e Algorithm-43 Input Data-a RI GIs I(AATAt n-s
integer- Set Algorithm-4A CFA Input Size 4 ni
Problem Size 15 sInput Size-# AATN | VA Aft Problem-4g 4AT GA FAE CBA
2.8.1 DA ALeb (Time Complexity):
Time Complexity 2 (A Algorithm-44 GATG Step t Primitive Operation(IGG
getA)-4T ART0A 0 i \ |Time Complexity-# Input data-s Size (n)-A Function A
s I 2|QIA Primitive Operation qS A0N, fC1, 59, S1, Logical Operation,
Memory Assignment Store at 0 t GAT|
Time Complexity- AIGA 00 Algorithm-4 AID Running Execution Time-4g f
S1 AIN | DR, Time Complexity-($ AITA|A Running Time ( DI AT 2T| Dao
Actual Running Time |39, Time Complexity Primitive 0Basic Operation-4 IG
I0 AIA lActual Running Time fýA UA Algorithm-ibg SGA Cot s0 Program q4R oN
Execution-4 g , 0 machine AýAI
D0ÈT ACEfsft faofa (Derivation of Time Complexity) :
Algorithm-4 Statement-a5 afs Execution-4 Step S0 G KO AS AUS
(Steps Per Execution) | Recursive Function yÍ4 4R 'steps/execution'-4% AA l
4s0 NI
AKA ets Statement-4g Step-afS times | frequency, fe Py0 Execute 4SI
CO I'steps/execution'-# frequency a109 PA Step-5t II RAID Running
Time 1Time Complexity it80 A|
aI(5 git Algorithm-44 Time Complexity fq AGTTG-0 2 |
Algorithm Factorial (n) steps/
execution frequency total steps
[Link] := 1 1
[Link] i:= 1t0 x 1 n + 1
3. fact := fact x x i
[Link] (fact)
total 2n +3
86
Statement 4 f Execution-4 Step t 14t QICS A, SONe steps/execution =1
Staterment-o for
step-st gataexecute 91|fa Loop-
bye Statement-4
( # (AftaI
Q step-f6 n + l 13 execute 93 (4AA loop
Condition Checking |
statement-4 step-j5 n-a execute
|
Body-4 Statement-sf, DOR
or
qOAR. at Step = 2n + 3 Time Complexity
Time complexity T(n) = 2n + 3 T(n) Function
2.8.2 C 2 f s f t (Space Complexity) :
Algorithm-4 Space Complexity
0\Al Size
Memory
(n)-e
1 (ANI Space Complexity- Input
(Temporary Space-)
Space qUIA PA
Space Complexity-8
AK GNRR9-4 Factorial (n) Algorithm-4 S(n) Function faAK (A QT |
CTNR
0 fact, i Sn bfo Variable-4g j ATGa lf6
Complexity S(n)=3, | qÍt Constant
I
Space 0R, Space
PE Memory
2.9
(AsymptoticNotation)
g t f f o seo, K TCA Algorithm-43 Complexity
Asymptotic Notation Running
JCA Input Size (n) i I CSË CS
Running TÉme-F S P 0 2A|
Notation-4
oTS fAs Asynmptotic Notation
JRIA AA| QN
Tcomipexity
(i) Big 'Oh' (0)
f ( n )
(ii) Big Omega (2)
T(n)
(ii) Big Theta (O)
((4st 8 GaTCG):
2.9.1 Big Oh' (0) Notation
unction T(n) = 0(f(n))ehe
n
(R1 Running Time-4
0A0 Positive Constant c8 Big Oh (O): T(n) =0 (f(n)
"Big O of f(n)" q1 2A, 2A
-44 G T(n) S c-f[n)3N, RIAn2 n,l
Running Time Function-41
Upper Bound 4
G0449-T(n) = 3n + 2= 0(n),
4 ONotation-4
023|
93n + 2 < 4n
Tcomiplexity JCA PIL0 Algorithm-48
T(n)
n 21
GCA1 aICDM):
2.9.2 Big Omega (9) Notation (f4
f(n)
Running tiine T(n) = 2 (1(n)) e "Omega of f(n)"
4o1 24, ( 1 1 Positive Constant c n,-41
T(D) zc fn) , AIA nzn,iS2 Notation o1 Running
Big Omega (2): T(n)
Tine Funcion % Lower Bound 44 gq|
BMIeAMYqM-ln) =10n' 2n +2- 2(n'), 19 10n+2n+2 >n' aIC n 21|
2.9.3|
Big Theta (0)
Notation (ft
Running Time T(n) = 0(f(n) qofe "Theta of f(n)" q1
Tcomiplexity Cf{n)
T ( n )
81
o Positive Constant C C2 n - 3 GA
C,f(n) 3, GTNA n2n,l0 Notation at
C G . f n )
<,An)s T(n) s
Upper s Lower Bound-g TA Tight Bound a t I
no
-T(n) = 3n + 2 = 9(n), at 3n s 3n + 2< 4n n
RIAn 2 |
Big Theta (0) : T(n) = 0 (f(n)
ARION GPA Algorithm-4s Complexity O Notation-q Upper Bound faAAt
Factorial Algorithm-41 Time Complexity T(n) = 2n +3= 0(n)
1. Algorithm-4 NI Goeft? 6. Na Problem -f9( q Sub-problem -4
A unambiguous
finite number of steps A recursive algorithm
set of rules
non-recursive algorithm
O machine dependent ) both A&B
2. Algorithm -( a AA R y OIterative algorithm
7. (IGl Algorithm obi efficient v of*
A pseudo code
B machine code actual running time
Omathematical function B time complexity
O object code Ospace complexity
3. Algorithm -4 comment ( Gelj 19A (D both B & C
8. H01 (4 IA0 algorithm-4a efficiency
A) # OL
4. (-l algorithm-q qo function AIA (A) Best case B) wOrst case
(O Average case (D Actual case
B Non-Recursive algorithm 9. (Ll algorithm-4 running time 2n 4n
(8 Repeatitive algorithm
Iterative algorithm BS2(n)
O Recurs0ve algorithm
5. (o1=(5 recursive algorithm -41 0 (n) D O(n)
0A2
10. (0C-l algorithm-a time complexity 23
A) Base case (A) execution time
8 conditional statement
9return statement
(8) algorithm-4 abas
O looping (O complex operation-4 ARTI
(D) Basic operations-4 ATI
machine
-fr6g?
88 C5A (Oerfo
memory 14. A) time complexity
faN1
11, 0 algorithmn at B space complexity
complexity
) worst case
(D) actual run-time
(A memory complexity
(8 space complexity algorithm-44
timee complexity
Ospace requirenment 15. Factorial
O(n) O O(n) D O(logn
(D space count D O(n) 2
complexity - lower -4sfo
12. talgorithm 4R time 16. O, S2,
Ts notation 2 (A) Letters
bound B Asymptotic notation
BigA
4 Big O Big S2OBig 0 D O Symbolicnotation
D Analyticnotation
notation -
bound
17. Tight O Big S2
time complexity A Big O
8 running time D Big A
Big
OAverage case complexity
Dcondition-4 ARTI
Algorithm-41 Complexity-(6 4
1.
algorithm-4 aog (extra) 7.
memory- S Q|
Non-Recursive A Output Size B Input Size
ARecursive
OIterative O Repeatitive O Problem Size (D) Statement-4 R
2. (A1 algorithm-4 best case time complexity 8. Algorithm-3 e3IG Memory- aÍsA=
running time A I
(D actual
A Time Complexity
3. GGA algorithm-43 case analysis-4
Space Complexity
RTCA GAAIN OK running time-4 upper Problem Size
bound. O Swap Space
A) Best BAverage () Worst D) Actual 9. Algorithm-43 Complexity-(*
4. ( l algorithm -4% running time faég A AiJCA Lower bound A* $
input size
ABig-O G Big
output size OBig-S2 DBig-A
Ospace requirement 10. Big-0 notation-4A Function-
D number of variables TCA
5. 4Q14 AAlgorithm ( | 3N | A) Lower Bound
A Pseudo Code
BMachine Code B Tight Bound
(O Upper Bound O Strict Bound
OFunction D Object 11. (-I
6. Base Case o Algorithm- Running
Algorithm-44 Time
(A) Best Case
A) Recursive
B Non-Recursive B) Worst Case
OJterative
(D Finite OAverage Case
(D Least Case
89
Column-A
Column-B 2. Column-A Column-B
) A l g o r i t h m (a) Operation-4R ) Algorithm (a) Upper Bound
(ii) Syntax
) Recursive (b)|General Case
(ii) Worst Case (c) Pseudo Code
Algorithm
iüi) Average Case (c)Unambiguity (iv)Recursive (d) g Function-4g
ir) Time Complexity(d) Base Case Algorithm
A(0-(a), (i)-(c), (ifi)-(d), (iv)-(b) (1)-(a), (ii)-(b), (iii)-(c), (iv)-(d)
)-(a), (ii)-(b), (i)-(d), (iv)-(c) O (i)-(b), (i)-(c), (ii)-(a), (iv)-(d)
o )-c),(ii)-(d), (ii)-(b), (iv)-(a) © (i)-(c), (i)-(b), (ii)-(a), (iv)-(d)
o (0)-(c), (i)-(b), (ii)-(d), (iv)-(a) O (i)-(a), (ii)-(c), (ii)-(d), (iv)-(b)
MI
Execution Time-4g s ( S |
AAlgorithm-41 PIA0 Machine-qg gT
OSpace Complexity GT Input Size-4g
O Recursive Algorithm-4 Base Case
1. Flowchart-4 conditonal statement (A4|A
A Connection B Comment
O Process D Input/Output
4. FlowChart-Q Comment (GRIN Ge IAQO QI
2 Flowchart -4
A) B II
Condition test B Input/output
O Process (D) Start
2. f(n) = 2n°+4n-3 2(1 4311 AN f(n) =
1 Algorithmn Add (X, y) A O(n)
1, z:= X + y
2. return (Z) (D (n)
Algorithm-fo4 Time Complexity 2(4
B) O(3) O O(1) (D O(n)
iteCa(KI) (Sem.-I|-7