Lecture 2 Ways Algorithms can be Constructed 1/20/04

advertisement
Lecture 2
1/20/04
Ways Algorithms can be
Constructed
Lecture 2
• Sequential construction
Introduction to Computer Science
(continued)
1/20/04
CS 100 - Lecture 2
• Conditional construction
• Iterative construction
1
1/20/04
CS 100 - Lecture 2
2
Sequential Construction
Conditional Construction
first do this
test
something
either do this
or do this
then do this
continue
1/20/04
CS 100 - Lecture 2
3
1/20/04
Iterative Construction
• Often good to begin by investigating how a
person would do it
• Control structures:
– Shows how to order the steps
– Reveals what different conditions may occur
if not, repeat
• Data structures:
test if done
– Shows how to organize information
– Reveals what additional information is required
if done, continue
CS 100
CS 100 - Lecture 2
4
Developing an Algorithm
do something
1/20/04
CS 100 - Lecture 2
5
1/20/04
CS 100 - Lecture 2
6
1
Lecture 2
1/20/04
Developing Decimal Addition
Algorithm: Example
0
1
1
8
1
4
7
2
1
15
1
5
1
1/20/04
Developing Decimal Addition
Algorithm: More General Case
carry
c
c
c
0
7
a3
a2
a1
a0
9
5
b3
b2
b1
b0
04
14
12
c4
cc3
cc2
cc1
cc0
4
4
2
c4
c3
c2
c1
c0
CS 100 - Lecture 2
c
7
a)
b)
c)
d)
Two positive numbers of m digits,
am−1am−2 La1a0 and bm−1bm−2 Lb1b0 .
Wanted : c m c m−1 Lc1c 0,
€
4. Set cm to value of c.
5. Print answer cmcm–1…c1c0.
1/20/04
1/20/04
9
If you can specify an algorithm to solve a
problem, then you can automate its solution
1/20/04
CS 100 - Lecture 2
CS 100 - Lecture 2
10
Requirements for
Automated Computation
Importance of Algorithms
CS 100
Set ci to c + ai + bi.
If ci < 10, then set c to 0,
otherwise set c to 1 and reset ci to ci – 10.
Add 1 to i.
where c m c m−1 Lc1c 0 = ( am−1am−2 La1a0 ) + (bm−1bm−2 Lb1b0 )
CS 100 - Lecture 2
8
1. Set value of c (carry) to 0.
2. Set value of i (column number) to 0.
3. Repeat steps (a) – (d) until i > m–1.
Given m ≥ 1
€
CS 100 - Lecture 2
Algorithm
Algorithm for Adding
Two m-Digit Numbers
€
1/20/04
carry
11
• Ability to make device to do basic
operations
• Ability to make control device for
sequencing and conditional tests
• Technology not available until early 20th
century
1/20/04
CS 100 - Lecture 2
12
2
Lecture 2
1/20/04
Non-Algorithmic
Information Processing
Algorithmic Problem Solving
• Are there algorithmic solutions for all problems?
– No! Gödel discovered algorithmically unsolvable
problems in 1930s
• “Cognitive inversion”: many things that are
easy for people are hard for computers, and
vice versa
• Can algorithmically solvable problems be solved
efficiently?
– e.g., face recognition vs.calculating
– No! They are inherently hard problems
• Even for problems that are efficiently
algorithmically solvable, efficient algorithms may
be difficult to find
• These are some of the issues addressed by
computer scientists
• Suggests that computers & brains work in
very different ways
• Much research in non-algorithmic
information processing in neural networks
1/20/04
1/20/04
CS 100 - Lecture 2
13
Definition of “Algorithm”
An algorithm is a well-ordered collection of
unambiguous and effectively computable
operations that produces a result and halts in
a finite amount of time
• …a well-ordered collection…
• …of unambiguous and effectively
computable operations…
• …that produces a result…
• …and halts in a finite amount of time
1/20/04
CS 100 - Lecture 2
15
CS 100 - Lecture 2
14
Definition of CS Reconsidered
Computer Science is the study of algorithms,
including:
• Their formal and mathematical properties
• Their hardware realizations
• Their linguistic realizations
• Their applications
1/20/04
Organization of Text
CS 100 - Lecture 2
16
Reading
• Read one chapter of S&G per week unless I
say otherwise
• Read ch. 2 for the next class
fig. from Schneider
& Gersting
1/20/04
CS 100
CS 100 - Lecture 2
17
1/20/04
CS 100 - Lecture 2
18
3
Download