creating “algorithms” - computer sciencers/talks/algorithms.pdf · beyond manual drafting. ......

29
Creating “Algorithms” Robert Sedgewick Princeton University

Upload: phungtu

Post on 06-Feb-2018

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Creating “Algorithms”

Robert SedgewickPrinceton University

Page 2: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Third Edition 5graph algorithms

~500 pages

Third Edition 1-4basic/ADTs/sort/search

~700 pages

Second Edition~650 pages

Algorithms~550 pages

Java

C++

C

Java

C++

C

Modula-3C++C

Pascal

Pascal1982

2003

2001

2001

2002

1998

1997

1993199219901988

Brief history of books

Translations: Japanese, French, German, Spanish, Italian, Polish, Russian 20 years, 11 books, 17+ translations, 400,000+ copies in print

Page 3: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Ground rules for book authors

1. You are on your own

2. Deadlines exist

3. Content over form

4. Focus on the task at hand

5. Tell the truth about what you know

6. Revise, revise, revise

Page 4: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

First edition 1977-1982

Goals:Algorithms for the massesUse real code, not pseudocodeExploit computerized typesetting technology

Problems:Real code hard to find for many algorithmsLaser printers unavailable outside research labslow resolutionsoftware to create figures?

Approach:emacs + TeX for textpen-and-ink for figures

Page 5: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

1977 historical context

Page 6: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

not enough G’s in Paris

First edition features:phototypeset final copyreal Pascal codepen-and-ink drawings

Page 7: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

loom

Goal: All the book’s code should be real code.

Problems: Pascal compiler expects code in .p fileTeX formatter expects code in .tex fileNot all the code goes into the bookCode has to be formattedContinually need to fix bugs and test fixes

Solution:Add comments in .p files to id and name code fragmentsAdd “include” lines to source that refer to namesloom: shell script to build .tex file

Page 8: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

loom example (1st edition)Text (.loom file)

program example(input,output);var a: array[1..100] of integer; N,i: integer;{include code}procedure solve; var i,j,t: integer; begin ... end;{end include code}begin...solve;...end.

\Sh{Example program}One of the simplest algorithms for this task works as follows: firstdo this, then do that. This codedoes this and that:\prog{%include example.p code}\noindentThis algorithm is sometimes useful.Its running time is proportional to... loom

Typescript (.tex file)\Sh{Example program}One of the simplest algorithms for this task works as follows: first do this, then do that. This code does this and that:\prog{procedure solve; var i,j,t: integer; begin ... end;}\noindentThis algorithm is sometimes useful.Its running time is proportional to...

Program (.p file)

Page 9: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Second edition 1986-87

Goals:Make content more widely accessibleEliminate pen-and-inkAdd visual representations of data structures

Problem: Figures are numerous and intricate

Opportunities:LaserWriter + PostScriptAlgorithm animation research

Approach:Add introductory material; move math algs to enddsdraw: package for drawing data structuresfig: use loom to include program output in figs

Page 10: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

dsdraw

PostScript code to draw data structuresbasic graphicsautomatic layout of snapshots

Ex: points in the plane/points % Points in the plane % Stack: array containing the points ([label,x,y] for each node). % (Example: [[(C) 1 3] [(B) 2 5] [(D) 3 5] [(A) 3 1]]) % Optional fourth argument can change nodestyle % Put a dummy point [N M] to fool (size) (?){/option exch def option (size) eq {dup /xmax 0 def /ymax 0 def {aload length 4 eq {pop} if dup ymax gt {/ymax exch def}{pop} ifelse dup xmax gt {/xmax exch def}{pop} ifelse pop} forall xmax ymax} if option (plot) eq {{aload length 3 eq {nodestyle} if drawnode} forall} if } def

Page 11: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

dsdraw: basic data structure drawings

[[[(X)] [(T) A][(P)] [(G)][(S) A][(O)][(N)] [(A)][(E)][(R) A][(A)][(I)][(M)]]](completetree)

[[[[(A) 1 7][(B) 2 5][(C) 4 5][(D) 2 3][(E) 4 3] [(F) 1 1][(G) 6 5][(H) 8 6][(I) 10 6][(J) 8 3] [(K) 10 3][(L) 8 1][(M) 10 1]] [[() 1 7][() 1 2][() 1 3][() 12 13][() 10 13] [() 10 12][() 10 11][() 5 4][() 6 4][() 8 9] [() 6 5][() 1 6][() 7 5]]]](graph)

