Introduction to Algorithms
We live our lives through algorithms with patterns and routines. These routines are:
- how we start our day by getting up and getting ourselves ready
- the route we take to school and work
- the recipe we follow when we are cooking dinner in the evening
- the way we go about our budget and paying our bills
All of these routines have steps we take often without thinking too much about them. Some of these steps require us to make decisions, such as our route to work based on traffic, what we wear based on the weather. Some of these steps are repeative, such as when we have multiple bills to pay, or we are plating up the same dinner once for each member of our family.
Computers need step-by-step instructions to operate. These instructions come through programs that represent the algorithms that the computer needs to follow. Similar to the routines and patterns in real-life, there is an order of steps with decisions and repeated patterns. Unlike when we insert a step into our routine without thinking about it, because we do it every day, the computer needs each individual step specified in order to function properly and provide the correct result.
Algorithms can be written in multiple ways. We are going to start by writing algorithms using pseudocode. Pseudocode is a way to represent an algorithm using your native language. You can write it as a paragraph or as a series of steps.
Sample Algorithm
Consider an algorithm to represent the steps you would take to make your bed in the morning. It might look like the following:
- On the right side, pull up the flat top sheet to the top of the bed.
- On the left side, pull up the flat top sheet to the top of the bed.
- On the right side, fold down the top of the flat top sheet about 12 inches.
- On the left side, fold down the top of the flat top sheet about 12 inches.
- On the right side, tuck in the sheet on the side.
- On the left side, tuck in the sheet on the side.
- On the right side, place the pillow at the top of the bed.
- On the left side, place the pillow at the top of the bed.
- On the right side, pull the comforter up and over the pillow.
- On the left side, pull the comforter up and over the pillow.
Using AI to Refine Algorithms
Put the algorithm into AI, such as ChatGPT, and ask if there are revisions to make the algorithm more succinct.
Use the following prompt
"Consider the following algorithm that describes the steps to make your bed in the morning. Make suggestions on how to improve the algorithm to make it more succinct."
Result from using ChatGPT
To improve the algorithm for making the bed and make it more succinct, we can eliminate repetitive instructions and combine similar steps. Here's a revised version of the algorithm:
Revised Algorithm for Making the Bed:
- Pull up the flat top sheet from both sides to the top of the bed.
- Fold down the top of the flat top sheet about 12 inches from both sides.
- Tuck in the sides of the flat top sheet on both sides.
- Place the pillows at the top of the bed, one on each side.
- Pull the comforter up and over the pillows from both sides.
Explanation of Changes:
- Steps 1 and 2 were combined by addressing both sides of the sheet in one step each.
- Steps 5 and 6 were also combined for the side-tucking action.
- Steps 7 and 8 were merged to place the pillows in one step.
- Steps 9 and 10 were simplified by combining the comforter action.
This version retains all the important actions but reduces redundancy, making it more efficient to follow.
Your Turn
Write a first draft of an algorithm to describe a task you do regularly and consider yourself really good at accomplishing. Once you have completed your algorithm, ask ChatGPT to make improvements.
Resources