Understanding Big O notation With The "Fried Plantain" Algorithm...Yeah you read that right

Understanding Big O notation With The "Fried Plantain" Algorithm...Yeah you read that right

·

8 min read

the_groundnut_cookbook_2_175_img_s1200x630_c1796x1049_l10x756.jpg

Nothing caused me so much pain in the head like wrapping my head around what Big O notation is. I am a disciple of the Famous Feynmann Technique: "if you cannot explain it simply, you do not understand it". My goal in this piece is to simply explain Big O to you without using verbose or strange words. At least for now.

What then is the Big O notation? What is the essence of O(n)? The Big O is simply a way to test how fast as well as how efficient an algorithm is. The exact time or duration of execution is not of so much importance as there are certain factors that can influence the timing of execution of an algorithm such as the computer spec. The key thing is the number of operations required by the algorithm.

What is an algorithm? steps or series of steps taken towards solving a given problem. The Big O notation works with the "worst-case scenario" of any given algorithm. Let's say for example you had an algorithm that searches through an array. With a simple search algorithm, you go one after the other. If your list is just 3-10 items, then you would believe your algorithm is alright. What happens when we feed it 100,000 items and the target item is at position 89,000? The execution time grows in a linear way. Assuming it takes 1ms to search each item, it means your algorithm would take 89000ms!

Well, this piece is for fried plantain so we will stick to that. lol.

Our Anecdote or story is this: we have a restaurant that serves fried plantain to its customers. The restaurant has two Managers James and John. They work shifts of two weeks each in a month. James takes the first 2 weeks and John takes the last 2 weeks. The restaurant is supplied with 20 bunches of plantain weekly. It is open Monday to Friday. On average, 40 customers order plantain daily. We will look at James' algorithm.

James:

James does the following steps.

  • He requires the kitchen staff to cut the plantain into chips, salt them, put them in Ziploc bags every Monday morning at least 1 hour before the store is open for business. This is done to all the bunches so they don't need to peel them throughout the week.

  • The chips are then kept in the fridge or freezer where they are kept fresh all week.

  • Whenever a customer or customers require plantain, the desired portion is taken out and fried immediately.

John:

John doesn't do the above. He simply waits for the customer to request for plantain then a kitchen staff has to cut the desired amount, peel them, cut them into chips, and then fry them. They have to determine how many portions are contained in a single plantain finger as the fingers vary. Also, there is the issue of some plantain bunches going bad due to heat in the store so there is a need to inspect from time to time. Also before frying, the plantain has to be inspected to be sure it's not too ripe.

Comparison:

From the above let's say we have one customer request at a time, It would seem as though both approaches chosen by James and John are not that different. But what happens when 30 customers request for plantain?

James has to simply take the chips out and fry the required portion for 30 persons. Regardless of the number of requests, his operation is the same, take out the chips from the fridge(1) and fry(2). James requires 2 operations regardless of the number of customers.

How about John? well, they have to go to the kitchen store(1), cut some plantain(2), peel off the skin(3), slice it(4), add some salt(4), and then fry(5). Not to mention, they have to determine how many portions are contained in a single plantain finger(6) as the fingers vary. Also, there is the issue of some plantain bunches going bad due to heat in the store so there is a need to inspect the store daily(7). Also before frying, the plantain has to be inspected to be sure it's not too ripe(8).

Again, James on the other hand can take out the desired number of chips and simply fry. In the end, James has 2 operations and John has 8! James's method is about 4 times more efficient than that of John. As the number of requests grows, the time to prepare increases for John. John's method is much slower. James' method is constant.

Let's assume it takes 5mins to fry. And the fryer can take up to 40 portions at a time. Even though 30 persons order for plantain, James' method is going to be constantly 5-6mins give or take when we factor in taking the chips from the fridge and putting them in the fryer.

John on the other hand in addition to the 5 mins of frying, has 8 operations to perform! John may clock at about 15 to 25 mins when the time for his 8 operations are taken into consideration. We can see from the time difference that again James method would be about 3 to 4 times faster. Even when the requests grow to 30 portions, James algorithm will still deliver within the 5-6mins constantly. How about John? We can see how his "execution" time grows as more requests are made.

Conclusion:

Now if you were the Store owner, whose method would you adopt?

Now obviously there are other concepts of Big O that we left out. The key was to first understand what it really is. Hopefully, this helped. And if you like plantain you may have learned how best to preserve it. lol