Held: Tuesday, January 27, 1998
algorithm
followed by the name of the algorithm, followed by the a parenthesized
list of the arguments.
algorithm minimum(Array values)
algorithm makeCookies(starch, sweetener, leavener, grease, others)
algorithm makePBJ(BreadLoaf bread, Spread pbutter, Spread jelly)
end name.
var = val
A[n].
perm[5].
if (condition) then ... end if
end if so that it's clearer what to do
when there are multiple things to do.
if (condition) then ... else ... end if
for var = start to last do ... end for
x = 1 for i = 1 to 10 do x = x*1 end for
while (condition) do ... end while
while (the batter does not have a uniform consistency) do stir once end while
return value
// 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
// 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
| A | B | C | D | |
|---|---|---|---|---|
| A | X | 2 | 4 | 7 |
| B | 6 | X | 8 | 1 |
| C | 3 | 5 | X | 9 |
| D | 5 | 5 | 5 | X |
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
permutations is ( (B,C,D), (B,D,C), (C,B,D), (C,D,B),
(D,B,C), (D,C,B) ).
i is 1.
current is (B,C,D)
trip is (A,B,C,D,A)
tripcosts[1] is 24.
i is 2.
current is (B,D,C)
trip is (A,B,D,C,A)
tripcosts[2] is 11
i is 3
current is (C,B,D)
trip is (A,C,B,D,A)
tripcosts[3] is 15
i is 4
current is (C,D,B)
trip is (A,C,D,B,A)
tripcosts[4] is 24
i is 5
current is (D,B,C)
trip is (A,D,B,C,A)
tripcosts[5] is 23
i is 6
current is (D,C,B)
trip is (A,D,C,B,A) is 23
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