1 I256: Applied Natural Language Processing Marti Hearst Oct 2, 2006.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code.
-
date post
20-Dec-2015 -
Category
Documents
-
view
221 -
download
0
Transcript of 1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code.
2
The central role of algorithms in computer science
From Brookshear; Copyright 2003 Pearson Education
3
What is an algorithm?
• The idea behind the computer program• Stays the same independent of
– Which kind of hardware it is running on– Which programming language it is written in
• Solves a well-specified problem in a general way• Is specified by
– Describing the set of instances (input) it must work on– Describing the desired properties of the output
Adapted from http://www.cs.sunysb.edu/~algorith/lectures-good/node1.html
4
Brookshear: The definition of an algorithm
From Brookshear; Copyright 2003 Pearson Education
The terminating part comes from theoretical considerations.
5
Important Properties of Algorithms
• Correct– always returns the desired output for all legal
instances of the problem.
• Unambiguous• Precise • Efficient
– Can be measured in terms of• Time• Space
– Time tends to be more important
Adapted from http://www.cs.sunysb.edu/~algorith/lectures-good/node1.html
6
Algorithms
• Hand-waving not allowed!
• Specifying algorithms requires you to say what is really involved in making it work.
• Example:– How does a computer work?– Hand-wave: zeros & ones – Real answer: see first three weeks of class.
• You learn to know when you don’t know– “I know nothing except the fact of my ignorance.”– Socrates, from Diogenes Laertius, Lives of Eminent Philosophers
7
Expressing Algorithms
• English description
• Pseudo-code
• High-level programming language
More precise
More easily expressed
Adapted from http://www.cs.sunysb.edu/~algorith/lectures-good/node1.html
8
Pseudo-code
• A shorthand for specifying algorithms• Leaves out the implementation details• Leaves in the essence of the algorithm
<
Code corrected from Brookshear; Copyright 2003 Pearson Education
10
Origami as Algorithm: Folding a bird from a square piece of paper.
From Brookshear; Copyright 2003 Pearson Education
11From Brookshear; Copyright 2003 Pearson Education
Origami as Algorithm: Folding a bird from a square piece of paper.
14
Primitive Operations
• Assign a value to a variable• Call a method• Arithmetic operation• Comparing two numbers• Indexing into an array• Following an object reference• Returning from a method
15
The RAM Model
• Random Access Machine (not Memory)• An idealized notion of how the computer works
– Each "simple" operation (+, -, =, if) takes exactly 1 step.
– Each memory access takes exactly 1 step– Loops and method calls are not simple operations,
but depend upon the size of the data and the contents of the method.
• Measure the run time of an algorithm by counting the number of steps.
Adapted from http://www.cs.sunysb.edu/~algorith/lectures-good/node1.html
16
Counting Primitive Operations
Algorithm ArrayMax(A,n)Input: An array A storing N integersOutput: The maximum element in A.
currentMax A[0]for i 1 to n-1 do
if currentMax < A[i] then currentMax A[i]
return currentMax
Adapted from Goodrich & Tamassia
17
Counting Primitive Operations
Algorithm ArrayMax(A,n)Input: An array A storing N integersOutput: The maximum element in A.
currentMax A[0]for i 1 to n-1 do
if currentMax < A[i] then currentMax A[i]
return currentMax
2 steps + 1 to initialize i
2 steps
2 steps
1 step
2 step each time (compare i to n, inc i)n-1 times
How often done??
It depends on the order the numbers appear in in A[]
Between 4(n-1) and 6(n-1) in the loop
Adapted from Goodrich & Tamassia
18
Algorithm Complexity
• Worst Case Complexity:– the function defined by the maximum number of
steps taken on any instance of size n
• Best Case Complexity:– the function defined by the minimum number of
steps taken on any instance of size n
• Average Case Complexity:– the function defined by the average number of steps
taken on any instance of size n
Adapted from http://www.cs.sunysb.edu/~algorith/lectures-good/node1.html
19
Problem Solving
• Polya’s Advice– Understand the problem– Devise a plan for solving the problem– Carry out the plan– Evaluate the solution for accuracy
• Reality– They are intermixed– Sometimes don’t know the full problem statement
until a solution is found!
20
Problem Solving Strategies
• Try to work the problem backwards – Reverse-engineer– Once you know it can be done, it is much easier to do– What are some examples?
• Stepwise Refinement– Break the problem into several sub-problems– Solve each subproblem separately– Produces a modular structure
• Look for a related problem that has been solved before– Java design patterns
21
Example of Stepwise RefinementSpiderman
• Peter Parker’s goal: Make Mary Jane fall in love with him
• To accomplish this goal:– Get a cool car– To accomplish this goal:
• Get $3000• To accomplish this goal:
– Appear in a wrestling match …
• Each goal requires completing just one subgoal
22
Example of Stepwise RefinementStar Wars Episode IV
• Luke Skywalker’s goal: Make Princess Leia fall in love with him (they weren’t siblings yet)
• To accomplish this goal:– Rescue her from the death star– To accomplish this goal:
1. Land on Death Star2. Remove her from her Prison Cell3. Disable the Tractor Beam4. Get her onto the Millennium Falcon
– To accomplish subgoal (2)• Obtain Storm Trooper uniforms
– Have Wookie pose as arrested wild animal• Find Location of Cell
– Have R2D2 communicate coordinates– To accomplish subgoal (3)
• Have last living Jedi walk across catwalks – To accomplish subgoal (4)
• Run down hall• Survive in garbage shoot
– Fight garbage monster – Have R2D2 stop compaction …
23
Practice Stepwise Refinement + Pseudo-code
• Recipe for Strawberry Rhubarb Pie• Define Pseudo-code at different levels of
description