CHAPTER IV
ALGORITHMS
Institute of Technology of Cambodia
ITC
November 18, 2023
Mr. PEN CHENTRA AMS November 18, 2023 1 / 31
Contents
1 Introduction
2 Examples of Algorithms
3 Analysis of Algorithsms
4 Recursive Algorithms
Mr. PEN CHENTRA AMS November 18, 2023 1 / 31
Contents
1 Introduction
2 Examples of Algorithms
3 Analysis of Algorithsms
4 Recursive Algorithms
Mr. PEN CHENTRA AMS November 18, 2023 2 / 31
Introduction
An algorithm is a step-by-step method of solving some problem.
The word “algorithm” derives from the name of the ninth-century
Persian mathematician al-Khowarizmi. Today, “algorithm” typically
refers to a solution that can be executed by a computer.
Mr. PEN CHENTRA AMS November 18, 2023 3 / 31
Character of algorithm
Character
Algorithms typically have the following characteristics:
Input The algorithm receives input.
Output The algorithm produces output.
Precision The steps are precisely stated.
Determinism The intermediate results of each step of execution are
unique and are determined only by the inputs and the
results of the preceding steps.
Finiteness The algorithm terminates; that is, it stops after finitely
many instructions have been executed.
Correctness The output produced by the algorithm is correct; that is,
the algorithm correctly solves the problem. Generality
The algorithm applies to a set of inputs.
Mr. PEN CHENTRA AMS November 18, 2023 4 / 31
Finding the Maximum of Three Numbers
Finding the Maximum of Three Numbers
Figure: Pseudocode
Mr. PEN CHENTRA AMS November 18, 2023 5 / 31
Finding the Maximum Value in a Sequence
Finding the Maximum Value in a Sequence
Figure: Psedocode
Mr. PEN CHENTRA AMS November 18, 2023 6 / 31
Contents
1 Introduction
2 Examples of Algorithms
3 Analysis of Algorithsms
4 Recursive Algorithms
Mr. PEN CHENTRA AMS November 18, 2023 7 / 31
Searching
Text search
Mr. PEN CHENTRA AMS November 18, 2023 8 / 31
Example
Example 1 (Example)
Shows a trace of Algorithm where we are searching for the pattern
“001” in the text “010001”
Mr. PEN CHENTRA AMS November 18, 2023 9 / 31
Sorting
Example 2
Suppose that i = 4 and s1 = 8, s2 = 13, s3 = 20, s4 = 27 is
8 13 20 27
If s5 is 16, after it is inserted,s1, · · · , s5 becomes
8 13 16 20 27
Mr. PEN CHENTRA AMS November 18, 2023 10 / 31
Sorting
Figure: Insertion Sort
Mr. PEN CHENTRA AMS November 18, 2023 11 / 31
Randomized Algorithm
A randomized algorithm does not require that the intermediate results
of each step of execution be uniquely defined and depend only on the
inputs and results of the preceding steps. By definition, when a
randomized algorithm executes, at some points it makes random
choices. In practice, a pseudorandom number generator is used
Mr. PEN CHENTRA AMS November 18, 2023 12 / 31
Example
Mr. PEN CHENTRA AMS November 18, 2023 13 / 31
Example
Figure
Mr. PEN CHENTRA AMS November 18, 2023 14 / 31
Contents
1 Introduction
2 Examples of Algorithms
3 Analysis of Algorithsms
4 Recursive Algorithms
Mr. PEN CHENTRA AMS November 18, 2023 15 / 31
Required time
Time to Execute an Algorithm if One Step Takes 1 Microsecond to
Execute. lg n denotes log2 n (the logarithm of n to base 2)
Figure: Table of required time to execute
Mr. PEN CHENTRA AMS November 18, 2023 16 / 31
Case time
We can ask for the minimum time needed to execute the algorithm
among all inputs of size n. This time is called the best-case time for
inputs of size n. We can also ask for the maximum time needed to
execute the algorithm among all inputs of size n. This time is called the
worst-case time for inputs of size n. Another important case is
average-case timethe average time needed to execute the algorithm
over some finite set of inputs all of size n.
Mr. PEN CHENTRA AMS November 18, 2023 17 / 31
Example
In practise, we are less interested in the exact best-case or worst-case
time required for an algorithm to execute than we are in how the
best-case or worst-case time grows as the size of the input increases
Example 3
Suppose that the worst-case time of an algorithm is
t(n) = 60n2
+ 5n + 1
for input of size n. For large n, the term 60n2 is approximately equal to
t(n)
Mr. PEN CHENTRA AMS November 18, 2023 18 / 31
Figure: Comparing growth of t(n) with 60n2
Under these assumptions, t(n) grows like n2 as n increases. We say that
t(n) is of order n2 and write t(n) = Θ(n2), which is read “t(n) is theta of
n2.” The basic idea is to replace an expression, such as
t(n) = 60n2 + 5n + 1, with a simpler expression, such as n2, that grows
at the same rate as t(n). The formal definitions follow.
Mr. PEN CHENTRA AMS November 18, 2023 19 / 31
Definition
Definition 4
Let f and g be functions with domain {1, 2, 3, ...}.
We write
f(n) = O(g(n))
and say that f(n) is of order at most g(n) or f(n) is big oh of g(n)
if there exists a positive constant C1 such that
|f(n)| ≤ C1|g(n)|
for all but finitely many positive integers n.
Mr. PEN CHENTRA AMS November 18, 2023 20 / 31
Definition
Definition
We write
f(n) = Ω(g(n))
and say f(n) is of order at least g(n) or f(n) is omega of g(n) if
there exists a positive constant C2 such that
|f(n)| ≥ C2|g(n)|
for all but finitely many positive integers n.
Mr. PEN CHENTRA AMS November 18, 2023 21 / 31
Definition
Definition
We write
f(n) = Θ(g(n))
and say f(n) is of order g(n) or f(n) is theta of g(n) if
f(n) = O(g(n)) and f(n) = Ω(g(n))
Mr. PEN CHENTRA AMS November 18, 2023 22 / 31
Example 5
Since
60n2 + 5n + 1 ≤ 60n2 + 5n2 + n2 = 66n2for all n ≥ 1, we may take
C1 = 66 , then
60n2
+ 5n + 1 = O(n2
)
60n2 + 5n + 1 ≥ 60n2 for all n ≥ 1, we may take C2 = 60, then
60n2
+ 5n + 1 = Ω(n2
)
Since 60n2 + 5n + 1 = O(n2) and 60n2 + 5n + 1 = Ω(n2), then
60n2
+ 5n + 1 = Θ(n2
)
Mr. PEN CHENTRA AMS November 18, 2023 23 / 31
Theorem
Theorem 6
Let
p(n) = aknk
+ ak−1nk−1
+ · · · + a1n + a0
be a polynomial in n of degree k, where each ai is nonnegative. Then
p(n) = Θ(nk
)
Mr. PEN CHENTRA AMS November 18, 2023 24 / 31
Contents
1 Introduction
2 Examples of Algorithms
3 Analysis of Algorithsms
4 Recursive Algorithms
Mr. PEN CHENTRA AMS November 18, 2023 25 / 31
A recursive function (pseudocode) is a function that invokes itself. A
recursive algorithm is an algorithm that contains a recursive function.
Recursion is a powerful, elegant, and natural way to solve a large class
of problems. A problem in this class can be solved using a
divide-and-conquer technique in which the problem is decomposed
into problems of the same type as the original problem. Each
subproblem, in turn, is decomposed
further until the process yields subproblems that can be solved in a
straightforward way. Finally, solutions to the subproblems are
combined to obtain a solution to the original problem.
Mr. PEN CHENTRA AMS November 18, 2023 26 / 31
Example
Example 7 (Example)
Recall that if n ≥ 1, n! = n(n − 1) · · · 2 · 1, and 0! = 1. Notice that if
n ≥ 2, n factorial can be written “in terms of itself” since, if we “peel
off” n, the remaining product is simply (n − 1)!; that is,
n! = n(n − 1)(n − 2) · · · 2 · 1 = n · (n − 1)!
Figure: Decomposing the Factorial
Problem
Figure: Combining Subproblems of
the Factorial Problem
Mr. PEN CHENTRA AMS November 18, 2023 27 / 31
Algorithms
Algorithm
Figure: recursive algorithm
Mr. PEN CHENTRA AMS November 18, 2023 28 / 31
Example
Example 8
A robot can take steps of 1 meter or 2 meters. We write an algorithm to
calculate the number of ways the robot can walk n meters. As
examples:
Figure: walk(n) = walk(n − 1) + walk(n − 2)
Mr. PEN CHENTRA AMS November 18, 2023 29 / 31
Robot walk
Mr. PEN CHENTRA AMS November 18, 2023 30 / 31
Robot Walking
Mr. PEN CHENTRA AMS November 18, 2023 31 / 31

