Chapter 3. Algorithms
Chapter 3. Algorithms
Chapter 3. Algorithms
Chapter 3. Algorithms
Contents
1. Algorithms
Contents
1. Algorithms
2. The Growth of Functions
Contents
1. Algorithms
2. The Growth of Functions
3. Complexity of Algorithms
Example.
Example.
Example.
Definition
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
• Finiteness. An algorithm should produce the desired
output after a finite (but perhaps large) number of
steps for any input in the set.
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
• Finiteness. An algorithm should produce the desired
output after a finite (but perhaps large) number of
steps for any input in the set.
• Effectiveness. It must be possible to perform each step
of an algorithm exactly and in a finite amount of time.
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
• Finiteness. An algorithm should produce the desired
output after a finite (but perhaps large) number of
steps for any input in the set.
• Effectiveness. It must be possible to perform each step
of an algorithm exactly and in a finite amount of time.
• Generality. The procedure should be applicable for all
problems of the desired form.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Solution
Solution
Solution
Solution
Solution
Solution
Solution
2, 3, 4, 5, 6, 8, 9, 11
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := b(i + j )/2c
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := b(i + j )/2c
if x > am then i := m + 1
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := b(i + j )/2c
if x > am then i := m + 1
else j := m
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := b(i + j )/2c
if x > am then i := m + 1
else j := m
if x = ai then location: = i
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := b(i + j )/2c
if x > am then i := m + 1
else j := m
if x = ai then location: = i
else location: = 0
Solution
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 :
increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := b(i + j )/2c
if x > am then i := m + 1
else j := m
if x = ai then location: = i
else location: = 0
return location {location is the subscript of the term
that equals x, or is 0 if x is not found}
Sorting
Sorting
3, 2, 4, 1, 5
j=2
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
3, 2, 4, 1, 5
j=4
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
i = 2, 5 > 2
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
i = 2, 5 > 2
i = 3, 5 > 3
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
i = 2, 5 > 2
i = 3, 5 > 3
i = 4, 5 > 4
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
i = 2, 5 > 2
i = 3, 5 > 3
i = 4, 5 > 4
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
i = 2, 5 > 2
i = 3, 5 > 3
i = 4, 5 > 4
Thus, → 1, 2, 3, 4, 5
Introduction
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Big-O Notation
Big-O Notation
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Let g (x ) = x 2
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Let g (x ) = x 2
We have C = 4, k = 1, |f (x )| ≤ C |g (x )|
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Let g (x ) = x 2
We have C = 4, k = 1, |f (x )| ≤ C |g (x )|
Thus, f (x ) is O (x 2 )
Big-O theorem
Big-O theorem
Big-O theorem
3. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
Big-O theorem
3. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
Big-O theorem
3. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
Example.
Example.
Example.
Example.
Solution.
Quizz
Quizz
Ans: C
Quizz
Quizz
Quizz
Quizz
Ans: a
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Introduction
Introduction
Introduction
Introduction
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Ans:f(n)=n+n=2n is O(n)
Example.
Example.
Example.
Example.
Quizz
Quizz
Quizz
Quizz
Ans: a (f (n) = n)
Algorithms
Algorithms
Complexity of Algorithms
Complexity of Algorithms
Complexity of Algorithms