Problem Solving and Computing (CSC-103 98S)
Outline of Class 2: Algorithms
Held: Thursday, January 22, 1998
- If you haven't filled out the
introductory survey, please do
so as soon as possible.
- Some preliminary responses
are now available.
- A few of you noted that you were uncomfortable with the
because you weren't sure you had a solid or perfect answer. At this point,
I don't expect perfect answers. (In fact, this assignment's primary
goal was to prepare you for today's class.)
- There are a few new people in class today (or there may be). Please
make sure to help those people when they have problems.
- So, what ideas did you come up with for the
four fours problem?
- For today, I'm handing out printed versions of the exercies and outline.
Is it okay if I assume you can read them on the computer from now on?
- An algorithm is the formal set of instructions for completing
a process. It is an attempt to unambiguously express a process or
- We develop algorithms for a number of things, from baking cookies to
finding plane flights. Computer scientists tend to try to generalize
problems and then write algorithms for those generalized problems.
- For example, instead of talking about "cheapest set of plane flights
that visit all these cities", we might generalize to "We have a set
of things with a cost to go from one to another. What is the
smallest cost path through all the things, returning to your
- There are often a number of different valid algorithms for solving
the same problem.
- It is also particularly difficult for beginners to develop algorithms,
so don't worry if you have trouble at first (including on assignments).
- The way you express an algorithm depends, in part, on the audience.
- Consider the problem of baking chocolate-chip cookies.
- Most people would be happy with a list of ingredients
(1/2lb butter, 3/4 cup white sugar, 3/4 cup brown sugar, 2 1/2 cups
flour, 1 tsp baking soda, 1/2 tsp salt, 2 tsp vanilla, 2 eggs) and a set of
- Preheat the oven to 375 degrees F.
- In a medium bowl, mix together the sugar, butter, and vanilla
- In a separate bowl, sift together the dry ingredients
- Combine the two mixtures, stirring until blended
- Place by teaspoonfuls onto ungreased cookie sheets
- Bake 9-11 minutes
- A novice baker might need to be told how to sift.
- An expert baker probably needs only the list of ingredients.
- A true novice might need to be cautioned that the mixture won't fit onto
one cookie sheet and that the teaspoonfuls need to be separated.
- We recently encountered the problem of finding the cheapest tour
that goes through all the cities in a set of cities. Is there
an algorithm that we could use to solve this problem?
- Yes, although it is a fairly slow algorithm.
1. List all the permutations (orderings) of the cities that are
not the starting city.
2. For each permutation,
2.a. Determine the cost of a trip based on that permutation
(starting at the starting city and going to each of the
cities in the permutation in order).
3. Pick the smallest of those costs.
- As you will see later, if there are more than a few cities, there
are a very large number of permutations.
- Because it's tedious to do exercises like this by hand, we often
write programs to solve them.
- (This is a problem that even computers will take a lot of time to
solve. We'll discuss why later this term.)
- Note also that we may have to give explicit directions for finding
the minimum in a list of costs and making all the permutations of
a list of cities.
- How do you compute the smallest number in a list (also known as the
minimum of the list).
- For short lists, most of us can just look at the list and tell.
- However, this doesn't work as well for longer lists.
- It also doesn't work for computers, which can't just tell by looking.
- So, we might try to come up with a formal set of instructions.
1. Assume that the first element is the smallest.
2. Look at each of the other elements in the list.
2.a. If the current element is smaller than the one we thought was
smallest, assume that the current element is the smallest.
3. When you run out of elements, use the element you most recently
assumed was smallest.
- Let's see how this might work on the list 5,2,4,3,1,7
- Initially, we assume that 5 is the smallest.
- We look at the next value and see that we were wrong. Now we
think that 2 is the smallest.
- We see a 4. 2 is smaller than 4, so we continue with our assumption.
- We see a 3. 2 is smaller than 3, so we continue with our assumption.
- We see a 1. We were wrong. 1 is smaller. We have a new assumption.
- We see a seven. 1 is smaller than 7, so we continue with our assumption.
- We haven't explicitly verified that 1 is smaller than the earlier elements.
However, we can argue indirectly that it is smaller (it's smaller than
an element that was smaller than the previous elements).
- Spend a few minutes working out instructions for the
sandwich question in computing
- We'll then attempt to run your algorithms.
- We'll also discuss your "first year" algorithms.
All of today's exercises focus on
defining or working with