[[...]](polygon)

permutationarray of ints2D arraypoints

completetreetreepolygongraph

Page 12: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

fig

Goal: Use programs to produce figures

Problem: figures are PostScript programs

Opportunities:loom

Solution:instrument Pascal code to produce .ps codeuse loom to include program output in .ps files

(filter out instrumentation)include refs to .ps files in .tex files

Page 13: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

fig example (2nd edition)Text (.loom file)

...{include code}procedure solve; var i,j,t: integer; begin ...{IE} for i:= l to r do{IE } write(a[i]:4); ... end;{end include code}...

\Sh{Example program}One of the simplest algorithms for this task works as follows: firstdo this, then do that. This codedoes this and that:\prog{%include example.p code | grep -v IE}\noindentThis algorithm is sometimes useful.This figure shows how it works:\fig{... psfile: fig1.ps ...}... loom

Typescript (.tex file)\Sh{Example program}One of the simplest algorithms for this task works as follows: first do this, then do that. This code does this and that:\prog{procedure solve; var i,j,t: integer; begin ... end;}\noindentThis algorithm is sometimes useful.This figure shows how it works:\fig{... psfile: fig1.ps ...}...Program (.p file)

[[127 125 126 124 115 117 122 123 ... ]](permutation) showdata

Figure (.ps file)

Note: can use loom here, too!

Page 14: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

dsdraw: automatic layout of snapshots

[[[(A)][(S)][(O)][(R)][(T)] [(I)][(N) A][(G)][(E)][(X)] [(A)][(M)][(P)][(L) B][(E) B]][[(A)][(S)][(O)][(R)][(T)] [(P) A][(N)][(G)][(E)][(X)] [(A)][(M) B][(I) A][(L)][(E)]][[(A)][(S)][(O)][(R)][(X) A] [(P)][(N)][(G)][(E)][(T) A] [(A) B][(M)][(I)][(L)][(E)]][[(A)][(S)][(O)][(R) A][(X)] [(P)][(N)][(G) B][(E) B][(T)] [(A)][(M)][(I)][(L)][(E)]][[(A)][(S)][(P) A][(R)][(X)] [(O) A][(N) B][(G)][(E)][(T)] [(A)][(M)][(I)][(L)][(E)]][[(A)][(X) A][(P)][(R) B][(T) A] [(O)][(N)][(G)][(E)][(S) A] [(A) B][(M)][(I)][(L)][(E)]][[(X) A][(T) A][(P) B][(R) B] [(S) A][(O)][(N)][(G)][(E)] [(A) A][(A) B][(M)][(I)][(L)] [(E)]]]

(completetree)

Page 15: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Beyond manual drafting

Page 16: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Second edition features

Algorithms for the masses

Uses real code, not pseudocode

Fully exploits technology

Original goals realized, PLUS

Innovative, detailed visualizations

Done?

Page 17: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Other languages (1990-1993)

Mandate: Spread the word in other programming languages

Challenges: Which languages? (Answer: C, C++, and Modula-3)Who translates? Early versions of new languages are unstable

Solution:Copy-and-edit to implement programs in new languageUse conditionals in typescript for language-dependent text

Problems:(figs were produced by Pascal programs)difficult to take advantage of language featurestypescript is a mess; layout is painful

Page 18: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Third edition 1993-Goals:

Full coverage, not summaryTake visualizations to next levelAnalyses with empirical verification

Challenges:Typescript filled with conditionalsProgram code filled with instrumentationfigs made with Pascal codeMany algorithms not well-understood

Approach:START OVER, one language at a time

Status: 9 books, 6 done

Algorithms inCC+

Java

Parts 1-4Part 5

Parts 6-8

Page 19: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Layout:Structured text, figures, exercises, programs, tablesMultiple story flows (figs with captions in margins)

Figures:Direct PostScript implementationsVisualize “large” examplesExplanatory captions

Programs:Full implementations to support empirical studiesEmphasize ADTs in all languagesUse consultants to champion language features

Exercises:All questions addressed

Tables:Summarize full empirical studies

Starting over (third edition)

Page 20: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

