Teaching the Principles of Computation Tom Cortina Carnegie Mellon University

Download Report

Transcript Teaching the Principles of Computation Tom Cortina Carnegie Mellon University

Teaching the Principles
of Computation
Tom Cortina
Carnegie Mellon University
1
Introducing CS to students

What is Computer Science (CS)?

Students see CS as computer programming.




"The more languages you know, the better you are as
a computer scientist."
"The more you know about one language, the better
you are as a computer scientist."
"All computer scientists write C programs."
If students are going to take only one course
introducing them to computer science, should
it be Programming in Java?
2
Re-think Intro to CS


15-105 Principles of Computation
http://www.cs.cmu.edu/~tcortina/15-105
Algorithmics: The Spirit of Computing
(3rd Edition)
by David Harel with Yishai Feldman
Publisher: Addison-Wesley (2004)
ISBN: 0321117840
3
History of Computing


What historical events brought us to the
modern computer?
When did the term computer originate?
The Oxford English Dictionary still describes a computer as
"a person employed to make calculations in an observatory, in surveying, etc."
(atariarchives.org)
4
The Early Days of Computing
Pascaline (1643)
Abacus
(500 BC)
Napier's
Bones
(1617)
Leibniz' step reckoner (1674)
5
Mechanical Computing
Jacquard's Loom
(1801)
Hollerith's
Census Collator (1890)
Charles Babbage's
Difference Engine (1832)
6
Electronic Computing
ARPANET
ENIAC
(1946)
UNIVAC I
(1951)
Personal Computer
(1981)
7
Faces of Computing
3
2
6
5
4
1
7
11
8
9
10
12
13
19
14
15
16
17
18
20
1 Allen
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Alan
Paul
John
Meg
Herb
Konrad
Charles
Bill
Douglas
Ada
Adm.
John
J.C.R.
Gordon
Herman
Steve
Larry
Sergey
Gates
Whitman
Allen
Turing
Simon
Atanasoff
Newell
Lovelace
von
Grace
Page
Jobs
Wozniak
Licklider
Zuse
Babbage
Brin
Moore
Hollerith
Engelbart
Neumann
Hopper
8
Algorithms:
Specifying Computation
• An algorithm is “a precise rule (or set of rules)
specifying how to solve some problem.”
(thefreedictionary.com)
• Algorithms in real life:
• Recipes
• Completing a Tax Form
• Knitting
• Have students go out and find algorithms in
their everyday lives!
9
GCD Algorithm
• Greatest Common Divisor (GCD) by the
Greek mathematician Euclid between 400
and 300 B.C.
• Algorithm to find the GCD of x and y:
1. Input x
2. Input y
3. While y is not 0, do the following:
a. Set x' equal to y
b. Set y' equal to x modulo y
c. Set x = x' and y = y'
4. Output x as the GCD
10
Visualizing Computations
using RAPTOR




Developed by Martin Carlisle and others at
the US Air Force Academy.
Uses flowcharts to allow students to build
computations and simulate them with almost
no syntax.
Includes support for input, conditionals,
loops, subroutines, graphics, sound, text
output
New version included on CS4HS CD
11
Data Structures






Arrays (Vectors, Matrices)
Linked Lists
Stacks
Queues
(Binary) Trees
Graphs
12
Syntax Diagrams


Students learn that computer scientists have
invented various ways to explicitly describe complex
rules of syntax and semantics.
A valid variable name is a string that starts with a
letter and is followed by any number of letters or
digits in any combination.
VARIABLE
LETTER
LETTER
DIGIT
13
Programming Languages

Why are there so many programming
languages?
14
Programming Languages

Look at all the programming languages!
15
Recursion & Algorithmic
Techniques

Students learn that computer science
effectively uses recursion to express
solutions to complex problems in a very
concise manner.
How many moves to
solve this puzzle for n discs?
2n - 1
X
Y
Towers of Hanoi
Z
For n = 64, if the monks move
1 disc/sec, it would take them
about 585 billion years to complete!
16
Recursion in Scheme
(define john (list 'Doe 85000
'Senior-Accountant 'Accounting))
(define jane (list 'Smith 97000
'Manager 'Web-Services))
(define (salary employee)
(first (rest employee)))
(salary john)
85000
john
Doe
first
85000
Senior-Accountant
Accounting
rest
17
Recursion in Scheme (cont'd)
(define (sum-salaries employees)
(if (null? employees)
0
(+ (salary (first employees))
(sum-salaries (rest employees)))))
(sum-salaries (list john jane mike))
Doe
85000
Senior-Acc.
Accounting
Smith
97000
Manager
WebSrvc
Brown
70000
Programmer
Systems
18
Recursion in Prolog
parent(alice, carol).
parent(bob, carol).
parent(bob, david).
parent(carol, ethel).
parent(carol, fred).
parent(fred, gayle).
alice
bob
carol
ethel
david
fred
predecessor(X, Z) :gayle
parent(X, Z).
predecessor(X, Z) :parent(X, Y), predecessor(Y, Z).
?- predecessor(alice, gayle).
yes
19
Correctness

How do we know if a computation is correct?
Why is this important?
Mariner I

AT&T Bug
Scud Missiles
Denver Airport
Loop invariants


Therac-25
Example: Reversing a string.
Induction

Example: Towers of Hanoi
20
Efficiency


Order of Complexity

Determine the relative rate of growth of the number
of computations as a function of the number of data
values processed (usually expressed using N)

Example: N, N + 5, 3N - 2, 6N + log2N are all O(N).
Assume an algorithm requires N data values to
process. If each operation takes 1 μs (1 millionth of a
second) to execute, how many μs will it take to run
the algorithm on 100 data values if the algorithm has
the following number of operations?
N, N log2N, N2, N3, N4, 2N, N!, NN
21
Comparing Algorithms (cont'd)
Number of Computations Execution Time
N
N log2N
N2
N3
N4
2N
N!
NN
100 μs
665 μs
10,000 μs = 0.01 sec
1,000,000 μs = 1 sec
100,000,000 μs = 1 min 40 sec
> 1030 μs = 3.5 quintillion centuries!
> 10160 μs
> 10201 μs
The number of protons in the known universe is < 1079
The number of microseconds since the Big Bang is < 1024.
22
Monkey Puzzle Problem

Given:




A set of N square cards whose sides are imprinted
with the upper and lower halves of colored
monkeys.
N is a square number, such that N = M2.
Cards cannot be rotated.
Problem:

Determine if an arrangement of the N cards in an
M X M grid exists such that each adjacent pair of
cards display the upper and lower half of a
monkey of the same color.
Source: www.dwheeler.com (2002)
23
Example
Images from: Simonas Šaltenis, Aalborg University, [email protected]
24
Analysis
If there are N = 9 cards (M = 3):
To fill the first cell, we have 9 card choices.
To fill the second cell, we have 8 card choices
left.
To fill the third cell, we have 7 card choices
remaining.
etc.
The total number of unique arrangements for N = 9 cards is:
9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 9! = 362,880
The total number of touching cards we need to check
for N = 9 cards is 12.
25
Intractability
For N cards, the number of arrangements to examine
is O(N!)
If we can analyze one arrangement in a millisecond:
N
Time to analyze all arrangements
9
362,880 ms ≈ 363 s ≈ 6 min
16
20,922,789,888,000 ms ≈ 663 years
25
15,511,210,043,330,985,984,000,000 ms
≈ 500 quadrillion centuries
26
Map Coloring
Given a map of N territories, can the map be colored using 3 colors
such that no two adjacent territories are colored with the same color?
27
Traveling Salesperson
12
B
10
7
8
C
6
7
F
4
D
9
A
7
E
3
5
G
11
Is there a route with cost at most 52?
Is there a route with cost at most 48?
28
Computability



The class P consists of all those decision problems
LOOK BEHIND
that can be solved on a deterministic
sequential
YOU, HOMER!
machine in an amount of time that is polynomial in
the size of the input
The class NP consists of all those decision problems
whose positive solutions can be verified in
polynomial time.
based on definitions from Wikipedia
The Clay Mathematics Institute is offering a $1M
prize for the first person to prove P = NP or
P ≠ NP. (http://www.claymath.org/millennium/P_vs_NP/)
29
Word Correspondence
Given a finite alphabet (e.g. {a, b})
Consider two groups (call them X and Y) of N words
made from the alphabet.
X
Y
1
abb
bbab
2
a
aa
3
bab
ab
4
baba
aa
5
aba
a
Is there a way to concatenate words in the same
sequence from X and Y to form the same word?
30
Word Correspondence
Sequence: 2, 1, 1, 4, 1, 5
X: a + abb + abb + baba + abb + aba
Y: aa + bbab + bbab + aa + bbab + a
Both sequences yield aabbabbbabaabbaba
Answer: yes
X
Y
1
abb
bbab
2
a
aa
3
bab
ab
4
baba
aa
5
aba
a
31
Word Correspondence
Consider the following word correspondence problem.
Is there a solution this time?
X
Y
1
bb
bab
2
a
aa
3
bab
ab
4
baba
aa
5
aba
a
32
Undecidability &
Noncomputability



A problem that has no general algorithmic solution is
called noncomputable.
If the noncomputable algorithm requires a yes/no
answer, the problem is called undecidable.
The word correspondence problem is undecidable.


For any set of n pairs of words for a finite alphabet of
any size, there is no general algorithm that can
determine if there is a valid correspondence or not.
The halting problem is another example.
33
Turing Machines
Introduces state diagrams to students.
Students look at computation from a "hardware" point
of view.


Turing Machine
#/#, L
1
#/#, R
2
0/0,R
1/0,R
Alphabet =
{0, 1}
3
State
Transition
Diagram
0/0,L
1/0,L
Read/Write
Head
Tape
... #
#
1
1
1
0
1
1
0
0
0
0
0
0
0
1
#
# ...
34
Parallel Computation
Assuming the instructions are run on separate
processors as shown, and X is initially 0, what possible
values can be stored in X once the instructions are
complete?
Processor A
❶X=X+5
❷X=X*2
Processor B
❶X=X+6
❷X=X*3
Processor C
❶X=X+7
❷X=X*4
(Assume each instruction is an atomic operation - it runs
in its entirety before another instruction is executed.)
35
Distributed Computing

The Dining Philosophers Problem
in the real world
Cars may not make turns through this
intersection; they may only proceed
straight ahead. When a car stops at this
intersection, it can proceed straight ahead
if there is no car to its left and to its right.
Otherwise it must wait some random
amount of seconds and then check again
to see if it's safe to proceed. If not, it waits
again, and so on.
deadlock? starvation?
36
Artificial Intelligence





What does it mean for a machine to be
intelligent?
Allen Newell and Herbert Simon (from CMU)
contributed to one of the first AI programs, the
General Problem Solver (GPS) in 1957.
Turing Test
Chinese Room Argument
Weak vs. Strong AI
Turing Test
37
Tic-Tac-Toe: Game Tree
X
X
X
X
X
X
X
O
O
X
X
X
X
O
X
O
X
X
O
X
O
X
O
X
O
38
Tic Tac Toe




How many configurations are on the 1st level of the
tree (below the root)?
How many configurations are on the 2nd level of the
tree?
What kind of bound can we give for the size of the
tree?
Game trees are usually extremely large, so game
programs also use heuristics to narrow the search
space in the tree that must be examined.
39
Deep Blue


IBM's "Deep Blue" computer beats Gary Kasparov
in a chess match in 1997.
Heuristics values:





For more info:


The value of each piece. (1 for pawn up to 9 for queen)
The amount of control each side has over the board.
The safety of the king.
The quickness that pieces move into fighting position.
http://www.research.ibm.com/deepblue/home/html/b.html
Is Deep Blue intelligent?
40
The future of computing
41
Futuristic Views
Robot: Mere Machine
to Transcendent Mind
Hans Moravec
The Future of Ideas
Lawrence Lessig
The Singularity is Near
Ray Kurzweil
Flesh and
Machines
Rodney A. Brooks
Nanofuture
J. Storrs Hall
42
CS ≠ Java



We can show students that computer science
has much more depth than just learning how
to program in Java.
We need an AP course that covers broader
principles of computing.
This is AP Computer Science!


The current AP course is really AP Computer
Programming.
SPREAD THE WORD!
43