Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code.

Similar presentations


Presentation on theme: "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code."— Presentation transcript:

1 1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code

2 2 The central role of algorithms in computer science From Brookshear; Copyright 2003 Pearson Education

3 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 4 Brookshear: The definition of an algorithm From Brookshear; Copyright 2003 Pearson Education The terminating part comes from theoretical considerations.

5 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 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 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 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

9 9 Sequential search algorithm in pseudo-code From Brookshear; Copyright 2003 Pearson Education

10 10 Origami as Algorithm: Folding a bird from a square piece of paper. From Brookshear; Copyright 2003 Pearson Education

11 11 From Brookshear; Copyright 2003 Pearson Education Origami as Algorithm: Folding a bird from a square piece of paper.

12 12 Origami primitives Syntax vs. Semantics From Brookshear; Copyright 2003 Pearson Education

13 13 Origami primitives Syntax vs. Semantics From Brookshear; Copyright 2003 Pearson Education

14 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 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 16 Counting Primitive Operations Algorithm ArrayMax(A,n) Input: An array A storing N integers Output: 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 17 Counting Primitive Operations Algorithm ArrayMax(A,n) Input: An array A storing N integers Output: 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 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 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 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 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 21 Example of Stepwise Refinement Spiderman 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 22 Example of Stepwise Refinement Star 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 Star 2.Remove her from her Prison Cell 3.Disable the Tractor Beam 4.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 23 Practice Stepwise Refinement + Pseudo-code Recipe for Strawberry Rhubarb Pie Define Pseudo-code at different levels of description

24 24 Pair Programming Two people programming together Both are engaged Only one keyboard and mouse NOT working separately and then joining results. Advantages? –Prevent bugs –Better code –Mutual learning –Almost as fast as two independent programmers


Download ppt "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code."

Similar presentations


Ads by Google