Episode 4: Wet Fry Chicken Algorithm

Episode 4: Wet Fry Chicken Algorithm

CS50 Week 3: Algorithms

At the risk of being simplistic let me give my best analogy of what an algorithm is:

Mercy's Algorithm:

When Mercy prepares Wet Fry Chicken she: Washes the chicken -> boils it in a pot under medium heat with garlic and ginger -> chops ALL the other ingredients and puts them in separate bowls -> takes out the chicken once cooked and places it in a separate bowl -> warms the pan before adding oil then fries the onion and celery till browned -> adds the tomatoes, capsicum etc and leaves to soften for a few minutes -> adds salt and spices to taste -> puts the boiled chicken back in -> leaves to simmer stirring occasionally until everything looks good.

Quite elaborate.

Richie's Algorithm:

When I make Wet Fry Chicken I: Fry onions and garlic until slightly browned -> add the chicken along with salt and all other spices -> after about 20 min add tomatoes, capsicum etc -> stir occasionally until dish is well done.

Quite simple.

Um so...?

An algorithm is simply a set of steps or instructions on how to solve a problem. You have raw chicken and you need to prepare it for your guests? Use Mercy's, Richie's, Kaluhi's or your own recipe (algorithm) to complete the task.

Remember Luhn's Algorithm from Episode 2 of this CS50 series? His algorithm is just a well described and more complex procedure for verifying the validity of credit cards, IDs etc which is a more complex problem than cooking chicken.

One of the key parameters in determining whether an algorithm is good or not is its running time. Does it solve the problem correctly, accurately and quickly? Richie's Algorithm might have the fastest running time but if you attempted to make kienyeji chicken like that, well, may the Lord have mercy on your guests.

You could argue that if you were making a Wet Fry Chicken App, Mercy's algorithm would be a safer bet since it works across all grades of chicken. In making your case you would need to consider that the algorithm requires a bigger kitchen space or more bowls to use and that would now be analogous to computer memory. More of that in Episode 5.

What if you had a pressure cooker? That would make Mercy's running time even better thus introducing the dynamic of computers' ever-increasing processing power. There's a lot to consider and that's why choosing the right algorithm for your program is important.

Recursion

That being said, the week 3 lecture was my favourite one yet. David Malan did a stellar job of explaining algorithms by contrasting different searching and sorting algorithms based on their correctness and effectiveness.

A simple linear search algorithm is nice but ineffective in larger scales. Maybe you can improve the running time by sorting the items in a particular order first? Okay, so what's the best way to sort the items? Maybe you could go over the entire list looking for the smallest item then repeat the process identifying items in increasing order of size each time until the list is sorted (selection sort algorithm)? Or you could look at two items at a time and swap them if they are unordered, do this for the entire list then repeat the process until all the items are sorted (bubble sort algorithm)? At this point I was pretty satisfied with these solutions until he suggested that we could do even better; Recursion.

Recursion blew my mind! Watch this visualisation video where merge sort and other recursive algorithms move like they're carrying miraa (Kenyans will understand). youtu.be/OOBBI-kSChM

Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself - That's the first definition when you google it. In programming it's where a function calls itself.

In the real world for instance, if you want to count the number of students in a high school you could -> split them into 2 groups of upper and lower classes -> count each group by splitting them into forms -> count each form by splitting into classes -> count each class by splitting in halves according to sitting arrangement -> count each half by splitting into columns -> count by splitting until you get to the base case; one student. From there you could merge the totals and get the student count. (Notice how the 'count' function calls itself)

I've said a lot .Maybe this picture where every branch looks like a smaller tree will speak my 1000 thousand words recursively:

RecursiveTree.jpg

By Brentsmith101 - Own work, Public Domain, commons.wikimedia.org/w/index.php?curid=381..