More Related Content

PPT
data unit notes from department of computer science
PPT
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
PPT
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
PDF
Anlysis and design of algorithms part 1
PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
PPTX
UNIT DAA PPT cover all topics 2021 regulation
PPTX
Lecture2a algorithm
PPT
Design and analysis of algorithm in Computer Science
data unit notes from department of computer science
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
Anlysis and design of algorithms part 1
Presentation_23953_Content_Document_20240906040454PM.pptx
UNIT DAA PPT cover all topics 2021 regulation
Lecture2a algorithm
Design and analysis of algorithm in Computer Science

Similar to Chapter IV Algorithm for I3ab Ams at itc (20)

PDF
Data Structure: Algorithm and analysis
PPT
lecture 1
PPTX
Data structures and ALGORITHMS methd binary SEARCH
PPTX
Data structures and algorithms (DSA) are foundational concepts in computer sc...
PDF
Algorithm review
PDF
01 Notes Introduction Analysis of Algorithms Notes
PDF
What is Algorithm - An Overview
DOCX
Basic Computer Engineering Unit II as per RGPV Syllabus
PPTX
DS Unit-1.pptx very easy to understand..
DOCX
Theoryofcomp science
PPT
Data_Structure_and_Algorithms_Lecture_1.ppt
PPT
PPT
Algorithms
PDF
Iteration, induction, and recursion
PPT
Design and Analysis of Algorithms
PPTX
Design and Analysis of Algorithms Lecture Notes
PDF
Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
PDF
Data Structure - Lecture 1 - Introduction.pdf
PPT
analysis of algorithms and asymptotic complexity
PPT
algorithms-1 master in computer application
Data Structure: Algorithm and analysis
lecture 1
Data structures and ALGORITHMS methd binary SEARCH
Data structures and algorithms (DSA) are foundational concepts in computer sc...
Algorithm review
01 Notes Introduction Analysis of Algorithms Notes
What is Algorithm - An Overview
Basic Computer Engineering Unit II as per RGPV Syllabus
DS Unit-1.pptx very easy to understand..
Theoryofcomp science
Data_Structure_and_Algorithms_Lecture_1.ppt
Algorithms
Iteration, induction, and recursion
Design and Analysis of Algorithms
Design and Analysis of Algorithms Lecture Notes
Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
Data Structure - Lecture 1 - Introduction.pdf
analysis of algorithms and asymptotic complexity
algorithms-1 master in computer application
Ad

