This is a lesson in the course, Introduction to Computer Science, which is a part of The School of Computer Science
ObjectiveThere are several definitions for the notion of an algorithm. As an introduction to the concept, we shall study some simple applications. By the end of this lesson, students should:
|
Intro![]() The word "algorithm" comes from the latin form of the name Al-Khwārizmī, a Persian mathematician who lived in the 8th century AD. What is an algorithm? An algorithm is the step-by-step solution to a certain problem. Richard Jeffery and George Boolos gave an informal description of an algorithm, and the powers it grants us, in Computability and logic:
Restated, this means that while no human could possibly write out an infinite series of things, humans are able to create a process by which a computer can generate the portions of that series which are relevant to solving a particular problem. The specific list of steps thus created constitutes an algorithm, a concept at the very core of computer science. |
Assignment 1Algorithms do not only exist within the realm of computer science. Beginners may find it helpful to recognize the algorithmic thinking they already apply for daily life. On a separate sheet of paper, list all the known possible steps in preparing/performing the following activities, be as descriptive and specific as you can be:
These activities above might seem easy, but as you perform the tasks, you will encounter a series of trial-and-error sub-activities. For beginners, this is normal and you may tend to miss a few steps. |
Assignment Solution and DiscussionAs you were able to see in the assignments above, Algorithms are lists of instructions which are followed step by step. Your solution to the case of egg frying should look like this: 1. Get the frying pan. 2. Get the oil. a. Do you have oil? 1) If yes, put it in the pan. 2) If no, do you want to buy oil? a) If yes, then go out and buy. b) If no, you can terminate. 3. Turn on the stove. 4. Etc... For the case of Frying an egg sunny side-up, have you forgotten to state that you will heat up the pan? Or maybe put in some oil? Should you put some oil in first before heating the pan, or should you heat the pan and then put some oil in? These are the possible conditions that you may encounter in preparing the algorithms for this task. Once you have gotten used to writing simple algorithms, you can attempt to write pseudocode. Pseudocode is simply an algorithm which is written in a way that resembles computer code - it does not have to be in any particular programming language. People write algorithms in pseudocode because it is easier to see what a computer program will look like after it is written, and it may also be easier to notice logical problems in the algorithm. Pseudocode doesn't lend itself well for non computerized tasks, but let's try writing some for the algorithm above: get(frying_pan) if you_have(oil) then get(oil) else if you_want_oil() then buy(oil) else terminate turn_on(stove) Spend some time reading that pseudocode. If you are acquainted with programming languages then the format above should seem familiar. |
Required Assignment 2Try writing pseudocode for the algorithms you wrote in the first assignment. |
Get(leash) Put(leash)on(dog) If(weather)=(sunny)then Go_to(park) If_not Cancel(dogwalk)
if (vehicle_location is known) { transport_to(vehicle) } else { locate(vehicle) } if (destination location is known) { if { vehicle is turned off) { turn_on(vehicle) } else { if (location of smart_phone is known) { use_GPS(directions_to_location) } else { locate(smart_phone) use_GPS(directions_to_location) }}} etc etc etc. Required Assignment 3Pseudocode (and programs in general) are best suited for mathematical problems. Try to write an algorithm in pseudocode for this math problem: (x4 + 25y2 + 2z) − 13 |
(x4 + 25y2 + 2z) − 13 ((x^4) + (25 * y^2) + (2 * z)) - 13 ConclusionAlgorithms are a series of step by step instructions for solving a problem. Pseudocode is an algorithm written in a way that resembles computer code. To practice writing algorithms in pseudocode, you first think of a complex problem and consider the logical solution before writing it out. References |