Introduction to Algorithms Dave Feinberg What vs How
Introduction to Algorithms Dave Feinberg
What vs. How • We can define the square-root function as √x = the y such that y ≥ 0 and y² = x • This defines precisely what a square root is, but it doesn't tell us how to compute the square root of a given number.
Algorithm • step-by-step instructions for performing some task (e. g. finding the square root of a number) • (Many of the following slides are based on David Harel's "Computers, Ltd. ")
Chocolate Mouse • • • Melt chocolate and 2 tablespoons water in double boiler. When melted, stir in powdered sugar; add butter bit by bit. Set aside. Beat egg yolks until thick and lemon-colored, about 5 minutes. Gently fold in chocolate. Reheat slightly to melt chocolate, if necessary. Stir in rum and vanilla. Beat egg whites until foamy. Beat in 2 tablespoons sugar; beat until stiff peaks form. Add egg whites and dead mouse to chocolate-yolk mixture Divide into individual serving dishes. Chill at least 4 hours. Serve with whipped cream, if desired.
Completing the Square • • x 2 - 6 x + 9 (x - 3) 2 x-3 x x -9 -9 -49 - 40 = 0 =0 = 49 =± 7 =3± 7 = 10, -4
Algebra • The word "algebra" comes from the Arabic word "al-jabr" which means "completion". • Muhammad ibn Musa al-Khwarizmi (c. 780850) wrote "The Compendious Book on Calculation by Completion and Balancing", which established algebra as a mathematical discipline.
From Chemistry • • Write two unbalanced half-reactions, one for the species that is being oxidized and its product, and one for the species that is reduced and its product. Insert coefficients to make the numbers of atoms of all elements except oxygen and hydrogen equal on the two sides of each half-reaction. Balance oxygen by adding H 2 O to one side of each half-reaction. Balance hydrogen atoms. For acidic solutions: Add H 3 O+ to each side of each half-reaction that is "deficient" in hydrogen and add an equal amount of H 2 O to the other side. For basic solutions: add H 2 O to the side of the half-reaction that is "deficient" in hydrogen and add an equal amount of OH- to the other side. Balance charge by inserting e- (electrons) as a reactant or product in each half-reaction. Multiply the two half-reactions by numbers chosen to make the number of electrons given off by the oxidation step equal to the number taken up by the reduction step. Then add the two half-reactions. Balance both sides for excess of H 2 O
From Physics • • Draw a free-body diagram. Identify direction of every force and of acceleration. Pick a coordinate system to minimize the number of things (forces and acceleration) that must be broken into components, especially unknown values. Draw the components for any forces or acceleration that does not lie along the X or Y axis, and identify the angle that is given (or being looked for). Pick one direction and write down all the forces or components of forces in that direction, using positive and negative signs to identify those in the positive and negative directions. Set the sum of the forces in that direction as equal to the mass multiplied by the acceleration in that direction. (If not moving or moving at a constant velocity in that direction, acceleration will be zero. ) Repeat for the other direction.
Chocolate Mouse • • • Melt chocolate and 2 tablespoons water in double boiler. When melted, stir in powdered sugar; add butter bit by bit. Set aside. Beat egg yolks until thick and lemon-colored, about 5 minutes. Gently fold in chocolate. Reheat slightly to melt chocolate, if necessary. Stir in rum and vanilla. Beat egg whites until foamy. Beat in 2 tablespoons sugar; beat until stiff peaks form. Add egg whites and dead mouse to chocolate-yolk mixture Divide into individual serving dishes. Chill at least 4 hours. Serve with whipped cream, if desired.
Which Instructions? • Stir in powdered sugar. • Take a little powdered sugar, pour it into the melted chocolate, stir it in, take a little more, pour, stir, . . . • Take 2, 365 grains of powdered sugar, pour them into the melted chocolate, pick up a spoon and use circular movements to stir it in, . . . • Move your arm towards the ingredients at an angle of 14 degrees, at an approximate velocity of 18 inches per second, . . .
Which Instructions? • Melt chocolate and 2 tablespoons water in double boiler. When melted, stir in powdered sugar; add butter bit by bit. Set aside. • Prepare chocolate mixture. • Prepare chocolate mouse.
Multiply 528 by 46 • • Multiply the 8 by the 6, yielding 48. Write down the units digit of the result, 8. Remember the tens digit, 4. The 2 is then multiplied by the 6, and the 4 is added, yielding 16. • The units digit 6 is then written down to the left of the 8 and the tens digit 1 is remembered. • And so on.
Multiply 528 by 46 • Multiply the 8 by the 6. • Look up the entry appearing in the eighth row and sixth column of multiplication table. • Add 6 to itself 8 times. • Multiply 528 by 46. • Why are we allowed to multiply 8 by 6 directly, but not 528 by 46?
Primitives input x move x y-2 left print y right n < 10 frontclear
Back Up left move left
Variables • A variable is a name for a place in memory where we can store a value. • We can change what value is associated with that name. Hence, "variable" • We use a variable whenever our computation needs to remember something.
Using Variables x y x 1 1 2 1 x x+1
What does this do? x y savedx x y x savedy y x savedy y savedx
Algorithms and Programs input n sum 0 n>0 no Given a positive integer n. Keep track of a running sum, starting with 0. Add each integer from 1 to n, to sum. Print the resulting sum. yes sum + n n n-1 print sum n = input("Enter a number: ") sum = 0 while n > 0: sum = sum + n n=n-1 print sum
Conditionals yes . . . ? no . . . input n n<0 yes n 0 -n print n no
age >= 21 no Conditionals yes print "DRINK" age no age >= 18 and US Citizen print "DRAFT" printed <nothing> VOTE print "VOTE" yes male DRINK yes male US Citizen no DRAFT DRINK, VOTE DRINK DRAFT VOTE, DRAFT DRINK, VOTE, DRAFT
Conditions input n yes print "positive" n>0 input n no yes ? no print "positive"
Simplifying Conditionals yes move frontclear no yes left move frontclear left no
Multiple Cases yes print a no a<b yes print b no a>b print "TIE"
Same Behavior? yes frontclear no yes move yes frontclear no left move frontclear no left
Loops no ? yes frontclear yes . . . move . left no
1 input n n sum next 1 2 3 sum 0 n>0 yes 4 sum + n 5 n n-1 6 print sum 3 no 2 3 0 3 3 0 4 3 3 5 2 3 3 2 3 4 2 5 5 1 5 3 1 5 4 1 6 5 0 6 3 0 6 6 6
1 input n n sum next 1 2 3 sum 0 n>0 yes 4 sum + n 5 n n-1 6 print sum 3 no 2 3 0 3 3 0 4 3 3 5 2 3 3 2 3 4 2 5 5 1 5 3 1 5 4 1 6 5 0 6 3 0 6 6 Each of these is a state Each instruction results in a change in state. The states visited constitute a process. A process may be longer/shorter than the original flowchart.
Same Behavior? 1 input n 2 sum 0 3 n>0 yes no n>0 yes 4 sum + n n n-1 5 n n-1 sum + n 6 print sum no
Alternative Conditions 1 input n 2 sum 0 3 n>0 yes 4 sum + n 5 n n-1 6 print sum no n >= ? n != ?
Loops a<b yes no What must be true here? . . . What must be true here?
n>0 no Simplify yes n>0 no n>0 yes T F T O O yes L n>0 no M M no
Move the robot n times input n no yes move n n-1
A Common Pattern input n n>0 yes INITIALIZE no TEST yes move DO SOMETHING n n-1 INCREMENT no
How many times? n 0 n <= 3 yes n 1 no n<3 yes n 0 no n>3 yes print "LOOP" n n+1 no
How many times? n 0 n<3 yes n 0 no n != 3 yes print "LOOP" n n-1 n n+2 no
What Do These Do? n>0 yes print "LOOP" no 3>2 yes no
input n print "NO" Is Prime? What is a prime number? This flow chart should print "YES" if n is prime, and should print "NO" if n is not prime. Is this a good algorithm? How can I determine if a number is prime?
Mod input n d 1 r n mod d r != 0 no "n mod d" means "remainder when n is divided by d" yes d d+1 print "NO" What is 29 mod 12?
Is Prime? input n d 1 r n mod d r != 0 no When I run this program, the computer always prints "NO" (even when n is 5). yes d d+1 print "NO" Why?
Is Prime? input n d 2 r n mod d r != 0 no When I run this program, the computer freezes on all odd inputs. yes d d+1 print "NO" Why?
Is Prime? input n d 2 r n mod d r != 0 no When I run this program, the computer always prints "NO" (even when n is 5). yes d d+1 r n mod d print "NO" Why?
Is Prime? input n d 2 r n mod d d < n and r != 0 yes d d+1 r n mod d print "NO" no When I run this program, the computer always prints "NO" (even when n is 5). Why? When should I print "YES"?
input n d 2 r n mod d d < n and r != 0 no yes d d+1 r n mod d d<n print "YES" print "NO"
Debugging: Scientific Method • If a program doesn't do what it's supposed to, we say it has a bug. • To fix it, we generate a hypothesis as to why it doesn't work. • We design and perform an experiment to test the hypothesis. • And repeat until we find our mistake.
Big Ideas • Algorithms tell us how to solve problems. • When we want someone to perform an algorithm, we have to know what operations they already know (their primitives). • A flowchart can describe an algorithm. • We use a variable whenever our algorithm needs to remember a value. • Most algorithms use conditionals and loops.
- Slides: 46