Recently uploaded (20)

PPTX
Solar energy pdf of gitam songa hemant k
PPTX
AI-Reporting for Emerging Technologies(BS Computer Engineering)
PPTX
Principal presentation for NAAC (1).pptx
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PPTX
chapter 1.pptx dotnet technology introduction
PPTX
CT Generations and Image Reconstruction methods
PDF
Project_Mgmt_Institute_-Marc Marc Marc .pdf
PPTX
Micro1New.ppt.pptx the main themes if micro
PDF
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PDF
Designing Fault-Tolerant Architectures for Resilient Oracle Cloud ERP and HCM...
PPT
Programmable Logic Controller PLC and Industrial Automation
PDF
electrical machines course file-anna university
PPTX
CNS - Unit 1 (Introduction To Computer Networks) - PPT (2).pptx
PDF
Mechanics of materials week 2 rajeshwari
PPTX
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
PPTX
Wireless sensor networks (WSN) SRM unit 2
Solar energy pdf of gitam songa hemant k
AI-Reporting for Emerging Technologies(BS Computer Engineering)
Principal presentation for NAAC (1).pptx
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
20250617 - IR - Global Guide for HR - 51 pages.pdf
chapter 1.pptx dotnet technology introduction
CT Generations and Image Reconstruction methods
Project_Mgmt_Institute_-Marc Marc Marc .pdf
Micro1New.ppt.pptx the main themes if micro
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
Designing Fault-Tolerant Architectures for Resilient Oracle Cloud ERP and HCM...
Programmable Logic Controller PLC and Industrial Automation
electrical machines course file-anna university
CNS - Unit 1 (Introduction To Computer Networks) - PPT (2).pptx
Mechanics of materials week 2 rajeshwari
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
Wireless sensor networks (WSN) SRM unit 2
Ad

