Big O Notation: Anson Ho
Big O Notation: Anson Ho
Big O Notation: Anson Ho
Big O Notation
Anson Ho
26 January 2019
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Motivation
• Analyse program runtime in a better way
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Motivation
• Analyse program runtime in a better way
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Time Complexity
• Description of runtime (or the number of operations)
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Time Complexity
• Description of runtime (or the number of operations)
• But a function
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Time Complexity
• Worst case
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Time Complexity
• Worst case
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Space Complexity
• Description of memory usage
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Big O Notation
• An usual representation for time/space complexity
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Big O Notation
• An usual representation for time/space complexity
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Big O Notation
• An usual representation for time/space complexity
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Big O Notation
• An usual representation for time/space complexity
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Advanced content (feel free to skip)
Big O Notation
Definition
f (n) = O(g(n)) if there exists n0 , c > 0 such that
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Convention
• Since Big O notation is about upper bound, 7(n + 1)2 can be represented
by O(nn )
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Convention
• Since Big O notation is about upper bound, 7(n + 1)2 can be represented
by O(nn )
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Convention
• Since Big O notation is about upper bound, 7(n + 1)2 can be represented
by O(nn )
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Examples
• Linear search
O(n)
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Examples
• Linear search
O(n)
• Bubble sort
O(n2 )
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Examples
• Linear search
O(n)
• Bubble sort
O(n2 )
• Binary search
O(log n)
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Examples
• Linear search
O(n)
• Bubble sort
O(n2 )
• Binary search
O(log n)
• Merge sort
O(n log n)
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Properties
• Addition
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Properties
• Addition
• Scalar multiplication
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Multivariable
• O(nmk)
• O(n2 m + nm2 )
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Constant
• Is O(n) always better than O(n log n)?
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Constant
• Is O(n) always better than O(n log n)?
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Constant
• Is O(n) always better than O(n log n)?
• Constant factor
• Nature of the algorithm
• Implementation
• Machine performance
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Constant
• Is O(n) always better than O(n log n)?
• Constant factor
• Nature of the algorithm
• Implementation
• Machine performance
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Amortized Complexity
• Sometimes, the time complexity of a specific function is analysed instead
of the whole program
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Amortized Complexity
• Sometimes, the time complexity of a specific function is analysed instead
of the whole program
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Amortized Complexity
• Sometimes, the time complexity of a specific function is analysed instead
of the whole program
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Amortized Complexity
• Sometimes, the time complexity of a specific function is analysed instead
of the whole program
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics
Big O Notation
Exercises
香港電腦奧林匹克競賽
Hong Kong Olympiad in Informatics