Title: Chapter 2: Algorithm Discovery and Design
1Chapter 2 Algorithm Discovery and Design
- Invitation to Computer Science,
- Java Version, Second Edition
2Objectives
- In this chapter, you will learn about
- Representing algorithms
- Examples of algorithmic problem solving
3Introduction
- This chapter discusses algorithms and algorithmic
problem solving using three problems - Searching lists
- Finding smallest and largest items in lists
- Matching patterns
4Representing Algorithms
- What is an algorithm?
- A series of steps to perform a task thats it.
- A recipe is an algorithm for how to cook chicken
enchiladas - Directions are an algorithm for how to get to
someones house
5Representing Algorithms
- Natural language
- Language spoken and written in everyday life
- English, Spanish
- Problems with using natural language for
algorithms - Imprecise
- Relies on context and experience of the person
you are talking to - I saw the man looking through the telescope
6Representing Algorithms
- High-level programming language
- Examples C, Java
- Problem with using a high-level programming
language for algorithms - During the initial phases of design, we are
forced to deal with detailed language issues
7Pseudocode
- English language constructs modeled to look like
statements available in most programming
languages - Ex ADD X Y
- No fixed syntax for most operations is required
- Everyone knows what you are talking about
8Pseudocode (continued)
- Less ambiguous and more readable than natural
language - Emphasis is on the process, not the specific
notation - Can be easily translated into a programming
language
9Operations
- Types of algorithmic operations
- Sequential input / output, assignment,
printing, etc. - Conditional doing one thing or another based on
a certain condition - Iterative looping doing something more than
once
10Sequential Operations (continued)
- Computation operations
- Example
- Set the value of variable to arithmetic
expression - X 10 (set x equal to 10)
- Variable
- Named storage location that can hold a data value
- X in the above example
11Sequential Operations (continued)
- Input operations
- To receive data values from the outside world
- Get a value for r, the radius of the circle
- Output operations
- To send results to the outside world for display
- Print the value of Area
12- Go to gas station
- Take out loan
- Figure 2.3
- Algorithm for Computing Average Miles per Gallon
13Conditional and Iterative Operations
- Sequential algorithm
- Executes its instructions in a straight line from
top to bottom and then stops - Control operations allow us to get more complex
- Conditional operations
- IF X10 THEN do some stuff
- Iterative operations
- WHILE X lt 100 do some other stuff
14Conditional and Iterative Operations (continued)
- Conditional operations
- Ask questions and choose alternative actions
based on the answers - Example
- if x is greater than 25 then
- print x
- else
- print x times 100
15Conditional and Iterative Operations (continued)
- Iterative operations
- Perform looping behavior repeating actions
until a continuation condition becomes false - Loop
- The repetition of a block of instructions
16Conditional and Iterative Operations (continued)
- Examples
- while j gt 0 do
- set s to s aj
- set j to j - 1
- repeat
- print ak
- set k to k 1
- until k gt n
17- Figure 2.4
- Second Version of the Average Miles per Gallon
Algorithm
18Conditional and Iterative Operations (continued)
- Components of a loop
- Continuation condition when do we stop?
- Loop body what we want to repeat
- Infinite loop
- The continuation condition never becomes false
and the loop runs forever - This is usually an error
19- Figure 2.5
- Third Version of the Average Miles per Gallon
Algorithm
20Conditional and Iterative Operations
- Pretest loop
- Continuation condition tested at the beginning of
each pass through the loop - It is possible for the loop body to never be
executed - Ex While loop
21Conditional and Iterative Operations (continued)
- Post-test loop
- Continuation condition tested at the end of loop
body - Loop body must be executed at least once
- Ex Do - While loop
22- Figure 2.6
- Summary of Pseudocode Language Instructions
23One other operation - functions
- Someone (possibly not you) writes a function.
- This function performs some action, like
averaging three numbers, and returns a result to
you. - You call the function, and it returns a result
- A function accepts parameters (the numbers to
average in this case) and you include these
parameters when you call the function.
24One other operation - functions
- Average (98, 77, 100) then gives me the average
of the 3 numbers - Assuming the function was written correctly ?
- Essential for creating re-usable code, not
reinventing the wheel, etc. - Many, many built in functions (methods) in Java.
25Example 1 Looking, Looking, Looking
- Examples of algorithmic problem solving
- Sequential search find a particular value in an
unordered collection - Find maximum find the largest value in a
collection of data - Pattern matching determine if and where a
particular pattern occurs in a piece of text
26Example 1 Looking, Looking, Looking (continued)
- Task
- Find a particular persons name from an unordered
list of telephone subscribers - Algorithm outline
- Start with the first entry and check its name,
then repeat the process for all entries
27Example 1 Looking, Looking, Looking (continued)
- Correct sequential search algorithm
- Uses iteration (loops) to simplify the task
- Handles special cases (like a name not found in
the collection)
28Uses the variable Found to exit the iteration as
soon as a match is found
- Figure 2.9
- The Sequential Search Algorithm
29Example 1 Looking, Looking, Looking (continued)
- The selection of an algorithm to solve a problem
is greatly influenced by the way the data for
that problem are organized
30Example 2 Big, Bigger, Biggest
- Task
- Find the largest value from a list of values
- Algorithm outline
- Keep track of the largest value seen so far
(initialized to be the first in the list) - Compare each value to the largest seen so far,
and keep the larger as the new largest
31Example 2 Big, Bigger, Biggest (continued)
- Once an algorithm has been developed, it may
itself be used in the construction of other, more
complex algorithms - Library
- A collection of useful algorithms created
(usually) by someone else. - Dont reinvent the wheel many solutions to
common problems are available, particularly in
Java - An important tool in algorithm design and
development
32- Figure 2.10
- Algorithm to Find the Largest Value in a List
33Example 3 Meeting Your Match
- Task
- Find if and where a pattern string occurs within
a longer piece of text - Algorithm outline
- Try each possible location of pattern string in
turn - At each location, compare pattern characters
against string characters
34Example 3 Meeting Your Match (continued)
- Abstraction
- Separating high-level view from low-level details
- Key concept in computer science LAYERS
- Makes difficult problems intellectually
manageable - Allows piece-by-piece development of algorithms
35Example 3 Meeting Your Match (continued)
- Top-down design
- When solving a complex problem
- Create high-level operations in first draft of an
algorithm - After drafting the outline of the algorithm,
return to the high-level operations and elaborate
each one
36Example 3 Meeting Your Match (continued)
- Pattern-matching algorithm
- Contains a loop within a loop
- External loop iterates through possible locations
of matches to pattern - Internal loop iterates through corresponding
characters of pattern and string to evaluate
match
37- Figure 2.12
- Final Draft of the Pattern-Matching Algorithm
38Summary
- Algorithm design is a first step in developing an
algorithm - Must also
- Ensure the algorithm is correct
- Ensure the algorithm is sufficiently efficient
- Pseudocode is used to design and represent
algorithms
39Summary
- Pseudocode is readable, unambiguous, and
analyzable - Algorithm design is a creative process uses
multiple drafts and top-down design to develop
the best solution - Abstraction is a key tool for good design