Chapter IV Algorithm for I3ab Ams at itc

  • 1. CHAPTER IV ALGORITHMS Institute of Technology of Cambodia ITC November 18, 2023 Mr. PEN CHENTRA AMS November 18, 2023 1 / 31
  • 2. Contents 1 Introduction 2 Examples of Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 1 / 31
  • 3. Contents 1 Introduction 2 Examples of Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 2 / 31
  • 4. Introduction An algorithm is a step-by-step method of solving some problem. The word “algorithm” derives from the name of the ninth-century Persian mathematician al-Khowarizmi. Today, “algorithm” typically refers to a solution that can be executed by a computer. Mr. PEN CHENTRA AMS November 18, 2023 3 / 31
  • 5. Character of algorithm Character Algorithms typically have the following characteristics: Input The algorithm receives input. Output The algorithm produces output. Precision The steps are precisely stated. Determinism The intermediate results of each step of execution are unique and are determined only by the inputs and the results of the preceding steps. Finiteness The algorithm terminates; that is, it stops after finitely many instructions have been executed. Correctness The output produced by the algorithm is correct; that is, the algorithm correctly solves the problem. Generality The algorithm applies to a set of inputs. Mr. PEN CHENTRA AMS November 18, 2023 4 / 31
  • 6. Finding the Maximum of Three Numbers Finding the Maximum of Three Numbers Figure: Pseudocode Mr. PEN CHENTRA AMS November 18, 2023 5 / 31
  • 7. Finding the Maximum Value in a Sequence Finding the Maximum Value in a Sequence Figure: Psedocode Mr. PEN CHENTRA AMS November 18, 2023 6 / 31
  • 8. Contents 1 Introduction 2 Examples of Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 7 / 31
  • 9. Searching Text search Mr. PEN CHENTRA AMS November 18, 2023 8 / 31
  • 10. Example Example 1 (Example) Shows a trace of Algorithm where we are searching for the pattern “001” in the text “010001” Mr. PEN CHENTRA AMS November 18, 2023 9 / 31
  • 11. Sorting Example 2 Suppose that i = 4 and s1 = 8, s2 = 13, s3 = 20, s4 = 27 is 8 13 20 27 If s5 is 16, after it is inserted,s1, · · · , s5 becomes 8 13 16 20 27 Mr. PEN CHENTRA AMS November 18, 2023 10 / 31
  • 12. Sorting Figure: Insertion Sort Mr. PEN CHENTRA AMS November 18, 2023 11 / 31
  • 13. Randomized Algorithm A randomized algorithm does not require that the intermediate results of each step of execution be uniquely defined and depend only on the inputs and results of the preceding steps. By definition, when a randomized algorithm executes, at some points it makes random choices. In practice, a pseudorandom number generator is used Mr. PEN CHENTRA AMS November 18, 2023 12 / 31
  • 14. Example Mr. PEN CHENTRA AMS November 18, 2023 13 / 31
  • 15. Example Figure Mr. PEN CHENTRA AMS November 18, 2023 14 / 31
  • 16. Contents 1 Introduction 2 Examples of Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 15 / 31
  • 17. Required time Time to Execute an Algorithm if One Step Takes 1 Microsecond to Execute. lg n denotes log2 n (the logarithm of n to base 2) Figure: Table of required time to execute Mr. PEN CHENTRA AMS November 18, 2023 16 / 31
  • 18. Case time We can ask for the minimum time needed to execute the algorithm among all inputs of size n. This time is called the best-case time for inputs of size n. We can also ask for the maximum time needed to execute the algorithm among all inputs of size n. This time is called the worst-case time for inputs of size n. Another important case is average-case timethe average time needed to execute the algorithm over some finite set of inputs all of size n. Mr. PEN CHENTRA AMS November 18, 2023 17 / 31
  • 19. Example In practise, we are less interested in the exact best-case or worst-case time required for an algorithm to execute than we are in how the best-case or worst-case time grows as the size of the input increases Example 3 Suppose that the worst-case time of an algorithm is t(n) = 60n2 + 5n + 1 for input of size n. For large n, the term 60n2 is approximately equal to t(n) Mr. PEN CHENTRA AMS November 18, 2023 18 / 31
  • 20. Figure: Comparing growth of t(n) with 60n2 Under these assumptions, t(n) grows like n2 as n increases. We say that t(n) is of order n2 and write t(n) = Θ(n2), which is read “t(n) is theta of n2.” The basic idea is to replace an expression, such as t(n) = 60n2 + 5n + 1, with a simpler expression, such as n2, that grows at the same rate as t(n). The formal definitions follow. Mr. PEN CHENTRA AMS November 18, 2023 19 / 31
  • 21. Definition Definition 4 Let f and g be functions with domain {1, 2, 3, ...}. We write f(n) = O(g(n)) and say that f(n) is of order at most g(n) or f(n) is big oh of g(n) if there exists a positive constant C1 such that |f(n)| ≤ C1|g(n)| for all but finitely many positive integers n. Mr. PEN CHENTRA AMS November 18, 2023 20 / 31
  • 22. Definition Definition We write f(n) = Ω(g(n)) and say f(n) is of order at least g(n) or f(n) is omega of g(n) if there exists a positive constant C2 such that |f(n)| ≥ C2|g(n)| for all but finitely many positive integers n. Mr. PEN CHENTRA AMS November 18, 2023 21 / 31
  • 23. Definition Definition We write f(n) = Θ(g(n)) and say f(n) is of order g(n) or f(n) is theta of g(n) if f(n) = O(g(n)) and f(n) = Ω(g(n)) Mr. PEN CHENTRA AMS November 18, 2023 22 / 31
  • 24. Example 5 Since 60n2 + 5n + 1 ≤ 60n2 + 5n2 + n2 = 66n2for all n ≥ 1, we may take C1 = 66 , then 60n2 + 5n + 1 = O(n2 ) 60n2 + 5n + 1 ≥ 60n2 for all n ≥ 1, we may take C2 = 60, then 60n2 + 5n + 1 = Ω(n2 ) Since 60n2 + 5n + 1 = O(n2) and 60n2 + 5n + 1 = Ω(n2), then 60n2 + 5n + 1 = Θ(n2 ) Mr. PEN CHENTRA AMS November 18, 2023 23 / 31
  • 25. Theorem Theorem 6 Let p(n) = aknk + ak−1nk−1 + · · · + a1n + a0 be a polynomial in n of degree k, where each ai is nonnegative. Then p(n) = Θ(nk ) Mr. PEN CHENTRA AMS November 18, 2023 24 / 31
  • 26. Contents 1 Introduction 2 Examples of Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 25 / 31
  • 27. A recursive function (pseudocode) is a function that invokes itself. A recursive algorithm is an algorithm that contains a recursive function. Recursion is a powerful, elegant, and natural way to solve a large class of problems. A problem in this class can be solved using a divide-and-conquer technique in which the problem is decomposed into problems of the same type as the original problem. Each subproblem, in turn, is decomposed further until the process yields subproblems that can be solved in a straightforward way. Finally, solutions to the subproblems are combined to obtain a solution to the original problem. Mr. PEN CHENTRA AMS November 18, 2023 26 / 31
  • 28. Example Example 7 (Example) Recall that if n ≥ 1, n! = n(n − 1) · · · 2 · 1, and 0! = 1. Notice that if n ≥ 2, n factorial can be written “in terms of itself” since, if we “peel off” n, the remaining product is simply (n − 1)!; that is, n! = n(n − 1)(n − 2) · · · 2 · 1 = n · (n − 1)! Figure: Decomposing the Factorial Problem Figure: Combining Subproblems of the Factorial Problem Mr. PEN CHENTRA AMS November 18, 2023 27 / 31
  • 29. Algorithms Algorithm Figure: recursive algorithm Mr. PEN CHENTRA AMS November 18, 2023 28 / 31
  • 30. Example Example 8 A robot can take steps of 1 meter or 2 meters. We write an algorithm to calculate the number of ways the robot can walk n meters. As examples: Figure: walk(n) = walk(n − 1) + walk(n − 2) Mr. PEN CHENTRA AMS November 18, 2023 29 / 31
  • 31. Robot walk Mr. PEN CHENTRA AMS November 18, 2023 30 / 31
  • 32. Robot Walking Mr. PEN CHENTRA AMS November 18, 2023 31 / 31