Problem Solving and Computing (CSC-103 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Journal] [Problems] [Computing] [SamR Docs] [Tutorial] [API] [Island]

# Outline of Class 3: Algorithms Revisited; Basic Problem Solving

Held: Tuesday, January 27, 1998

• I haven't yet graded your first assignment. Sorry, it was a busy weekend (the faculty were on a retreat to discuss the future of Grinnell). I did not that most of you emphasized your solutions (the answer) rather than a full resolution (how you derived the answer). Your resolutions don't have to be as long as mine, but they should indicate some of your problem-solving process.
• I've prepared my own resolutions for many of the exercises in problem set 1. I worked out some in advance and some while writing up this handout. In the future, my resolutions may look a little bit more like your journals (that is, reflect all of the positive and negative steps of my problem solving process). Let me know what you think.
• Handout (read for next Tuesday): The rest of chapter 1 of Thinking Mathematically. Recall that to successfully read this text, you will need to stop and do the exercises. Keep a pencil, paper, and calculator handy.
• Handout (skim for Thursday): An introduction to HTML.
• For Thursday, write in your journal on problem solving.
• For Thursday, write in your journal about observations of the web.
• For next Tuesday, work on problem set 2.
• The Grinnell Review is seeking submissions of academic papers (research, essays, etc.) for publication in the 1997-98 edition of the magazine. The papers can be about any topic, so long as they are of general interest. All members of the Grinnell community are invited to submit their work. Send submissions to box 09-26 by February 20th. Vax [REVIEW] with any questions.

## Components of Algorithms

• As you may have noted some similarities in our algorithms.
• We often name values (e.g., "smallest"). Named values are often called variables.
• We often work with collections of elements, sometimes referring to the elements in the sequence by number. Such sequences are often called arrays.
• We sometimes test a condition before deciding what to do. Such tests are called conditionals.
• We sometimes do an action repeatedly ("keep stirring until mixed"; "for each element of this list"). These are called loops. There are two kinds of loops.
• Some loops repeat a fixed number of times ("for each element"; "for all the numbers from 1 to 10"). These are called for loops.
• Some loops repeat as long as a condition holds ("keep stirring as long as the batter isn't mixed"). These are called while loops.
• We sometimes need to define an algorithm to explain a part of another algorithm (e.g., when we had to describe how to sift or find a minimum). These related algorithms are often called subroutines or functions.
• Sometimes we need to get input for our algorithm or write output from our algorithm.

### A Simple Syntax for Representing Algorithms

• Often, we choose a uniform syntax for representing each of the core concepts of algorithms.
• "Syntax" simply means "What we write".
• Having a uniform syntax helps ensure that we all understand the components and intent of the algorithm.
• A uniform syntax can also help us think about what we need to include in our algorithm.
• Because computers are stupid, they need to have algorithms expressed in a fixed syntax.
• We'll come up with an English-like syntax to use for our initial algorithms. Later this term, we will learn the syntax of Java.
• It is a good thing to name our algorithms and to indicate what "arguments" they work on (e.g., our minimum algorithm works with a list of values). We'll write the word `algorithm` followed by the name of the algorithm, followed by the a parenthesized list of the arguments.
• For our minimum algorithm, we might write,
`algorithm minimum(Array values)`
• For a generic cookie-making recipe, we might write
`algorithm makeCookies(starch, sweetener, leavener, grease, others)`
• For a pb&j sandwich maker, we might write
`algorithm makePBJ(BreadLoaf bread, Spread pbutter, Spread jelly)`
• Often, we indicate the end of our algorithm with `end name`.
• To represent "Let variable var have the value val", we'll use the shorthand
`var = val`
• To represent "The nth element of array A, we'll use
`A[n]`.
For example, the fifth permutation might be
`perm[5]`.
• To represent a conditional ("if condition then do bleh"), we'll write
`if (condition) then ... end if`
We use the `end if` so that it's clearer what to do when there are multiple things to do.
• To represent a conditional with an alternative (e.g., "if your altitude is over 5000 feet, set your oven to 400 degrees; otherwise, set it to 450 degrees") we'll use
`if (condition) then ... else ... end if`
• To represent a for loop in which we're counting values (e.g., "for each value of i from 1 to 10"), we'll use
`for var = start to last do ... end for`
• For example,, we could compute the product of 1 through ten with
```x = 1
for i = 1 to 10 do
x = x*1
end for
```

• To represent a while loop, we'll write
`while (condition) do ... end while`
• For example
```while (the batter does not have a uniform consistency) do
stir once
end while
```

