Teaching the development of algorithms Teach skill, not only facts David Gries Computer Science Department Cornell University November 2004 - PowerPoint PPT Presentation

About This Presentation
Title:

Teaching the development of algorithms Teach skill, not only facts David Gries Computer Science Department Cornell University November 2004

Description:

It can revolutionize your teaching, because students can see you develop programs. ... DrJava is itself a Java program. ... Invariably, they couldn't. 2. ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 23
Provided by: tri5194
Category:

less

Transcript and Presenter's Notes

Title: Teaching the development of algorithms Teach skill, not only facts David Gries Computer Science Department Cornell University November 2004


1
Teaching the development of algorithmsTeach
skill, not only facts David GriesComputer
Science DepartmentCornell UniversityNovember
2004
DrJava is a free IDE for Java it has an
interactions pane into which one can type any
expression or statement and have it evaluated or
executed immediately. It can revolutionize your
teaching, because students can see you develop
programs. For DrJava, type in DrJava download
into google you should get a download site.
DrJava is itself a Java program.
The book Multimedia Introduction to Programming
Using Java by Gries Gries is published by
Springer Verlag. It comes with a CD that contains
over 250 recorded lectures with synched
animation. Great for teaching the development of
programs. An objects-and-classes first approach.
2
Nato Software Engineering Conference
1968Academicians and industrial people alike
admitted that We dont know what we are
doing.That conference sparked interest in
programming, programming methodology, formal
correctness ideas, software engineering
3
Late 1960s 1980
A Discipline of Programming
The Science of Programming
4
TodayWe have failed to move what we knew in
1980 about programming methodology into
thefirst and second programming courseswhere
they belong
5
The situation with todays texts
Pay lip service to stepwise refinement, but
dont really use it. Dont show developments
of programs. Many texts dont mention loop
invariant. Few texts use assertions and loop
invariants in a meaningful way. Fail to make
clear what the specification of a method is and
to adhere to its use throughout.
6
Consequences
Many CS students graduate without every
hearing the term assertion or invariant. Those
that have heard about them view them only as
academic exercises. Students dont have good
programming skills. Our teaching of basic
algorithms (searching, sorting, dealing with
links lists, etc.) is inefficient. We describe
loops by showing how they execute. Students
dont value practical mathematical underpinnings
of the field.
7
Algorithms are not memorable
With current practices, the only way for a
student to know an algorithm is to memorize its
code.Neither effective nor efficient!Suppose
we memorize code for selection sort of an
array bWe will still have difficulty writing an
algorithm for Selection sort of an array
segment bh..k Selection sort of an array
segment bh..k-1
Dont memorize code. Instead, learn to Develop
an algorithm from its specification
8
Specification of a method
A contract between the programmer of a method and
the user of the method.From the spec, a user
should know how to write calls on the
method.Precondition constraints on the
parameters.What the method does stated as a
postcondition, a command, or a formula (for a
function).
Formalism is not required. Precision and
thoroughness is.
9
Stepwise refinement
/ the English equivalent of n, for 0 lt n lt
1,000,000, e.g. anglicize(5004) five
thousand four /public static String
anglicize(int n)
Develop the method in class.
If at all possible, use a computer and an IDE and
show students incremental program development and
testing. I can demo this at the end if you want.
We should be teaching the thought processes
behind good program design and development.
10
Software engineering reason for annotating a loop
/ Sort array b /int k 0while (k !
b.length) int j index of minimum of
bk..b.length1 Swap bj and bk k k
1
Why is k set to 0 initially?
Why stop when k b.length?
Why does the loop body do its first two steps?
You cant tell until you have read all the
code!!!!
You cant tell until you have read all the
code!!!!
You cant tell until you have read all the
code!!!!
Without proper annotation, one must understand
the WHOLE LOOP before any part of it can be
understood.
11
loop with invariant
/ Sort array b /int k 0while (k !
b.length) int j index of minimum of
bk..b.length1 Swap bj and bk
k k 1
invariant is true before and after each iteration
1. Set k to 0 makes P true b0..1 is empty,
b0.. is whole array
2. When loop stops, b0..k1 whole array and is
sorted.
3. In each iteration, k k1 makes progress
toward termination.
4. Each iteration maintains the invariant.
With the invariant, we can understand the parts
separately
12
Find the invariant
/ Sort array b /
13
developing the loop from the invariant
/ Sort array b /
k 0
while ( k ! b.length)
j index of minimum of bk..b.length 1 Swap
bk and bj
Four loopy questions used to develop or
understand any loop
k k 1
1. How does it start?
2. When does it stop?
3. How does it make progress toward termination
4. How does it maintain the invariant?
14
Binary search
1. Dijkstra used to ask people to write one.
Invariably, they couldnt.2. Backhouse recently
found two texts with serious errors in their
binary search.3. Egghart Herwig in recent email
Asked 17 computer scientists to write a correct
binary search. Most couldnt.4. Mark Weiss says
in his text on data structures Binary search is
surprisingly tricky to code. Because he has no
methodology for developing algorithms or
understanding them.
15
Given sorted bh..k, store in t to truthifyR
bh..t x lt bt1..k
u
How does it start? Make left segment empty and
right segment empty t h1 u k1
When can it stop? When t 1 u.
How does it make progress? Decrease size of
middle segment.How does it keep invariant true?
16
Memorable develop it whenever you want.
If x in, finds rightmost occurrence
t h1 u k1while (t 1 ! u)
int e (tu) / 2// h1 t lt e lt u
k1
If x not in, finds where it belongs.
if (be lt x) t eelse u e
Works when bh..k is empty (h1 k)
Just as fast as other binary searches. WHY USE
OTHERS?
17
Dutch National Flag
contains red, white, blue
balls. Permute elements to produce R
18
Dutch National Flag
d h a h c k while (
)
a lt c
if (ba is red) Swap bd, ba d d 1
a a 1 else if (ba is white ) a a
1 else Swap ba, bc a a 1
19
On naming variables
1. The short names are perfectly reasonable,
because their meaning is clear from the invariant
picture.
2. Short names make the program shorter and more
readable.
3. Long names that try to incorporate meaning can
be wrong and misleading. instead of d use
firstWhite?
wrong if no whites
instead of d use firstNonRed?
wrong at end if all reds
20
Summary
We can teach students about program development.
The problem is that we have to change our way of
thinking first. Become more assertion oriented.
Become more development oriented. ASSERT
YOURSELVES!
Mark Twain Nothing needs changing so much as the
habits of others.
Textbooks need to be totally rewritten.
Rest of time Answer questions? Show you a
multimedia text that has the right
perspective? Show you development of
anglicize? Show you Dijkstras shortest path
algorithm on one slide?
21
Dijkstras shortest path algorithm
Nodes 0..n-1. Each edge has a positive
weight. Start node v. For each node n, store in
Ln the length of shortest path from v to n
Lv 0 Lu 7 Lw 10
2
4
v
u
1
4
3
w
3
22
Frontier F
Red R
For all w, Lw 8 Lv 0
F v R
Set R is not needed
while ( )
F !
f node in F with min L value Remove f from F,
add it to R
1. For red node r, Lr is length of shortest v
--gt r path.
for each edge (f,w)
if (Lw is 8) add w to F
2. All edges leaving R go to F.
if (Lf weight (f,w) lt Lw) Lw Lf
weight(f,w)
3. For node f, Lf is length of shortest v
--gt f path using red nodes (except for f)
4. For black node b, Lb 8
For a node f in F with min L value, Lf is
shortest path length
Write a Comment
User Comments (0)
About PowerShow.com