Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Computer Science: An Overview
Tenth Edition
by
J. Glenn Brookshear
Chapter 5:
Algorithms
What is an algorithm?
2-3
• Let’s play a game!
2-4
• Step1: Please stand up
• Step2: Each student initiate with number X
= 1
• Step 3: Add the X of your closest neighbor
to your X Xnew = Xold + X
• Step 4: Please let your neighbor sit down
• The remaining standing students repeat
from Step 3 to Step 4
2-5
Algorithm and program
• Algorithm– An ordered set of unambiguous, executable steps
that defines a terminating process to solve the
problem
• Program
– A set of instructions, which describe how computers
process data and solve the problem
6
Two elements of algorithm
• Algorithm: operation + control
structure
• Operation:
– Arithmetic: +, -, *, / , etc.
– Relation: >=, <=, etc.
– Logic: and, or, not, etc.
– Data transfer: load, store
7
A
B
条件
B A
成立 不成立
Sequential Conditional
• Sequential
• Conditional
• loop
8
Yes NoMeet the
condition ?
Control structure
5-9
Loop structure
While loop structure Repeat loop structure
5-10
Algorithm Representation
• Requires well-defined primitives
• A collection of primitives constitutes a
programming language.
Algorithm representation
• Natural language
• Traditional flow chart
• N-S flow chart
• Pseudo code
11
5-12
Figure 5.2 Folding a bird from a
square piece of paper
5-13
Natural language: Origami primitives
Traditional flow chart
14
15
Example: estimation of π
Computation of Pi
16
Start
End
Pi <- 0; S <- 1;i <- 1; T <- 1;
|t|≥10-8
Pi <- Pi + t; S <- -1*S;i <- i + 1; T <- s*1/(2 * i -
1);
Output Pi * 4
YesNo
N-S flow chart
• Proposed by I.Nassi and B.Shneideman
• For structured programming
17
结构化程序设计(structured programming)的主要观点是采用自顶向下、逐步求精及模块化的程序设计方法;使用三种基本控制结构构造程序,任何程序都可由顺序、选择、循环三种基本控制结构构造。结构化程序设计主要强调的是程序的易读性。
3 control structures for
N-S flow chart
18
5-19
Pseudocode Primitives
• Assignment
name expression
• Conditional selection
if condition then action
5-20
Pseudocode Primitives (continued)
• Repeated execution
while condition do activity
• Procedure
procedure name (generic names)
5-21
Figure 5.4 The procedure Greetings
in pseudocode
Real code?
2-22
What algorithms can do?
2-23
2-24
2-25
Algorithm discovery
• Art
= analysis + knowledge + experiment +
inspiration (potential)
2-26
5-27
Polya’s Problem Solving Steps
• 1. Understand the problem.
• 2. Devise a plan for solving the problem.
• 3. Carry out the plan.
• 4. Evaluate the solution for accuracy and
its potential as a tool for solving other
problems.
5-28
Getting a Foot in the Door
• Try working the problem backwards
• Solve an easier related problem
– Relax some of the problem constraints
– Solve pieces of the problem first (bottom up
methodology)
• Stepwise refinement: Divide the problem into
smaller problems (top-down methodology)
5-29
Ages of Children Problem
• Person A is charged with the task of determining
the ages of B’s three children.
– B tells A that the product of the children’s ages is 36.
– A replies that another clue is required.
– B tells A the sum of the children’s ages.
– A replies that another clue is needed.
– B tells A that the oldest child plays the piano.
– A tells B the ages of the three children.
• How old are the three children?
5-30
Figure 5.5
ACM ICPC 2014 (International
Collegiate Programming Contest)
2-31
5-32
Iterative Structures
• Pretest loop:
while (condition) do
(loop body)
• Posttest loop:
repeat (loop body)
until(condition)
5-33
Figure 5.8 The while loop structure
5-34
Figure 5.9 The repeat loop structure
Search algorithms
• Sequential search (顺序查找)
• Binary search (二分查找)
5-35
5-36
Sequential search algorithm in
pseudocode
5-37
Figure 5.7 Components of repetitive
control
5-38
Recursion
• The execution of a procedure leads to
another execution of the procedure.
• Multiple activations of the procedure are
formed, all but one of which are waiting for
other activations to complete.
5-39
Figure 5.12 Applying our strategy to
search a list for the entry John
5-40
Figure 5.14 The binary search
algorithm in pseudocode
5-41
Figure 5.15
Looking for Bill
Alice
Bill
Carol
David
Evelyn
Fred
George
5-42
Figure 5.16
Looking for David
Alice
Carol
Evelyn
Fred
George
5-43
Figure 5.17
Looking for David
Sort algorithm
• Insertion sort (插入排序)
• Selection sort (选择排序)
• Bubble sort (冒泡排序)
5-44
5-45
Insertion sort
5-46
Figure 5.11 The insertion sort
algorithm expressed in pseudocode
5-47
Algorithm Efficiency
• Measured as number of instructions
executed
• Big O notation: Used to represent
efficiency classes
– Example: Insertion sort is in O(n2)
• Best, worst, and average case analysis
5-48
Figure 5.19 Graph of the worst-case
analysis of the insertion sort algorithm
worst:1+2+3+…+(n-1)=1/2(n2-n)
average:是最差情况的一半1/4(n2-n)
复杂度:O(n2)
5-49
Figure 5.18 Applying the insertion sort in
a worst-case situation
5-50
Figure 5.20 Graph of the worst-case
analysis of the binary search algorithm
worst:lgn
复杂度:O(lgn)
5-51
Software Verification
• Proof of correctness
– Assertions
• Preconditions
• Loop invariants
• Testing
5-52
Chain Separating Problem
• A traveler has a gold chain of seven links.
• He must stay at an isolated hotel for seven nights.
• The rent each night consists of one link from the chain.
• What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?
5-53
Figure 5.21 Separating the chain
using only three cuts
5-54
Figure 5.22 Solving the problem with
only one cut
5-55
Figure 5.23 The assertions associated
with a typical while structure
• Questions?
2-56
Movie: the social network
2-57
Learning Algorithms
• Every class in CS has some relations with
algorithms
– But some classes are not obviously related to
algorithms, such as math classes.
–微积分,离散数学,线性代数,概率统计,…
• Why should we study them?
– They are difficult, and boring, and difficult…
5-58
Why Study Math?
• Train the logical thinking and reasoning
– Algorithm is a result of logical thinking:
correctness, efficiency, …
– Good programming needs logical thinking and
reasoning. For example, debugging.
• Learn some basic tricks
– Many beautiful properties of problems can be
revealed through the study of math
– Standing on the shoulders of giants
5-59
Try to Solve These Problems
1. Given a network of thousands nodes, find the shortest path from node A to node B
– The shortest path problem (离散数学)
2. Given a circuit of million transistors, find out the current on each wire
– Kirchhoff's current law (线性代数)
3. In CSMA/CD, what is the probability of collisions?
– Network analysis (概率统计)
5-60
微积分
• Limits, derivatives, and integrals of continuous functions.
• Used inalmost every field
–网络分析,效能分析–信号处理,图像处理,模拟电路设计–科学计算, 人工智能,计算机视觉,计算机图学– …
• Also the foundation of many other math
–概率统计,工程数学
5-61
离散数学
• Discrete structure, graph, integer, logic,
abstract algebra, combinatorics
• The foundation of computer science
– Every field in computer science needs it
– Particularly, 算法,数字逻辑设计,密码学,编码理论,计算机网络,CAD,计算理论
• Extended courses
– Special topics on discrete structure, graph
theory, concrete math
5-62
线性代数
• Vectors, matrices, tensors, vector spaces,
linear transformation, system of equations
• Used in almost everywhere when dealing
with more than one number
–网络分析,效能分析
–信号处理,图像处理
–科学计算, 人工智能,计算机视觉,计算机图学
– CAD,电路设计
5-63
概率统计
• Probability models, random variables,
probability functions, stochastic processes
• Every field uses it
– Particularly,网络分析,效能分析,信号处理,图像处理,科学计算, 人工智能,计算机视觉…
– Other examples: randomized algorithm, queue
theory, computational finance, performance
analysis
5-64
工程数学
• A condensed course containing essential
math tools for most engineering disciplines
– Partial differential equations, Fourier analysis,
Vector calculus and analysis
• Used in the fields that need to handle
continuous functions
–网络分析,效能分析,信号处理,图像处理,科学计算,人工智能,计算机视觉, CAD,电路设计
5-65