Title: The Algorithmic Foundations of Computer Science
1Chapter 2 The Algorithmic Foundations of Computer
Science
1
2Objectives
- After studying this chapter, students will be
able to - Explain the benefits of pseudocode over natural
language or a programming language - Represent algorithms using pseudocode
- Identify algorithm statements as sequential,
conditional, or iterative - Define abstraction and top-down design, and
explain their use in breaking down complex
problems
3Objectives (continued)
- After studying this chapter, students will be
able to - Illustrate the operation of sample algorithms
- multiplication by repeated addition
- sequential search of a collection of values
- finding the maximum element in a collection
- finding a pattern string in a larger piece of text
4 Introduction
- Algorithms for everyday may not be suitable for
computers to perform (as in Chapter 1) - Algorithmic problem solving focuses on algorithms
suitable for computers - Pseudocode is a tool for designing algorithms
- This chapter will use a set of problems to
illustrate algorithmic problem solving
5Representing Algorithms
- Natural language
- Language spoken and written in everyday life
- Examples English, Spanish, Arabic, etc.
- Problems with using natural language for
algorithms - Verbose
- Imprecise
- Relies on context and experiences
- to give precise meaning to a word or phrase
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
7Representing Algorithms
- Pseudocode
- English language constructs modeled to look like
statements available in most programming
languages - Steps presented in a structured manner (numbered,
indented, etc.) - No fixed syntax for most operations is required
- Less ambiguous, more readable than natural
language - Emphasis is on process, not notation
- Well-understood forms allow logical reasoning
about algorithm behavior - Can be easily translated into a programming
language
8Representing Algorithms
- Pseudocode is used to design algorithms
- Natural language is
- expressive, easy to use
- verbose, unstructured, and ambiguous
- Programming languages are
- structured, designed for computers
- Formal syntax, grammar
- grammatically fussy, cryptic
- Pseudocode lies somewhere between these two
9(No Transcript)
10(No Transcript)
11Types of algorithmic operations
- Sequential step by step
- Conditional if
- Iterative - loop
12Representing Algorithms (continued)
- Sequential operations perform a single task
- Input gets data values from outside the
algorithm - Computation a single numeric calculation
- Output sends data values to the outside world
- A variable is a named location to hold a value
- A sequential algorithm is made up only of
sequential operations - Example computing average miles per gallon
13(No Transcript)
14Representing Algorithms (continued)
- Control operation changes the normal flow of
control - Conditional statement asks a question and
selects among alternative options - Evaluate the true/false condition
- If the condition is true, then do the first set
of operations and skip the second set - If the condition is false, skip the first set of
operations and do the second set - Example check for good or bad gas mileage
15(No Transcript)
16(No Transcript)
17Representing Algorithms (continued)
- Iteration an operation that causes looping,
repeating a block of instructions - While statement repeats while a condition remains
true - continuation condition a test to see if while
loop should continue - loop body instructions to perform repeatedly
- Example repeated mileage calculations
18Iterative Operations loops
- Components of a loop
- Continuation condition
- Loop body
- Infinite loop (avoid)
- The continuation condition never becomes false
- An error
19(No Transcript)
20(No Transcript)
21Representing Algorithms (continued)
- Do/while, repeat/until alternate iterative
operation - continuation condition appears at the end
- loop body always performed at least once
- post-test loop
- Primitive operations sequential, conditional,
and iterative are all that is needed
22(No Transcript)
23(No Transcript)
24Examples of Algorithmic Problem SolvingExample
1 Go Forth and Multiply
-
- Given two nonnegative integer values,
- a 0, b 0, compute and output the product
(a b) using the technique of repeated addition.
That is, determine the value of the sum a a a
. . . a - (b times).
25Examples of Algorithmic Problem SolvingExample
1 Go Forth and Multiply (continued)
- Get input values
- Get values for a and b
- Compute the answer
- Loop b times, adding each time
- Output the result
- Print the final value
- steps need elaboration
26Examples of Algorithmic Problem SolvingExample
1 Go Forth and Multiply (continued)
- Loop b times, adding each time
- Set the value of count to 0
- While (count lt b) do
- the rest of the loop
- Set the value of count to count 1
- End of loop
- steps need elaboration
27Examples of Algorithmic Problem SolvingExample
1 Go Forth and Multiply (continued)
- Loop b times, adding each time
- Set the value of count to 0
- Set the value of product to 0
- While (count lt b) do
- Set the value of product to (product a)
- Set the value of count to count 1
- End of loop
- Output the result
- Print the value of product
28(No Transcript)
29Example 2 Looking, Looking, Looking
- Examples of algorithmic problem solving
Searching - 2. Sequential search find a particular value in
an unordered collection - 3. Find maximum find the largest value in a
collection of data - 4. Pattern matching determine if and where a
particular pattern occurs in a piece of text
30Examples of Algorithmic Problem SolvingExample
2 Looking, Looking, Looking
-
- Assume that we have a list of 10,000 names that
we define as N1, N2, N3, . . . , N10,000, - along with the 10,000 telephone numbers of
those individuals, denoted as - T1, T2, T3, . . . , T10,000.
- To simplify the problem, we initially assume
that all names in the book are unique and that
the names need not be in alphabetical order.
31Examples of Algorithmic Problem SolvingExample
2 Looking, Looking, Looking (continued)
- Finding the correct solution to a problem is
called algorithm discovery and is the most
challenging and creative part of the
problem-solving process. - Three versions here illustrate algorithm
discovery, working toward a correct, efficient
solution - A sequential algorithm (no loops or conditionals)
- An incomplete iterative algorithm
- A correct algorithm
32(No Transcript)
33(No Transcript)
34(No Transcript)
35Example 2 Looking, Looking, Looking (continued)
- Correct sequential search algorithm
- Uses iteration to simplify the task
- Refers to a value in the list using an index (or
pointer) - Handles special cases (like a name not found in
the collection) - Uses the variable Found to exit the iteration as
soon as a match is found
36Example 3 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
37Examples of Algorithmic Problem SolvingExample
3 Big, Bigger, Biggest
- A building-block algorithm used in many
libraries - Library A collection of pre-defined useful
algorithms - Given a value n 1 and a list containing
exactly n unique numbers called A1, A2, . . . ,
An, find and print out both the largest value in
the list and the position in the list where that
largest value occurred.
38Example 3 Big, Bigger, Biggest (continued)
- Find Largest algorithm
- Uses iteration and indices like previous example
- Updates location and largest so far when needed
in the loop
39(No Transcript)
40Example 4 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
41Examples of Algorithmic Problem SolvingExample
4 Meeting Your Match
- Pattern-matching common across many applications
- word processor search, web search, image
analysis, human genome project - You will be given some text composed of n
characters that will be referred to as T1 T2 . .
. Tn. You will also be given a pattern of m
characters, m n, that will be represented as P1
P2 . . . Pm. The algorithm must locate every
occurrence of the given pattern within the text.
The output of the algorithm is the location in
the text where each match occurred.
42Examples of Algorithmic Problem SolvingExample
4 Meeting Your Match (continued)
- Algorithm has two parts
- Sliding the pattern along the text, aligning it
with each position in turn - Given a particular alignment, determine if there
is a match at that location - Solve parts separately and use
- Abstraction, focus on high level, not details
- Top-down design, start with big picture,
gradually elaborate parts
43Example 4 Meeting Your Match (continued)
- Abstraction
- Separating high-level view from low-level details
- Key concept in computer science
- Makes difficult problems intellectually
manageable - Allows piece-by-piece development of algorithms
44Example 4 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 - Repeat until all operations are primitives
45Example 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
46(No Transcript)
47(No Transcript)
48Pattern Matching
- Some applications
- Matching phrases in Literature or text
- Find a pattern and replace it with another
- Web search ( Google)- matching strings
- Patterns in X-rays, CAT scans, etc.
- Patterns in the human genome whole new field of
bioinformatics
49Summary
- Pseudocode is used for algorithm design
structured like code, but allows English and math
phrasing and notation - Pseudocode is made up of sequential,
conditional, and iterative operations - Algorithmic problem solving involves
- Step-by-step development of algorithm pieces
- Use of abstraction, and top-down design
50Important Algorithms
- Sequential search
- ( Get values) and Start at the beginning of list
- Set found false
- Repeat until found or end of list
- Look at each element
- If element target
- set found true and
- print desired element
- else
- move pointer to next element
- End loop
- If found false then print message not on list
- Stop
51Important Algorithms
- Find Largest
- ( Get values) and Start at the beginning of list
- Set found false and Largest to first element
- Repeat until end of list
- Look at each element
- If element gt largest
- set (found true) and
- (largest element) and (location i)
- move pointer to next element
- End loop
- Print largest and location
- Stop
52Important Algorithms
- Pattern matching in text
- Location 123456789.
- Text A man and a woman
- Pattern an
- Output There is a match at position 4,
etc. - Patterns in genes
- Gene TCAGGCTAATCGGAAGT
- Probe TAATC
- Match Yes!
53Definitions
- Algorithm Discovery finding a correct and
efficient solution to a problem - Library collection of useful algorithms
- Iteration repeated operations
- Index the position of an element in a list
- Abstraction- ability to separate the high level
view of an object from the low level details - Top-Down Design viewing an operation at a high
level of abstraction and later adding the details
in steps - ( stepwise refinement)
54Research
- Write an algorithm that generates a Caesar cipher
(See p. 86, 21) and Chapter 13 - Investigate the field of bioinformatics
- Basic Explanations
- htthttp//en.wikipedia.org/wiki/Bioinformatics
- http//biotech.icmb.utexas.edu/pages/bioinfo.html
- BLAST tool used to search protein databases
- http//www.ncbi.nlm.nih.gov/Education/BLASTinfo/in
formation3.html