Lecture 2 Introduction to Computer Science (continued) 5/29/2016

advertisement
Lecture 2
Introduction to Computer Science
(continued)
5/29/2016
CS 100 - Lecture 2
1
Ways Algorithms can be
Constructed
• Sequential construction
• Conditional construction
• Iterative construction
5/29/2016
CS 100 - Lecture 2
2
Sequential Construction
first do this
then do this
5/29/2016
CS 100 - Lecture 2
3
Conditional Construction
test
something
either do this
or do this
continue
5/29/2016
CS 100 - Lecture 2
4
Iterative Construction
do something
if not, repeat
test if done
if done, continue
5/29/2016
CS 100 - Lecture 2
5
Developing an Algorithm
• 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
• Data structures:
– Shows how to organize information
– Reveals what additional information is required
5/29/2016
CS 100 - Lecture 2
6
Developing Decimal Addition
Algorithm: Example
5/29/2016
carry
0
1
1
8
1
4
7
7
2
9
5
1
15
04
14
12
1
5
4
4
2
1
CS 100 - Lecture 2
7
Developing Decimal Addition
Algorithm: More General Case
c
c
c
0
a3
a2
a1
a0
b3
b2
b1
b0
c4
cc3
cc2
cc1
cc0
c4
c3
c2
c1
c0
c
5/29/2016
CS 100 - Lecture 2
carry
8
Algorithm for Adding
Two m-Digit Numbers
Given m 1
Two positive numbers of m digits,
am1am2 a1a0 and bm1bm2 b1b0 .
Wanted : c m c m1
where c m c m1
5/29/2016
c1c 0,
c1c 0 = am1am2
CS 100 - Lecture 2
a1a0   bm1bm2
b1b0 
9
Algorithm
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.
a)
b)
c)
d)
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.
4. Set cm to value of c.
5. Print answer cmcm–1…c1c0.
5/29/2016
CS 100 - Lecture 2
10
Importance of Algorithms
If you can specify an algorithm to solve a
problem, then you can automate its solution
5/29/2016
CS 100 - Lecture 2
11
Requirements for
Automated Computation
• 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
5/29/2016
CS 100 - Lecture 2
12
Algorithmic Problem Solving
• Are there algorithmic solutions for all problems?
– No! Gödel discovered algorithmically unsolvable
problems in 1930s
• Can algorithmically solvable problems be solved
efficiently?
– 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
5/29/2016
CS 100 - Lecture 2
13
Non-Algorithmic
Information Processing
• “Cognitive inversion”: many things that are
easy for people are hard for computers, and
vice versa
– e.g., face recognition vs.calculating
• Suggests that computers & brains work in
very different ways
• Much research in non-algorithmic
information processing in neural networks
5/29/2016
CS 100 - Lecture 2
14
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
5/29/2016
CS 100 - Lecture 2
15
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
5/29/2016
CS 100 - Lecture 2
16
Organization of Text
fig. from Schneider
& Gersting
5/29/2016
CS 100 - Lecture 2
17
Reading
• Read one chapter of S&G per week unless I
say otherwise
• Read ch. 2 for the next class
5/29/2016
CS 100 - Lecture 2
18
Download