• To return a value from an algorithm, we'll write
`return value`
• To use a subroutine, we'll write the name of the subroutine followed by any arguments in parentheses.
• Since not everyone will be able to understand our syntax, we may also add notes or comments. We'll preface every comment with two slashes.

### Finding a Minimum, Revisited

• Here's an algorithm to find the minimum number in an array, using our simple syntax.
```// Determine the smallest element in an array
algorithm minimum(Array list)
// Assume that the first element is the smallest
smallest = list[1]
// Look at each of the other elements in the list
for i = 2 to list.length do
// If the ith element is smaller than the one we thought was
// smallest, try the ith element.
if list[i] < smallest then
smallest = list[i]
end if
end for
// Use the element we most recently called "smallest"
return smallest
end minimum
```

• Note that I used an English-language algorithm as comments to guide my use of the "algorithm syntax".

### Shortest Tour, Revisited

• How might we represent the shortest tour algorithm in a more "computer-like" form?
```// Given a set of cities, a starting city, and a cost matrix that
// assigns a cost to a flight between any two cities, determine
// the cost of the cheapest tour that starts at one city and visits
// every city.
algorithm shortestTour(City start, CityList cities, Matrix costs)
// List all the permutations (orderings) of the cities that are
// not the starting city.
permutations = computePermuations(cities)
// For each permutation
for i = 1 to permutations.length do
current = permutations[i]
// Build a trip that starts and ends at the starting city and
// uses the permutation for the intermediate cities.
trip = start + permutation + start
// Determine the cost of that trip
tripcosts[i] = determineCost(trip, costs)
end for
// Print out the cost
print "The cheapest tour costs " + minimum(costs)
end shortestTour
```

• Consider how this might work in the simple case of four cities, A, B, C, and D, with the following costs matrix
A B C D
A X 2 4 7
B 6 X 8 1
C 3 5 X 9
D 5 5 5 X
• We'll assume that we start with city A. Initially,
• `start` is A
• `cities` is (B,C,D). That is, `cities[1]` is B, `cities[2]` is C, and `cities[3]` is D.
• `costs` is the matrix appearing above
• We build all the permutations
• `permutations` is ( (B,C,D), (B,D,C), (C,B,D), (C,D,B), (D,B,C), (D,C,B) ).
• We begin the for loop.
• The first time through,
• `i` is 1.
• `current` is (B,C,D)
• `trip` is (A,B,C,D,A)
• `tripcosts[1]` is 24.
• The next time through the for loop
• `i` is 2.
• `current` is (B,D,C)
• `trip` is (A,B,D,C,A)
• `tripcosts[2]` is 11
• The next time through the for loop
• `i` is 3
• `current` is (C,B,D)
• `trip` is (A,C,B,D,A)
• `tripcosts[3]` is 15
• The next time through the for looop
• `i` is 4
• `current` is (C,D,B)
• `trip` is (A,C,D,B,A)
• `tripcosts[4]` is 24
• The next time through the for loop
• `i` is 5
• `current` is (D,B,C)
• `trip` is (A,D,B,C,A)
• `tripcosts[5]` is 23
• The next time through the for loop
• `i` is 6
• `current` is (D,C,B)
• `trip` is (A,D,C,B,A) is 23
• We've now exhausted all the values of i between 1 and 6.
• The smallest of the values we computed is 11.
• Note that we might then have to separately determine which tour has that value.

## Rubric Writing

• Many of us (myself included) have some trouble thinking about what to write when working on resolving a problem.
• Our text suggests that you follow a framework in writing in your journals about problem solving. Our authors call this framework rubric writing.
• The core of this idea is that you rely on key words to indicate particular steps in your problem solving. Often, you will write these key words in the margin to help you find and structure your notes.
• We'll visit a number of key words across the term, but we'll begin with four.
• Stuck! You're stuck, and aren't sure what to do. See if you can explain why you are stuck ("I tried an idea, and it didn't work"; "I can't think of other ideas"; "I seem to be making progress, but I'm not sure where to go next"; "I have too many choices"; ...).
• Aha! An important realization that may help you on the way to a solution. Often, we get ideas in groups, start to follow one, and forget the others. By writing them down as you get them, you have a way to return.
• Check. Checking of an answer (or partial answer). You'll note that I often need to do this.
• Reflect. We try to do this at the end of each class, but you should also do it while problem solving. Write down some key ideas, particular moments, and what you can learn.

On to Organizing and Representing Information; HTML
Back to Algorithms
Outlines: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Current position in syllabus

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Journal] [Problems] [Computing] [SamR Docs] [Tutorial] [API] [Island]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Thu May 7 10:53:50 1998.

This page generated on Thu May 7 10:58:46 1998 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu