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
Sorting
Sorting
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.
Quizz
Quizz
Quizz
Introduction
Introduction
Introduction
Introduction
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Example.
Quizz
Quizz
Algorithms
Algorithms
Complexity of Algorithms
Complexity of Algorithms
Complexity of Algorithms