PostScript as algorithm visualization tool

/insert { /X rand 1000 idiv N mod def /N N 1 add def /sum 0 def /a [ 0 1 a length 1 sub { a exch get /nd exch def X sum ge X sum nd add lt and { nd 1 add M 1 add ge { M 1 add 2 div dup /S S 1 add def } { nd 1 add } ifelse } { nd } ifelse /sum sum nd add def } for ] def } def

/doit { /a [ M ] def showline Nmax { insert showline } repeat } def

Page 21: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Third edition featuresprograms

C, C++, Javafigures

dsdrawndirect

tablesempiricalsummaries

exercises(1000s)

properties(theorems)

layout designlinks**

** not enough (stay tuned)

Page 22: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Creating “Algorithms”

programs

tables

Java

G R A P H P R O P E R T I E S A N D T Y P E S Many computational applications naturally involve not just a set of items, but also a set of connections between pairs of those items. The relationships implied by these connections lead immediately to a host of natural questions: Is there a way to get from one item to another by following the connections? How many other items can be reached from a given item? What is the best way to get from this item to this other item? To model such situations, we use abstract objects calledgraphs. In this chapter, we examine basic properties of graphs indetail, setting the stage for us to study a variety of algorithms thatare useful for answering questions of the type just posed. Thesealgorithms make effective use of many of the computational tools thatwe considered in Parts 1--4. They also serve as the basis forattacking problems in important applications whose solution we couldnot even contemplate without good algorithmic technology....

text sections

program euclid(input,output);var x,y: integer;function gcd(u,v:integer): integer; begin if v=0 then gcd:=u else gcd:=gcd(v, u mod v) end;beginwhile not eof do begin readln(x,y); if x<0 then x:=-x; if y<0 then y:=-y; writeln(x,y,gcd(x,y)); end;end.

figures

Write a representation-independent graph-initialization ADT function that, given an array of edges, returns a graph.

exercises

BookmakerEnglish

Page 23: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Bookmaker (the lonely author)

TeX

.psemacs

.pdf

Unix

juggler image from Northern Lights Software

Page 24: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Facts and figures

0

0

Third edition 1-8 (est.)

Third edition 1-5 (typical)

Second edition(typical)

Algorithms

filesexercisestablesfiguresprogramspages

40,000

25,000

6,000

600

3,500

2,000

400

400

120

75

800

500

350

150

400

250

200

140

2000

1200

650

550

Page 25: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

digression: PostScript as math visualization tool

/doit{ /M exch def /Nmax exch def /A [0 M 1 sub M div 1 M div 0] def 3 1 Nmax { /N exch def [ 0 1 1 N { /k exch def A k 1 sub get M div A k get M 1 sub mul M div add } for 0 ] /A exch def A drawcurve } for} def

Page 26: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Fourth edition 2003-??

Goals:Do answers to exercisesStabilize contentCreate interactive and dynamic eBook supplements

Problems:Tens of thousands of filesThousands of exercisesDifferent typescripts for C, C++, JavaDeep hacks throughout figs (need new dsdraw)Ancient typesetting engine

Approach:Back to single typescript??Layout language??Scripting language??

Page 27: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Needs for fourth edition

1. Structured-document authoring and editing toolsimple system- and machine- independent editormanage nonlinear organization of fragmentsTeX-like plugin for equationsapplication-independent primary source formatcross-reference/indexing across all types of fragments

2. Programming toolsSource language with flexible ADT and IO mechanismsPostscript

3. Flexible document-creation enginesemiautomatic layoutprogramming languagesmart filters with link/embed/unlink/unembed

Page 28: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Inventing the Future

Q: Where is the “Algs” e-/dynamic-/interactive- book?

A: (1984): Done. Balsa (with M. Brown).

1985 choice: content over form

Triumph of content leads to (reasonable) demand for:Answers to exercisesOnline lecture notesCustomizable versionsDynamic figuresInteractive testing/drill...

Page 29: Creating “Algorithms” - Computer Sciencers/talks/algorithms.pdf · Beyond manual drafting. ... PostScript as algorithm visualization tool /insert ... Creating “Algorithms”

Inventing the Future

Q: Where is the “Algs” e-/dynamic-/interactive- book?

A: (2002): Where are the tools that an individual author could use to make one??