We will talk about how we can calculate the Big O in terms of Time Complexity, and then deep dive to understand Big O Rules. And the last, we will learn some techniques to improve the performance of our algorithm.
3. - Principal Software Architect at Telkom Indonesia
- Engineering Lead of Digital Product Engineering Chapter Digital Business & Technology
- Solutions Architect at Online Single Submission System (oss.go.id)
- Head of Engineer at Peduli Lindungi
- Software Architect Lead at Telkom Indonesia
- Professional Mentor at Purwadhika Digital Technology School
- Cloud Computing Instructor at Google Bangkit
- Software Engineer
- Professional Lecture
- Researcher
- Speaker
- Author
Rony Setyawan, S.T., M.Kom., CCP., CSAP.
11. Section One
Theory and Practice of Big-O
Conclusion Experiment 1, 2, 3
The increase in the amount of data (elements) will be directly proportional to the increase in the time
needed to run the task (number of operations needed).
Within Big O, the main topic of discussion is how do we make our algorithm as good as possible in the
Excellent, Good, or Fair area. And we need to avoid Horrible and Bad areas.
So, this is not a contradiction with Conclusion 1.
An algorithm is said to be Scalable, if the algorithm is O(log n), O(1), or O(n).
Apart from doing some refactoring so that we can enter into one of those areas of Big O notation.
In experiments 1, 2, and 3, the function is in O(n).
12. Section One
Theory and Practice of Big-O
Linear Time Big-O
For Loop
While Loop
What kind of
implementation?
16. Section One
Theory and Practice of Big-O
Conclusion Experiment 4, 5, 6
The amount of data we use has no effect at all on the performance of the function
Time is always constant (fixed) even though the amount of data changes whether it's getting smaller or bigger
In experiments 4, 5, and 6, the function is in O(1).
17. Section One
Theory and Practice of Big-O
Constant Time Big-O
All Single Statement
No Loop Function
What kind of
implementation?
20. Section One
Theory and Practice of Big-O
Conclusion Experiment 7
Nested Loop (marked by an indentation) it means that it indicates multiplication (*) not Addition (+)
The number of menus generated is 16 menus, this is obtained from multiplication (foods, drinks)
In experiments 7, the function is in O(n^2).
21. Section One
Theory and Practice of Big-O
Nested For Loop
Nested While Loop
What kind of
implementation?
Nested Loop
Quadratic Time Big-O
22. Section One
Theory and Practice of Big-O
Experiment 8
Problem Statement:
● There are several candidates for the presidential candidates for
Indonesia in 2024.
● They will be scheduled to debate the presidential candidates and take
serial numbers for their identities later during the campaign.
● At the meeting, of course, several chairs are needed to be used by these
candidates, then how can we arrange these chairs?
● Please provide an alternative to the seat arrangement for the three
presidential candidates!
25. Section One
Theory and Practice of Big-O
Conclusion Experiment 8 and 9
When we use 3 input data, it produces 6 output data (3! = 6)
When we use 4 input data, it produces 24 output data (4! = 24)
The exact number of operations required will of course depend on the algorithm used
In experiment 8 & 9, use the recursive function approach.
In experiments 8 and 9, the function is in O(n!) or Factorial Big-O.
27. Section Two
Big-O Rules
Big O Rule 1 :
Worst Case Scenario is the most we care about / Drop non-dominant terms
Big O Rule 1 states that we cannot always expect ideal conditions such as for example ideal input
on the contrary we need to pay attention to conditions that are not ideal
so that our program runs in the most inefficient state.
Therefore, in Experiment 10 we can say that the function belongs to O(n).
Experiment 10
● Input 'Telkom' (0), so it only requires 1 search process
● Input 'AT&T' (4), so it only takes 5 searching processes
● Input 'T-Mobile' (10), then it requires 11 searching processes
28. Section Two
Big-O Rules
Experiment 11
Problem Statement:
● The marketing and product teams have customer
data totaling 100 mobile numbers.
● Approaching the end of the year, there is an
initiative to send promos to all customers using
existing mobile number data.
● Apart from the promo, there is also a special
discount given to only 10 customers selected by
the product team.
29. Section Two
Big-O Rules
Result Experiment 11
Big O Rule 2 :
Remove Every Constants
Let's take a closer look at Experiment 11, specifically for the SendPromoDiscount function.
In Big O calculations for this function actually consists of O(n) and O(10).
Simplify it then becomes O(n + 10).
Big O Rule 2 states that We Must Remove Every Constant, then ignore the O(10).
Compared to O(n), which is not fixed and can have a very large impact.
32. Section Two
Big-O Rules
Experiment 12 and 13
● Based on Big O Rule 1, we can ignore the existing O(2).
● Based on Big O Rule 2, in Experiment 12 we can ignore the
existing Contacts. So, we can simplify O(2n) to only O(n).
● Based on Big O Rule 3, we cannot simplify Experiment 13 so
that the Big O notation we get is O(m + n).
Big O Rule 3 :
Different Terms for Inputs
Big O Rule 3 states that if we find a function/program that uses different input data, then we
can not simplify Big O notation because different input data can significantly affect the
complexity or number of operations required.
34. Section Three
Solution Design
Hash Table
https://www.miraclesalad.com/webtools/md5.php
https://www.cs.usfca.edu/~galles/visualization/OpenHash.html
Hash table, also known as hash map, is a data structure that implements an associative array or dictionary.
It is an abstract data type that maps keys to values.
A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or
slots, from which the desired value can be found.
During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored.
Hash Function Simulation
39. Section Three
Solution Design
Dynamic Programming
Can be divided into subproblems
Commonly based on plain recursive solution
Are there repetitive subproblems
Memoize subproblems