KEMBAR78
Chapter IV Algorithm for I3ab Ams at itc | PDF
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

Chapter IV Algorithm for I3ab Ams at itc

  • 1.
    CHAPTER IV ALGORITHMS Institute ofTechnology of Cambodia ITC November 18, 2023 Mr. PEN CHENTRA AMS November 18, 2023 1 / 31
  • 2.
    Contents 1 Introduction 2 Examplesof Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 1 / 31
  • 3.
    Contents 1 Introduction 2 Examplesof Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 2 / 31
  • 4.
    Introduction An algorithm isa 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 Algorithmstypically 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 Maximumof Three Numbers Finding the Maximum of Three Numbers Figure: Pseudocode Mr. PEN CHENTRA AMS November 18, 2023 5 / 31
  • 7.
    Finding the MaximumValue 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 Examplesof Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 7 / 31
  • 9.
    Searching Text search Mr. PENCHENTRA AMS November 18, 2023 8 / 31
  • 10.
    Example Example 1 (Example) Showsa 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 thati = 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 randomizedalgorithm 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 CHENTRAAMS November 18, 2023 13 / 31
  • 15.
    Example Figure Mr. PEN CHENTRAAMS November 18, 2023 14 / 31
  • 16.
    Contents 1 Introduction 2 Examplesof Algorithms 3 Analysis of Algorithsms 4 Recursive Algorithms Mr. PEN CHENTRA AMS November 18, 2023 15 / 31
  • 17.
    Required time Time toExecute 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 canask 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, weare 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 growthof 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 fand 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 Examplesof 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) Recallthat 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 robotcan 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. PENCHENTRA AMS November 18, 2023 30 / 31
  • 32.
    Robot Walking Mr. PENCHENTRA AMS November 18, 2023 31 / 31