### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# Introduction to Computing Explorations in Language, Logic, and Machines CS 1120

UVA

GPA 3.71

### View Full Document

## 7

## 0

## Popular in Course

## Popular in ComputerScienence

This 216 page Class Notes was uploaded by Mrs. Carolyne Abbott on Monday September 21, 2015. The Class Notes belongs to CS 1120 at University of Virginia taught by David Evans in Fall. Since its upload, it has received 7 views. For similar materials see /class/209651/cs-1120-university-of-virginia in ComputerScienence at University of Virginia.

## Reviews for Introduction to Computing Explorations in Language, Logic, and Machines

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/21/15

Class 33 Computing with Photons and Life Color Plate 1 Early model of the Tinkenoy computer n display at The Computer Museum Boston also plays ticrtacLoe From The Tinertoy Computer and Other Machnations by A K Dewdney c 70 1Wglance CS 150 Computer Science University of Virginia David Evans Computer Science httpwwwcsvirginiaeduevans Turing Machines and Complexity Stronger version Complexity classes P NP and NPcomplete are defined for Turing machine steps but apply identically to all normal computers 0 Today Abnormal Computers Mghtchange what is computable probably don t Do change what a normal step is w A C5150 Fall 2005 Lecture 33 Alternate Computing Models Computer Selence 4n IlvLil l ll3931quotrlRil Generalized Normal Steps 0 Require a constant amount of time 0 Perform a fixed amount of work Localized Cannot scale indefinitely with input size C5150 Fall 2005 Lecture 33 Alternate Computing Models U1 Computer Scienc quotHfEr UVIVI39HSHY 4V1me ChurchTuring Thesis 0 Church s original 1935 Lambda calculus is equivalent to real world computers can compute any computable function Turing s version Every function which would naturally be regarded as computable can be computed by a Turing machine Generalized version Any computation that can be done by an algorithm can be done by a mechanical computer All normal computers are equivalent in computing power C5150 Fall 2005 Lecture 33 Alternate Computing Models N quot Computer Science m A UNl39l Rgtll LfVIRtinH Normal Steps 0 Turing machine Read one square on tape follow one FSM transition rule write one square on tape move tape head one square 0 Lambda calculus One beta reduction 0 Your PC Execute one instruction o What one instruction does varies h A C5150 Fall 2005 Lecture 33 Alternate Computing Models Computer Selence ill II an lausm39 3i Vmuixn Abnormal Imaginary Computer 0 Accelerating TM Like a regular TM except the first step takes 1 second second step takes 12 second third step takes 14 second nth step takes 12n second 0 Is our Accelerating TM more powerful than a regular TM C5150 Fall 2005 Lecture 33 Alternate Computing Models m A quotquot Com uter Selence Plum UNlYlR 39 391 m gr39Vikmx Quantum Physics for Dummies Light behaves like both a wave and a mamF 2391 2155wa particle at the same time A single photon is in many states at once Can t observe its state without forcing it into one state Schrodinger s Cat Put a live cat in a box with cyanide vial that opens depending on quantum state Cat is both dead and alive at the same time until you open the box Quantum Computing Feynman 1982 0 Quantum particles are in all possible states 0 Can try lots of possible computations at once with the same particles In theory can test all possible factorizationskeyspathsetc and get the nghtone In practice very hard to keep states entangled once disturbed must be in just one possible state A CS150 Fall 2005 Lecture 33 Alternate Computing Models 7 COI pUtgf Selegce n 1quot NIYICRS39I T or lRUlNIA CS150 Fall 2005 Lecture 33 Alternate Computing Models 8 M Cquot Qubit Regular bit either a O or a 1 0 Quantum bit 0 1 or in between p probability it is a 1 o A single qubit is in 2 possible states at once If you have 7 bits you can represent any one of 27 different states If you have 7 qubits you have 27 different states at once Quantum Computers Today 0 Several quantum algorithms Shor s algorithm factoring using a quantum computer Actual quantum computers 5qubit computer built by IBM 2001 5 3 Implemented Shor s algorithm to factor 0 World s most complex quantum computation Los Alamos has built a 7qubit computer To exceed practical normal computing need gt 30 qubits A CS150 Fall 2005 Lecture 33 Alternate Computing Models 9 Computgr SClCPCC nm Nl39iRSl l39Y af v iRmxH cs1soFau2wsAddiaguanethannqubit is more tha Complexity for Quantum Computer 0 Complexity classes are different than for regular computers because a step is different 0 Quantum computer each step can take both possible decisions at once Means a quantum computer is a nondeterministic computer It can solve problems in class NP in polynomial time Number of qubits you need 0 What matters Number of nondeterministic steps Computing with DNA Leonard Adleman Mathematical Consultant for Sneakers 1995 CS150 Fall 2005 Lecture 33 Alternate Computing Models 11 Computgr SCnglCC 1 Mr NJVICRSI T of lRlNlc A CS150 Fall 2005 Lecture 33 Alternate Computing Models 12 Computgr Selegce IN1 xn39mest n 3139 mow DNA 0 Sequence of nucleotides adenine A guanine G cytosine C and thymine T 0 Two strands A must attach to T and G must attach to C Hamiltonian Path Problem 0 Input a graph start vertex and end veltex Output either a path from stait to end that touches each vertex in the graph exactly once or frallcse indicating no such path exists start CH0 CH0 BWI end BWI How hard is the IAD Hamiltonian path problem L C5150 Fall 2005 Lecture 33 Alternate Computing Models 13 Compqucr Scxoncc wwmnwmw C5150 Fall 2005 Lecture 33 Alternate Computing Models 4 Computh Sticncc u Mmkmnnirwo H Encoding The Graph 0 Make up a two random 4nucleotide sequences for each city CHO CHOl AC39I39I39 CHOZ gcag RIC RICl TCGG RICZ actg IAD IAD1 GGCT IAD2 atgt BWI BWI1 GATC BWI2 tcca o If there is a link between two cities A gtB create a nucleotide sequence A2 Bl CHO gtRIC gcagTCGG SEZejeft itt Wmd39s nilp www mine oigieseaimnanoleonnapgo H O odioriidna niml Encoding The Problem 0 Each city nucleotide sequence binds with its complement A lt gt T G lt gt C CHO CH01 ACTT CHOZ gcag CHO TGAA cgtc RIC TCGGactg RIC AGCCtgac IAD GGCTatgt IAD CCGAtaca BWI GATthca BWI CTAGaggt 0 Mix up all the link and complement DNA strands they will bind to show a path CSl50 Fall 2005 Lecture 33 Alternate Computing Models H 5 am C mmt35 slrasa C5150 Fall 2005 Lecture 33 Alte nate Computing Models 16 m Path Binding CHO IAD RIC BWI TGAACgtcCCGAtacaAGCCtgacCTAGaggt llll llllllll llllllll llll gcagGGCTatgtTCGG actgGATC CHO gtIAD IAD gtRIC RIC gtBWI TCGGactg RIC CHO BWI ACTTgcag GATthca IAD GGCTatgt Getting the Solution 0 Extract DNA strands staiting with CH0 and ending with BWI Easy way is to remove all strands that do not start with CH0 and then remove all strands that do not end with BWI 0 Measure remaining strands to find ones with the right weight 7 8 nucleotides 0 Read the sequence from one of these strands C5150 Fall 2005 Lecture 33 Alternate Computing Models 17 gm Cmmra essm A C5150 Fall 2005 Lecture 33 Alte nate Computing Models 13 quotml Comput r SCICPCC my on n a mm L tur RH Pgra m iquot frtquot Reminder Start thinking of ideas of PS9 and discussing them on the forum httDwwwcsviroiniaeduforumsviewforumDhDf28 You can also vote in the should we have a quiz Mondayquot pol l htt wwwcsvnr Inlaedu forums viewto ic h t1651 httbwwwsoontslinecomcollegebasketbaquotscoreboard A Lecture 23 Programming with Objects 2 ProblemSolving Strategies PSlPS4 Functional Programming Focused on procedures Break a problem into procedures that can be combined to solve it 0 PS5 Imperative Programming Focused on data Design data for representing a problem and procedures for updating that data ProblemSolving Strategies 0 PS6 ObjectOriented Programming Focused on objects package procedures and state Model a problem by dividing it into objects Lots of problems in real and imaginary worlds can be thought of this way A Lecture 23 Programming with Objects 3 A Lecture 23 Programming with Objects 4 Counter Object define makecounter let count 0 Instance varable lambda message cond eq message reset Methods set count 0 eq message next set count 1 count eq message current count else error quotUnrecognized messagequot Defining ask ask Object Met700 gt define bcounter makecounter gt ask bcounter 39current 0 gt ask bcounter 39next gt ask bcounter 39current A Lecture 23 Programming with Objects 5 1 define ask object message object message Lecture 23 Programming with Objects 6 Elli sssss v sssss 1 Inheritance Lecture 23 Programming with Objects 7 A 39 39A C m r 1 nce m 0 PBFENH ERH makefraction define makefraction lambda numerator denominator lambda message cond eq message 39value lambda self numerator denominator eq message 39add lambda self other ask self 39value ask other 39value eq message getnumerator lambda self numerator eq message getdenominator lambda self denominator Same as in makenumber Note our add method evaluates to a number not a fraction object There are many kinds of numbers 0 Whole Numbers 0 1 2 o Integers 23 73 0 o Fractions 12 78 o Floating Point 23 00004 314159 0 But they can t all do the same things We can get the denominator of a fraction but not of an integer A Lecture 23 Programming with Objects 8 Why is redefining add a bad thing 0 Cutandpaste is easy but 0 There could be lots of number methods subtract multiply print etc 0 Making the code bigger makes it harder to understand 0 If we fix a problem in the number add method we have to remember to fix the copy in makefraction also and real complex float etc which would be better A Lecture 23 Programming with Objects 9 Comput5 c1etcei W 1 gt my H 1 ma kefraction define makefraction numer denom let super makenumber f lambda message cond eq message 39value lambda self numer denom eq message 39getdenominator lambda self denom eq message 39getnumerator lambda self numer else super message Lecture 23 Programming with Objects 11 r Lecture 23 Programming with Objects 10 A 39A m t I 1 nc MCO Making Subobjects define makefraction numer denom makesubobject makenumber f lambda message cond eq message 39value lambda self numer denom eq message 39getdenominator lambda self denom eq message 39getnumerator lambda self numer else f A Lecture 23 Programming with Objects 12 Implementing makesubobject define makesubobject super imp lambda message if eq message super lambda self super let method imp message if method method su per messag e lulure 2 pm mmmm my will clued 13 we I ComRuteI L gt trace ask gt trace eq gt ask half 39add half ask ltproceduregt add 12ltproceduregt eq add value I f ask ltprooeduregt value eq add getdenominator 3 value value I eq add getnumerator I 1 ask ltprooeduregt value Using Fractions gt define half makefraction 1 2 gt ask half 39value gt ask half 39getdenominator gt ask half Iadd makenumber1 gt ask half Iadd half 1 p mmmm W 4 compw ma kenumber ma kefra ction gt ask half 39add half ask ltproceduregt add 12ltproceduregt eq add value I ask ltprooeduregt value eq add getdenominator 3551 Value Value t eq add getnurrerator I 12 ask ltprooeduregt value eq add value if value value eq add add I 12 t 1 1 p mmmm m 15 campw eq add value 3 Value Value I eq add add I 12 t I1 1 p mmmm m 15 campw 3 Inheritance Inheritance is using the definition of one class to make another class make fraction uses makenumber to I39l77eril the behaviors of number I 2 w mmmm m m an 17 Speaking about Inheritance Fraction inherits from Number Fraction is a subclass of Number The supercass of Fraction is Number p W la me Class 17 Sex Religion and Politics C5150 C t S 39 ompu ermgence avld Evans Untversny of V19 D Computer Science hm WWW Cs Vlrgll ila eduevans Menu 0 Debt Population 9 2 0 Is it useful for a problem to be hard 0 How the Allies broke Fish A ESl Fall 2m5 Letmre 17 Saving the wmd17991944 2 Inquot Compqtye Sg mgggu PS4 Question 1 For eachfand g pair below argue convincingly whether or notfis 1 0g 2 Q g and 3 6g as explained above For all questions assume n is a non negative integer the federal debt n years from today g the US population n years from today this one requires a more informal argument Malthusian Catastrophe Reverend Thomas Robert Malthus Essay on the Principle of Population 1798 The grat and unlooked for discoveris that have taken place of late yars in natural hilosophy the increasing diffusion ofgeneral knowledge from the tension of the art of printing the ardent and unshackled spirit of inquiw that prevails throughout the lettered and even unlettered world ave all concurred to lead many able men into the opinion that we were tnuching on a period big with the most important changs chan 5 that would in some measure be decisive ofthe future fate of man in c5159 Fall 2m5 Letmre 17 saving me wmd17991944 4 c5159 Fall 2m5 Letmre 17 SavmgIhE wmd17991944 3 mi Comp th 5ng Malthus Postulates I thinkI may fairly make two postulata First That food is necessary to the existence of man Secondly That the assion between the sexes is necessary and wi I remain nearly in its present state These two laws ever since we have had any knowledge of mankind appear to have been fixed laws of our nature and as we have not hitherto seen any alteration in them we have no ri ht to conclude that they will ever cease to be w at they now are Malthus Conclusion Assuming then my postulata as granted I say that the power of population is indefinitely greater than the power in the earth to produce subsistence for man Population when unchecked increases in a geometrical ratio Subsistence increases only in an arithmetical ratio A slight acquaintance with numbers will show the immensity of the first power in comparison of the second Esl rzll 2m5 Letmre 175awn9mewmd17991944 5 c5159 Fall 2m5 Letmre 17 saving me wmd17991944 5 me Malthusian Catastrophe 0 Food supply growth is linar n What does this mean as ngt m Food per person food supply population 9 n 9 k As H approaches infinity food per person approaches zero 0 Fbpulation growth is geometric Jw Jrgt 1 Malthus Fallacy Malthus Fallacy 0 He forgot how he started The great and unlocked for dlscoverles that have taken place of late years ll l natural p lloso the ll39lcreasll39lg dlffusol39l ofgeneral knowledge from the extenslol39l of tne art of pnnu ng the ardent and unshackled Splrlt otlnqulry that prevalls tnrougnout the lettered and even unlettered world 0 Agriculture is an endless golden agequot eld 7 Producuon from the same land lncreases as N on n2 7 n asll39l owledge of famll lg w adner forecasung plant dornesueauon geneuc englneerlng pest repdlane dlsmpuuon cnannels etc misnr llmi Man Mummera am a comm cum Upcoming Malthusian Catastrophes 0 Human m consumption of fossil fuels grows constant 7 umnllmi Man Mummera gm m Malthus was wrong about 2 Also eduta changes 2 q vegulatlan rn Chlna have reduced k ll r5 lt I n many tauntnes nawl Cornocopian View 0 Few resources are really nite 0 Most things have endlss golden ags man ingenui nomics and politics will solve problems before they become catastrophes No one will sell the last gallon ofgas for 298 Kay sian View The best way to predict the future is to invent it Alan Kay Influence of Malthus In October 1838 that is fifteen months after I had begun my systematic incpiry I happened to read for amusement Malthus on ulation and being well prepared to appreciate e struggle for existence which everywhere goes on 39om longcontinued observation of the habits of animals and plants it at once struck me that under these circurrstances favorable variations would tend to be preserved and unfavorable ones to be destroyed The results of this would be the formation of a new saecies Here then I had at last got a theory by which to work Charles Darwlrl li l nls autoblography 1876 PS4 Question 1 US population n e Debt lrrcr gt as rr lrrcrea rnrs wlll eventually he a Dmhlem but ardevs af qvawth misnr llmi Mum mumme mew ff the federal debt n years from today g the y ars 39om today th39 requires a more informal argument eases Spendlng e Revmues unls vanes butusually posuves interest on the ltlst1rlg Debt exponenual Pcpulatlurl lrltre e s ngt eprnmtlal rate Drltlrlues cg detre e ses debt per person approaches W l Is one nlty analysls daesn39t say Wnen Problem Classes if P 2 NP Simulating Umverse 0013 Find Best em NP Cumplzie Nate the n l me less l5 s vlrlq Traveling eut ners are sgesp quott 25 M Culunn Sumng e 3quot g n in g n a cmnnggnenee small Mum mumme mew u ComlmrrYl fc n55 Graph Map Coloring Input a graph of nodes wi Output either a coloring of same color or no If given a coloring easy to no connected nodes have th edgs connecting t em and a minimum number of colors such that no connected nods have the the 5 color and the number of colors used Problem the nodes check if it ame Best Map Coloring Problem Input a graph of nodes with edges connecting them ut the minimun nunber of colors that can be used to color the map without any adjacent nodes having the same color de ne beeternepmlgrlng graph de ne urnelnelpe graph guess 25h 7m lr Snuirrgpmlgnnga graph guess WWW numb D larnrenelper gryh 1 guess a 3 Map lsrnelnelpe graph u we knnw besternape Dlnrlng ls alsu ln NP n Is it ever useful to be confident that a problem is ard Factoring Problem Input an ndigit number Output two prime factors whose product is er 0 Easy to multiply to check factors are correct 0 Not proven tn be NPComplete Most people think it is except Sneakers 0 Most used public key clyptnsystem RSA depends on this being hard click on the key symbol at the bottom of your web browser it g y Breaking Fish 0 GCHQ learned about rst Fish link Tunny 39n May 1941 Intercepted unencrypted Baudotencoded test messages 0 August 30 1941 Big Brmk cperatnr retransniis failed message with same starting configuration Gels lazy and uses some abbreviations makes some mistakes uSPRUCHNUMlVERSPRUCHNR Selal Number Two Time Pad 0 Allis have intercepted C1 C2 M2 XOR K1 Same key used for both same starting configuration 0 Breaking message 1 OR c2 M1 XOR K1 XOR M2 XOR K1 M1 XOR M2 XOR K1 XOR K1 Know C1 C2 intercepted cipherteXt OR C2 M1 XOR M2 Don t know M1 or M2 7 But can make some guesses crlbs wRucHNuMNER Seneumes elles rmved wlps sent out embers tn help the pmgraphes get good crlbs cw Given guess for M1 calculate M2 M2 c XOR c2 XOR M1 Once glasses that work for M1 and M2 M1 XOR M2 small We meme Hausa 2 a Comm chcnu memm We meme Hausa 22 a Comm seem Cribs Finding K1 0 From the 2 intercepted mssages Col John Tiltman worked on guessing cribs to nd M1 and M2 4000 letter message med 4000 letter key 0 Bill Tutte recent Chemistry graduate given task Ofdetermining machine structure from key Already knew it was 2 sets of 5 wheels and 2 wheels of unknown function 2a Why learn Python Lecture 28 Implementing Interpreters 2 Computer SClC CC rrr Mr an39Iiksrr r39 fxrlRGlNlA th Description Reason 1 Vocational Skill STOP EVERYTHING mums DOlNG RIGHT NOW THE NEXT FlVE Job listings at monstercom In Virginia MINUTES MAY CHANGE YOUR LIFE 39 39 l won t waste your time l lll get straight to the point You are looking at an unique 3 I I business opportunitybut as in anyr businessyou must bewillingtol E 1M I m To be serious in this opportunity as in EM 2 l you must be willing to lNVEST 995 after you go through our FREE presentation P h 0 n 2 7 4 O 2 O O K Still Interested Read On Or Ciick Here to find out mm mm mm l have a very simple story to tell looking on Monstercom to try to find a better job because ll was sick oi myjob and l was sick of living paycheck to paygheck I got lucky and found a great opportunity that has given me the l ir39aj iz 39 a 7 7 O 3 5 2 O O K l wanted and the freedom to work from home ifl chose to sol can stay home and spend time with my kids l am NOT going to show you a picture of an expensive car an oceanfront house or 1 1 3 8 6 O 4 O O K some other ridiculously expensive luxury why Because I don39t have those things l39m not a millionaire i don39t go on vacation every month nor do l own some island amp 9 BUT l D0 work at home l D0 make enough to to live an upper middle class lifestyle with two kids l 1 spend much more time with my kids and husband and Whatl am doing on Monster is to give back and share what I have done and share the opportunity that was given to me as l was desrerately looking tor a better way of paying the bilis This is NOT a getrichquick but guess what All of the hard work that you do only bene ts YOU llaLueLIDS a Simply click the link below to hear more about my story and get more information on how you can do it too Heard Enough CLICK HERE TO GET STARTED a C S 399 C S 39 Lecture 28 Implementing Interpreters 3 r omputer Clenoe I omputer Clence Illllll IIIIIII altJr UMHRsm gr VIRGINIA ma UMI mam q Vnamxm Reason 2 Expanding Minds Languages Change the way we think JOHBZ 31m tam The more languages you know the more different ways you have of thinking about and solving problems Jamais Jamais Jamaisquot from Harmonca Musces Odhecaton A Printed by Ottaviano Dei Petrucci in 1501 first music with movable type 0 Lecture 28 Implementing Interpreters 5 m Computer Selence Lecture 28 Implementing Interpreters 5 m Computer Selence 1 r1 UNlVlZRSI I Y grquot V 1R0 NM rrr rir UNl39l39lZRSl39l39Y If V lRGleA III 1 RBI m nu woman l IIIIA ci za k lkig gates 4 321 1amp1 fall Cw W 2 V j39 W3939 77 iii 772 Gigc haywire 39 t Em A Jamais Jamais Jamais from J Coffee Cant ataj Harmonica Musices Odhecaton A BWV 211 1732 1501 www npjcomhomepageter towejsbhand html A Lecture 28 Implementing Interpreters Reason 4 Building Confidence By learning Python mostly on your own the next time you encounter a problem that is best solved using a language you don t know you will be confident you can learn it rather than trying to use the wrong tool to solve the problem A Lecture 28 Implementing Interpreters 9 Python o A universal programming language Everything you can compute in Scheme you can compute in Python and vice versa 0 Imperative Language Designed to support a programming where most of the work is done using assignment statements X e o ObjectOriented Language Every data thing is an object Built in support for classes inheritance Reason 3 Deepening Understanding By seeing how the same concepts we encountered in Scheme are implemented by a different language you will understand those concepts better especially classesobjects assignment data abstraction A Lecture 28 ImplementIng Interpreters Reason 5 Fun Programming in Python is fun Especially because a It is an elegant and simple language Most programs mean what you think they mean a It is dynamic and interactive It can be used to build web applications PS8 PS9 It is named after Monty Python s Flying Circus It was designed by someone named Guido A Lecture 28 Implementing Interpreters 10 Lecture 28 Implementing Interpreters 11 Learning New Languages Syntax Where the etc all go If you can understand a BNF grammar this is easy Semantics What does it mean Learning the evaluation rules Harder but most programming languages have very similar evaluation rules Style What are the idioms and customs of experienced programmers in that language 0 Takes many years to learn 0 Need it to be a professional Python programmer but not to make a useful program A quot Com ter Sc ence mi P v iiiV UVlVl39lNl l39 L Lecture 28 Implementing Interpreters 12 Python If Instruction if Expression Block Evaluate Expression If it evaluates to true evaluate the Block It is similar to if Express0n begin Statements Differences Indenting and new lines matter Changing the indentation changes meaning of code What true means Scheme anything that is not f Python anything that is not False None 0 and empty string or container Lecture 25 Implementing Interpreters 13 4 Com uter ScIence gm 9 t t i Usn Ali G Problem 0 Input a list of 2 numbers with up to d digits each 0 Output the product of the 2 numbers Is it computable Yes a straightfonNard algorithm solves it Using elementary multiplication techniques it is 0d2 Can real computers solve it Computability in Theory and Practice Intellectual Computability Discussion on TV Video Lecture 25 Implementing Interpreters 14 U v mu AIL EM Computer Sgeg e JElll liljgle g t grew risen Fglma Gals QaLa mam sh 5 x CE v A PRODUC TC lCS H C 999999999 999999999 999999999 99 99 99 99 99 99 99 99 99 9800999990199 97029899902970 96059600903940400 449mlmlelulmle 1 if quotMpg mun If F I ll l ll Readv aligscm DrSchen39le W quot Elle Edit how Language Sgheme Special Mndows Help gt 999999999 99 99 9800999990199 gt 999999999 99 99 9 97029899902901 gt 999999999 99 99 99 99 96059600903940399 A Lecture 25 Implementing Interpreters 15 m Computer Science arr UVll ms n i lunlx e mz Elle Edit ihaw Language Sgheme Special Mndaws Help gt it 99999999999999999999999999999999999999 9 9999999999999999999999999999 999999999999999 9999999999999999999999999aEaas999999999999999999999999 9 99999 999999999999999999999999999999999999999 191919191919199 99999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999 99999999 999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999 999999999 99999999999999999999aaa99999999999999999999999999999999999999 9999 na25212175444643n31597733243399999992169 gt WE 36 ReadWnleD rm nnnn mg Ali G was Right 0 Theory assumes ideal computers Unlimited perfect memory Unlimited finite time 0 Real computers have Limited memory time power outages flaky programming languages etc There are many computable problems we cannot solve with real computer the actual inputs do matter in practice but not in theory A Lecture 25 ImplementingInterpreters 1s Computer Sctegce r u Uw w w u Implementing Interpreters Inventing a Language Design the grammar What strings are in the language Use BNF to describe all the strings in the language Make up the evaluation rules Describe what everything the grammar can produce means Build an evaluator A procedure that evaluates expressions in the language mm 2 lmvlemenlmqlnlervrelex 19 W ComRuteI c mm 2 lmvlemenlmqlnlervrelex 2n m C R 5 Is this an exaggeration SW 38 It is no exaggeration to regard this as the most fundamental idea in programming The evaluator which determines the meaning of expressions in t e programming language is just another program To appreciate this point is to change our images of ourselves as programmers We come to see ourselves as designers of languages rather than only users of languages designed by others J Environmental Model of Evaluation 1 To evaluate a combination evaluate all the subexpressions and apply the value of the rst su begtltpression to the values of the other su begtltpressions 2 To apply a compound procedure to a set of arguments evaluate the body of the procedure in a new environment To construct this environment make a new frame with an environment pointer that is the environment of the procedure that contains places with the formal parameters bound to the arguments mm 2 lmvlemenlmqlnlervrelex 21 m CDmeEL m CDMPHESE mm 2 lmvlemenlmqlnlervrelex 22 Eval and Apply are de ned in terms of each other def mevalexpr env if isPn39mitiveexpr return evaan39mitiveexpr elif isOonditionaKexpr return evalOonditionaKexpr env elif isLambdaexpr rn evalLambdaexpr env elif isDe nitionexpr evalDe nitionexpr env elif isNameexpr re urn evalNameexpr env elif isApplicationexpr return evalApplicationexpr env Implementing meval else evalErmr quotUnknown expression type quot strexpr mm 2 lmplemenlmq murmur m Compquoter mm 2 lmvlemenlmqlnlervrelex 24 Lecture 10 Quicker Sorting What does 9 really mean 0 0x it is no more than x work upper bound 0 x work scales as x tight bound 0 9x it is at least x work lower bound If 0x and 9x are true then x is true A c5150 Fall 2005 Lecture 10 Measuring Work 3 ITquot Comput5 geagfl M 39isquotg Menu compose and ntimes Measuring Work What 9 really means Quicker Sorting C5150 Fall 2005 Lecture 10 Measuring Work m 1 l n 2 Fur P35 5 Meaning of 0 big Oh fx is 0g x means There is a positive constant c such that 6 fx lt 806 for all but a finite number Ofx values A C5150 Fall 2005 Lecture 10 Measurlng Work 4 I39lullll39l l w z 0 Examples fx is 0g x means There is a positive constant c such that 6 fx lt 00 for all but a finite number of x values x is 0 x2 Yes 0 1 works fine 10x is 0 x Yes c 09 works fine No no matter what 6 we pick M we gt x for blg enough x Lower Bound 52 Omega fx is Q g x means There is a positive constant c such that 6 fx gt 806 for all but a fl ite number ofx values Difference from 0 this was lt c5150 Fall 2005 Lecture 10 Measuring Work 5 quot Computsr cieaggn C5150 Fall 2005 Lecture 10 Measuring Work 5 I39llquot H v u w x is Q g x means E I There is a positive constant c such that xamp es 3 xgtgx for all but a nite number ofx values x is 0g x means Th re is a positive constant c such that lt g x for all but a nite number ofx values xls Q x x is Yesl PiCk C 2 Yes pick c 5 10xisQ x 39 10 is Xx Yes pick c 1 Yes pick c 09 IS x2 9 X x2 is not Xx Yes cs nhllmn t mmqu 7 Comglms 6 Examples 39 10 is 6 x Yes since 10x is Q x and 10 is Xx 0 Doesn t matter that you choose different c values for each part they are independent x2 isis not 9 x No since x2 is not 0 x o x isis not 9 x2 No since x2 is not Q x cs In an mus ledIre m Mezsunnq ww 9 ECDmP f i g Is our sort good enough Takes over 1 second to sort 1000length list How long would it take to sort 1 million items 15 time to sort 1000 45 time m sort 2000 nz 1M is 1000 1000 Sorting time is n2 so sorting 1000 times as many items will Lake 10002 times as long 1 million seconds 11 days Note there are 800 Million VISA cards in circulation It would mke 20000 years to process a VISA transaction at this rate Tight Bound l9 Theta x iS 0 g 96 iff x is 03 96 and x is Q g 96 amalgam1Nnnwa a WComputeI wtkl Sorting de ne simplesort cf st I de ne ndbest cf st If null st st insertl let bst ndbest cf st ambda if cfcl c2 c1 c2 est s simplesort cf car In delete st most Simplesort IS nz If we double the length of the list we amount of work sort dos apmehiatey quadruples cs In an mus ledIre m Mezsunnq ww m m CDMPHESE Divide and Conquer sorting simplesort find the lowest in the list add it to the front of the result of sorting the list after deleting the lowest Insertion sort insert the first element of the list in the right place in the sorted rest of the list amalgam1Nnnwa n mummy mnalm1Nnnwa 2 mummyI insertsort define insertsort cf Ist if null Ist null insertone cf car Ist insertsort cf cdr lst as F m m m Mum m 3 E Compuwt eyr saws insertone define insertone cf e Ist if null Ist list el if cf e car Ist cons e Ist cons car Ist insertone cf e cdr st as F m m m Mum m 4 E Compuwt eyr saws How much work is insertsort de ne insensmt Cf Ist de ne insertone cfel Ist if null Ist ifr1u Ist u list e insertone cf if Cf 539 car I50 car Ist cons e Ist insensort cf cons gar 3952 f I Inser ne c e W 3950 cdr Istm How many times does Worst case Insertsort evaluate Insertone Average case n times once for each element insertsort is My Insertone IS n gt insensort lt revinlslto 20 12345678910111213141516171819 20 Requirs 190 applications of lt gt insensort lt inlsto 20 12345678910111213141516171819 20 Requirs 19 applications of lt gt insensort lt randintIist 20 0 11 16 19 23 26 31 32 32 34 42 45 53 63 64 81 82 84 84 92 Requirs 104 applications of lt as F m m m Mum m 5 m campuggx 63m ammumFmmm mCumpu t g gt simplesort lt inlsto 20 12345678910111213141516171819 20 Requires 210 applications of lt gt simplesort lt randintIist 20 4 4 16 18 19 20 23 32 36 51 53 59 67 69 73 75 82 82 88 89 Requires 210 applications of lt simplesort vs insertsort 0 Both are nz worst case reverse list 0 Both are nz average case random But insertsort is about twice as fast 0 insertsort is n best case ordered list m5 F m m m Mum m 7 m comp F we ammumFmmm 1 mcumm a VVhatis Computer Science 1 Introduction 2 quot Com uter Science I P m UMYIiRH l afVIIuIIxII Let AB and CD be the two given numbers not relatively prime It is required to find the greatest common measure of AB and CD If now CD measures AB since it also measures itself then CD is a common measure of CD and AB And it is manifest that it is also the greatest for no greater number than CD measures CD Euclid s Elements Book VII Proposition 2 3OOBC The note on the in ected line is only difficult to you because it is 50 easy There is in fact nothing in it but you think there must be some grand mystery hidden under that word in ected Whenever from any point Withouta given line you draw a long to any point in the given line you have in ected a line upon a given 0779 Ada Byron age 19 letter to Annabella Acheson explaining Euclid 1834 1 Introduction 3 r quot m 1 ll 11 1 Introduction 4 quot m 1 1 11 By the word operation we mean any process which alters the mutual relation of two or more things be this relation of what kind it may This is the most general definition and would include all subjects in the universe Supposing for instance that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent Ada Byron 1843 What is the difference between Euclid and Ada 39 39 39 It depends on what your definition of is is Bill Gates at Microsoft s antitrust trial E I 1quot amen quotD 35 519 IE 393 T h 1 Introduction Com uter Science I 5 Pu r11 an39linsrrv gf V I R Il l 1 Introduction 6 a Computer Science III III UN39l R5l39l Y 439 V IRIIINH Geometry vs Computer Science 0 Geometry mathematics is about declarative knowledge what is If new CD measures AB since it also measures itself then CD isa common measure of CDand AB m I s about imperative know ege ow to Computer Science has little to do with beige or translucent blue boxes called computers and is not a real science Computer Science How to knowledge 0 Ways of describing information rocesses com utations p p l 0 Ways of predicting properties of information processes What kinds of things do we want to predict Science Engineering Other 1 lmmdmhun 7 Computg E Ezlss 1 lmmdumm a M Com pgg gigggg Scnence 0 Understanding Nature through Observation About reathings like bowling balls black holes antimatter electrons comets etc 0 Math and Computer Science are about fake things like numbers graphs functions lists etc Computer Science is a useful tool for doing real science but not a real science A 1 lmmdmh un 9 mi Compu A 1 inumumm 10 m Compll Engineering Engineering is design under constraint Engineering is synthetic it strives to create what can be but it is constrained by nature by cost by concerns of safety reliability environmental impact manufacturability maintainability and many other such 39iIities William Wulf Apollo Guidance Computer 1969 Why did they need to fit the 1 CUb39c Foot guidance computer in the rocket 1 mmquot 11 m Comp31f FSEPE 1mmquot 12 m Comm 55359 Measuring Computers 1 bit smallest unit of information True or False 0 or 1 If we start with 2 possible choices and get 1 bit we can eliminate one of the choices la g Cvmnuse 5quotws Computing Power 19692008 in Apoo Control Computer Units Moore s Law computing power roughly doubles every 18 months l J avi m iagag yc y m5quot F m MW 15 m compug g So what is computer science wSeienee No its about fake things like numbers not about observing and understanding nature No we don t have to deal with engineering type constraints Liberal Art Mn 17 Compom cicnc How much power 0 Apollo Computer 30720 bits of changeable memory 0 Lab machines have 1 GB RAM 1 Gigabyte 1024 Megabyts 1 Megabyte 1024 Kilobyts 1 Kilobyte 1024 BytsW 1 Byte 8 bis ifyou dontnow gt 1024 1024 1024 8 8589934592 N 86 Billion bits gt round 1024 1024 1024 8 30720 279620 You have 105 404 times more power than AGC IIf Apollo Guidance computer power is 1 inch you have 44 miles 14 ComRuter o39e 55 m 1 Constraints Computer Scientists Face Not like those for engineers Cost weight physics etc If 20 Million times what people had in 1969 isn t enough for you wait until 2010 and you will have 80 Million times More like those for Musicians and Poets Imagination and Creativity Complexity of what we can understand Llnlmdudmn 15 m cvmvw s es Liberal Arts 1100 Illiberal Arts arts for the nonfree pursued for economic reasons Liberal Arts arts for the free pursued for intrinsic reasons 1a Cump t f songs The Liberal Arts age 00 M Trivium 3 roads Quadrivium 4 roads Gram Logic Arithmetic Music study of meaning in Rhetoric argument number written expression comprehension for Geometry in time of discourse discovering quantification truth of space Astronomy IWe will see all of these in this class 1 Introduction 19 399 Computer Science M mum a Vim w Course Roadmap Computer Science lst Class from Euclid and Ada to PS 16 Quantum Computing Lecture and the War0 Wide Web PS 78 IEHDBIIBJUI sw IEJaqHII SW Imaqn Course Expectations 1 Introduction 20 399 Com uter Science Like Drinking from a Firehose l l rm r Dont be overwhelmed You will do fine It may hunt a little bit and a lot of water will go by you but you won t go away thirsty 1 1 t d t39 quotAquot Com uter Science I n m c Inquot PitIiiUVli HNn ni VIHG KH 1 1 t d t39 quot Com uter Science 39 n m quotc mquot 22 PmMiuvmm mmw New Book written for course Chapters 2 and 3 out today Bonuses for helping me improve it Less pretentious title More exciting cover Notice any mistakes Improve the writing or presentation GEB Course Book l GODELESCHERBACH l 7 G n 1h Help Available 0 Me David Evans Call me Dave or Coach Office Hours will be posted after your surveys Always available by email ifI don t reply in 24 hours send again and complain Assistant Coaches Richard Hsu and Kinga Dobolyi Staffed lab hours in Small Hall Upcoming lab hours Thursday 69pm Friday after class Web site httpwwwcsvirginiaeducslSO Everything goes on the web you should visit it often Your classmates read the course pledge carefully 1 Introduction 24 1 Introduction 23 l39llllll comp t x w g my Commits What I Expect of You 1 Everything on the Course Pledge You should actually read it notjust sign it you will lose points on P51 if our submission reveals that you didn t read it 2 You are a Jeffersonian Student 1Beieve knowledge is powerful 21nterested in lots of things ahead of your time 3Want to use what you learn to do good things 4Care more aboutwhat you learn than grades and degree requirements I Mum 25 n39A i Cm m Scle vlf mums A Course for Everyone CLAS SEAS Commerce Arch etc o 15 2nd 3rd 4 5 11 Years Community Scholars Faculty No background expectedbut challenging even for students with lots of previous CS courses Computer Science future majorsbut worthwhile even if you don t take another CS course Ludhiana 27 What is the longest word in the English language I Mum 29 n i C mPL f sf 5 Background Expected Language Reasonable reading and writing in English Understanding of subject verb and object Math Numbers add subtract multiply divide Exponentiation logarithms we will review Logic and or not Computer Literacy read email browse web If I ever appear to expect anyihing else stop me 25 m C0 R li First Main Theme Recursive Definitions Ludhiana za According to Guinness floccipoccinihilipilification the act 0frenden77g usee55 mind an Compti f Scig cg 255 2005 T 131 5 Exam 1 Reminders Review Session is tonight at 630 in Olsson 228E I have office hours after class today and Thursday at 330 Kinga will be in Small Hall Friday morning 101130am If you have topics you want me to review in Friday s class email me 7 Lecture 16 QUIckest Sorting 2 M Last class Sorting Cost define bestfirstsort lst cf if null lst lst let best findbest lst cf cons best bestfirstsort delete lst best cf define findbest lst cf if 1 length lst car lst pickbetter cf car lst findbest cdr lst cf The running time of bestfirst sort is in n2 where n is the number of elements in the input ist This is wrong Length define length lst if null lst O 1 length cdr lst The running time of length is in n where n is the number of elements in the input list Lecture 16 Quickest Sorting 3 399 Computer Science m 51 UNIVERSITY f VIRUINH Lecture 16 Quickest Sorting 4 39 vA Com inter Solence Pu Mr UNIVERSITY V RG XIquot findbest Cost define findbest lst cf if 1 length lst car lst pickbetter cf car lst findbest cdr lst cf assumption cf is constant time procedure n number of elements in input list there are n recursive applications of findbest each one involves an application of length lst which is in n The running time of findbest using length is in nZ where n is the number of elements in the input list Sorting Cost define bestfirstsort lst cf if null lst lst let best findbest lst cf cons best bestfirstsort delete lst best cf define findbest lst cf if 1 length lst car lst pickbetter cf car lst findbest cdr lst cf The running time of bestfirst sort is in n3 where n is the number of elements in the input list This is right but very inefficient Lecture 16 QUIckest Sorting 5 M Comp tgg c lga a Lecture 16 Quickest Sorting 6 A quot Com ut r Selence Pu Mir Emuazer 39l39 mexu bestfirstsort de ne best rstsort lst cf if null lst lst let best ndbest lst cf oons best best rstsort delete lst bst cf de ne ndbest lst c if null cdr lst car lst pickbetter cf car lst ndbest cdr lst cf The running time of best rstsort is in n2where n is the number of elements in the input list This is right mm m annuamsmng 7 Comp uter Scien ce WWW Can we do better insertone lt 88 list 1 2 3 5 6 23 63 77 89 90 Suppose we had procedures firsthalf lst secondhalf lst that quickly divided the list in two halves mm as annuamsmng 9 campm same Mm Evaluating insertone gt armature lt 3list1 2 4 5 7 ionsewune mumqu made 3 1 2 4 5 7 lt 3 1 i de ne onsemme el lstd 0N l l 35 null lst listel l rt lfmll odrlst i Mature 4tltuucaduet12cadltgt 3 124 Md el car lst i lt 3 1 oors el lst l f listcarlstel l t3 0 let front rstrhalflst 7 7 back seoondrhalf ist 1 mauve tuned ltgt 3 1 2 0f dd w bado f append irsertrone el frontd badlt i lt 32 append mm l f t lb k l immune museum name a 2 my one 6 ac mm l lt 32 l 2 Every time we call Insertone l l123 the length of the list is l 1234 i023 457 approximately halved 12 345 7 m e gamma 1 Cump l f games Last class insertsort de ne insertsort lst cf if null lst null insertone car lst insertsort cdr lst cf cf de ne insertone el lst cf if null lst list el if cf el car lst cons el lst oons car lst insertone el cdr lst cf Assuming cfis a constant time procedure insert sort has running time in nz where n is the number of elements in the input list mm m annuamsmng a Computer Sc mum s it insertone using halves de ne insertone el lst cf if null lst list el if null cdr lst if cf el car lst cons el lst list car lst el let front firsthalf lst back secondhalf lst if cf el car back append insertone el front cf back append front insertone el back cf a m campmsc Mva How much work is insertone Each time we call de ne irsertrone el lstcf insertone the size of ll r ll lffmsrt tl lst halves So doubling dd r lst the size of the list only 5321 6 Increases the number of Gettmt rstrhalflst calls by 1 badlt secondhalf ist car badlt if d e append onsemme el front f badlt appen ont List Size Number of insertrone apphcauons Gnsert39me el back cm 1 1 2 2 4 3 8 4 15 5 a 2 cumpmsc mm m Remembering Logarithms Changing Bases log nxmeans bx n 10 n 110 19 10 n What is log2 1024 gb w gk Ifkand bare What is log10 1024 constants this is oonstant Islogmn in 9log2 n logzn E loglon E log n No need to include a constant 39 raters m m mm m 3 Comm gs rm m m mm m 14 ComRuteI fe 55 w t Number of Applications insertsort de ne insertrsort lst cf angilQES geil 5 m Assuming the list is well balanced quot23533 0 E aiml iiim 00m 6 St the number of applications of car 3950 mammal Insertrsort cdr lst cf let front rstrhalflst insertone is in log n where n is cf dag kg arigavist the number of elements in the Eggggjggegme iimtc bm ll39Bert39Ol39E el back d insertsort using halves would have running time in n log n if we had firsthalf secondhalf and append procedures that run in constant time mum IE um mm 15 m 1 campw 93 Orders of Growth Is there a fast firsthalf procedure No at least not on lists To produce the first half of a list length n we need to cdr down the first nZ elements So firsthalf has running time in n ioi725334i49 es73313997105 mum is gum mm 17 m comptm sums mum IE um mm IE compm smug mm Making it faster We need to either 1 Reduce the number of applications of insertone in insertsort Impossible need to consider each element 2 Reduce the number of applications of insertone in insertone Unlikelyeach application already halves the list 3 Reduce the time for each application of insertone Need to make firsthalf secondhalf and append faster than n Lecture 15 Quickest Sorting 19 Computsgu sieg g Happy Bird Tree by Jon Morgan and Nadine Natour 21 tCOmniitsmisass Lecture 15 Quickest Sorting Tree Example null null Cf lt Lecture 15 Quickest Sorting 23 A 39 m C m iit t t i EDCC quotburning yal iiow Elbow you Newton Lecture 16 Quickest Sorting 20 1 n Uw39l w w Vwiwu Computer Science left A tree containing all elements xsuch that cf xel is true Sorted Binary Trees A tree containing all elements xsuch that cf xel is false right Lecture 16 Quickest Sorting 22 v Vim A M Computer gegycgh r u Us de ne maketree left el right cons el cons left right de ne treeelement tree car tree de ne treeleft tree car cdr tree de ne treeright tree cdr cdr tree Representing Trees left and right are trees null is a tree tree must be a nonnull tree tree must be a nonnull tree tree must be a nonnull tree Lecture 16 Quickest Sorting 24 l Computer Scrence um Mi r 5 mum Class 26 Modeling Computing C8150 Computer Science University of Virginia Computer Science David Evans httpwwwcsvirginiaeduevans DrScheme o Is DrScheme a proof that Scheme exists quot 39 4 101 40 5 5o 6924 A o c5150 Fall 2005 Lecture 25 Undecidable Problems 3 m Compuhtler Selegce um Nl39l llt51l 39qr IRUINH Modeling Computation o For a more convincing proof we need a more precise but simple model of what a computer can do 0 Another reason we need a model Does complexity really make sense without this how do we know what a step is are they the same for all computers A C5150 Fall 2005 Lecture 25 Undecidable Problems 5 Computer Selence u m Ul39l39Rl l3939 gr39VlRmxu How convincing is our Halting Problem proof define contradicthalts X if halts contradicthalts null loopforever t contradictshalts cannot exist Everything we used to make it except halts does exist therefore halts cannot exist IThis proof assumes Scheme exists and is consistent I A c5150 Fall 2005 Lecture 25 Undecidable Problems 2 mm Computgr Selegce mv Nl39l k39ll3939 of Gun Solutions 0 Option 1 Prove Idealized Scheme does exist Show that we could implement all the evaluation rules 0 Option 2 Find some simpler computing model Define it precisely Show that contradicthalts can be defined in this model A cs15o Fall 2005 Lecture 25 Undecidable Problems 4 m Compubtler Seletice 11 NIVIERSHT f lRIXl How should we model a Computer Inttnducing The IBM 5100 Portable Clamp um w model we ll use today in 1936 What computer was he modeling l Pra uc vllymynurdndL39lrmlan fnnmdli him audit V i it cs15o Fall 2005 Lecture IBM 5100 1975 5 quotquot Computer Selence rutu UN39I RllY g39Vlkule Turing s Computer Computing is normally done by writing certain symbols on paper We may suppose this paper is divided into squares like a child s arithmetic book Alan Turing 0n computabe numbers with an appcation to the EntscIedungsprobem 1936 A cs 150 Fall 2005 Lecture 25 Undecidable problems 7 milI Computer Scene Modeling Computers 0 Input Without it we can t describe a problem 0 Output Without it we can t get an answer 0 Processing Need some way of getting from the input to the output 0 Memory Need to keep track of what we are doing twllwlwm 7 t w Modeling Input quotn c s A cs150 Fall 2005 Lecture 25 Undecidable problems 3 lm Computer Scxence nwlhluvml yVlm Simplest Input 0 Noninteractive like punch cards and paper tape 0 Onedimensional just a single tape of values pointer to one square on tape 0011001000 How long should the tape be In nitely long We are model ga computer not building one Our model should not have silly practical limitations like a real computer does Modeling Output 0 Blinking lights are cool but hard to model 0 Output is what is written on the tape at the end of a computation Connection Machine CMS 1993 A cs 150 Fall 2005 Lecture 25 Undecidable problems 11 luluquot Computer Seance M r u mt Um C5150 Fall 2005 Lecture 25 Undecidable Problems 1 mlquot wearers rm um um Modeling Processing 0 Evaluation Rules Given an input on our tape how do we evaluate to produce the output 0 What do we need Read what is on the tape at the current square Move the tape one square in either direction Write into the current square 0011001000 Is that enough to model a computer mow w mum Modeling Processing 0 Read write and move is not enough 0 We also need to keep track of what we are doing How do we know whether to read write or move at each step How do we know when we re done 0 What do we need for this c511 Fall mus Letmre 25 umaudable Pnialems Finite State Machines 13 IE Cnmmr sisnse c511 Fall mus Letmre 25 umaudable Pn uns 14 m Compn ss ex nfs Hmmmmaybe we don t need those infinite tapes after all not a not a Daren paren Sta rt What if the next input symbol is in state 2 How many states do we need not a not a paren c511 Fall mus Letmre 25 umaudable Pnialems 15 m Comping iewe Finite State Machine 0 There are lots of things we can t compute with only a nite number of States 0 Solutions Infinite State Machine Hard to describe and draw Add an infinite tape to the Finite State Machine c511 Fall mus Letmre 25 umaudable Pnialems 17 EE39I Comwsrfl ri n cam Fall mus Letmre 25 umaudable Prc mls 15 m Comping qiew Turing s Explanation We have said that the computable numbers are those whose decimals are calculable by finite means For the present I shall only say that the justification lies in the fact that the human memory is necessarily limited c511 Fall mus Letmre 25 umaudable Pn uns IE IIIT me i Menu Lecture 9 Of On and Off Grounds Sorting 0 P52 0 Sorting 0 PS3 Corree Beam Sorting in Guatemala m C5150 Computer Science University of Virginia Computer Science David Evans mttp WWW ts Virginia ElmEvans Email me letwe a smug was is9 1 Problem Sets 0 Notjust meant to review stuff you should Wh already know Get you to explore new idms Motivate what is coming up in the class 0 The main point of the P55 is learning not PSZ Question 3 y is de ne highercard cardl cardZ gt cardrank card 1 cardrank cardZ better than de ne highercard cardl cardZ evaluation gt car card 1 car card 2 Don39t give up if you can39t nd the answer in the 7 b k in rims class we won tworry too much about designrlg programs 00 Wil good apsuacuors smoe ihe programs we are dealing Wil are r Discuss wim other studems fairly smai For large programs good apsnacuoms are essemuai Thats what most ofCSZOlO is about rammed an a PSZ Question 8 9 Can we do better 0 Predict how long it will take Identify ways to make it faSter de ne ndbest hand holecards communitycards car sort higherhand possible ands holecards Much of this week and later classes will be focused C mmun39tyfardsm on how compumr scientisE predict ow Ion programs will take and on how to make them fasmr 5 m Complgl s cnmpm findbesthand define ndbesthand Ist if null cdr Ist car Ist let restbest ndbesthand cdr lst if higherhand car Ist restbest car Ist restbest de ne inserd lst f smpval if null Ist ndbest hand smpval f car Ist insertl cdr Ist f 5topval define ndbesthand Ist inser l lambda hand1 hand2 if higherhand hand1 hand2 hand1 hand2 cdr Ist already used the car as stopval car lst 31511 F m m m 7 m Computer saws mum 31511 F m m m a m Computer saws mum findbest de ne findbest cf Ist insertl lambda c1 c2 if cf c1 c2 c1 c2 cdr Ist car st de ne findbest cf Ist inser lambda c1 c2 if cfcl c2 c1 c2 cdr Ist car lst define ndbesthand Ist findbest higherhand Ist 31511 F m m w W Compuggrs m a mamahams m mCumpuggrs m How much work is findbest Why not just time it Moore s Law computing power ouble 18 months mamahams n Cumputcrs m m n mamahams 2 Cumputcrs m m n How much w ark IS findbest de ne ndbest cf lst insert lambda c1 2 if cfcl c2 c1 2 lst car lst Work to evaluate findbestf lst Evaluate insertl lambda c1 c2 lst te Ist These don t depend on the length of the list so we don t care about Evaluate ca hem Work to evaluate insertl f nu stopval r car lst nserd fcdr lst stopval How many times do we evaluate ffor a list of length n n insertl IS n Theta 7 If we double the length of the list we amount of work required approximately dou es We will see a more rmal de nition of 9 next class and a more formal de nition of Amount ofworkquot in November cs n F mus m 9 m 3 E Computer science Hk as cs n F mus m 9 m 4 E Computer science Hk as Simple Sorting We know how to findbest How do we sort Use findbest lst to find the best Remove it from the list Repeat until the list is empty Simple Sort define sort cf Ist if null Ist Ist let best findbest cf st cons best sort cf delete Ist best mommamomma 5 mcomput exs ncc mommamomma 5 mcomput exs ncc Sorting Hands define sorthands lst sort higherhand lst Sorting de ne sort cf Ist define ndsbest cf Ist if null lst Ist inserd let most ndsbest cf lst lambda c1 c2 cons if cf c1 c2 c1 c2 most ls sort cf car lst delem Ist best 0 How much work is sort 0 We measure work using orders of growth How does work grow with problem size mommamomma 7 cumputets ncn Mn nx 39 mommamomma 1 cumputets ncn Mn nx 39 de ne sort cf Ist 39f lll t l t Sorting I bst ndbest cf lst cons best sort cf delete lst best o What grows 7 the number of elements in Ist o How much work are the pieces findbest is n delete is n o How many times does sort evaluate findbest and delete cslsn lel mus lulure 9 smug 19 EComRuteI L tug s Timing Sort gt time sort lt revintsto 100 c u time 20 real time 20 c ti 39 c rea gt time sort lt revintsto 400 cpu time 31 real time 311 go time 0 gt time sort lt revintsto 800 cpu time 1362 real time 1362 go time 0 gt time sort lt revintsto 1600 cpu time 6650 ral time 6650 go time 0 cslsn lel mus lulure 9 smug 21 ECDmeEL g Is our sort good enough Takes over 1 second to sort 1000length list How long would it take to sort 1 million items ls time to sort 1000 45 time m sort 2000 nz 1M is 1000 1000 Sorting time is n2 so sorting 1000 times as many items will Lake 10002 times as long 1 million seconds 11 days Note there are 800 Million VISA cards in circulation It would mke 20000 years to process a VISA transaction at this rate usmaum mung smug 23 m compute un de ne sort cf Ist 39f llltlt Sorting I E bst ndbest cf lst cons best sort cf delete lst best 0 I7 the number of elements in lst o findmost is n delete is n o How many times does sort evaluate findmost and delete 7 sort is nz If we double the length of the list the amount of work apmehutey quadruples usmaumtsuusgsmug 2g Ecomputer wtkl nz measured times 2500 cslsn lel mus lulure 9 smug 22 m Cvmvm PS3 Lindenmayer System Fractals usmaum mung smug 24 m computer u m CommandSequence CommandLst CommandLst Command CommandLst L m S L m CommandLst Command F CommandSequence C ommandLst Rewriti n 9 Command RAnge C d OC d5 C ommandLst Command C ommandLst 0mm 0mm aqua 66 C ommandLst Start F Rewrite Rule ChmWW F F e F OR3O F F OR6O F F Command RAnge Work like BNF replacement rules except Command OCommandSequence replace all instances at once Why is this a better model for biological systems a A a c5150 Fall 2005 Lecture 9 Sorting 25 mi Computer Scrence c5150 Fall 2005 Lecture 9 Sorting 26 m Computer Selencc n 6 UNl t liRSl lT of VlRGlxlA If Mr UNlVIZRSI39I39Y cf V1 Romy gt9 gt N x gt9 Levelo Leve1 r F F FOR30FFOR60FF Sta t gt Level 2 Level 3 F F OR30 F F OR60 F F c5150 Fall 2005 Lecture 9 Sorting 28 flit ComPPaFSEVEStEtlESa is M quot39 The Great Lambda Tree of Ultimate Kn owl ed 9 e pcccc pcaccca Mara arctic and ma Fan circaWand Fracca Ra uel Johnathan F 39 N39 N q d rustratlon lco e umbers and Pink Buds by Kate Morandi Jon Carter I and Erika Vogel a n n l n l e 3 lt1 ower m g tax j 3 a L2 1 3 Logo LinCOIn Hamilton Spirograph Leah Nylen and Dan Nguyen o 29 m Computer Scrence c5150 Fall 2005 Lecture 9 Sorting 3o mi Computer Sglence be UMVlZRSH39Y g VlRGiNIA Ar UNiVIiRSI39I Y cr39ViRcilxm Lecture 41 P NP I will have extra office hours after class today 13pm To be eligible to present Monday your team must send an email with your URL before midnight tonight l 1 39lr 1 J a DNA Helix P otomirom coverrof Nature 15 Feb 2001 made by Eric Lander a C8150 Computer Science University ofVirginia DaVId Evans Computer Science httpwwwcsvirginiaeduevans Pegboard Problem A 392 m 6139 1 I1 6 Ledure 41 P NP 2 C0 Pgtljxn39i i SVMEHM Pegboard Problem Input a configuration of n pegs on a cracker barrel style pegboard Output if there is a sequence of jumps that leaves a single peg output that sequence of jumps Otherwise output false How hard is the Pegboard Problem A Lecture 41 P NP 3 mi Computer Scxence 1 II an39rksx x 39 f IiRltH Problems and Procedures 0 To know a 0m bound for a problem we need to find a 90 procedure that solves it The sorting problem is 0n log n since we know a procedure that solves it in n log n e To know a 200 bound for a problem we need to prove that there is no procedure faster than 90 that solves it We proved sorting is Qn log n by reasoning about the number of decisions needed A quot Com uter Selence LBCture 41 P NP 4 PunyUNH39ICRHH39u39Vllill How much work is the Pegboard Problem 0 Upper bound 0 0 n Try all possible permutations Lower bound 9 2 n Must at least look at every peg Tight bound 9 No one knows 39339 m er 1 n Ledure 41 P NP 5 C0 PEELIN1 1 S39VILE21I Complexity Class P Tractable Class P problems that can be solved in a polynomial 0nk for some constant k number of steps by a deterministic TM Easy problems like sorting making a photomosaic using duplicate tiles simulating the universe are all in P u quotY m I39 1 fl Ledure 41 P NP 6 C0 REFSNlYI l EYSVHESH Complexity Class NP Class NP Problems that can be solved in a polynomial number of steps by a nonde termHistic TM at once we could identify the solution I polynomial time Omniscient If we had a magic guesscorrectly procedure that makes every decision correctly we could devise a procedure that solves the problem in polynomial time Omnipotent If we could try all possible solutions n 7 IE C mP 5 5quot lle Is the Pegboard Problem in P No one knows We can t find a 0 11quot solution We can t prove one doesn t exist m CDmPtlt ir ledurethrazND g Orders of Growth pegboard puzzl e 123458789101112131415 team on p e w 11 Compm of cicnc NP Problems Can be solved byjust trying all possible answers until we find one that is rig t Easy to quickly check if an answer is right Checking an answer is in P o The pegboard problem is in NP We can easily try n different answers We can check if a guess is correct in On check all n jumps are legal 3 ComRuteI fe 55 we a Orders of Growth slmulaung unlverse pegboard puzzle 2quotltn ll39lserBort qulcksort m Cmvw s es ledurethrazND 1n Orders of Growth Pegboard puzzl e lrmactabl e tractabl e rance and maximum manorr Imamled cauldnatda 7 Batman may be able to solve inhacbable problems but computer I solve 39 large n 12 Compm of SukiASE Intractable Problems ogiogscae Moore s Law Doesn t Help 0 If the fastest procedure to solve a problem is 2quot or worse Moore s Law doesn t help much 0 Every doublingin computing power increases the solvable problem size by 1 m u p a w 14 m compu g Complexity Classes Class F problems that can be solved in polynomial time by deterministic TM Easy problems like simulating the universe are all in P Class NP problems that can be solved in polynomial time by a nondeterministic TM Includes all problems in P and some problems possibly outside P like the Pegboard puzzle Problem Classes if P at NP S imulau39ng Universe 0n3 Find Best 9a How many problems are in line on class7 in nite in me on class7 in nite How many problems are in NP but not lullmohpsz 15 m CDmeEL g Problem Classes if P NP Simulau39ng Universe 0n3 Find Best 9a How many problems W are in me on class7 in nite How many problems are in P but not n e c Som39ng n log n Som39ng P7 n log n Pg boar infinite am and 9a in u p a m Compatgg PNm Is P different from NP is there a problem in NP that is not also in P If there is one there are infinitely many Is the hardest problem in NP also in P If it is then every problem in NP is also in P o The most famous unsolved problem in computer science and mat Listed first on Millennium Prize Problems 1M an automatic A in this course lullmohpsz 17 m u p a M 1a m compw NPComplete NPComplete is the class of problems that are the hardest problems in NP 0 Cook and Levin proved that 3SAT was NP Complete 1971 If 3SAT can be transformed into a different problem in polynomial time than that problem must also be NPcomplete Pegboard ltgt 3SAT Either all NPcomplete problems are tractable in P or none of them are A 2 39A C m ter Clence Ladure 41 P NP 0 UVWI XgtHY g Vykva NPComplete Problems 0 Easy way to solve by trying all possible guesses o If given the yes answer quick in P way to check if it is right 0 If given the no answer no quick way to check if it is right No solution can t tell there isn t one No way can t tell there isn t one This part is hard to prove requires showing you could use a solution to the problem to solve a known NPComplete problem A 39m m 6139 l I C Ledure 41 P NP C0 Pgtljmu i zeVl EIVH Pegboard Problem 2 quot 1 n Lecture 41 P NF 21 M Comp g gyg gg Pegboard Problem Input a configuration of n pegs on a cracker barrel style pegboard Output if there is a sequence of jumps that leaves a single peg output that sequence of jumps Otherwise output false If given the sequence of jumps easy 0n to check it is correct If not hard to know if there is a solution A 39 7 quot Lecture 41 P NF 22 M Most Important ScienceTechnology Races 1930405Decryption Winner British Reason Bletchley Park had computers and Alan Turing Nazi s didn t 19405 Atomic Bomb Nazis vs US Winner US Reason Heisenberg miscalculated US had better physicists computers resources 19605 Moon Landing Soviet Union vs US Winner US Reason Many better computing was a big one 199052001 Sequencing Human Genome Nazis vs British 39339 m er 1 n Ledure 41 P NP C0 PBFLINH l SYSVUEEM Human Genome Race Francis Collins Craig Venter Director Of President of public National Ceera Centerfor Genomics 2 Human Genome Research 7 Picturefrom UVa CLAS 1970 0 San Mateo College 0 Yale PhD 0 Courtmartialed o Tenured Professor at U Denied tenure at SUNY Michigan Buffalo Lecture 41 P NF 24 Iii ading the Genome rquot Allli 39iw39 v 1 ul a N i I 39 1quot 411311 iquot 39 39 ifr i1 w E 1 h Plili t v I in l 39 l Tquot I l I uri 939 39I39W quot u39 quotii5 i 3 1 v 2 w v11 i I t i l v Hi 1 V 39 I A quot Com uter Sclence LeCture 41 P NP PHI34 UNlYlilw r39 Q39mexm Genome Assembly Rd1 ACCAGAATACC Read 2 TCCAGAATAA Rd3 TACCCGTGATCCA Input Genome fragments but without knowing where they are from Ouput The full genome 39A Computer Science a My UNn Iiksr n39 p39ViRGINIA Lecture 41 P NP 27 Common Superstring Input A set of n substrings and a maximum length k Output A string that contains all the substrings with total length 3 k or no if no such string exists Gene Reading Machines One read about 700 base pairs Butdon t know where they are on the chromosome 1w3 TACCCGTGATCCA Read 2 TCCAGAATAA Ream ACCAGAATACC Actual AGGCATACCAGAATACCCGTGATCCAGAATAAGC Genome Lecture 41 P NP 26 A quot III 1 l n CO Genome Assembly Rd1 ACCAGAATACC Rd2 TCCAGAATAA Rd3 TACCCGTGATCCA Input Genome fragments but without knowing where they are from Ouput The smallest genome sequence such that all the fragments are substrings ACCAGAATACC ACCAGAATACC TCCAGAATAA TCCAGAATAA lgt I TACCCGTGATCCA TACCCGTGATCCA ACCAGAATACCCGTGATCCAGAATAA n26 Lecture 41 P NP 29 I39ll quot Computer Science m1 UNl39l Rl39l39 afVlRGlXH Lecture 41 P NP 28 Common Superstring Input A set of n substrings and a maximum length k Output A string that contains all the substrings with total length 3 k or no if no such string exists ACCAGAATACC TCCAGAATAA lgt Not possible TACCCGTGATCCA n 25 Lecture 41 P NP 30 E Common Superstring In NP Easy to verify a yes solution just check the letters match up and count the superstring length In NPComplete Similar to Pegboard Puzzle Could transform Common Superstring problem instance into Pegboard Puzzle instance m 00mm 599555 0er m lullnonrasz a Shortest Common Superstring Also is NPComplete def scsuperstring pieces maxlen sum of lengths of all pieces for k in range1 maxlen if commonSuperstring pieces k return commonSuperstring pieces k Shortest Common Superstring Input A set of n substrings Output The shortest string that contains all the substrings ACCAGAATACC TCCAGAATAA TACCCGTGATCCA m 00mm 599555 0er m lullnonrasz 32 Human Genome 3 Billion base pairs 600700 bases per read 8X coverage required gt 8 3000000000 650 36923076 1213 So n a 37 Million sequence fragments Celera used 272 Million reads but could get more than 700 bases per read m u p w 34 mi CumP lti iiE 93 mwnww 33 cmw gg 53 Give up No way to solve W M lime an NPComplete all problem best quot 9 known solutions being 02quot for n a 20 Million ASE Approaches Human Genome Project Collins Change problem Start by producing a genome map using biology chemistry etc to have a framework for knowing where the fragments should go Celera Solution Venter Approximate we can t guarantee finding the shortest possible but we can develop clever algorithms that get close most of the time in 0n log n m u p z w 35 m compim was m u p z w 36 m compim was Class 24 Computability HmmgprvbemgHmkelYum Luge cslsu Computu saem Unwast a View David Evans m Hsz mm earem some problems mat it is impossible m wrim a program m solve m g 3 Godel39s Proof 6 This statement of number theory does not have any proof in the system of PM If Gwere provable then PM would be 39nconsismnt 1r Gis unprovable then PM would be 39noomplem PM cannot be complete and consistent What does it mean for an axiomatic system to be complete and consistent Derives all true srammems and no false slammem s slarling from a nim number ofaxioms and following mechanical inference rules a WWW m Comm What does it mean for an axiomatic system to be complete and consistent It mems the axiomatic Sysmm is weak is so weak it cannot express This stamment has no proof quot Why is an InconsLstentAxiomatic System less useful than an IncompleteAxiomatic System 5 cmyrrrrscmnfg a IfcnyragsomSE Inc0n5StentAxiomatic System 39Derives all true39 statements and someffalse statements starting from a finite numbeer axioms and following mechanical inference rule s some arse statements 7 L mm 39 955 u can prove one false stateme Once yo nt everything can be proveni false 2 anything Proof Checking Problem Input an axiomatic system a set of axioms and inference rules a statement S and a proof P containing n steps of S Output true ifP is a valid proof of S false otherwise How much work is a proofchecking procedure We can write a proofchecking procedure that is 6n cs m hug1mm 2o Eomvulzblllly g Proof Finding Problem Input an axiomatic system a statement S Output If S is true output a valid proof If S is not true output false How much work It is impossible It might take infinite workquot Godel s theorem says it cannot be done ES 1517 F m t 2 ey n m comptw cicncc Proof 0 A proof of S in an axiomatic system is a sequence of strings T0 T1 Tquot where The first string is the axioms For all ifrom 1 to n Tquot is the result of applying one of the inference rules to Tn1 7Tquot is S o How much work is it to check a proof a L were 8 rs HNIteLength Proof Finding Problem Input an axiomatic system a set of axioms and inference rules a statement S n the maximum number of proof steps Output A valid proof of S with no more then n steps if there is one If there is no proof of S with lt n steps unprovable At worst we can try all possible proofs r inferenoe rules 0 n sleps I possible prook mUCh Checking each proof is 9a work So there is a procedure that is 901W but it might not be the best one mamastm2e m mCumpug cISc cc Computability 12 Cumpx l f sense Algorithms 0 What s an algorithm A procedure that always terminates 0 What s a procedure A precise mechanizable description of a process Computability o Is there an algorithm that solves a problem 0 Decidable computable problems There is an algorithm that solves the problem Make a photomosaic sorting drug discovery winning chess it doesn t mean we know the algorithm but there is one o Undecidable problems There is no algorithm that solves the problem There might be a procedure but it doesn t always terminate A ESl Fall 2m5 mmquot 24 Eunpu39ablllly 13 am Comth Scl glce A ESl Fall 2uus Letmre 24 Eunpu39ablllly 14 m Com putrgr 616UFC Are there any undecidable problems The ProofFinding Problem Input an axiomatic system a statement S 0 Output If S is true output a valid proof If S is not true output false Any others How would you prove a problem is undecidable Hint how did we prove 3SAT was NP Complete once we knew Smiley Puzzle was ESl Fall 2m5 Letmre 24 cunpucabiiity 15 mi impassa esass A ESl Fall 2 u5 Letmre 24 Eunw39ablllly 15 m Compllt r 53e c Undecidable Problems 0 We can prove a problem is undecidable by showing it is at least as hard as the proof finding problem 0 Here s a famous one Halting Problem Input a procedure P described by a Scheme program and its input I Output true if executing P on I halts finishes execution false otherwise Alan Turing 19121954 0 Codebreaker at Bletchley Park Broke Enigma Cipher Perhaps more important than Lorenz 0 Published On Computable Numbers 1936 Introduced the Halting Problem Formal model of computation now known as Turing Machine After the war convicted of homosexuality then a crime in Britain committed suicide 5 years after eating cyanide apple G del39s proof ESl Fall 2m5 Letmre 24 cunpucabiiity A ESl Fall 2 u5 Letmre 24 Eunw39ablllly 15 mm Comp Halting Problem Define a procedure halts that takes a procedure and an input evaluates to t if the procedure would terminate on that input and to f if would not terminate define halts procedure input cs n r inns in 2 emuiy 9 E Computer sciervree we is Halting Examples gt halts Iambda gtlt define fact n if n 1 1 n fact n 1 fact gtlt 7 t gt halts Iambda gtlt define fact n if n 1 1 n fact n 1 fact gtlt 0 f museumiuim2emuiiiy 21 Cumpw gE quotwee lchealedalillle herer nd pro we oriy nowwe een i do mis ior true define find roof 5 agtlt39 If S isevaluates to a proof of S OthenNise evaluates to f if halts findproof exhaustive S axioms rules findproof exhaustive S agtltioms rules Where ndsproofsexhaustive s axioms rules is a procedure that mes all possible proofs siaru39ng from me axioms mat evaluams to a proof if it inds one and keeps working if it doesn t memeium2emuii 23 compr rs nee Examples gt halts lambda x x x 3 t gt halts lambda x define f x f x f x 27 f cs n r inns in 2 emuiy 2 E Computer sciervree we is Can we define halts c We could try for a really long time get something to work for simple examples but could we solve the problem make it work for all possible inputs Could we compute findproof if we had halts memeium2emuii 22 mCumpug oI ncc Another Informal Proof define contradicthalts x if halts contradicthalts null loopforever t If contradicthalts halts the if test is true and it evaluates to loopforever it doesn t halt If contradicthalts doesn t halt the if test if false and it evaluates to t It halts memeium2emuii 24 compurers nee oiis is r Lecture 3 Rules of Evaluation l a c5150 Computer Science University or Virgin David Evans Comp uter Science new WWW a Virginia ecuevens Menu 0 Language Elements 0 Why don t we just program computers using English 0 Evaluation 0 Procedures man ral m team a kale oIEvzlnlmn 2 Are there any nonrecursive natural languages What would happen to a society that spoke one Not for humans at least They would run out of original things to say Chimps and Dolphins are able to learn non recursive languages some linguists argue they are not really languages but only humans can learn recursive languages Running out of Ideas Its all been said before Eventually true for a nonrecursive language Never true for a recursive language There is always something original left to a man ral mm hamquot 3 m than 3 m Language Elements When learning a foreign language which elemenls are har est to learn Pnrrmves loe otdnern and nard to learn real mean g Means of Cornblnauon 7 Complex but all namral langalages nave slrn erall languages knew smzx knew llar ones Cl39lomsk sov 45 may 2 svo 42 v V517 Kurean a n at V517 2 vso9 knew V2117 snowmen Welsh lladdadd v ddvalq vdvn quotKllled the dragon the nan oev 1 Tuban New cunea Ednernlsn Baasm VBl7 mam Means of Absnacuon few oftlnese but olcky to learn differences across languages Ei l S I We Tok nsln Papua N W cunea rn l mlmpela nesne and l mllrlpela e both ofthem and l nlpela all oflhem and l yumluapela you and l yumllrlpela both of you and l yumlpela all ofyou andl man ral mm hamquot 3 m than 5 umpllrex Science Pages in Re V 5ed5 Report on the Algorithmc Language Scheme E n g i is h Standard Procedures 18 Morphemes Primitives Primitive EXPFESSiOHS 2 Words in Oxford 500000 Identifiers numerals 1 English Dictionary Means of Expressions 2 Grammar Rules 1005 b Program structure 2 English Grammar 384 pages Com mation forDummes Book Means of Definitions 12 Pronouns 20 Abstraction 48 pages total includes formal specification and examples C5150 Fall 2005 Lecture 3 Rules of Evaluation 10 Why don t we just program computers using English 0 Too hard and complex Nonnative English speakers don t need convincing The rest of you have spent your whole life learning English and first 5 years of your life doing little else and still don t know useful words like floccipoccinihilipilification There are thoughts that even native speakers find it hard to express By the end of today you will know enough Scheme nearly the entire language to express and understand everycomputation By PS7 you will know enough to completely and precisely describe Scheme in terms of itself try doing that in English C5150 Fall 2005 Lecture 3 Rules of Evaluation 12 quotquot Com uter Scmence RH 21w UNI39I RSl39l39Y If VI Rilquoti Why don t we just program computers using English 0 Not concise enough EngHsh To find the maximum of two numbers compare them If the first number is greater than the second number the maximum is the first number Otherwise the maximum is the second number Scheme define max a b if gt a b a b Why don t we just program computers using English 0 Limited means of abstraction There are only a few pronouns he she it they these English doesn t even have a genderneutral pronoun for a person Only Webster and Oxford can make up new ones define allows any programmer to make up as many pronouns as she wants and use them to represent anything C5150 Fall 2005 Lecture 3 Rules of Evaluation 13 A quot m 1161 lence PHI11 UNIVI ltEY g VIRtIIxIx C5150 Fall 2005 Lecture 3 Rules of Evaluation 14 quot Com t 139 menu SVlVlZSRS ITY r VIRqum Why don t we just program computers using English 0 Mapping between surface forms and meanings are ambiguous and imprecise Would you rather be paid biweekly or every week Dictionary lziiweeklyI bi wek39le 4 adj 1 Happening every two weeks 2 Happening twice a week semiweekly The exact meanings of every Scheme expression is determined by simple unambiguous rules we will learn today and refine later in the course EssentIal Scheme Express0n Express0n Express0n Express0n if Express0n Expressmg ExpressM3 Express0n define name Express0n Express0n Primitive Grammar cle a Prmt V9 1 i number fellow the Primfive l l l But does it all PrmtIe C5150 Fall 2005 Lecture 3 Rules of Evaluation 15 quot Computer Science In 12 Jlel RM39I Y g VIRquIA C5150 Fall 2005 Lecture 3 Rules of Evaluation 16 A CO PEF8N139I EYE VIFEINIA Evaluation Expressions and Values Almost every expression has a value Have you seen any expressions that don t have values When an expression with a value is evaluated its value is produced ES 1517 F m l a a mum la m Computer saws mum Evaluation Rule 1 Primitives If the expression is a prmitive it is selfevaluating gt 2 2 gt t t gt ltprimitivegt cs m rquot mus mm 1 mg 039 Evzllullon 9 Evaluation Rule 2 Names If the expression is a name it evaluates to the value associated with that name gt define two 2 gt two 2 cs m rquot mus mm 1 mg 039 Evzllullon 2n Evaluation Rule 3 Application 3 If the expression is an application a Evaluate all the subexpressions of the combination in any order b Apply the value of the rst subexpression to the values of all the other subexpressions expression0 expression1 expression2 hum 21 m cmpu t tyIsc a Rules for Application 1 If the procedure to apply is a primitive just do it 2 If the procedure is a compound procedure evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value cs n rquot mus mm 1 mg 039 22 Cump l f sci gg Making Procedures lambda means make a procedure Expression lambda Parameters Expression Parameters Parameters Vame Parameters 31511 F mm m a a sum 23 m Computer saws mum Lambda Example Tautology Function lambda make a procedure Wit7 I70 parameters 1 Wit7 body t gt Iambda t 150 ltproceduregt expects no arguments given 1 150 gt Iambda t t gt Iambda x x 150 150 31511 F mm m a a sum 24 m Computer saws mum You ve Already Used Lambda define closercolor sample color1 colorZ Expr is a shortcut for define closercolor lambda sample color1 colorZ EXPO Eval and Apply are defined in terms of each other Without Eval there would be no Apply Without Apply there would be no Eval mm 25 m campuggr s m mm 25 m campuggr s m All of Scheme Once you understand Eval and Apply you can understand all Scheme programs Exce t D We have special Eval rules for special forms like if Evaluation Rule 4 If it is a specksform do something special mm 27 m comwrs Evaluating Special Forms Eval 4if If the expression is if Expresgbno Lquotxpresson1 EXprSSSD z evaluate Expressone If it evaluates to f the value of the if expression is the value of Expre59bn2 OthenNise the value of the if expression is the value of Express0m Eval 4lambda Lambda expressions self evaluate Do not do anything until it is applied mm 2 m compa rs Class 16 NPCompleteness The Story so Far c5150 Camp Dam d Evans 332331523 13 mm HWWWS wan Banam Menu 3SAT Complexity class NPComplete The Story so Far Some NPComplete Problems an F m m F F Erma 2 W Compuwt e r Problem Classes if P at NP S imulaljng Universe 0n3 Find Best 9a How many problems are n me on class7 in nite n me on class7 in nite How many problems Sur ng arepi7n NP but not n log n Smiley infinite 0nl and 9a cs n F mus ledIre us up Enmvlelenexs Problem Classes if P NP Simulau39ng Universe 0n3 Find Best 9a How many problems W are n me on class7 in nite How many problems are ll l P but not in n How many problems Sur ng arepi7n NP but not in em 10g 0 Smileys 901quot a ECDmeEL an F m m F F Erma 4 m campuggx The 3SAT Problem 0 Input a sentence in propositional grammar where each clause is a disjunction of 3 names which may be negated 0 Output Either a mapping from names to values that satisfies the input sentence or no way meaning there is no possible assignment that satisfies the input sentence an F m m F F Erma 5 m comm Sentence clause 2 Clausev laus or and not 3SAT Example 3SAT avbv ic A av bvd A avbv d Abv cvd gta true 1 false c false d false an F m m F F Erma F m comp 3SAT a Smiley 0 Like 3stoneappletower puzzle we can convert every 3SAT problem into a Smiley Puzzle problem 0 Transformation is more complicated but still polynomial time 0 So if we have a fast P solution to Smiley Puzzle we have a fast solution to 3SAT also cs n F 7 i i w 7 m Compnt eyi s 39 Problem Classes if P at NP Simulating Universe 003 I NPC omplete 0 Note me NPF Complete class is a ring a odners are Cl rcl es 1 g39 Find Best 9a How many problems are n dne 0 class7 in nite How many problems Sm 39n are in NP but not in P7 n log n Smileys infinite 3SAT an F ms i is r saunas a Campus Quiz Responses What is Computer Science Remembered or almost remembered lirsl class mperaye s d ocedures and ang agel m ylng orem nadenon was on ne a Howl ll olhll lgs nsead o la we slaleme ls a a ulhowlo hlngs we adeaspeclllc dellnlllon in ne begnnng o ne year bu l orge l would say na compuer science 4 omenods o l d d e sudy o mperave knowledge l doesn t need o nave a compuer and ll s no a Solel39lce doesn t deal wn real things nsead numbers and daa u u u on normaon sudy o mperave Knowledge sudy o deren ypes o m it s more o a llberal ars malor Q o E 3 1 3 o 2 a 9 lt o 3 2 Q compuer problems an F a i s F mun m comps NP Complete Cook and Levin proved that 3SAT was NP Complete 1971 Take csooo m see how A problem is NP complete if it is as hard as the hardest problem in NP If 3SAT can be transformed into a different problem in polynomial time than that problem must also be NPcomplete Either all NPcomplete problems are tractable in P or none of them are I Computer 5 39 csisnriims ledquote 1670 Enmvlelenexx a W Quiz Answers What would we need to do to prove a problem is 0 n4 Find a procedure mat solves die problem mat is wt What would we need to do to prove a problem is Q n4 Prove that mere is no procedure mat solves die problem mat is faster man 9 csisn Fii mus ledquote is up Enmvlelenexx Quiz Responses What is Computer Science Problem solving logic language A way o lhll lkll lg abou compuers and programs A sysemac way o V U nrnkrn A r ml l n ll n w ne compuer language luncllol ls l seems o be o neonze on now o sysemacally solve problems n a unconal language ne sudy o loglo a way l bl The sudy problems sudy o languages used o creae nellgen compuer w ne a sclence o compulers7 Solvlrlg problemswn compuers The sudy and use o languages o lmprove ecnnology an program compuers es s ne ar o 40 an csisn Fii mus ledquote is up Enmvlelenexx Quiz Responses What is Computer Science Amusing Computer Science is a great way to find new world The bane of my existence 31511 F m m F NF amtquot 3 m Computer Science no Where we ve been Where we re going 31511 F m m F NF amtquot 5 m Computer Science mum Computer Science Expanded Definition from Class 1 Study of information processes 0 How to describe information processes by de ning prooedures o How to predict properties about information prooesses o How to elegantly and ef ciently implement information prooesses in hardware and software What have we spent most of our time on so far 31511 F m m F NF amtquot 4 m Computer Sc MM Computer Scrence CS150 so far 0 How to describe information processes by defining procedu res Programming with prooedures lists recursion Class 2 4 5 6 7 8 9 10 11 12 o How to predict properties about information processes Measuring work 9 0 Q oomplexity classes Class 9 10 11 12 13 14 15 16 o How to elegantly and ef ciently implement information processes in hardware and software Class 3 rules of evaluation 31511 F m m F NF amtquot 5 m Computer s wum CS150 upcoming o How to describe information processes by defining procedu res Programming with mutation objects databases networks How to predict properties about information processes What are we oounting when we measure work Are there problen39s which can39t be solved by prooedures o How to elegantly and ef ciently implement information processes in hardware and software How to implement a Scheme interpreter Not much in C5150 on hardware see C5230 and C5333 m5 F m m F F when 7 m camp 1 mac Famous Computer Scientists Ada first computer scientist She s in the course name Grace Hopper first compiler First bug Navy ship David Letterman nano John Backus BNF Fortran UVa dropout Tony Hoare Quicksort 31511 F m m F NF amtquot 1 m camp 5 1 n Famous Computer ScientisE vent Wndws mhevfate wmd mew pc 3 nasny rnyenrea by Dana Endamvt am XEROX Pat in are v exnuenenrea 5 am rnraprerer but want rnyenr amt NP Complete Problems NPsComplete Problems Iquen the wesanswen quremn v way m deck r r rs nan e amen m We are rrrr my mm kx qmvenl nixAug la nsne erAna aqrd vaaxman n hue nne Iquen the Wanswer rm my way m mack r r rs nan e re a ulmn un k xe rnee rm ans e re Hum re there m1 ane If Traveling Salesperson Problem drsunre runneran unern and a rmrumum mm dstant emuut ermer a man that yrsrcs eadn ynnn a mst ess than the rnrnrrnum m am 9 mm eyery my Wm 65 man mmmum drsmnoe Uave ed yen a pann easy to chedlt m 4an a avauh er names ynnn edges canneran men and a rmrumum numba er mars emuut Etha 2 Emma er me nudes sum that rm mnnadad nudes have the same duh a nu xrgryen a odormg easy to dwedlt rr rt mm L 4nqu 2 Dnsmm urn sq a Mrnesyyeeper emuut anner a assrgrrnmt er burnts m squares m a medlt rfrtrs comment u re m m2 Esme cdor and me mmber ofoo ors Lsed Chapter 3 Programming The Analytical Engine has no pretensions whatever to originate any thing It can do whatever we know how to order it topeiform It canfollow analysis but it has nopower ofanticipating any analytical relations or truths Its province is to assist us in making available what we are already acquainted with This it is calculated to effect primarily and chie y of course through its executive faculties but it is likely to exert an indirect and reciprocal in uence on science itself in another manner For in so distributing and combining the truths and the for mula ofanalysis that they may become most easily and rapidly amenable to the mechanical combinations of the engine the relations and the nature of many subjects in that science are necessarily thrown into new lights and more profoundly investigated Augusta Ada Countess of Lovelace in Notes on the Analytical Engine 1843 What distinguishes a computer from other tools is its programmabiliZy Without a program a computer is an overpriced and not very effective door stopper With the right program though a computer can be a tool for communicating across the continent discovering a new molecule that can cure cancer writing and recording a symphony or managing the logistics of a retail empire Programming is the act of writing instructions that make the computer do some thing useful It is an intensely creative activity involving aspects of art engi neering and science The best programs are written to be executed ef ciently by computers but also to be read and understood by humans The ideal programmer would have the vision of Issac Netwon the intellect of Albert Einstein the mem ory of Joshua Foer the courage of Amelia Earhart the determination of Michael Jordan the political savvy of Abraham Lincoln the creativity of Miles Davis the 13 14 CHAPTER 3 PROGRAMMING aesthetic sense of Maya Lin the wisdom of Benjamin Franklin the foresight of Garry Kasparov the hindsight of Edward Gibbon the writing talents of William Shakespeare the oratorical skills of Martin Luther King the pragmatism of Abra ham Lincoln the humility of Socrates and the self con dence of Grace Hopper Fortunately it is not necessary to possess all of those rare qualities to be a good programmer Indeed anyone who is able to master the intellectual challenge of learning a language1 can become a good programmer Since programming is a new way of thinking many people nd it challenging and even frustrating at rst Because the computer does exactly what it is told any small mistake in a program may prevent it from working as intended With a bit of patience and persistence however the tedious parts become easier and you will be able to focus your energies on the fun and creative problem solving parts 31 Problems with Natural Languages Natural languages such as English work reasonably well for human human com munication but are not well suited for human computer or computer computer communication There are many reasons for this including Complexity Although English may seem simple to you now at least compared to learning a new language it took many years of intense learning for you to learn it Despite all those years of effort you only know a small fraction of the entire language The Oxford English Dictionary contains 615000 words of which a typical native English speaker knows about 40000 Ambiguity Not only do natural languages have huge numbers of words most words have many different meanings To understand which meaning is intended requires knowing the context and sometimes pure guesswork For example what does it mean to be paid biweekly According to the American Heritage Dictionary Fourth Edition biweekly has two de nitions 1 Happening every two weeks 2 Happening twice a week semiweekly 1Which presumably anyone who has gotten this far has done at least in English 31 PROBLEMS WITH NATURAL LANGUAGES 15 So depending on which de nition is intended someone who is paid biweekly could either be paid once or four times every two weeks Even if we can agree on the de nition of every word the meaning of English sentences is often ambiguous Here is one of my favorite examples taken from the instructions with a shipment of ballistic missiles from the British Admiralty2 It is necessary for technical reasons that these warheads be stored up side down that is with the top at the bottom and the bottom at the top In order that there be no doubt as to which is the bottom and which is the top for storage purposes it will be seen that the bottom of each warhead has been labeled TOP Irregularity Because natural languages evolve over time as different cultures interact and speakers misspeak and listeners mishear natural languages end up a morass of irregularity Nearly all grammar rules have exceptions For example English has a rule that we can make a word plural by adding an s The new word means more than one of the original word s meaning actually even the standard rule is complicated since it may also be used to mean zero of them This rule works for most words word n words language n languages person n persons It does not work for others however The plural of goose is geese and gooses is not an English word the plural of deer is deer and deers is not an English word and the plural of beer is controversial and may depend on whether you speak American English or Canadian English3 These irregularities may be charming for a natural language but they are a constant source of dif culty Uneconomic It requires a lot of space to express a complex idea in a natural language Many super uous words are needed for grammatical correctness even though they do not contribute to the desired meaning Since natural languages evolved for everyday communication they are not well suited to describing the precise steps and decisions needed in a computer program As an example consider a process for nding the maximum of two numbers In English we could describe it like this To nd the maximum of two numbers compare them If the rst num ber is greater than the second number the maximum is the rst number Otherwise the maximum is the second number 2Reported in The Hummus Report httpww text le ll TMl TQhnmn mm 3See httpcrofsblogstypepadcomenglishZOO506beerorbeershtml I didn t have time to write a short letter so I wrote a long one instead Mark TWain 16 CHAPTER 3 PROGRAMMING Perhaps shorter descriptions are possible but any much shorter description proba bly assumes the reader knows a lot already By contrast we can express the same steps in Scheme in very concise way4 define max a b if gt a b a b Limited means of abstraction Natural languages provide small xed sets of pronouns to use as means of abstraction and the rules for binding pronouns to meanings are often unclear Since programming often involves using simple names to refer to complex things we need more powerful means of abstraction than natural languages provide 32 Programming Languages Hence natural languages are not well suited to programming computers Instead we need languages that are simples less ambiguous more regular more eco nomic and that provide more powerful means of abstraction than natural lan guages A programming language is a language that is designed to be read and written by humans to create programs that can be executed by computers5 Programming languages come in many avors One reason for this is that they are at different levels of abstraction Ultimately we want a program the computer can execute This means at the lowest level we need languages the computer can understand directly At this level the program is just a sequence of zeros and ones eg l l l O l O l l l l l l l l l 0 Code at this level is not easy for humans to understand or write but it is easy for a processor to execute quickly The machine code encodes instructions that direct the processor to take simple actions like moving data from one place to another performing simple arithmetic and jumping around to nd the next instruction to execute For example the sequence of zeros and ones encodes an instruction for the Intel x86 processor used on most PCs that tells the processor to jump backwards two locations In fact two locations is the amount of space needed to hold this instruction so jumping back twp locations actually jumps back to the beginning of this instruction hence it gets stuck running forever without making any progress 4Don t worry if this doesn t make sense yet It should by the end of this chapter 5We will provide a more precise de nition of programming language in Chapter after we have a formal model of a computer 32 PROGRAMMING LANGUAGES 17 The computer s processor is designed to execute very simple instructions like this one This means each instruction can be executed very quickly A typical modern processor can execute billions of instructions in a single second6 Until the early 1950s all programming was done at the level of simple instruc tions The problem with instructions at this level is that they are not easy for humans to write and understand and you need many simple instructions before you have a useful program In the early 1950s Admiral Grace Hopper developed the rst compilers A com piler is a computer program that generates other programs It can translate an input program written in a high level language that is easier for humans to cre ate into a program in a machine level language that is easier for a computer to execute An alternative to a compiler is an interpreter An interpreter is a tool that translates between a higher level language and a lower level language but where a compiler translates an entire program at once and produces a machine language program that can be executed directly an interpreter interprets the program a small piece at a time while it is running This has the advantage that we do not have to run a separate tool to compile a program before running it we can simply enter our program into the interpreter and run it right away This makes it easy to make small changes to a program and try it again and to observe the state of our program as it is running A disadvantage of using an interpreter instead of a compiler is that because the translation is happening while the program is running the program may execute much slower than a similar compiled program would Another advantage of com pilers over interpreters is that since the compiler translates the entire program it can also analyze the program for consistency and detect certain types of program ming mistakes automatically instead of encountering them when the program is running or worse not detecting them at all and producing unintended results This is especially important when writing large critical programs such as ight control software 7 we want to detect as many problems as possible in the ight control software before the plane is ying 6When a computer is marketed as a ZGHZ processor that means the processor executes 2 billion cycles per second This does not map directly to the number of instructions it can execute in a second though since some instructions take several cycles to execute Nobody believed that I had a running compiler and nobody would touch it They told me computers could only do arithmetic Grace Hopper 18 CHAPTER 3 PROGRAMMING 33 Scheme For now we are more concerned with interactive exploration than with perfor mance and detecting errors early so we use an interpreter instead of a compiler The programming system we use is depicted in Figure 33 The input to our pro gramming system is a program written in the Scheme programming language Scheme7 was developed at MIT in the 1970s by Guy Steele and Gerald Sussman based on the LISP programming language that was developed by John McCarthy in the 1950s A Scheme interpreter interprets a Scheme program and executes it on the machine processor Scheme Program Scheme Code Scheme Interpreter Low Level Language Processor Machine Language Figure 31 Levels of Abstraction Although Scheme is not widely used in industry it is a great language for learn ing about computing and programming The primary advantage of using Scheme to learn about computing is its simplicity and elegance The language is simple enough that you will learn nearly the entire language by the end of this chapter we defer describing a few aspects until Chapter and by the end of this book you will know enough to implement your own Scheme interpreter By contrast programming languages such as C and Java require thousands of pages to de scribe and even the world s experts in those languages do not agree on exactly what all programs mean 7Originally it was named Schemer but the machine used to develop it only supported 6 letter le names so the name was shortened to Scheme 34 EXPRESSIONS 19 34 Expressions Scheme programs are composed of expressions and de nitions Section 35 An expression is a syntactic element that has a value The act of determining the value associated with an expression is called evaluation A Scheme interpreter such as the one provided in DrScheme is a machine for evaluating Scheme expressions If you enter an expression to a Scheme interpreter it responds by printing the value of that expression Expressions may be primitives Scheme also provides means of combination for producing complex expressions from simple expressions The next subsections describe primitive expressions and application expressions Section describes expressions for making procedures and Section 37 describes expressions that can be used to make decisions 341 Primitives An expression can be replaced with a primitive Expression 22 PrimitiveExpression As with natural languages primitives are the smallest units of meaning Hence the value of a primitive is its pre de ned meaning Scheme provides many different primitives Three useful types of primitivesi numbers Booleans and primitive proceduresiare described next Numbers Numbers represent numerical values Scheme provides all the kinds of numbers you are familiar with and they mean almost exactly what you think they means Example numbers include 150 O 712 8The details of managing numbers on computers are complex and we do not consider them here 20 CHAPTER 3 PROGRAMMING 314159 34 999999999999999999999 Numbers evaluate to their value For example the value of the primitive expres sion 150 is 1509 Booleans Booleans represent truth values There are two primitives for repre senting true and false PrimitiveExpression 22 t l f The meaning of t is true and the meaning of f is false Primitive Procedures Scheme provides primitive procedures corresponding to many common functions Mathematically a function is a mapping from inputs to outputs A function has a domain the set of all inputs that it accepts For each input in the domain there is exactly one associated output For example is a procedure that takes zero or more inputs each of which must be a number The output it produces is the sum of the values of the inputs We cover how to apply a function in the next subsection Table 31 describes some of the primitive procedures 342 Application Expressions Most of the actual work done by a Scheme program is done by application expres sions The grammar rule for application is Expression i ApplicationExpression ApplicationExpression 22 Expression MoreExpressions MoreExpressions 22 6 Expression MoreExpressions 9By convention we use the typewrite r font to show program text and the 31 an ted font to show values In the DrS cheme interactions window values are shown in blue 34 EXPRESSIONS 21 Symbol Description Inputs Output Arithmetic add zero or more sum of the input numbers 0 if there numbers are no inputs multiply zero or more product of the input numbers 1 if numbers there are no inputs 7 subtract two numbers the value of the rst number minus the value the second number divide two numbers the value of the rst number divided by the value of the second number Comparison is equal to two things t if the input values have the same value otherwise f lt is less than two numbers t if the rst input value has lesser value than the second input value otherwise f gt is greater than two numbers t if the rst input value has greater value than the second input value otherwise f lt is less than or two numbers t if the rst input value is not equal to greater than the second input value otherwise f gt is greater than two numbers t if the rst input value is not less or equal to than the second input value otherwise f Table 31 Selected Scheme Primitive Procedures Some of these are de ned for more inputs than just the ones shown here For example the 7 procedure works on one number and on more than two numbers 22 CHAPTER 3 PROGRAMMING The value of the rst expression should be a procedure All of the primitive pro cedures are procedures in Section 36 we will see how to create new procedures The remaining expressions are the inputs to the procedure For example the expression l 2 is an application expression consisting of three subexpressions Although this example is probably simple enough that you can probably guess that it evaluates to 3 we will demonstrate in detail how it is evaluated by breaking down into its subexpressions using the grammar rules The same process will allow us to understand how any complex expression is evaluated Here is a parse tree for the expression Expression ApplicationExpression Expression MoreExpressions PrimitiveExpression Expression MoreExpressions l PrimitiveExpression Expression MoreExpressions l l l l PrimitiveExpression e l 2 The rst subexpression is a primitive expression It evaluates to the primitive addition procedure The next subexpression l evaluates to the number 1 The nal subexpression 2 evaluates to the number 2 The application expression is evaluated by applying the value of the rst expression the primitive procedure to the inputs given by the values of the other expressions In this case l 2 evaluates to 3 The subexpressions in an expression can also be complex expressions As with any nonterminal the Expression nonterminals in the application expression can be 34 EXPRESSIONS 23 replaced with anything that appears on the right side of an expression rule includ ing the application expression rule Hence we can build up complex expressions like this l 10 10 25 25 The partial parse tree is Expression ApplicationExpression Expression MoreExpressions PrimitiveExp ression Expression MoreExpressions J r Applicationl ixpression Expression MoreExpressions H 10 10 ApplicationExpression 6 25 25 This tree is similar to the previous tree except instead of the subexpressions of the rst application expression being simple primitive expressions they are now ap plication expressions We have not shown the complete parse tree for the nested application expressions instead denoting them using triangles To evaluate the output application we need to evaluate all the subexpressions The rst subexpression evaluates to the primitive procedure The second subex pression x l O l O evaluates to 1 OO and the third expression 2 5 2 5 evaluates to 5 0 Now we can evaluate the original expression using the values for its three component subexpressions x l O l O 2 5 2 5 evaluates to 1 5 0 Exercise 31 Predict how each of the following Scheme expressions is evalu ated After making your prediction try evaluating the expression in DrScheme 24 CHAPTER 3 PROGRAMMING If the result is different from your prediction explain why the Scheme interpreter evaluates the expression as it does a 150 b 150 c 100 50 x 2 0 d gt 100 50 lt 100 50 e not gt 100 100 gt 100 51 51 f g ltgt H ltgt 35 De nitions Scheme provides a simple yet powerful mechanism for abstraction We can introduce a new name using a de nition De nition 22 ole f i he Name Expression A de nition is not an expression since it does not evaluate to a value Instead of producing a value a de nition changes the state of the interpreter After a de ni tion the name in the de nition is now associated with the value of the expression in the de nition10 10Alert readers should be worried that we need a more precise de nition of the meaning of de nitions to know what it means for a value to be associated with a name This one will serve us well for now but we will provide a more precise explanation of the meaning of a de nition in Chapter 3 6 PROCEDURES 25 A name can be any sequence of letters digits and special characters such as e gt and I that starts with a letter or special character Examples of valid names include a Ada AugustaeAda gold4 9 and yikes 11 After a name has been bound to a value by a de nition that name may be used in an expression Expression 22 NameExpression NameExpression 22 Name For example 0 6 meters per secondu gt define speediofilight 299792458 gt speediofilight 299792458 gt define secondsiperihour 60 60M gt speediofilight secondsiperihour 1079252848800 36 Procedures A procedure is a sequence of actions that take inputs and produce an output In Section 341 we saw that Scheme provides some primitive procedures To con struct complex programs however we need to be able to create our own proce dures Procedures are similar to mathematical functions in that they provide a mapping between inputs and outputs but they are different from mathematical functions in two key ways 11We don t recommend using most of these names in your programs however A good pro grammer will pick names that are easy to read pronounce and remember and that are not easily confused with other names 12Text after a is a comment The Scheme interpreter will ignore any text after a until the end of the line 26 CHAPTER 3 PROGRAMMING 0 State 7 in addition to producing an output a procedure may access and modify state This means that even when the same procedure is applied to the same inputs the output produced may vary State makes procedures much harder to reason about In particular it breaks the substitution model of evaluation we introduce in the next section We will ignore this issue until Chapter and focus until then only on procedures that do not involve any state Resources 7 unlike an ideal mathematical function which provides an in stantaneous and free mapping between inputs and outputs a procedure re quires resources to execute before the output is produced The most impor tant resources are space memory and time A procedure may need space to keep track of intermediate results while it is executing Each step of a pro cedure requires some time to execute Predicting how long a procedure will take to execute and nding the fastest procedure possible for solving some problem are core problems in computer science We will discuss these in Chapter In fact even knowing if a procedure will nish that is ever produce an output is a challenging problem In Chapter we will see that it is impossible to solve in general For the rest of this chapter however we will view procedures as idealized mathe matical functions we will consider only procedures that involve no state and we will not worry about the resources our procedures require 361 Making Procedures Scheme provides a general mechanism for making a procedure The syntax is Expression i ProcedureExpression ProcedureExpression 22 lambda Parameters Expression Parameters 22 6 I Name Parameters Evaluating a ProcedureExpression produces a procedure that takes as inputs the Parameters following the l ambcla13 The body of the procedure is the Expression 13Scheme uses lambda to make a procedure because it is based on LISP which is based on Lambda Calculus which we will cover in Chapter 3 6 PROCEDURES 27 Note that a ProcedureExpression can replace an Expression This means any where an Expression is used we can create a new procedure This is very power ful since it means we can use procedures as inputs to other procedures and create procedures that return new procedures as their output 362 Substitution Model of Evaluation For a procedure to be useful we need to apply it In Section 342 we saw the syntax and evaluation rule for an ApplicationExpression when the procedure to be applied is a primitive procedure The syntax for applying a constructed procedure is identical Expression i ApplicationExpressi on ApplicationExpression 22 Expression MoreExpressions MoreExpressions 22 6 l Expression MoreExpressions We need a new rule though for evaluating the application In this case the rst Expression evaluates to a procedure that was created using a ProcedureExpres sion so we can think of the ApplicationExpression as the underlined part is the replacement for the ProcedureExpression ApplicationExpression i lambda ParametersExpression MoreExpressions To evaluate the application we evaluate the MoreExpressions in the application expression These expressions are known as the operands of the application The resulting values are the input to the procedure There must be one exactly expres sion in the MoreExpressions corresponding to each name in the parameters list Then evaluate the evaluate that is the body of the procedure Except instead of evaluating it normally whenever any name that matches a name in the parame ters list appears that name evaluates to the value of the corresponding input This is similar to the way binding worked in Post Production Systems Section When a value is matched with a procedure parameter that parameter is bound to the value When the parameter name is evaluated the result is the bound value 28 CHAPTER 3 PROGRAMMING Example 31 Squaring Consider evaluating the following expression lambda X X X X 2 It is an ApplicationExpression where the rst sub expression is the ProcedureEx pression lambda X X X To evaluate the application we evaluate all the subeexpressions and apply the value of the rst subexpression to the values of the remaining subexpressions The rst subexpression evaluates to a procedure that takes one parameter named X and has the expression body X X There is one operand expression the primitive 2 that evaluates to 2 To evaluate the application we bind the rst parameter X to the value of the rst operand 2 and evaluate the procedure body X X After substituting the parameter values we have 2 2 This is an application of the primitive multiplication procedure Evaluating the application results in the value 4 The procedure in our example lambda X X X is a procedure that takes a number as input and as output produces the square of that number We can use the de nition mechanism from Section 35 to give this procedure a name so we can reuse it define square lambda X X X X This de nes the name square as the procedure After this we can use square to produce the square of any number gt square 2 4 gt square l4 116 gt square square 2 l6 Abbreviated Procedure De nitions Since we commonly need to de ne new procedures Scheme provides a condensed notation for de ning a procedure De nition 22 de f lne Name Parameters Expression 3 7 DECISIONS 29 This is incorporates the lambda invisibly into the de nition but means exactly the same thing For example define square lambda X X X X can be written equivalently as define square X X X X The two de nitions mean exactly the same thing Exercise 32 9 De ne a procedure cube that takes one number as input and produces as output the cube of that number 9 De ne a procedure computeecost that takes as input two numbers the rst represents that price of an item and the second represents the sales tax rate The output should be the total cost which is computed as the price of the item plus the sales tax on the item which is its price time the sales tax rate For example computeecost l 3 O 05 should evaluate to 1 3 65 37 Decisions We would like to be able to make procedures where the actions taken depend on the input values For example we may want a procedure that takes two numbers as inputs and evaluates to the maximum value of the two inputs To de ne such a procedure we need a way of making a decision A predicate is a test expression that is used to determine which actions to take next Scheme provides the if expression for determining actions based on a predicate The IfExpression replacement has three Expression terms For clarity we give each of them names as denoted by the subscripts 30 CHAPTER 3 PROGRAMMING Expression 22 IfExpression IfExpression 22 1 f Expressionprediwte Expressioncwsequem EXPTBSSiOHAltemate gt The evaluation rule for an IfExpression is to rst evaluate the predicate expression If it evaluates to any non false value the value of the IfExpression is the value of the consequent expression and the alternate expression is not evaluated at all If the predicate expression evaluates to false the value of the IfExpression is the value of the alternate expression and the consequent expression is not evaluated at all The predicate expression determines which of the two following expressions is evaluated to produce the value of the IfExpression Note that if the value of the predicate is anything other than false f the con sequent expression is used For example if the predicate evaluates to t to a number or to a procedure the consequence expression is evaluated The if expression is a special form This means that although it looks syntacti cally identical to an application that is it could be an application of a procedure named i f it is not evaluated as a normal application would be Instead we have a special evaluation rule for if expressions The reason a special rule is needed is because we do not want all the subexpressions to be evaluated With the nor mal application rule all the subexpressions are evaluated and then the procedure resulting from the rst subexpression is applied to the values resulting from the others With the if special form evaluation rule the predicate expression is always evaluated but only one of the following subexpressions is evaluated depending on the result of evaluating the predicate expression This means an if expression can evaluate to a value even if evaluating one of its subexpressions would produce an error For example if gt3 4 7 evaluates to 7 even though evaluating the subexpression x would produce an error Because of the special evaluation rule for if expressions the consequence expression is never evaluated Example 32 Maximum 38 SUMMARY 31 Now that we have procedures decisions and de nitions we can understand the max procedure from the beginning of the chapter The de nition define max a b if gt a b a b is a condensed procedure de nition It is equivalent to define max lambda a b if gt a b a b This de nes the name max as the value of evaluating the procedure expression lambda a b if gt a b a b This is a procedure that takes two inputs named a and b Its body is an if expres sion with predicate expression gt a b The predicate expression compares the value that is bound to the rst parameter a with the value that is bound to the sec ond parameter b and evaluates to t if the value of the rst parameter is greater and f otherwise According to the evaluation rule for an if expression if the predicate evaluates to any non false value in this case t the value of the if expression is the value of the consequent expression a If the predicate evaluates to f the value of the if expression is the value of the alternate expression b Hence our max procedure takes two numbers as inputs and produces as output the greater of the two inputs Exercise 33 ltgt De ne a procedure max3 that takes three inputs and produces as output the maximum value of the three inputs For example max3 5 7 3 should evaluate to 7 38 Summary At this point we have covered enough of Scheme to write useful programs In fact as we will see in Chapter we have covered enough to express every possible computation 32 CHAPTER 3 PROGRAMMING Here we summarize the grammar rules and evaluation rules Each grammar rule has an associated evaluation rule This means that any Scheme fragment that can be described by the grammar also has an associated meaning that can be produced by combining the evaluation rules corresponding to the grammar rules Program 22 6 Expression Program De nition Program A program is a sequence of expressions and de nitions De nition 22 ole f i he Name Expression A de nition associates the value of the expression with the name De nition 22 ole f i he Name Parameters Expression Abbreviation for define Name l ambcla Parameters Expression Expression 22 PrimitiveExpression NameExpression ApplicationExpression ProcedureExpression IfExpression The value of the expression is the value of the replacement expression PrimitiveExpression 22 Number l t l f primitive procedure Evaluation Rule 1 Primitives A primitive expression evaluates to its pre de ned value NameExpression 22 Name 38 SUMMARY 33 Evaluation Rule 2 Names A name evaluates to the value associated with that name ApplicationExpression 22 Expression MoreExpressions Evaluation Rule 3 Application To evaluate an expression a Evaluate all the subexpressions h Then apply the value of the rst subexpression to the values of the remaining subexpressions MoreExpressions 22 6 Expression MoreExpressions ProcedureExpression 22 lambda Parameters Expression Evaluation Rule 4 Lambda Lambda expressions evaluate to a procedure that takes the given parameters and has the expression as its body Parameters 22 6 Name Parameters IfExpression 22 1 f Expression predicate Expressioncmequem Expressioninmate Evaluation Rule 5 If To evaluate an if expression a evaluate the predicate expression then b if the value of the predicate expression is a false value then the value of the if expression is the value of the alternate expression otherwise the value of the if expression is the value of the consequent expression The evaluation rule for an application Rule 3b uses apply to perform the appli cation We de ne apply using the two application rules 0 Application Rule 1 Primitives If the procedure to apply is a primitive procedure just do it Menu 0 Environments 0 Evaluation Rules 0 Exam 1 GamaquotSS 955951 define nest lambda x Elilion model 39l39 X ofevaluation tell gt nest 3 4 3357 8 valuate Review Names Places Mutation o A name is a place for storing a value 0 define creates a new place 0 cons creates two new places the car and 0 set name an changes the value in the place name to the value of expr o setcar pair expl changes the value in the car place of pa rto the value of expr o setcdr pair saw changes the value in the cdr place of parto the value of expr a a cumr r r c 19 39 i C P35 K i a Lambda and Places 0 lambda x also creates a new place named x o The passed argument is put in that place gt de ne x 3 gt lambda x x 4 4 How are these 3 places different Location Location Location 0 Places live in frames 0 An environment is a pointer to a frame 0 We start in the global environment 0 Application creates a new frame 0 All frames except the global frame have exactly one parent frame global frame has no parent 5 Compiy gmcsV Environments 1 envlronment 2ltprimitivegt null zltpnmiuvenullgt The global environment points to the outermost frame It stans with all Scheme primitives gt define x 3 mm m mus mum 19 z 7 oompumsre nce v m How to Draw a Procedure A procedure needs both code and an environment We ll see why soon Environment We draw procedures like this pointer Code pointer parameters X body x x mm m mus mum 19 Envlronmenl Procedures envlronment 2ltprimitivegt null 1ltprimitivenullgt gt define double lambda x x x parameters x body xx mm m mus mum 19 z n Cumputh cicncE mm Procedures global environment 2ltprimitivegt null 1ltprimitivenullgt gt define double lambda x x x a L C R 5 T rs How to Draw a Procedure for artists only Environment pointer gtltgtlt Ptocedme Body Input paramemts in mouth mm m mus mum 19 Envlronmenl m mcvmpw s es Application Old rule Substitution model Apply Rule 2 Compounds If the procedure is a compound procedure evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value 12 CumputhSciclAc c mm Lecture 18 TheStory So Far C5150 Computer Science Univemity of Vnginia Computer Science David Evans httpwwwcsvirginia eduevans insertonetree define insertonetree cf el tree if null tree Each time we call maketree nu er nuquot insertonetree the size if cf e getelement tree of the tree approximately maketree halves if it is well inserteltree cf el getleft tree balanced getelement tree getrig ht tree maketree getleft tree getelement tree Each application IS inserteltree cf el getright tree COI39lStant tlmE The running time of insertonetree is in 6 log n where n is the number of elements in the input tree which must be well balanced A Lecture 18 Mutation 3 mi Computh Science extractelements We need to make a list of all the tree elements from left to right define extractelements tree if null tree null append extractelements getleft tree cons getelement tree extractelements getright tree Menu Finish insertsort tree Course roadmap Introducing Mutation A few people have extensions on Exam 1 so no talking about the exam questions until Wednesday If you have an extension on Exam 1 don t read Chapter 9 until you turn in the exam Lecture 18 Mutation 2 m Comm insertsorthelper define insertsorthelper cf lst if null lst null insertonetree cf car lst insertsorthelper cf cdr lst No change other than using insertonetreebut evaluates to a tree not a list 0 1 O 2 O 5 O 8 0 A Lecture 18 Mutation 4 COmPHES rsl le x 9 C s 39 Lecture 18 Mutation 5 IlllllI omf ltt fn Eu Running time of insertsorttree define insertonetree cf el tree ifnull tree maketree null el null if cf el getelement tree maketree insertone tree cf el getleft tree getelement tree getright tree maketree getleft tree getelement tree insertonetree cf el getright tree n number of elements in tree log n define insertsort tree cf lst define insertsort helper cf lst ifnull lst null insertonetree cf inscear sslttthelpercfcdrlst log n extractelements insertsort helper cf lst n number of elements in lst Lecture 1839 Mutation 5 399 Com inter Science 39 M mm on r 5 Vlwdxm Growth of time to sort random list quot2 insertsort n log n insertsort tree i 7 compuwt gs gt 31757 n 4000time 66137 337 6 37 345 389 503 319 4 09 402 gtteshgrow insertrsortrhee 250 time 30 n 2000tim 01 n 4000 me 11537 n 4000time 1001 45 317 463 436 83 06 20 33 Cvmvasi lullquot in mi 9 What if tree is not wellbalanced insertone worst case A pathologically worst case is in ni unbalanced tree is as bad as a list i a compuwt gs Can we do better Making all those trees is a lot of work Can we divide the problem in two halves without making trees This is the famous Quicksort algorithm invented by Sir Tony Hoare See Chapter 8 I There are lots ofways m do a little bit better but no way to do I i i i I in ma log n We ll explain why later in the course I Ledquote IR Mulzllon ID A Course Roamap cn v 1 E 25 u E e u u m 4 n E E v 5 3 A g 9 E 5 En m o E at 3 E a s g e g1 31 8 g g o 3 g 3 it 390 a 5 i 3 I 5 a in a S 3 a 6 e a e 19 K g N m H e E I z E t ou In E 8 are T E 131 U U u g c a quot39 so here 2 E E 8 i 6 8 39 vs 3 a a Analysis 395 6 6 E 5 lullquot in mi 11 Computer Science C5150 so far 0 How to describe information processes by defining procedu res Programming with procedures lists recursion Chapters 3 4 o How to predict properties about information processes Predicting running time 9 0 Q o How to elegantly and ef ciently implement information processes Chapter 3 rules ofevaluation lullquot in mi 12 Class 14 Intracta ble Problems C5150 Computer Science David Evans Smileys Problem Input n square tiles Output Arrangement of the tiles in a square where the colors and shapes match up or no its impossible A r3515 Fall zuus Letlue 141mrznable prmlems 2 II Compu e r cieg Thanks to Peggy Reed for making lhe Smiley Puzzlesl Problems and Procedures 0 To know a 07 bound for a problem we need to find a f procedure that solves it The sorting problem is 0n10g n since we know a procedure that solves it in n log n c To know a 20 bound for a problem we need to prove that there is no procedure faster than 90 that solves it We proved sorting is Qn log n by reasoning about the number of decisions needed 4 Ef cmpsessr s ms r3515 Fall zuus Letlue 141mrznable prmlems How much work is the Smiley s Problem 0 Upper bound Q 0n Try all possible permutations 0 Lower bound 52 Must at least look at every tile 0 Tight bound 6 No one knows Complexity Class P Tractable Class P problems that can be solved in polynomial time 00 for some constant k Easy problems like sorting making a photomosaic using duplicate tiles simulating the universe are all in P A ESl Fall zuus Letmre 141nnzdable Prmlems 5 m Compufgr A c nevncc A E515 Fall 2 5Lelue 141mrznable Prmlems 5 i Comput cr Scxcgcc Complexity Class NP Np probems Class NP roblems that can be solved in nondel ermgistic ol nomial time can be SOIVPTd bYJUSt trymg allpoSS39ble If Id p y bl I answers until we find one that Is right we cou try a possi e so utions at once we could identify the solution in polynomial time lijzzeiiigufrznguszslfna answer 395 nght Alternately If we had a magic guesscorrectly h bl procedure that makes every decision correctly T e 5m39 eys pro em 395 In NP we coulddevise a procedure that solves the We can easily try n different answers pmblem 39 p lyn m39al t39me39 We can quickly check if a guess is mlsde muonlsnotpreclse ugn to be satisfyan yetl We wlll correct check a n mes need to understand better what a stepquot means when we measure work to delne lS ro erly mannered xapa 7 PM Compuwt eyg mannered xapa a computersc were r Is the Smiley s Problem in P Orders of Growth No one knows slrnulaung unlverse We can t find a 0 11quot solution We can t prove one doesn t exist srnlleys puzzle 2quotltn ll lSerBOrt qulcksort cs n all me team u lnlndzble mum Computer Scicncc Mme cs n all me team u lnlndzble mum m campatasa urer Orders of Growth Orders of Growth Srnlleys puzzle lrmactabl e tractable endurance and maximum scream Imawed abudnatda r 7 Batman may be able to solve inhacbable problems but computer solve H large n 23453739101112131415 cs n all me team u lnlndzble mum n mcumpm fgcttneg cs nhllmn 12 E mm Lecture 9 Recursing Recursiver ya 39A ii39 739 71 7 Richard Feynman39s Van parked outside the theater where QED is playing T man in QED David Evans httpwwwcsvirgin ia eduevans Menu Recursive Procedures GEB Chapter V Fibonacci RTNs Music and Recursion A Lecture 9 Recursing Recursively 2 m Computgr Sgepge Example De ne a procedure findclosestnumber that takes two inputs a goal number and a list of numbers and produces the number in the list numbers list that is closest to goal gt findclosest number 150 list 101 110 120 157 340 588 157 gt findclosest number 12 list 1 11 21 11 gt findclosest number 12 list 95 95 Find Closest Number Be optimistic Assume you can de ne findclosestnumber goal numbers that nds the closest number to goal from the list of numbers What if there is one more number Can you write a function that nds the closest number to match from new number and numbers A Lectue 9 Recursing Recursively 3 ll Computer Scmgc e Lecture 9 Recursing Recursively 4 m Computsr Selervmce Finding the Closest Strategy If the rst number is closer than the closest number of the rest of the numbers use the first number Otherwise use the closet number of the rest of the numbers Optimistic Function define findclosest goal numbers if lt abs goal car numbers abs goal findclosestnumber goal cdr numbers car numbers findclosestnumber goal cdr numbers Lectue 9 Recursing Recursively 5 mi Computgr mu C mR9335 Lecture 9 Recursing Recursiver 5 Defining Recursive Procedures 2 Think of the simplest version of the problem something you can already solve If there is only one number that is the best match team 9Reur nq Rumslvely 7 we s ComRuteI L de ne ndnclosestnrlumber goal numbers lf 1 lengun numbers car num ers lf lt abs 7 abs 7 goal car numbers oal ndnclosestnrlumber I goal cdr numbers T estl n 9 car numbers ndnclosestnrlumber goal cdr numbers gt ndclosestnumber 150 list 101 110 120 157 340 588 57 1 gt ndclosestnumber 0 list 1 1 gt ndclosestnumber 0 list rst expects argument of type ltnonempty istgt given The Base Case define ndclosestnumber goal numbers if 1 length numbers car num ers iflt abs goal car numbers 5 goal findclosest number goal cdr numbers car nu mbels findclosestnumber goal cclr numbers Same as before may a Comp uwt eyg Generalizing findclosestnumber How would we implement findclosest numberwithoutgoingover What about findclosest word The closenss metn39c should be a procedure parameterl teammate summary 9 Cvmprlsggit r man In u Cvmpm findclosest de ne ndclosest goal lst closeness if 1 length lst car lst if lt closeness goal car lst c osenss goal nd losest goal cdr lst closeness car lst ndclosest goal odr lst closeness I How can we implement d 39 39 wiu39l ndclosest may ew findclosestnumber define findclosestnumber goal numbers findclosest goal numbers lambda a b abs a b define findclosestbelow goal numbers findclosest goal numbers lambda a b if gt a b a b 99999 may 2 cmp r g findclosest findclosest define findclosest goal lst closeness define findclosest goal lst closeness if 1 Gength st lf 1 length lSt car lst Far lSt pickclosest closeness goal car lst If lt Closeness goal car lSt findclosest goal cdr lst closeness closeness goal I tlf39nd39Closest goal Cdr lst closeness define pickclosest closeness goal num1 num2 Far 5 iflt closeness goal num1 findclosest goal cdr Ist closeness closeness goal numz I How can we avoid needing to evaluate find closest tvvice numz Letlure g Retursma Returslvely 13 Compuktgr ciepgeh Letmre g Retursing Retursively 14 Corn pu35 cvisrlsg Seen Anything Like This GEB Chapter V define findbest match sample tiles colorcomparator You could spend the rest of your life just studying ifengthties1 things in this chapter 25 pages cartiles Music Harmony pickbettermatch Stacks and Recursion sample TheOlOQY car tiles Language Structure findbestmatch de ne pickbettermatch Number Sequences sample sample tilel tileZ Chaos cdr tiles colorcomparator Fractals PS3 out today coorcomparator ifcolorcomparamr sample Quantum Electrodynamics later lecture colorcomparator tilecolor tilel tilecolor tile2 DNA ater lecture Sameness indifferentness Gameplaying algorithms later lecture Letlure g Retursma Returslvely 15 Compllwtlekr39 Letmre g Returslng Returslvely 15 Comp u FibonaCCI 5 Problem Rabbits I t mil Filius Emma 1202 in Pisa 8 1 Suppose a newlyborn pair of rabbits one male one 8 1 female are put in a field Rabbits mate at the age of one month so that at the end of its second month a female can t produce another pair of rabbits 8 2 l Suppose that our rabbits never die and that the female 39 5 always produces one new pair one male one female 8 a g 3 every month from the second month on How many pairs will there be in one year 8 g 8 5 From Lemma 9 Returst Returswely 17 Inquot Compufgg ggeg cew Letmre g Returslng Returslvely 15 m Compus fu Egan s Fibonacci Numbers GEB p 136 These numbers are best defined recursively by the pair of formulas FIBO n FIBO n 71 FIBO n 7 2 for n gt 2 FIBO 1 FIBO 2 1 Can we turn this into a Scheme prooedure m 00mm Seems we H mm 9Reur nq many 19 Defining fibo fibo n evaluates to the nth Fibonacci number define fibo n if or n 1 n 2 FIBO1FIBO21 1 base case FIBOn f39b 1 FIBOn71 I o n FIB0n72 fibo n 2 forngt2 mum 9Reur nq many 21 you can solve it if you could how would you solve a bigger problem Defining FIBO Be optimistic assume These numbers are best defined recursively by the pair of formulas gt fibo 10 55 gt fibo 2 Think of the simplest FIBO n version of the problem FIBO n 71 something you can FIBO n 7 2 already solve or n gt 3 Combine them to solve FIBO1 FIBO 2 1 the problem i 9 km NW 2n Comauggg 938955 Fibo Results gt fibo 2 Why can t our 4Mx Z bo 3 Apollo Guidance gt bo 4 Computer figure out how many rabbits there will be in 5 years 60 5t7 work g a er 4 hours To be oontinued m Cmvm SE Recursive Transition Networks ORNATE NOUN I Can we describe this using Backus Naur Form km 23 CurrWEE mm 9 Rumsmq may m Cmvm SE Recursive Transition Networks ORNATE NOUN b39 La ORNA TE NOUN NOUN mm 9 Rumsmq may n Cummf Recursive Transition Networks ORNATE NOUN ORNA TE NOUN NOUN ORNA TE NOUN AR 77le ADJEC 77 V5 NOUN mmmmmq may 25 mi Comnuggg g ms Recursive Transition Networks ORNATE NOUN mg DRVA TENDUIV AR Tl EADfCTllES NOUN ADfCTllES ADECTIVEADEETIVES ADfCTllES may 27 IIICquot PH5 E39 LXW Music Harmony Keines Harmom39sches Labyrinth Little Harmonic Labyrinth mum 9Reur nq many 29 Recursive Transition Networks ORNATE NOUN beg7 DRVA TENDUIV AR Tl EADfCTllENDUN DRVA TENDUIV AR Tl EADfCTVEADEETIVENDUN mmmmmq may 25 mi Comnuggg g ms Recursive Transition Networks ORNATE NOUN bem DRAM TENDUIV DPTARTIQEADfCTllESNDUN ADECTIVES ADECTIVEADfCTllES E Which notation is better 23 mcvmvm 39 Hey Jude John Lennon and Paul McCartney 1968 mum 9Reur nq many an Class 32 Computability in Theory and Practice c5150 Computer Science CA University oivlrgmla DaVId vans mo Www c5 Vll glnla eduevans Menu Lambda Calculus Review Computability in Theory and Practice Learning to Count slsn Fall znns team 32 nnwtahllll l 2 l39IETECmPllssx ciwss Universal Computation IIIIIIzlzlzIzlzlzlzlzlzlzlzlzlzlzIzlzlz ReadWrite Infinite Tape Mutable Lists Finite State Machine Numbers to keep track of state Processing Way of making decisions if Way to keep going To prove Lambda Calculus is as powerful as a UTM we must show we can 39 w we need m simulate any TM Don t search for T search for if T M Qty x My x M 7 y y cha pca as II III III 51m Fall znns team 2 mullahllly a 4 Comm Finding the Truth T E M y x F E M y y if kp M M pca if T MN Apca pm My x MN 130611 11091 0 0a MN W W may x MN eBayMnNe M Is the if necessary and and or and E M Ky ifxy F 01 E be Ky ifx T y amalzmlm 32mm 5 ow slsnrallznnsLummaztnnwtahlllly 5 moomw Lambda Calculus is a Universal Computer zlz I2 Izlzlzlzlzlzlzlzlzlzlzlzlzlz I2 lzlzl Computability in Theory and Ti Practice 39 R dW39 f39 39 T ea MUEEEIQL39L39E ape Intellectual Computability finite State Machine Discussion on TV Video Numbers to keep track of state Processing Way of making decisions if Way to keep going A C5150 Fall 2005 Lectule 32 Computability 7 I39llquot Selegce C5150 Fall 2005 Lecture 32 Computability w Mm i r mm A s m Compirg sesasa Ali G Multiplication Problem 7 m 0 Input a list of 2 numbers with up to d 2992222222222222 WW digits eaCh 0 Output the product of the 2 numbers S it decidabe gggggggggggggzzzzzzzzzzzzzzmm Yes a straightforward algorithm 2222222222222222222999999 solves it Is it tractable how much work 3 Yes it using elementary multiplication techniques it is 0d2 v Can realcomputers solve it quotquotquotquot c5150 Fall 2005 Lecture 32 Computability 9 E Computg Ejale m M m Fm rm Data Mnduw mi i A a i J V 3 What about c 999999999 999999999 999999999 3 3 g int main void 99 int alig 999999999 Results from SunOS 58 9800999990199 970298999029701 96059600903940400 printf quotValue dnquot alig Value 3quot t wEm m m l L aligtaligl 99 d I Vaue 215752093 L nlgsl prin quot aue o nquotaig Elle Edit how Language Sgheme Special Windows elp i Plr39ntfquotIageQ9 odnquot ang Value 1462353861 A a 399 a 399 l gt 9 999999999 99 99 9390999990199 pr39ntf I39value o dnquot al39g gt w 999999999 99 99 99 919293999929791 gt ii 999999999 99 99 99 99 A C5150 Fall 2005Lectule 32Computability Ali G was Right Theory assumes ideal computers Unlimited perfect memory Unlimited finite time Real computers have Limited memory time power outages flaky programming languages etc There are many decidable problems we cannot solve with real computer the numbers do matter M F m m 2 cy 3 E Computer 5 39 mum What is 42 fortytwo XLII cuarenta y dos cs 15 r mus m 2 comma 5 Cumputch My M Meaning of Numbers pred succ N gt N succ pred N gt N succ pred succ N gt succ N M F m m 2 cy 7 m compa r s Lambda Calculus is a Universal Computer IIIIIIzlzlzIzlzlzlzlzlzlzlzlzlzlzIzlzlz ReadWrite Infinite Tape Mutable Lists S achm Finite State Machine 39 7 Numbers to keep track of state Processing 39 Way of making decisions if 7 Way to keep going cs 15 r mus m 2 comma 4 compums mum Meaning of Numbers 42ness is something who s successor is 43ness 42ness is something who s predecessor is 41ness Zero is special It has a successor oneness but no predecessor cs5msm2c 5 mCumputhS cncc My Meaning of Zero zero zero gt T zero succ zero gt F zero pred succ zero gt T cs 15 r mus m 2 comma IE Cquot39 P fs Is this enough Can we define add with pred succ zero Can we de ne lambda terms and law that behave like zero zero pred and succ add a Mylf zero x y add pred x succ y Hint what if we had cons car and cdr cs n rquot mus mm 2 mummy 19 Comp cs n rquot mus mm 2 Mummy 2 Comp u Numbers are Lists Making Pairs zero null pred E cdr define makepair x lambda selector y if selector x y succzkx cons Fx define carofpair p p t define cdrofpair p p f cs n rquot mus mm 2 mummy 21 gm CDmP sgi sienss cs n rquot mus mm 2 mummy 22 gm CDMPwESSEiiSE cons and car cdr too cons a Amyxuxy cons a Myzzxy cons M N Mhy zzxy M N A B Wham N car kpp T gt B MJMN Cdr 5 11717 F carsxppT Tammy cdr consMN car cons M N 2 car MzMN 2 App T MzMN cdr AzzMN 2 App F AzzMN gtBuzzMN TgtBTMN BLZ ZMN F ABMMXMN ABFMN gt my MN gt B M gt B N Emmi 23 cmmx Emmi 24 cmmg Null and null Null and null nuIIEAxT nuIIEAxT null E Ml Xylzf null E Ml Xylzf null null gt 7xx ijtzf 70 T null cons M N gt 7xx Aylzf MzMN a B 70 T7ty7tzF a B M1 MN7y7tzF gtBT gtB7ty7 zF MN gt B F ensemmmumem 25 ComRuwt gsfienee ensemmmumemy 25 comnuwt gsf 42 WMzxymymltxzzmmy Counting My7nzzxy My y Myllzzxy My y Myllzzxy My y 0 a null 1 E cons F 0 2 E cons F 1 3 E cons F 2 succ E Mtcons F x y y y y y y y y y y y y pred E Mtcdr x heyhwy My y My7zzxy My y My7zzxy My mixuxy My y MT csnsnrzumnsmnmazcompuubIny 27 CDmp ggg clcncg emmumneumaeeiy 2 Cumpgg ggEe Arithmetic Lambda Calculus is a Universal Computer zeronu zz z Izlzlz zlzlzlzlzl zlzlzlzlzlz z Izlz suc M cons Fx pred MXF pred 1 My F cons F null ABcons FnuF ReadWrite Infinite Tape 39 Mutable LISlS Mywy F null F F nne39sB W Finite State Machine gt B M1 F null F 39 mbers to keep track of state gt B F F null Processing We have lhiS bui aBnull Wecneaiedu ge Wa ofmakin decisions if W N ayto eep going E 0 lo make recursive deliniiionsl cs n F me 1 2 ey 29 m cummt e science cs n F me 1 2 ey 3 m Computer Sc mm m Lecture 36 M od el l n g Co m p u U n g C8150 Computer Science University of Virginia DaVld EVa 5 Computer Science httpwwwcsvirginiaeduevans How convincing is our Halting Problem proof define contradicthalts x if halts contradicthalts loopforever t contradictshalts cannot exist Everything we used to make it except halts does exist therefore halts cannot exist IThis proof assumes Scheme exists and is consistent I A Lecture 36 Modellng Computlng 2 DrScheme Is DrScheme a proof that Scheme exists ma n 1 10000 SchemeCharmePythonetc all fail to evaluate some program Lecture 36 Modeling Computing 3 Solutions 0 Option 1 Prove Scheme does exist Show that we could implement all the evaluation rules if we had Python our Charme interpreter would be a good start but we don t have Python 0 Option 2 Find a simpler computing model Define it precisely Show that contradicthalts can be defined in this model mi Computer SClenCC rum UNn Izksirr y39VIRmxu A Lecture 36 Modeling Computing 4 quot Computer Selence in 16 UV1 ikl n39 a VIRGIN Modeling Computation For a more convincing proof we need a more precise but simple model of what a computer can do 0 Another reason we need a model Does complexity really make sense without this how do we know what a step is are they the same for all computers Lecture 36 Modellng Computlng 5 6D VxHJ a A Lecture 36 Modellng Computlng 6 How should we model a Computer Introducing The IBM 5100 Pu ah e computer Turing inve quot model we ll use today in 1936 What computerI was he modeling M MMWMFWmenmdh Winn madiL Erie Lecture 36 Modeling Con Computers before WWII IBM 5100 1975 lm Computer Selence mm UNh39lZRSHY 7139 VIRGIN a C S Lecture 36 Modellng Computing 8 I39llquot Modeling Computers 0 Input Without it we can t describe a problem 0 Output Without it we can t get an answer 0 Processing Need some way of getting from the input to the output 0 Memory Need to keep track of what we are doing Lecture 36 Modeling Computing 9 Compgtgv ggg gm Modeling Input i I I u 39939 l39l Apple39s Newton Messagead rulfN UNlVlik SllT I39VlRUlXH 39 1 Engelbart s mouse and keypad v3 J Lecture 36 Modeling ompu Ing Tu ring s Computer Computing is normally done by writing certain symbols on paper We may suppose this paper is divided into squares like a child s arithmetic book Alan Turing On computable numbers with an application to the Entscheidungsproblem 1936 Simplest Input 0 Noninteractive like punch cards and papertape o Onedimensional just a single tape of values pointer to one square on tape 0011001000 How long should the tape be Infinitely long We are modeInga computer not building one Our model should not have silly practical limitations like a real computer does Lecture 36 Modeling Computing 11 A Lecture 36 Modeling Computing 12 Modeling Output 0 Blinking lights are cool but hard to model 0 Output is what is written on the tape at the end of a computation A Lecture 35 Modeling computing 13 m Computer Seance Modeling Processing Brains Rules for steps Remember a little For the present I shall only say that the justification lies in the fact that the human memory is necessarily limited Alan Turing A Lecture 35 Modeling computing 14 m Computer Science Modeling Processing 0 Evaluation Rules Given an input on our tape how do we evaluate to produce the output 0 What do we need Read what is on the tape at the current square Move the tape one square in either direction Write into the current square 0011001000 3915 that enough to model a computer Modeling Processing 0 Read write and move is not enough 0 We also need to keep track of what we are doing How do we know whether to read write or move at each step How do we know when we re clone 0 What do we need for this A Lecture 35 Modeling computing 15 WT Computsr Saegce Finite State Machines A Lecture 35 Modeling computing 17 mm Clmputsl Selevce A Lecture 35 Modeling computing 16 m Computff Solarice Hmmmmaybe we don t need those infinite tapes after all not a not a What if the next input symbol is in state 2 A Lecture 35 Modeling computing 18 m Computer Science How many states do we need not a not a paren Letme as mama umpuuna 19 m Comptit39eg ggy se39 Turing s Explanation We have said that the computable numbers are those whose decimals are calculable by finite means For the present 39 I shall only say that the justi cation lies in the fact that the human memory is necessarily limited Letme as mama Computing 21 am Compus Turing Machine I1Ol1101110110 1I11 TT mam1 I npyt 1 Write390 7 Move lt7 Inputio 1 Wri 11 1 input OY 1 Write 0 I Move a Move a 39 Letme as M dlna umpuuna 23 l Comwsa ri nsa Finite State Machine 0 There are lots of things we can t compute with only a nite number of states 0 Solutions Infinite State Machine Hard to describe and draw Add an infinite tape to the Finite State Machine Letme as mum Eunpmmg 20 m CompLXI er Sale c FSM Infinite Tape 0 Start FSM in Start State Input on Infinite Tape Pointer to start of input 0 Step Read one input symbol from tape Write symbol on tape and move L or R one square Follow transition rule from current state Einich Letme asnvmum Dom mm T a g a 22 L t mC mPH 5 9lellF Matching Pa rentheses 0 Find the leftmost If you don t find one the parentheses match write a 1 at the tape head and halt 0 Replace it with an X 0 Look left for the first If you find it replace it with an X they matched If you don t find it the parentheses didn t match end write a 0 at the tape head and halt Letme as mum Eunpmmg 24 m Compu vt egv 15953 Class 22 Inheritance CS150 Computer Science David Evans University ofVirginia httpwwwcsvirginiaeduevans Computer Science Objects When we package state and procedures together we have an object 0 Programming with objects is objectoriented programming A c 39A C m r 1 nce 3 llll C5150 Fall 2005 Lecture 22 Inheritance Counter in Scheme using let define makeocounter let count 0 lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count Menu 0 Objects Review 0 ObjectOriented Programming 0 Inheritance C5150 Fall 2005 Lecture 22 Inheritance quot I Computer Science ul 1 ll lgtll n Vmuu N E Counter in Scheme define makeocounter lambda count lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count 0 C5150 Fall 2005 Lecture 22 Inheritance A 39 quot m t I 1 11C 4 gmgcquot UI Defining ask ask Object Met700 gt ask bcounter 39howmany 0 gt ask bcounter 39next gt ask bcounter 39howmany 1 define ask object message object message 39A Computer Science u m rmHm n39 ViRUIVi a C5150 Fall 2005 Lecture 22 Inheritance Com uter Science lquotquot pullquot UMHMsin u VmnH C5150 Fall 2005 Lecture 22 Inhen39tance makenumber define makenumber lambda n lambda message cond eq message 39value lambda self n eq message 39add lambda self other Wellsee Whylaiefioday ask self 39value ask other 39value 7 cbmpmexsrewe sle cs n lel mus ledIre 22 x de ne makemdmber global lambda n environment lambda message cond lambda self n eq7 message add lambda self other make quotumber39 ask self value ask other value gt de ne san makenumber 3 gt ask san 39value gt ask san 39add makenumber 4 parameters message body oond eq cs n lel mus ledIre 22 mum 9 m CDmP t i Simula Considered the first objectoriented programming language Language designed for simulation by Kristen Nygaard and OleJohan Dahl Norway 1962 Had special syntax for defining classes that packages state and procedures together cs n lel mus ledIre 22 x 11 Cumpx l f cicnc ask with arguments define ask object message object message The means take all the rest of the parameters and make them mfo a llst define ask object message fargs apply object message object args 3 Computer o39e of wlkl s ObjectOriented Programming mamas 1n mcbmpuggrse be Counter in Simula class counter integer count begin procedure reset count 0 end procedure next count count 1 end integer procedure howmany howmany count end 12 Cumpx l f sense XEROX Palo Alto Research Center PARC 19705 0 Bitmapped display 0 Graphical User Interface Steve Jobs paid 1M to visit and PARC and returned to make Apple LisaMac 0 Ethernet 0 First personal computer Alto o PostScript Printers Don t worry about What anybody else is g0ng to do The best way to predict the future is to in vent it Really smat people with reasonable funding can do quot just about anything that Dyn a bOOk 19 doesn t lioate too many of 7 III Just a model VEm ons Laws ObjectOriented Programmlng A an Kale 1971 C5150 Fall 2005 Lecture 22 Inheritance 13 Compgtgg c5150 Fall 2005 Lecture 22 Innentanue 14 r Commits Dynabook 1972 Tablet computer Intended as tool for learning 0 Kay wanted children to be able to program it also o Hallway argument Kay claims you could BYTE define the most powerful language in the Magazine world in a page of code A t Proof Smalltalk Ugus Scheme is as powerful but takes two pages 1981 c5150 Fall 2005Lecture221nheritanue 15 Compgrt Eleljl lem 15 Compgt a cleggsn Smalltalk Counter in Smalltalk Everything is an object class name counter Objects communicate by sending and instance variable names count receiving messages new count lt 0 o ObJects have their own state which may contain other objects neXt count lt count 1 o How do you do 3 4 howmany A count send the object 3 the message 4quot A I39l39fll39l Computer SCICHCC C5150 Fall 2005 Lecture 22 Inheritance 18 ldlAwUN l39l ullw mauler A c5150 Fall 2005 Lecture 22 Inheritance 17 Illlll Computer Scxence Aill UMl lvm l H Vm ll Inheritance cs F m 19 m computer saws mums makefraction de ne makefraction lambda numeratordenominator lambda message cond eq message 39value eq messa e a game as lambda self other makernumber e Nole ouradd lambda SEW numerator melhod eyaluales eq message getdenomlnator 0 a numben HO lambda self denominator W sccgl woi e D beller cs m rquot mus mm 22 Man 21 There are many kinds of numbers Whole Numbers 0 1 2 Integers 23 73 0 Fractions 12 78 Floating Point 23 00004 314159 But they can t all do the same things We can get the denominator of a fraction but not of an integer cs F m 2H m computer saws mums Why is redefining add a bad thing Cutandpaste is easy but There could be lots of number methods subtract multiply print etc Making the code bigger makes it harder to understand If we fix a problem in the number add method we have to remember to fix the copy in makefraction also and real complex float etc II Mm W consume 22 mcumpuggrs m makefraction define makefraction numer denom let su per makenumber f lambda message cond eq message 39value lambda self numer denom eq message 39get denominator lambda self denom eq message 39getnumerator lambda self numer e lse super messag e Using Fractions gt define half makefraction 1 2 gt ask half 39value gt ask half 39getdenominator gt ask half 39add makenumber1 3 2 gt ask half 39add half 1 consume 23 mcumpms H mm m cc consume 24 mcumpms m mm it v gt trace ask gt trace eq gt ask half 39add half ask 12ltproceduregt add 12ltproceduregt eq add Va ue I f ask ltprooeduregt value eq add getdenominator 5 Value Value eq add getnumerator i 12 ma ke n u m be r 5 ma kefra ction gt ask half 39add half ask ultproceduregt add 22ltproceduregt eq add va ue ask ltprooeduregt value f eq add getdenominator i KEEP value Value ask ltprooeduregt value eq add value i Keq Value Value I lift eq add add I 12 t 1 1 cs n r mus red 22 we 25 Comp uwt eyg injures Inheritance Inheritance is using the definition of one class to make another class makefraction uses makenumber to I39nreril the behaviors of number I lift add etnumerator i 1 I eq 9 ask ltprooeduregt value eq add value i He ll Value value I lift eq add add I 12 t 1 1 cs nhllmmLulumzzunhenlzme 2s Comp uwt gie 0 English A Fraction is a IrilrdofNumber o C Fraction is a alsivea cass whose base cbss is Num Java Fraction extends Number 0 Eiffel Fraction lhheliE from Number Noie people 0 Beta melimes draw lhis dillerenl Ways Fraction is a subpattem of Number 0 Smalltalk 72 and Squeak 05 Don39t have inheritance es r me 27 m cumpmer science new A measure 23 mcumpmerse Ce Mm m CS 150 Fraction inherits from Number Fraction is a subclass of Number The supercass of Fraction is Number Subtyping Subtyping is very important in statically typed languages like C C C Java Pascal where you have to explicitly declare a type for all variables method Numberadd Number n Because 01 subryprng ember a Number or 3 Fraction subtype 0 Number could be passed as be ergumem We won t cover subtyping although we will talk more about types later es r me 29 m computer Science new r es r me an m computer sc mm m new y l Menu ears 199 ATLANTIC PIER AMUSEMENTSJ INC 3 quot 5 quotMill in mm i L 39 i IN ORBER 1 0 P Rd 39E CT Class 17 Sex Religion and Politics 0 Debt Population 9 2 0 Is it useful for a problem to be hard 0 How the Allies broke Fish i OU R GUESTS PLEASE w t OBSERVE mi For 7 A quot v j l i m6 RIMES LLow g H i I Various lies ii tg 94 C8150 Computer Science David Evans University Of Virginia httpwwwcsvirginiaeduevans Computer Science CS150 Fall 2005 Lecture 17 Saving the World 17981944 2 E PS4 Malthusian Catastrophe Question 1 For each f and g pair below argue RevergndThomas R0bert Malthus 5553 0 convincingly whether or not f is 1 0g 2 the PrnCPe OfPOPUal39lon 1798 Qg and 3 g as explained above For all The great and unlooked for discoveries that have questions assume n is a n0nnegative integer taken place of late years in natural philosophy the increasing diffusion of general knowledge from the extension of the art of printing the ardent and f f the federaI n years from today g the unshackled Spirit of inquiry that prevails throughout the lettered and even unlettered Us pPpUIatlon n Years from tOday thls one world have all concurred to lead many able reqUIres a more Informal argument men into the opinion that we were touching on a period big with the most important changes changes that would in some measure be decisive of the future fate of mankindquot C5150 Fall 2005 Lecture 17 Saving the World 17981944 3 ler Compgfgg smgigg g cs150 Fall 2005 Lecture 17 Saving the World 17981944 4 Emir Compywtgrm giggggx I I Malthus Postulates Malthus Conclusnon Assuming then my postulata as granted I I think I may fairly make two postulata say that the power of popuation is First That food is necessary to the existence of man indefinitely greater than the power in the Secondly That the assion between the sexes earth to produce subsistence for man Erggec fi glt eand Wquot rema39 early 39 39ts Population when unchecked increases These two laws ever since we have had any in a geometrical ratio Subsistence increases 1knozilvlledge of mankind appgar to have beent only in an arithmetical ratio A slight ixe aws 0 our nature an as we ave no hitherto seen any alteration in them we have no acquamicance W39thnumbers W39 Show the ri ht to conclude that they will ever cease to be ImmenSIty of the first power In comparison A A CS150 Fall 2005 Lecture 17 Saving the World 17981944 5 quot Computer SCICHCC CS150 Fall 2005 Lecture 17 Saving the World 17981944 5 n Computer Selence mw UNlVlZkSlIY fViRmxn in My UNH likrsl 1 v gr39VlRtilXH Malthusian Catastrophe 0 Population growth is geometric kn kgt 1 0 Food supply growth is linear n What does this mean as 7H oo Food per person food supply population n kn As n approaches infinity food per person approaches zero A quot Com uter Scxence Illll l CS150 Fall 2005 Lecture 17 Saving the World 17981944 Malthus Fallacy c He forgot how he started The great and unlooked for discoveries that have taken place of late years in natural philosophy the increasing diffusion of general knowledge from the extension of the art of printing the ardent and unshackled spirit of inquiry that prevails throughout the lettered and even unlettered worldquot 0 Agriculture is an endless golden agequot field Production from the same land increases as 9002quot Increasing knowledge of farming weather forecasting plant domestication genetic engineering pest repellants distribution channels etc A cs150 Fall 2005 Lecture 17 Saving the World 17931944 9 m Computsr Selerllce dHJr MVIlhlll39 of 2le Malthus was wrong about 2 Also Source m 9 World Population reached 1 billion in 180 39 39 192 aldUBdJO SUDIIIIE we reduce our growth rate soon World Population will reach 7 billion in 2013 14 years later 8 billion in 2028 15 years later tar World Population 19502050 Cl lumdamoauoo 10 9 billion in 2054 26 wars la 9 B 0 200 400 500 800 1000 1200 1400 1500 1800 2000 6 Year g 5 E 4 Advances In SCIence birth control 5 3 2 1 medicine higher life expectancy education and societal and political changes e g regulation in China have reduced k it is lt 1 in many countries now 1950 1960 1970 1980 1990 2000 2010 2020 2030 2040 2050 Year Source us Census Bureau Inlemzltinnal Data Base April 2005 velsinn o quot Com uter SCICHCC PJHN UVlVl Nail 739VR rl39l oo CS150 Fall 2005 Lecture 17 Saving the World 17981944 Upcoming Malthusian Catastrophes Fig 3 Trends in World Fossil Fuel Consumption 0 Human Minnowequivalent consumption of fossil fuels grows 213331 as kquot fairly 333 large k like 108 2 0 Available fuel is O I l v I 1700 1800 1900 2000 constant Source Environment Agency s quotWhile Paper on the Flu irollmcnlquot 998 I i39lH n39ll n n n m I annnmgzgooshtml cs150 Fall 2005 Lecture 17 Saving the World 17931944 10 m Computgr SmogIce uJr Vl39Zlll39 7 lR1ll A cs 150 Fall 2005 Lecture 17 Saving the World 1793 1944 11 m Computgr Selegce mlv 4Vl r gtll 39nl39 lRKIYH II Cornocopian View 0 Few resources are really finite 0 Most things have endless golden ages 0 Human ingenuity and economics and politics will solve problems before they become catastrophes No one will sell the last gallon of gas for 298 N A CS150 Fall 2005 Lecture 17 Saving the World 17981944 1 Computgr SCICErlce mt Vl 39llll gf lklll Kay sian View The best way to predict the future is to invent it Alan Kay cs n rquot mus lulure n smug lhe mad menu is EComputerScienee mucusquot PS4 Question 1 ff the federal debt n years from today g the US population n years from today this one requires a more informal argument Debt incrases Spending Revenues this varies but usually positives Interest on the existing Debt exponential 90 Population increase is not exponential rate continues to decrease gt as n incrases debt per person approaches in nity This Will eventLa ybe a problem but orders of growth analysis doesn39t say when cs n rquot mus lulure n smug lhe mad menu gums would tend to be preserve Influence of Malthus In October 1838 that is fifteen months afterI had begun my systematic inquiry I happened to read for amusement Malthus on P0 uatbn and being well prepared to appreciatet e struggle unfavorable ones to be destroyed The results of this would be the formation of a new species Here then I had at last got a theory by which to worllt Charles Darwin in his autobiography 1876 cs n rquot mus lulure n smug lhe mad menu 14 cgmpumsu cums u Simulating Universe 0n3 Note the NP Com lete class is a nng Sorting n log n Cure Cancer Problem Classes if P i NP Find Best 9a Traveling Salesperson Map Coloring cs n rquot mus lulure n smug lhe mad menu 15 Computchcscne e 15 cumpumsu Mkinei Graph Map Coloring Problem Input a graph of nodes with edges connecting them and a minimum number of colors Output either a coloring of the nodes such that no connected nodes have the same color or noquot If given a coloring easy to check if it no connected nodes have the same color and the number of colors used cs n rquot mus lulure n smug lhe mad menu Best Map Coloring Problem Input a graph of nodes with edges connecting them Output the minimum number of colors that can be used to color the map without any adjacent nodes having the same color define bestsmapscoloring graph define bmcshelper graph guess if hassmapscoloring graph guess guess bmcshelper graph 1 guess bmcshelper graph 0 u P r w is in NP and we can solve calls to has mapscoloring we now estsmaps coloring is also in NP 17 m vaer cs n rquot mus lulure n smug lhe mad menu u cumpumsu uns us Is it ever useful to be confident that a problem is hard cs n F m m 1 3 i WW 113919 9 m Computer 5 revue Breaking Fish GCHQ learned about first Fish link Tunny in May 1941 Intercepted unencrypted Baudotencoded test messages August 30 1941 Big Break Operator retransmits failed message with same starting configuration Gets lazy and uses some abbreviations makes some mistakes 0 SPRUCHNUMMERSPRUCHNR Serial Number ms F m m 1 3 i WW ms 2 m compose Factoring Problem Input an n digit number Output two prime factors whose product is the input number Easy to multiply to check factors are correct Not proven to be NPComplete Most people think it is eXcept Sneakers Most used public key cryptosystem RSA depends on this being hard click on the key symbol at the bottom of your web browser cs n Fii mus e 3 lhede 113919 2 mComputeISc39e be wlki Two Time Pad Allies have intercepted C1 M1 XOR K1 C2 M2 XOR K1 Same key used for both same starting configuration Breaking message c1 XOR c2 M1 XOR K1 XOR M2 XOR K1 M1 XOR M2 XOR K1 XOR K1 M1 XOR M2 Cribs 0 Know C1 C2 intercepted cipherteXt C1 XOR C2 M1 XOR M2 0 Don t know M1 or M2 But can make some guesses cribs SPRUCHNUMMER Sometimes allies moved ships sent out bombers no help me cryptographers get good cribs 0 Given guess for M1 calculate M2 M2 C1 XOR 2 XOR M1 0 Once guesses that work for M1 and M2 K1 M1 XOR C1 M2 XORQ cs n F m m 1 3 i WW 113919 23 m compm F Science mom m m lhede 113919 22 mCumpuggr c cc Finding K1 From the 2 intercepted messages Col John Tiltman worked on guessing cribs to find M1 and M2 4000 letter message found 4000 letter key Bill Tutte recent Chemistry graduate given task of determining machine structure from ke Already knew it was 2 sets of 5 wheels and 2 wheels of unknown function cs n Fii mus e 3 lhede 113919 24 mcumpm gseimee Class 16 NPCompleteness The Story so Far c5150 Camp Dam d Evans 332331523 13 mm HWWWS wan Banam Menu 3SAT Complexity class NPComplete The Story so Far Some NPComplete Problems an F m m F F Erma 2 W Compuwt e r Problem Classes if P at NP S imulaljng Universe 0n3 Find Best 9a How many problems are n me on class7 in nite n me on class7 in nite How many problems Sur ng arepi7n NP but not n log n Smiley infinite 0nl and 9a cs n F mus ledIre us up Enmvlelenexs Problem Classes if P NP Simulau39ng Universe 0n3 Find Best 9a How many problems W are n me on class7 in nite How many problems are ll l P but not in n How many problems Sur ng arepi7n NP but not in em 10g 0 Smileys 901quot a ECDmeEL an F m m F F Erma 4 m campuggx The 3SAT Problem 0 Input a sentence in propositional grammar where each clause is a disjunction of 3 names which may be negated 0 Output Either a mapping from names to values that satisfies the input sentence or no way meaning there is no possible assignment that satisfies the input sentence an F m m F F Erma 5 m comm Sentence clause 2 Clausev laus or and not 3SAT Example 3SAT avbv ic A av bvd A avbv d Abv cvd gta true 1 false c false d false an F m m F F Erma F m comp 3SAT a Smiley 0 Like 3stoneappletower puzzle we can convert every 3SAT problem into a Smiley Puzzle problem 0 Transformation is more complicated but still polynomial time 0 So if we have a fast P solution to Smiley Puzzle we have a fast solution to 3SAT also cs n F 7 i i w 7 m Compnt eyi s 39 Problem Classes if P at NP Simulating Universe 003 I NPC omplete 0 Note me NPF Complete class is a ring a odners are Cl rcl es 1 g39 Find Best 9a How many problems are n dne 0 class7 in nite How many problems Sm 39n are in NP but not in P7 n log n Smileys infinite 3SAT an F ms i is r saunas a Campus Quiz Responses What is Computer Science Remembered or almost remembered lirsl class mperaye s d ocedures and ang agel m ylng orem nadenon was on ne a Howl ll olhll lgs nsead o la we slaleme ls a a ulhowlo hlngs we adeaspeclllc dellnlllon in ne begnnng o ne year bu l orge l would say na compuer science 4 omenods o l d d e sudy o mperave knowledge l doesn t need o nave a compuer and ll s no a Solel39lce doesn t deal wn real things nsead numbers and daa u u u on normaon sudy o mperave Knowledge sudy o deren ypes o m it s more o a llberal ars malor Q o E 3 1 3 o 2 a 9 lt o 3 2 Q compuer problems an F a i s F mun m comps NP Complete Cook and Levin proved that 3SAT was NP Complete 1971 Take csooo m see how A problem is NP complete if it is as hard as the hardest problem in NP If 3SAT can be transformed into a different problem in polynomial time than that problem must also be NPcomplete Either all NPcomplete problems are tractable in P or none of them are I Computer 5 39 csisnriims ledquote 1670 Enmvlelenexx a W Quiz Answers What would we need to do to prove a problem is 0 n4 Find a procedure mat solves die problem mat is wt What would we need to do to prove a problem is Q n4 Prove that mere is no procedure mat solves die problem mat is faster man 9 csisn Fii mus ledquote is up Enmvlelenexx Quiz Responses What is Computer Science Problem solving logic language A way o lhll lkll lg abou compuers and programs A sysemac way o V U nrnkrn A r ml l n ll n w ne compuer language luncllol ls l seems o be o neonze on now o sysemacally solve problems n a unconal language ne sudy o loglo a way l bl The sudy problems sudy o languages used o creae nellgen compuer w ne a sclence o compulers7 Solvlrlg problemswn compuers The sudy and use o languages o lmprove ecnnology an program compuers es s ne ar o 40 an csisn Fii mus ledquote is up Enmvlelenexx Quiz Responses What is Computer Science Amusing Computer Science is a great way to find new world The bane of my existence 31511 F m m F NF amtquot 3 m Computer Science no Where we ve been Where we re going 31511 F m m F NF amtquot 5 m Computer Science mum Computer Science Expanded Definition from Class 1 Study of information processes 0 How to describe information processes by de ning prooedures o How to predict properties about information prooesses o How to elegantly and ef ciently implement information prooesses in hardware and software What have we spent most of our time on so far 31511 F m m F NF amtquot 4 m Computer Sc MM Computer Scrence CS150 so far 0 How to describe information processes by defining procedu res Programming with prooedures lists recursion Class 2 4 5 6 7 8 9 10 11 12 o How to predict properties about information processes Measuring work 9 0 Q oomplexity classes Class 9 10 11 12 13 14 15 16 o How to elegantly and ef ciently implement information processes in hardware and software Class 3 rules of evaluation 31511 F m m F NF amtquot 5 m Computer s wum CS150 upcoming o How to describe information processes by defining procedu res Programming with mutation objects databases networks How to predict properties about information processes What are we oounting when we measure work Are there problen39s which can39t be solved by prooedures o How to elegantly and ef ciently implement information processes in hardware and software How to implement a Scheme interpreter Not much in C5150 on hardware see C5230 and C5333 m5 F m m F F when 7 m camp 1 mac Famous Computer Scientists Ada first computer scientist She s in the course name Grace Hopper first compiler First bug Navy ship David Letterman nano John Backus BNF Fortran UVa dropout Tony Hoare Quicksort 31511 F m m F NF amtquot 1 m camp 5 1 n Famous Computer ScientisE vent Wndws mhevfate wmd mew pc 3 nasny rnyenrea by Dana Endamvt am XEROX Pat in are v exnuenenrea 5 am rnraprerer but want rnyenr amt NP Complete Problems NPsComplete Problems Iquen the wesanswen quremn v way m deck r r rs nan e amen m We are rrrr my mm kx qmvenl nixAug la nsne erAna aqrd vaaxman n hue nne Iquen the Wanswer rm my way m mack r r rs nan e re a ulmn un k xe rnee rm ans e re Hum re there m1 ane If Traveling Salesperson Problem drsunre runneran unern and a rmrumum mm dstant emuut ermer a man that yrsrcs eadn ynnn a mst ess than the rnrnrrnum m am 9 mm eyery my Wm 65 man mmmum drsmnoe Uave ed yen a pann easy to chedlt m 4an a avauh er names ynnn edges canneran men and a rmrumum numba er mars emuut Etha 2 Emma er me nudes sum that rm mnnadad nudes have the same duh a nu xrgryen a odormg easy to dwedlt rr rt mm L 4nqu 2 Dnsmm urn sq a Mrnesyyeeper emuut anner a assrgrrnmt er burnts m squares m a medlt rfrtrs comment u re m m2 Esme cdor and me mmber ofoo ors Lsed Menu Lecture 2 Questions from Lecture 1 Notes Formal Formal Systems Systems MIDLsystem and Languages English Languages Scheme C5150 Computer Science University ofViIginia David Evans Computer Science hm WWW c5 Virginia ecuevans Slsnrallzms mime Language 2 Cumiatg s g ensg If it takes 60 seconds to compute a photomosaic for Problem Set 1 today on a typical PC estimate how Are there any nonrecursive natural long It Will tke CSZOQast denlts In tokcompzugfp languages What would happen to a t e same p otomosaic ow ong WI It ta e In society that Spoke one gt 2008 2005 12 18 D ff rence In years 12 number of morith gt U 50 2 2 Number of monine 18 numberofdoublings a cording to Moore 5 Law gt2 quot 1218 4 60 seconds today Zdwblings by 2008 Not covered In Class 1after today you gt 60 at 2 2 2 2 lSsecmds In 2amp38 154 should be able to answer this gt exactgtinexact 60 2 2 2 2 33975 60 seconds today 4 doublings by 2011 3 75 seconds In 2011 oonunue forever Slsnfall znns ream 2 mutant 3 mummvrts sswsa 515 Fall zms ream 2 Language 4 FM Cmmsy a ea Formal Systems Set of symbols Primitives Formal SyStems Set of rules for manipulating symbols Hofstadter Rules of Production Rules of Inference Also Rules of Combination Slsnfall znns ream 2 mutant Computersuence swarm new image s E Computer scienc The MIU System Symbols M I U Rules of Production Rule I If you have a string ending in I you can add a U at the end Rule II Suppose you have Mx Then you may ad Max to your collection Rule III If III occurs in one of the strings in your collection you may make a new string with U in place ofIII Rule IV If UU occurs inside one of your strings you can drop it 31511 F 2 m 2 myqu 7 m Compuwt eyr SakiIce Survey Summary 26 Responses Years 6 First 1 Second 9 Third 8 Fourth Majors 10 Cognitive Science 4 Biology 3 Undecided 2 Economics Art History Chemistry Electrical Engineering Foreign Affairs Mathematics Neuroscience Psychology Sociology Previous computing 15 None 5 A Little 6 Lots MIU System Example Start with MUI produce MIU Rules of Production Rule I If you have a string ending in I you can add a U at the end Rule II Suppose you have Mx Then you ma add Mxx to yourcollection Rule III If III occurs in one of the strings in your collection you may ma e a new string with U in place 0 Ru e IV If UU oocurs inside one of your strings you can drop t 31511 F 2 m 2 myqu a m Compuwt eyr SakiIce Survey Summary Bodos vs Krispy Food 18 Bodos 9 Krispy Kreme Full survey responses will be posted over the weekend 31511 F 2 m 2 myqu m Compug ers m 31511 F 2 m 2 myqu m Compug ers m Languages 31511 F 2 m 2 myqu m compu gs m What is a language Webster A systematiemeans of communicating ideaseefed rngs by the use ofeenventie alieeel signs sounds gestures or marks having understood meanings 31511 F 2 m 2 myqu 2 m compu gs m Linguist s Definition Charles Yang A description of pairs 5 M where Sstands for sound or any kind of surface forms and M stands for meaning A theory of language must specify the properties of Sand M and how they are related an F 2 m 2 cm 3 m Computer Languages and Formal Systems What is the difference between a formal system and a language With a language the surface forms have meaning Caveat computer scientists often use language to mean just a set of surfaoe forn39s an F 2 m 2 cm 4 m Compu What are languages made of o Primitives almost all languages have these The simplest surface forms with meaning 0 Means of Combination all languages have these Like Rules of Production for Formal Systems Ways to make new surface forms from ones you already have 0 Means of Abstraction all powerful languages have these Ways to use simple surface forms to represent complicated ones cs 5 F ms 22 2 my 15 What is the longest word in the English language gm Write E It ftff 31511 F 2 m 2 myqu 5 m campugg suit According to Guinness floccipoccinihilipilification the act afrenderhg useless 31511 F 2 m 2 myqu 7 m Cumpmhl r sag Making Longer Words antifloccipoccinihilipilification the act of rendering not useless antiantifloccipoccinihilipilification the act of rendering not not useless as F 2 m2 myqu m Cumpmg r 39 9c Language is Recursive No matter what word you think is the longest word I can always make up a longer one word anti word If you have a word you can always make up a new word by adding anti in front Since the result is a word you can make a longer new word by adding anti in front again Recursive Definitions We can define things in terms of themselves Recursive definitions are different from circular definitions they eventually end with something real word a nti word W0rd floccipoccinihilipilification 31511 F 2 m 2 MW m Computer 5 mum 31511 F 2 m 2 MW 2 m Computer 5 mum Recursive Definitions Allow us to express infinitely Pany things starting with a ew This is powerful We will see lots of examples In this course Does English have these Primitives o eg anti oocipoocinihilipili cation Morphemes smallest units of meaning 0 eg anli opposite Means of combination eg Sentence Subject Ierb Object Precise rules but not the ones you learned in grammar sc 0 51439 a senlence with a praaasl39b39an is sumeb u39ng L with which we m7natput Winsmn Churchill 31511 F 2 m 2 MW 2 m Compuggr m 31511 F 2 m 2 MW 22 m Compuggr m Does English have these Means of abstraction Pronouns she he it they which etc Confusing since they don t always mean the same thing it depends on where they are used The these in the slide title is an abstraction for the three elements of language introduced 2 slides ago The they in the confusing sentence is an abstraction for pronouns How should we describe languages Detour History of Computer Programming 31511 F 2 m 2 MW 2 m compn x s m 31511 F 2 m 2 MW 24 m compn x s m Electronic Numerical Integrator and Computer Early WWII computer But not the world s rst PS4 Built to calculate bombing tables Memory size twenty 10 decimal digit accumulators 664 bits ENIAC 1946 12 mm Apollo Guidance Computer 1969 1 inch You 23 miles cs150 Fall 2005 Lecture 2 Language 25 Directions for Getti ng 6 Choose any regular accumulator ie Accumulator 9 Direct the Initiating Pulse to terminal 5 The initiating pulse is produced by the initiating unit39s a terminal each time the Eniac is started This terminal is usually by default plugged into Program Line 11 described later Simply connect a program cable from Program Line 11 to terminal 539on this Accumulator Set the Repeat Switch for Program Control 5 to 6 Set the Operation Switch for Program Control 5 to Set the ClearCorrect switch to C Turn on and clear the Eniac Normally when the Eniac is first started a clearing process is begun If the Eniac had been previously started or if there are random neons illuminated in the accumulators the Initial Clearquot button of the Initiating device can be pressed 9 Press the Initiating Pulse Switchquot that is located on the Initiating WN PONQMP deVIce 10Stand back tut C quot FRFS 93 PEE C5150 Fall 2005 Lecture 2 Language 25 m Computer Sciencre alumni u m Admiral Grace Hopper 19061992 0 Mathematics PhD Yale 1934 o Entered Navy 1943 0 First to program Mark I rst large computer 51 feet long 0 Wrote rst compiler 1952 program for programming quotNobody beI39e ied that I had a running compUtel39S compVerandnobody o Codesigner of COBOL most walld touch it They Mime commaS Widely used programming DUdgnydg language until a few years ago arithmetic cs150 Fall 2005 Lecture 2 Language 27 Dare and Doquot Guest on David Letterman tut C quot FRFS 93 PEE C5150 Fall 2005 Lecture 2 Language 25 m Computer Sciencre alumni u m Nanostick How far does light travel in 1 nanosecond gt de ne nanosecond 1 1000 1000 1000 1 billionth of a s gt de ne lightspeed 299792458 m s gt lightspeed nanosecond 149896229500000000 gt exactgtinexact lightspeed nanosecond 0299792458 just under 1 foot Dell machines in Small Hall have 18GHz Pentium 4 CPU GHz GigaHertz 1 Billion times per second They must finish a step before light travels 66 inches cs 150 Fall 2005 Lecture 2 Language 29 m Computer dewritte by hm ma mist Compiler translates J from code in a high Compiler level language to machine code Code maiC hirie can run DiScheme uses an interpreter An interpreter is like a compiler except it runs quickly and quietly on small bits of code at a time cs 150 Fall 2005 Lecture 2 Language 30 m Computer it LN John Backus 0 Chemistry major at UVA entered 1943 o Flunked out after second semester 0 Joined IBM as programmer in 1950 o Developed Fortran first commercially successful programming language and compiler C5150 Fall 2005 Lecture 2 Language 31 IBM 704 Fortran manual 1956 STATEMENT NORMAL SEQUENCING a l3 Next executable statement GO TO ii Statement n 7 EWEMTWWM last W F ASSIGN i T0 n Next executable statement 7 GO TO nhnzp ohm i Statement n1 IF ta nnzn3 Statement l11ltzl l3 as a less than or greater th SENSE LIGHT i 7 Next executable statement Statement nnZ as Sense Light i ON or OFF quot as Sense Switch i DOWN or UP 7 quot A in El IF SENSE LIGHT i nhn IF SENSE SWITCH i hm a m In It I n rutCo Pt C5150 Fall 2005 Lecture 2 Language 32 m m 1 r SCI n M Cquot Pita Ev Describing Languages Fortran language was described using English Imprecise Verbose lots to read Ad hoc Do 10 11 10 Assigns 1 10 to the variable D0101 Do 10 11 10 Loops for I 1 to 10 Often incorrectly blamed for loss of MarinerI Wanted a more precise way of describing a language C5150 Fall 2005 Lecture 2 Language 33 Backus Naur Form nontermna replacement We can replace nontermnawith replacement A Bmeans anywhere you have an A you can replace it with a B Some replacements are termnals a terminal is something that never appears on the left side of a rule A in C mP FS 9i99ESH C5150 Fall 2005 Lecture 2 Language 34 A tut manganese BNF Example Sentence NP Verb NP 33 NOW What are the Noun Dave term77315 Dave Scheme rocks sucks Noun Scheme How many Verb 3 rocks different things can we express Ver 33 SUCkS with this language 4 but only 2 are true BNF Example Sentence NP Verb NP Noun NP Noun and NP How many Noun 3 Dave different things can we express N 0 3 SCheme with this Verb rocks language Infinitely many Verb sucks Recursion is powerful A cs1so Fall zoos Lecture 2 Language 35 lm Computg cienkcen A cs1so Fall zoos Lecture 2 Language 35 IE Compgtgrnyhswc tenkoen Lecture 4 Programming with Data 39 cs1so Computer Science 1 University of Virginia I Evans Computer Scxence Dav39 my wa cs Virginia eduevaws Menu Evaluation Question from Monday Programming with Data Take Pictures 2 1 Comp s uare 4 Eval Rule 3a Applica on sqtare 4 Eval Rule 2 Names 1 Eval Rule 1 WWW lambda x a X x 4 Eval Rule 33 Applicauom Appl Rue 2 Compound Applicauon 4 4 Eval Rue 3a Applicauon 4 Eval Rule 1 Primmvei i Eval Rule 1 WWW ltprimilivegt 4 4 Eval Rule 3b Appiicauon ApplyRule 1 WWW Appiimuon 15 Ways to Design Programs 1 Think about what you want to do and turn that into code 2 Think about what you need to represent and design your code around that Which is better mam mm mm a quot092man mu Dan 1 History of Scheme 0 Scheme Guy Steele amp Gerry Sussman 1975 Guy Steele codsigned Scheme and created the rst Scheme interpreter for his 4 year project More recendy Steele speci ed Java 1995 Conniverquot 1973 and Planner 1967 0 Based on LISP John McCarthy 1958 Based on Lambda Calculus Alonzo Church 19305 Last few lectures in course LISP Lots of Insipid Silly Parentheses LISt Processing language Lisls are pretty important hard to write a useful Scheme program without them 5 enmpug us Making Lists cs n r m t o mam a D 7 E Compuwt eyr SukiIce Splitting a Pair gt car cons 1 2 W W 1 gt cdr cons 1 2 2 1 2 car extracts first part of a pair cdr extracts second part of a pair Making a Pair gt cons 1 2 pa 1 2 cons constructs a pair cs n r m t o mam a D a E Compuwt eyr SukiIce Why car and cdr Original 19505 LISP on IBM 704 Stored cons pairs in memory registers car Contents of the Address part of the Register cdr Contents of the Decrement part of the Register coulder Doesn t matter unless you have an IBM 704 Think of them as first and rest define first car define rest cdr The DrScheme Pretty Big language already de ne these but they are not part ofstandard Scheme IL Write Esme a nun r mus team 6 qunmmlnq wllh 9 sun r mus team 6 m WM lug cvaw 39 Pairs are fine but how do we make threesomes Triple A triple is just a pair where one of the parts is a pair define triple a b c cons a cons b c define tfirst t car 0 define tsecond t car cdr t define tthird t cdr cdr t mumMWmw n mcumpdt r wee IZECWWE cs n r mus team 6 qunmmlnq wllh o Quadruple A quadruple is a pair where the second part is a triple define quadruple a b c d cons a triple b c d define qfirst q car q define qsecond q tfirst cdr t define qthird t tsecond cdr t define qfourth t tthird cdr t 31511 F m m h mm W D 3 m Computer saws mum Lists List cons Element List A list is a pair where the second part is a list One big problem how do we stop This only allows infinitely long lists cs n F mus m o qunmmlnq wllh o Null List cons Element List List null A listis either a pair where the second part is a list or empty null cs n F mus m o m 17 ECquot P E 5 Multuples A quintuple is a pair where the second part is a quadruple A sextuple is a pair where the second part is a quintuple A septuple is a pair where the second part is a sextuple An octuple is group of octupi A is a pair where the second part is a 31511 F m m h mm W D 4 m Computer saws mum Lists List cons Element List List It39s hard to write this A listis either a pair where the second part is a list or empty cs n F mus m o qunmmlnq wllh o List Examples gt null gt cons 1 null gt list null gt list cons 12 gt list cons 1 null t 31511 F m m h mm W D 1 m compu t F 5659c Menu P53 Question 4 Operators for Measuring Growth Sorting Lecture 130 Omega Theta 2 E Compgtggn ggggglx Sorting Cost Growth Notations define sort Ist cf r if null Ist Ist 39 8 E 00 C B39g39Oh I let best findbest Ist cf cons best sort delete Ist best cf g grows no faster than f upper bound define findbest Ist cf I if 1 length st car Ist g E 90 C Them I pickbetter cf car Ist findbest cdr Ist cf g grows as fast as f tight bound If we double the length of the list the amount of o g E 201 Omega work approxmateyquadruples there are twice as many applications of findbest and each one takes 8 grows 0 Slower than f lower bound tW39Ce as long Which one would we most like to know Lecture 13 0 Omega Theta 3 E Lecture 13 0 Omega Theta 4 E Meaning of 0 big OhU 0 Examples g is in 0 f iff there are positive constants g iS in 0 f iff c and no such that gn s cfn for all n 2 n0 There are positive constants c and no such that I 2 Snln 0n Yesc1andn1 works gm 3 cfn I 0 5 10m In 0 Yes 6 09 and n01 works 2 39 7 No no matter what 6 we pick IS n In 0 I on2 gt n for big enough n n gt C for all n 2 no A Lecture 13 0 Omega Theta 5 quot computer SCICHCC Lecture 13 0 Omega Theta at My UMvItksm 5139 V IRGINJA 5 395 Computer Science m 171 an ialzsrry sf rlRGleA Lecture 40 Computing with Glue ancl Photons Color Plate 1 Early model of the Tinkenoy computer display at The Computer useum oston also plays ticrLaCioe The Tinkertoy Computer and Other Machnations by A K Dewdney C8150 Computer Science University of Virginia DaVId Eva ns Computer Science http wwwcsvirginia ed uevans Equivalent Computers IZIZ IZIZIZIZ I 2 term variable term term term 1 7 varlable term Ky M a 7w M yoc v Where v does not occur in M MMNBMxocN Eini39tei state Machine Turing Machine Lambda Calculus A Lecture 40 Computing with Glue and Photons 2 Computgr Selerice unh Nn39lv39RMn y39 lRill Lambda Calculus is a Universal Computer ZIZIZIZIZIZZZZZZZZZZZZIZIZIZI ReadWrite Infinite Tape Mutable Lists 0 Finite State Machine Numbers 0 Processing Way to make decisions it Way to keep going What is 42 42 XLII fortytwo cuarenta y dos Lecture 40 Computing with Glue and Photons 3 A m l Computer Selence quot quotlquot Uquotquotquotquot quotquot er39VlRmVu Lecture 40 Computing with Glue and Photons 4 Computgr Seiegce ilIIJi NlYlilhllY 31 Hle Meaning of Numbers 0 42nessquot is something who s successor is 43nessquot o 42nessquot is something who s predecessor is 41nessquot 0 Zero is special It has a successor oneness but no predecessor Meaning of Numbers pred succ N a N succ pred N a N succ pred succ N a succ N zero zero a T zero succ zero a F A Lecture 40 Computing with Glue and Photons 5 COmPUtSI Selence ll r l39l39RSll39 if IRGIVH A Lecture 40 Computing with Glue and Photons 6 Computsr Selerflce HiK NlYlR ln 37 lRK39leH Is this enough Can we define add with pred succ zero and zero add 2 Myquot zero x y add pred x succ y mum on Eonvulan a Glue aa Phalanx Numbers are Lists zero 2 null pred cdr succEkxconst IThe length of lhe list con39sponds to the number value mum on Eonvulan a Glue aa Phalanx gm Write Ewe cons and car cons a Axxyluxy cons M N Mhy zzxy M N E B Oly zzMy N E B MzMN carskppT Tab52x car cons M N E car MzMN E App T MzMN a B7lzzMN T a BTMN a Body x MN a Bay MN E B M mum on Eonvulan a Glue aa Phalanx m Comm Can we define lambda terms that behave like zero zero pred and succ Hint what if we had cons car and cdr mum on Eonvulan a Glue aa Phalanx a Making Pairs define makepair x y lambda selector if selector x y define carofpair p p t define cdrofpair p p f mum on Eonvulan a Glue aa Phalanx gm CDMPHESE Ewe cdr too cons E Myzzxy car E 11717 T cdr E 11717 F cdr consMN cdr MzMN App F MzMN A B kzzMN F gt B FMN gt B N mum on Eonvulan a Glue aa Phalanx m campa t a Null and null null abaT null E 70x Xylzf null null gt MU Aylzf 70 T gt B 70 T7cy7LZF Null and null null abaT null E 70x Xylzf null cons M N gt 7on0 Aylzf MzMN gt B M1 MN7cy7 z F gt B MM F MN gt B F mum on Compullnq wllh Glue na Phalanx 14 ComRuwt eyg agate gt B Mmquot WWWn mm mm 13 ComRut e f iosewzs Coun ng 0 a null 1 E cons F 0 2 E cons F 1 3 E cons F 2 succ E Mtcons F x pred E Mtcdr x 42 W7zz xy y Myouzzxy 31 My Macy My y My zz Icy My y My zz Icy My y y y y y y y y y y y y y My Macy My y My Macy My y My Mz xy My man My y MT m u mm mu a m mm 15 m Compug urs m m u mm mu a m mm 15 m Compug urs m Lambda Calculus is a Universal Computer IIIIIIzlzlzIzlzlzlzlzlzlzlzlzlzlzIzlzlz III 1 0 ReadWrite In nite Tape Mutable Lists 0 Finite State Machine 39 Num ers to keep track of state 0 Prooessin We have this but Way III Finn39e iale39Machiri 9 of making decisions if N ay to eep goIng de n i ons m u mm mu a m mm 17 m comptws m Way to Keep Going hf Oh XfOOO 7h X fOOCDD Mel gtB M7Lzzxx 7 x M0000 AB zz 7c x7 zzxx 7c x7uzzxx AB 70c7 zzxx 7c x7uzzxx 5 mum on Bumpulmq wllh Glue na Phalanx Lambda Calculus is a Universal Computer IIIIIIzlzlzIzlzlzlzlzlzlzlzlzlzlzIzlzlz 0 ReadWrite In nite Tape V Mutable Lists 0 Finite State Machine V Numbers to keep track of state 0 Processing V Way of making decisions if V Way to keep going O E FinnesylaleMachin m M aamaama aaa aa aa aaaaaa 19 E Computer Science wow Universal Computer Lambda Calculus can simulate a Turing Machine Everything a Turing Machine can compute Lambda Calculus can compute also Turing Machine can simulate Lambda Calculus we didn t prove this Everything Lambda Calculus can compute a Turing Machine can compute also ChurchTuring Thesis this is true for any other mechanical computer also mum on Eonvulan wllh Glue aa Phalanx 21 campaaaa sagaaaa mam Generalized Normal Steps Require a constant amount of time Perform a fixed amount of work Readwrite a constant amount of stuff Make a constant number of decisions Localized Cannot scale indefinitely with input size m a mama W aa aa aaaaaa 23 m campa a Equivalent Computers term variable n term term term 7 varlable a term can simulate Ay M 2n M M y 0 v can Emulate Where v does not occur in Mt MMNBM 0 N V v E Firm slam Madwme Tun39ng Machine Lambda Calculus mamcamaagmaaama 2a caaapamsa mums a Normal Steps Turing machine Read one square on tape follow one FSM transition rule write one square on tape move tape head one square Lambda calculus One beta reduction Your PC Execute one instruction 7 o What one instruction does van39es mum on Eonvulan wllh Glue aa Phalanx 22 campama Sc mam What about nonmechanical computers m M aamaama aaa aa aa aaaaaa 24 m Computer Sc mm m Quantum Physics for Dummies Light behaves like both a wave and a windm Semeriiemr quot particle at the same time A single photon is in many states at once Can t observe its state without forcing it into one state Schrodinger s Cat Put a live cat in a box with cyanide vial that opens depending on quantum state Cat is both dead and alive at the same time until you open the box Lecture 40 Computing with Glue and Photons 25 Quantum Computing Feynman 1982 0 Quantum particles are in all possible states 0 Can try lots of possible computations at once with the same particles In theory can test all possible factorizationskeyspathsetc and get the nghtone In practice very hard to keep states entangled once disturbed must be in just one possible state A quot Com uter Selence pdf 1quot le flfliglvl39y of VIRCINIA A Lecture 40 Computing with Glue and Photons 26 COIIIPUtgf SCICI IICC u NH39ERSI 1T 5f mmxm Qubit Regular bit either a 0 or a 1 0 Quantum bit 0 1 or in between p probability it is a 1 o A single qubit is in 2 possible states at once If you have 7 bits you can represent any one of 27 different states If you have 7 qubits you have 27 different states at once Quantum Computers Today 0 Several quantum algorithms Shor s algorithm factoring using a quantum computer 0 Actual quantum computers 5qubit computer built by IBM 2001 Implemented Shor s algorithm to factor World s most complex quantum computation 15 5 3 DWave 16qubit quantum computer 2007 Solves Sudoku puzzles To exceed practical normal computing need gt 50 qub s Adding another qubit is more than twice as hard A Lecture 40 Computing with Glue and Photons 27 Computler SCICIICC 1 1h Nl39ZRSl l39Y 3f v iRmxH Nondeterministic Computing Input 1 Input 1 There can be multiple Start Wr39it 1 transntlons on the same 1 Mme 2 Input NTM takes all of I 39 them at once Each 39 gets its own independent copy of the tape If any path 39IIhPVUFI39 finds halting state that Wyr39te 3 is the result Move Lecture 40 Computing with Glue and Photons 29 EITJI A Lecture 40 Computing with Glue and Photons 28 Compuhtgr Selegce m MYERSI39E39I39gf mama Two Ways of Thinking about Nondeterminstic Computing Omniscient allknowing machine always guesses right the right guess is the one that eventually leads to a halting state Omnipotent allpowerful machine can split in two every step all resulting machines execute on each step if one of the machines halts its tape is the output A Lecture 40 Computing with Glue and Photons 30 Computsr SC169CC 1 Mn XH39ICRSIE Y 31 molxu Lecture 25 Godel and Computability Menu Review and nish coders Proof from Monday D is g iz Computability WWW Danna ms NorWorm Proof General Idea oTheorem In any interesting axiomatic system there are statements that cannot roven either true or false 0 Proof Find such a statement Godel s Statement G This statement does not have any proof in the system Possibilities Gis true 2 Ghas no proof System is incom ate 2 Gis false 9 Ghas a proof System Is mconslstent kw WM 3 a nonnnuinm kw t n wow m 5vaan Finishing The Proof Turn Ginto a statement in the Pn39ncv39pla Mathemata system Is PMpowerful enough to express This statement does not have any proof in the PMsystemquot How to express does not have any proof in the system of PM I What does have a proof of Sin PMquot mem 7 There s a ce of steps that follow the infeence rules that staB with the ll il al axiom and en s Wl What does it mem m not have any proof in PM 7 There is no sequmce of steps that follow the infeence rules that me with the initial axiom kw mmwwn 5 cmynroggoonfg kw t n wow 1 Comogonna Can PM express unprovability There is no sequence of steps that follows the inference rules that starts with the initial axioms and ends with S Sequence of steps T0 T1 T2 TN T0 must be the axioms TN must include S Every step must follow from the previous using an inference ru e l 25 gm cy 7 m Computer saws mum Can we express This statement 0 Yes That s the point of the TNT Chapter in GEB c We can write turn every statement into a number so we can turn This statement does not have any proof in the system into a number l 25 gm cy a m Computer saws mum Godel s Proof G This statement does not have any proof in the system of PM If Gis provable PM would be inconsistent If Gis unprovable PM would be incomplete PM can express 6 Thus PM cannot be complete and consistent mm 25 am And Eomvulzblllly g Generalization All logical systems of any complexity are incomplete there are statements that are true that cannot be proven within the system mm 25 am And Eomvulzblllly 1n Practical Implications Mathematicians will never be completely replaced by computers There are mathematical truths that cannot be determined mechanically We can build a computer that will prove only true theorems about number theory but if it cannot prove something we do not know that that is not a true theorem l 25 gm cy 1 m compt x St a What does it mean for an axiomatic system to be complete and consistent Derives all true statements and no false statements starting from a finite number of axioms and following mechanical infere nce rules l 25 gm cy 12 m Cumputcr saw My What does it mean for an axiomatic system to be complete and consistent It means the axiomatic system is weak Indeed it is so weak it cannot express This statement has no proof t 25 an cy 13 E Comptrth Science Inc0n5StentAxiomatic System De rives all true statements andsomefalse statements starting from a finite number of axioms and following mechanical inference rules 39 Once you can prove one false statement everything can be proverrl false anything mm 25 Eodel 2nd mummy 15 some false statements um Scicncc mm Comp Surprise Quiz Can this be a true statement Q You will have a surprise quiz some day next week If the quiz is Friday it is not a surprise Q is false Sinoe the quiz can39t be Friday if the quiz is not on Monday it isn39t a surprise if it is on Wednesday Q is false Sinoe the quiz can39t be Wednesday if is not a surprise quiz ifit is on Monday Q is false mm 25 Eodel 2nd mummy 17 Pick one Derives some but not all true In erence ru es inference rules Inconsistent Axiomatic System Incomplete Axiomatic System t 25 an cy 14 E Comptrth st Quiz Answers 1 b e read Tyson s essay 2 50 successor of the successor of 0 2 3 MU is not a theorem read Chapter IX Results on these questions were quite poor Only 6 people got gt 4 points on quiz Doing these reading may not have a great direct impact on yourgrade but they are very interesting and worthwhile If that isn39t enough motivation we39ll have anotherquiz some day next week on the same material Computer st mm 25 Eodel 2nd mummy 15 tum Question 5 Computer Scientists 1 models John Backus 19242007 0 Chemistry major at UVA entered 1943 flunked out rst year 0 Joined IBM as programmer in 950 39 i unked our every year never studed Mater 0 Developed Fortran rst studying Wasusr commercially successful 900W WOWquot had the dei rfuoonsequence programmmg language and that everyyearWenr to compiler summer schoo in New 0 Invented BNF replacement grammars John Backus on Si mpIICIty Because it takes pages and pages of gobbledygook to describe how a programming language works it39s hard to prove that a given program actually does what it39s supposed to Therefore programmers must learn not only this enormously complicated language but to prove their programs will wor they must also learn a highly technical logical system in which to reason about them Now in the kinds of systems I39m trying to build you can write a program as essentially an equation like equations in high school algebra and the solution oft at equation will be the program you want The entire language can be described in one page But there39s a catch T ey39re what I call applicative functional language which means there39s no concept of a stored memory at all quot ream n Eodel a campulabrily 19 FL Programming def f E iszero gt NO 0 f0 dec id e1 a e2 e3 if e1 e2 e3 0 lambda x 0 o composition No need to name parameters All expressions are applied to whatever the function is applied to Question 4 o What is computer science 0 Answer for this course study of information processes How to describe them precisely procedures How to predict their properties analysis so far mostly running time What problems can be solved by different types of procedures algorithms polynomial rims ina reasonable amomt of time later finish eventually now ee2ee 2 compw team 25 Eodel a t nmpulzbrlrly 22 ComirIImS Computer Science 0 Another common definition Knuth s The study of algorithms Algorithms What s an algorithm A procedure that always terminates What s a procedure A precise mechanizable description of a process Computability o Is there an algorithm that solves a problem 0 Computable decidable problems There is an algorithm that solves the problem Make a photomosaic sorting drug discovery winning chess it doesn t mean we know the algorithm but there is one o Uncomputable undecidable problems There is no algorithm that solves the problem There might be a procedure but it doesn t always terminate Are there any uncomputable problems Letmre 25 Endel and Empmabulily 25 399 Computer Science 9M mum ix Letmre 25 am am Emuutabillly 2s m Com putgr 1599 The Hapting Problem Input a specification of a procedure P Output If evaluating an application of P halts output true Otherwise output false Alan Turing 19121954 0 Codebreaker at Bletchley Park Broke Enigma Cipher Perhaps more important than Lorenz 0 Published On Computable Numbers 1936 Introduced the Halting Problem Formal model of computation now known as Turing Machine After the war convicted of homosexuality then a crime in Britain committed suicide eating cyanide apple 5 years after G del39s proof Letmre 25 Endel and Empmabulily 27 A Com er SCIEnCe Illlll P M m U A Letmre 25 nudel am Eunputablllly 25 m Complltfg 53a c Halting Problem Define a procedure halts that takes a procedure specification and evaluates to t if evaluating an application of the procedure would terminate and to f if Examples gt halts ambda 3 3 t gt halts ambda evaluating an application of the would not de ne f 0 terminate 0 f define halts proc Letmre 25 Endel and Empmabulily 29 lll Comping 5993 Lemquot 25 nudel am Eunputablllly 30 gig Compgtgg S cgsgsgh Class 24 Computability HmmgprvbemgHmkelYum Luge cslsu Computu saem Unwast a View David Evans m Hsz mm earem some problems mat it is impossible m wrim a program m solve m g 3 Godel39s Proof 6 This statement of number theory does not have any proof in the system of PM If Gwere provable then PM would be 39nconsismnt 1r Gis unprovable then PM would be 39noomplem PM cannot be complete and consistent What does it mean for an axiomatic system to be complete and consistent Derives all true srammems and no false slammem s slarling from a nim number ofaxioms and following mechanical inference rules a WWW m Comm What does it mean for an axiomatic system to be complete and consistent It mems the axiomatic Sysmm is weak is so weak it cannot express This stamment has no proof quot Why is an InconsLstentAxiomatic System less useful than an IncompleteAxiomatic System 5 cmyrrrrscmnfg a IfcnyragsomSE Inc0n5StentAxiomatic System 39Derives all true39 statements and someffalse statements starting from a finite numbeer axioms and following mechanical inference rule s some arse statements 7 L mm 39 955 u can prove one false stateme Once yo nt everything can be proveni false 2 anything Proof Checking Problem Input an axiomatic system a set of axioms and inference rules a statement S and a proof P containing n steps of S Output true ifP is a valid proof of S false otherwise How much work is a proofchecking procedure We can write a proofchecking procedure that is 6n cs m hug1mm 2o Eomvulzblllly g Proof Finding Problem Input an axiomatic system a statement S Output If S is true output a valid proof If S is not true output false How much work It is impossible It might take infinite workquot Godel s theorem says it cannot be done ES 1517 F m t 2 ey n m comptw cicncc Proof 0 A proof of S in an axiomatic system is a sequence of strings T0 T1 Tquot where The first string is the axioms For all ifrom 1 to n Tquot is the result of applying one of the inference rules to Tn1 7Tquot is S o How much work is it to check a proof a L were 8 rs HNIteLength Proof Finding Problem Input an axiomatic system a set of axioms and inference rules a statement S n the maximum number of proof steps Output A valid proof of S with no more then n steps if there is one If there is no proof of S with lt n steps unprovable At worst we can try all possible proofs r inferenoe rules 0 n sleps I possible prook mUCh Checking each proof is 9a work So there is a procedure that is 901W but it might not be the best one mamastm2e m mCumpug cISc cc Computability 12 Cumpx l f sense Algorithms 0 What s an algorithm A procedure that always terminates 0 What s a procedure A precise mechanizable description of a process Computability o Is there an algorithm that solves a problem 0 Decidable computable problems There is an algorithm that solves the problem Make a photomosaic sorting drug discovery winning chess it doesn t mean we know the algorithm but there is one o Undecidable problems There is no algorithm that solves the problem There might be a procedure but it doesn t always terminate A ESl Fall 2m5 mmquot 24 Eunpu39ablllly 13 am Comth Scl glce A ESl Fall 2uus Letmre 24 Eunpu39ablllly 14 m Com putrgr 616UFC Are there any undecidable problems The ProofFinding Problem Input an axiomatic system a statement S 0 Output If S is true output a valid proof If S is not true output false Any others How would you prove a problem is undecidable Hint how did we prove 3SAT was NP Complete once we knew Smiley Puzzle was ESl Fall 2m5 Letmre 24 cunpucabiiity 15 mi impassa esass A ESl Fall 2 u5 Letmre 24 Eunw39ablllly 15 m Compllt r 53e c Undecidable Problems 0 We can prove a problem is undecidable by showing it is at least as hard as the proof finding problem 0 Here s a famous one Halting Problem Input a procedure P described by a Scheme program and its input I Output true if executing P on I halts finishes execution false otherwise Alan Turing 19121954 0 Codebreaker at Bletchley Park Broke Enigma Cipher Perhaps more important than Lorenz 0 Published On Computable Numbers 1936 Introduced the Halting Problem Formal model of computation now known as Turing Machine After the war convicted of homosexuality then a crime in Britain committed suicide 5 years after eating cyanide apple G del39s proof ESl Fall 2m5 Letmre 24 cunpucabiiity A ESl Fall 2 u5 Letmre 24 Eunw39ablllly 15 mm Comp Halting Problem Define a procedure halts that takes a procedure and an input evaluates to t if the procedure would terminate on that input and to f if would not terminate define halts procedure input cs n r inns in 2 emuiy 9 E Computer sciervree we is Halting Examples gt halts Iambda gtlt define fact n if n 1 1 n fact n 1 fact gtlt 7 t gt halts Iambda gtlt define fact n if n 1 1 n fact n 1 fact gtlt 0 f museumiuim2emuiiiy 21 Cumpw gE quotwee lchealedalillle herer nd pro we oriy nowwe een i do mis ior true define find roof 5 agtlt39 If S isevaluates to a proof of S OthenNise evaluates to f if halts findproof exhaustive S axioms rules findproof exhaustive S agtltioms rules Where ndsproofsexhaustive s axioms rules is a procedure that mes all possible proofs siaru39ng from me axioms mat evaluams to a proof if it inds one and keeps working if it doesn t memeium2emuii 23 compr rs nee Examples gt halts lambda x x x 3 t gt halts lambda x define f x f x f x 27 f cs n r inns in 2 emuiy 2 E Computer sciervree we is Can we define halts c We could try for a really long time get something to work for simple examples but could we solve the problem make it work for all possible inputs Could we compute findproof if we had halts memeium2emuii 22 mCumpug oI ncc Another Informal Proof define contradicthalts x if halts contradicthalts null loopforever t If contradicthalts halts the if test is true and it evaluates to loopforever it doesn t halt If contradicthalts doesn t halt the if test if false and it evaluates to t It halts memeium2emuii 24 compurers nee oiis is r Class 13 Quicksort Problems and Procedures Queen s University Belfast Northern Ireland 08150 Computer Science David Evans UHIVCFSIty Of Vlrglnla httpwwwcsvirginiaeduevans Computer Selence Art of Computer Programming Donald E Knuth 0 Volume 1 1968 Fundamental Algorithms 0 Volume 2 Seminumerical Algorithms Random numbers arithmetic Volume 3 Sorting and Searching 0 Volume 4 Combinatorial Algorithms in preparation 2005 0 Volume 5 Syntactic Algorithms estimated for 2010 0 Volume 6 7 planned 777e rst nder of any error in my books receives 2 5 6 signi cant suggestions are also worm 0 32 each If you are really a careful reader you may be able to recoup more Man the cost of me books W5 way w C5150 Fall 2005 Lecture 13 Problems and Procedures A 39A C m ut r len gm 0 Can we do better 0 Making all those trees is a lot of work 0 Can we divide the problem in two halves without making trees Why are we spending so much time on sorting Reason 1 its important 0 Reason 2 it is a well THIS CLASSIC VORK NEWLY UPDA I39IED AND REVISED defined problem for TheArt of exploring algorithm Computer Programrmng deSIgn and complexnty analysis A sensible programmer rarely if ever writes DONALD 15 KNUT g their own code for sorting there are sont procedures provided by all major languages 800 pages long N C5150 Fall 2005 Lecture 13 Problems and Procedures 39m39 Computer Science u 1 Ui39i kii q ViRUNA Recap insertsorttree define inserteltree cf el tree if null tree maketree null el null if cf el getelement tree maketree inseIteItree cf el getleft tree getelement tree getrig ht tree maketree getleft tree getelement tree inseIteItree cf el getright tree n number of elements in tree 0log n n number of elements in lst define insentsonttree cf lst define insertsontworker cf lst if null lst null inserteltree cf car lst insentsontworker cf cdr lst n extractelements insentsont worker cf lst quot Computer Science uiv Lixii39iiiii 4 mexi h C5150 Fall 2005 Lecture 13 Problems and Procedures U1 C5150 Fall 2005 Lecture 13 Problems and Procedures Quicksort 0 Sir C A R Tony Hoare 1962 Divide the problem into Sorting all elements in the list where cf car list el is true it is lt the first element Sorting all other elements it is gt the first element Will this do better OI C5150 Fall 2005 Lecture 13 Problems and Procedures Computgr SCICQCC um Vn39rkqn39nr mumA 39 39A Com uter lence IIIII PM Quicksort define quicksort cf Ist if null Ist lst quicksort cf lter lambda el cf el car lst cdr lst list car Ist quicksort cf filter lambda el not cf el car lst cdr lst mamas1 apxww 7 EcomputeISL 1 le 3 de ne quicksort cf Ist if null Ist lst HOW much append uicksort cf Work IS filmr lambda el cf el car Ist cdr Ist list car Ist qurcksort mum lter lambda el not cf el car Ist son What it the input list is sorted Worst Case nz What it the input list is random Expected n log2 n mm hum1Napx mm a M Computer Comparing sorts gt teshgrowth insertsortrhee n 20 gt testgrowlh quicksort 250 time me 20 n 500 hme 80 t39m 562 n 64000time 16634 n 128000 me 165261 n 128000 time 35813 4 0 19 31 19 21 52 33 20 26 40 11 18 27 20 23 23 33 22 Both are n 10g2 n Absolute time of quicksort much faster 8 m rquot mus mm 1 mum 2nd Duneinn Good enough for VISA n 128000 time 35813 36 seconds to sort 128000 with quicksort n 10g2 n How long to sort 800M items gt log 4 13852943511198905 gt 128000 log 128000 1505252 549491 gt 128000 log 128000 35 128000 log 128000 41812 5 5 9997487578923 800000000 log 800000000 41812 5 9 228 5054130373 392000 seconds 45 days wvwv 9 mcvmpw t g 8 m rquot mus mm 1 mum 2nd Duneinn m m Cvmvnse Are there any procedures more complex than simulating the universe 9013 mm hum1Napx mm 1 m compth Permuted Sorting A possibly really dumb way to sort Find all possible orderings of the list permutations Check each permutation in order until you find one that is sorte Example sort 3 1 2 All permutations 312321213231132 lsrsonedV lsrsonedV lsrsonedV lsrsonedV lsrsonedV lS39SO cs F m w WM 2 m cumpw permutesort issorted define issorted cf Ist or null Ist 1 length Ist and cf car Ist cadr Ist issorted cf cdr st de ne permutesort cf Ist car filter lambda Ist issorted cf Ist allpermutations lst humMilanntmtw 13 quot Computetstattvtce humMilanntmtw 14 Ecomput e ISciqIce we we gt time permutesort lt randintlist 5 cpu time 10 real time 10 gc time 0 de ne allpermutations Ist 4 14 14 45 51 atone gt time permutesort lt randintlist 6 c u time 40 real time gc time 0 map lambda n if length Ist 1 list Ist The permutations of a are a map lambda oneperm 6 29 39 40 54 69 gt time permutesort lt randintlist 7 cpu time 261 real time 260 gc time 0 6 7 35 47 79 82 84 gt time permutesort lt randintlist 8 cpu time 3585 real time 3586 gc time 0 4 10 40 50 50 58 69 84 cons nth ISt n oneperm gt time permutesort lt randintlist 9 allpermutations exceptnth Ist n Crashesl intsto length lst 39 lm mm m ml 15 cumpwg n53 lm mm m ml 15 cumpwg n53 de ne permutesort cf Ist HOW mm at Number of permutations work is lter lambda Ist issorted cf Ist map allpermutations lst lambda n permutesort if length Ist 1 list Ist map lambda oneperm cons nth lst n oneperm allpermutations exceptnth lst n c We evaluated lssorted once for each intsto length lst PermUtatlon 0f ISt 0 There are n length Ist values in the rst map How much work is issorted77 for each possible rst element 0 Then we call allpermutations on the list without n that element length n7 o How many permutations of the list are There are quot 1 1 PermUtati ns there 0 Hence there are n lists to check nl Bataan m Miltmuntmtw 17 n ulcumputtts net Ewan m Miltmuntmtw la chumptht ts m mm it vl Are there any procedures more complex than simulating the universe 9013 Maybe this is the wrong question a F m Mm WM 19 m compiler ScietIce The Sorting Problem 0 Input a list and a comparison function 0 Output a list such that the elements are the same elements as the input list but in order so that the comparison function evaluates to true for any adjacent pair of elements cs n rquot mus mm 1 mum 2nd Duneinn Problems and Procedures Sorting problem is 0n10g n We know a procedure quicksort that solves sorting in n log n o Is the sorting problem n log n To know this we need to prove there is no procedure that solves the sorting problem with time complexity better than n log n samum Mikmu mdw 23 mcompwsc a Procedures and Problems So far we have been talking about procedures how much work is permute sort We can also talk about problems how much work is sorting A problem defines a desired output for a given input A solution to a problem is a procedure for finding the correct output for all possible inputs cs n rquot mus mm 1 mum 2nd Duneinn 2n Ecompumsgggm mums Problems and Procedures If we know a procedure that is that is fn that solves a problem then we know the problem is 0fn The sorting problem is 0n since we know a procedure permutesort that solves it in G n o Is the sorting problem is n No we would need to prove there is no better procedure 22 Cump cs n rquot mus mm 1 mum 2nd Duneinn Sorting problem is 001 log n c There are n possible orderings Each comparison can eliminate at best 12 of them So best possible sorting procedure is 910g2n Sterling s approximation n 201quot 50 best possible sorting procedure is Qlog n QM log n Recall iog rgumglivcauon is normal 3 it log m1 log m log n a F m Mm WM 24 m compt x saw Lecture 10 Puzzling Pegboards mversity of Virginia omputer Science David Evans http Www cs Virgll lla eduevans A CSISO Computer Science rm u C Problem Sets Not just meant to review stuff you should already know Get you to explore new ideas Motivate what is coming up in the class The main point of the P55 is learning not evaluation Don t give up if you can t find the answer in the book you won t solve many problems this way Do discuss with other students Menu Problem Sets 2 and 3 Pegboard Puzzler Elli CDmRuteI fe cs cm gt mm In new Punk 2 PSZ Question 3 Why is define highercard cardl card2 gt cardrank cardl cardrank card2 better than define highercard cardl card2 gt car cardl car card2 Mumnmbuupuu a rmcvmpuggr PSZ Question 8 9 Predict how long it will take Identify ways to make it faster Most of next week and much of many later classes will be focused on how computer scientists predict how long programs will take on how to make them faster m H mm punk 5 E comptw sonnet m n Mun punk 4 r Cvmpumf 5 Mm Can we do better define findbesthand holecards communitycards car sort possiblehands holecards communitycards higherhand m H mm punk 5 E comptw SukiACE Hmmm de ne ndclosest goal lst closeness if 1 length lst pickclosest closenss goal ca lst Fndclosest goal cdr lst closeness dane pickclosest closeness goal numl numZ if lt closeness goal numl closeness goal num2 numl numZ E were knuk up Pegboard punk findbesthand de ne ndbestest lst bestiness if 1 length lst car lst ickbstier bestiness car lst ndbestest goal cdr lst bstiness de ne pickbestier bestiness numl nu if lt bestiness num1bestiness numZ numl numZ define findbest hand Ist findbestest de ne ndbestest lst bestiness if 1 length lst car lst pickbstier bestiness nd de ne pickbestier bestiness numl numZ if lt bestiness numl bestiness numZ numl numZ ar st bestest goal cdr lst bestiness compum Mk findbestest Ist higherhand Next week how much better is this cvmvw u knuk up Pegboard punk LSystems CommandSeqlence Commandlisl m Cumm knuk up Pegboard punk knuk up Pegboard punk PS3 Lindenmayer System Fractals knuk up Pegboard punk m CDMPHESE ammmmmmmmammmmm mandlist Command Cummandlist LSystem Rewriting ugp ummand OfummandSEQtence Start F Rewrite Rule OR30 F F OR60 F F F A F Work like BNF replacement rules except replace all instances at once Why is this a better model for biological systen39s m Comm knuk up Pegboard punk LevelO Start F F Level1 F gt F 0R30 F F 0R60 F F F OR30 F F OR6O F F The Great Lambda Tree of Ultimate Knowledge and Infinite Power Level 5 with color gt3 Kt gt2 gt V LGVGI 2 Level 3 Lecture 10 Pegboard Puzzle 14 m all N39iS39 aj IGN Lecture 10 Pegboard Puzzle 15 39A Computer Science m rbr UNlVlikSl39l Y of VIRGNA Tie Dye by Bill Ingram Rose Bush by Jacintha Henry and Rachel Kay Lecture 10 Pegboard Puzzle 16 A quot39 Com ter Clence Perr UNWIESRSI l Y y ViRGINlA Peg board Puzzle Lecture 10 Pegboard Puzzle Solving the Pegboard Puzzle 0 How to represent the state of the board Which holes have pegs in them 0 How can we simulate a jump board state jump positions a board state 0 How can we generate a list of all possible jumps on a given board 0 How can we find a winning sequence of A quotquot 1 11 1 1 n 17 C0 L t 1039P b dP I 39Q39Com uterScience ec ure I eg oar uzze Pymy UNWIERSI39I Yg39ViRGlNlA mum in Pegboard Punk Removing a Peg removerpeg evaluates to me board you get by removing a peg at posn from me passed board removing a peg ad s a hole define removepeg board posn makeboard boardrows boar d cons posn boardholes board Adding a Peg 39 addpeg evaluates to the board you get by adding a peg at posn to board addi g peg removesa ho de ne addpeg board posn makeboard boardrows Comp ul e I Scien a o rem ngm m Remove Hole define removehole Ist posn if sameposition car Ist posn cd r Ist cons car Ist removehole cdr Ist posn Could we define removehole using map N apflst is always we same as lmgdw ist pm overhole needs to remove elemenislrom we list What if We had a procedure filmr proc Ist that removes l irom Istall eiemems or which procapplied to that eiemem is alse7 ream m gimme 21 Compgg eggeienee de ne lter proc lst if null7 lst null if proc car lst pro cons car lst lter filter lambda pos mum in Pegboard Punk Filter Remove c is true keep it proc cdr lst lter proc cdr lst proc is false drop it define removehole Ist posn not sameposition pos posn Ist computer mm ce mum in Pegboard Punk removehole boardholes board posn Comp um Sc trims Filter define filter proc Ist if null Ist null if proc car Ist proc is true keep it cons car Ist filter proc cdr st filter proc cdr st proc is false drop it gt lter lambda x gt x 0 list 1 4 3 2 1 4 2 mum in Pegboard Punk Comp um Sc r Mk Solving the Peg Board Game Try all possible moves on the board Try all possible moves from the positions you get after each possible first move Try all possible moves from the positions you get after trying each possible move from the positions you get after each possible first move mum in Pegboard Punk computer so mm m LECtU re I S mptti G FOWth CS150 Computer Science University of Virginia Computer Science David Evans httpwwwcsvirginia eduevans Proof by Construction 0 We can prove a there exists an X with property Y theorem but showing an X that has property Y 00 means there are positive constants c and no such that gn s Cfn for all n 2 no So to prove g is in 00 we need to find c and no and ShOW that gn S Cfn for all n 2 no 39 Computer Science 3 far Umwinsrrt cngIRGiNH Lecture 14 Asymptotic Growth 3 Growth Notations g 6 01 BigOh g grows no faster than f upper bound g e f Theta g grows as fast as f tight bound g e 90 Omega g grows no slower than f lower bound Proof Techniques 0 Theorem There exists a polygon with four sides Proof It IS a polygon It has four sides QED What kind of proof is this Lecture 14 Asymptotic Growth 2 Eli Comp tgiggigg gm DisProof by Construction 0 To prove g is not in 00 0f means there are positive constants c and no such that gn s Cfn for all n 2 no So to prove g is not in 00 we need to find a way given any c and no to find an n 2 no SUCh that gn gt Cfn Lecture 14 Asymptotic Growth 4 39A Computer Science Jtt bf UNWlZR SI39I39T y VIRGINM Lecture 14 Asymptotic Growth 5 391 Computer Science m we UNiwzksrri39 Vmoiicm go f to W gltj Funeoons Functions that grow that grow 13 5 no no faster than f than f Lecture 14 Asymptotic Growth 6 393 Computer SCIENCE in m UNIVERSH39Y gr VIRGiKM g is in 0 0 iff there are positive 0 and Q constants c and no such that Examples gn s c n for all n2 n0 g is in 1 0 iff there are positive constants c and no suc gn Z cfn for all n 2 no nisin 20 n is in 001 Yes pickc1 Yes pickc1 10mg in QM 39 1071 is in On Yesl pickc1 Yes pickc10 15 n2 in 20 n2 is not in On Yes pickc1 Pickngtc in 14 mmquot mm 7 Comp uwt e 55 ws Theta Tight Bound gis fiff g is in 0f andgis in 2f m CDmP t i mm m kymvlollt mwlh g 9 Examples 0 IS 1071 in n Yes since 10 is QM and 107i is in On 0 Doesn t matter that you choose different c values for each part they are independent 0 IS n2 in n No since n2 is not in 0n 0 Is It in 012 No since n2 is not in 520 in m mmquot mm 1 m compim cicnc Example 0 IS n in S2012 7126712 for all 712710 12671 for alann0 No matter what c is I can make this false by using n 1c 1 g is in 1 0 iff there are positive constants c and no such that gn Z cfn for all n 2 no lawman mi 3 compus rs TheSetS 00 00 and 90 510 Func uonsi ydnatgrow 39 n o siow er mm 90 How big are 00 20 and 690 m Cmvw s eel mm m kymvlollt mwlh 1n Sorting Cost 0 What grows in the number of elements in Ist o How much work are the pieces ndbest work scales as n increases by one delete work scales as n increases by one o How many times does sort evaluate findbest and delete n 0 Total cost scales as n2 in m mmquot mm 12 m compim Suki1on3 Class 31 Cookie Monsters and Semi Secure Websites C5150 Compute Science University Virginia David Evans camp uta39 science mp lwww rs wqima eduevans Why Care about Security Security Confidentiality keeping secrels Promct user s data Integrity making data reliable iPre39ven ng bampeing rOnIy authorized people can 39nsertmodify dam Availability sProvide service even when attadlted Can39t do mud1 about this wilhout resources How do you authenticate Something you know iPassword Something you h ve iPhysical key email account transparency Something you are iBiometIics voicepr39nt ngerprint etc Serious authentication requires at least 2 kinds in contrast mcmpnu nmtt Early Password Schemes d a Email Login oes rect uni Lo eiysse Password spot Fa ed iog Guess age Login Process Terminal tam alyssa Password mp login sends 39 Trusted subsystem lt aiyssa mes Password Problems Need to store the passwords Dangerous to rely on database being Solve this today Need to transmit password from user to host Dangerous to rely on Internet being f39d t con I en la Solve this Wednesday cs 1517 F m m 3 an MW 7 W Compiler s ruvm Hashing o Manytoone maps a large number of values to a small number of hash values Even distribution for typical data sets values and n 0V Hchar s s0 7 3 mod 10 1 Ef cient h is easy to oompute 9 m Campers cs n rquot mus mum 31511th mm Example OneWay Function Input two 100 digit numbers x and y Output the middle 100 digits ofx y Given x and y it is easy to calculate fxy select middle 100 digits xy Givenfxy hard to nd xand y a F m mm W n m compt x Science First Try Encrypt Passwords 0 Instead of storing password store password encrypted with secret IC 0 When user logs in encrypt entered password and compare to stored encrypted password UserID Problem if Kisn39t so secret decryptK encrypt H P cs nhllmn ue WWW a EComput e ISc39e Ce Cryptographic Hash Functions Oneway Given h it is hard to find x such that Hx h Collision resistance Given x it is hard to find y x such that Hy Hx m mcvmprsgg s es cs n rquot mus mum 31511th mm A Better Hash Function 39 Hx encryth O Weak collision resistance Given 1 it should be hard to find 32 x such that Hy Hx Yes encryption is onetoone There is no such y o A good hash function No its output is as big as the message 12 Cumpx l f scicngg Actual Hashing Algorithms Based on cipher block chaining Start by encrypting 0 with the rst block Use the next block to encrypt the previous block SHA NIST95 512 bit blocks 160bit hash MD5 Rivest92 512 bit blocks produces 128bit hash This is what we will use built in to PHP a F m an MW 13 m computer 5 quote th nce W it Hashed Passwords 14 gm Cmm rs Dictionary Attacks Try a list of common passwords All 14 letter words List of common dog names Words from dictionary Phone numbers license plates All of the above in reverse Simple dictionary attacks retrieve most userselected passwords Precompute Hx for all dictionary entries a F m an MW 15 m campuggr Salt of the Earth This is the standard UNIX password scheme Salt 12 random bits UserID 1125 schemel DES m key salt is an encryption algorithm that encrypts In a way that depends on the salt How much harder is the offline dictionary attack a F m an MW 17 m compt x scams at least 86 of users are ASCII samecase letters lowercase letters in or names Python Code We use the username as a quotsaltquot sinoe they must be unique enclyptedpass clyptclypt passwold user I user l password I lalyssa l9928ef0d7a0e4759ffefbadb8bc84228 levans lbafd72c60f450ed665a6eadc92b3647f 1a Comptw sutures Authenticating Users User proves they are a worthwhile person by having a legitimate email address Not everyone who has an email address is worthwhile Its not too hard to snoop or intercept someone s email But provides much better authenticating than just the honor system 19 compms cuwr cs n rquot mus ledIre cu Could Mondays Users and Passwords dercreateUserseir user email rsmames lasmarne password c seirdp cursor ryptcrypt password user lNSERT 0 users u ernail rs es lasmame password quer c execute query seirdp commrt defcheckPasswordself user password seirdp cursor query HSELECT password FROM users WHERE user user m c execute query pwd c fetchorve o d if not pw remrrv False else wd mIvtcrvlvl passwordruser FmrnusersDHmokieDroczsingam return encpwd pwd extwnorv meremmed 00mm cs nhllmn ledIre 3EooheMonxlers 21 Using Cookies Look at the PS7 provided code cookiespy Cookie must be sent before any HTML is sent utilprintHeader does this Be careful how you use cookies anyone can generate any data they want in a cookie Make sure they can t be tampered with use mdS hash with secret to authenticate Don t reuse cookies easy to intercept them or steal them from disks use a counter than changes every time a cookie is used comma Murmame 23 momma Registering for Account User enters email address Sent an email with a temporary password md surandomrandint 0 9999999 s11randomrandint 0 9999999 encrnd cryptcrypt md strrandomrandint 0 99999 usersuserTabIecreateUser user email rsmames Iasmame encrnd sendMailsend email quothooridesbotcsvirgimaeduquot quotReset Passwordquot quotYou quotcons n ie ame quot account has been created To login usen user quot user quotn password quot encrnd 39nquot From registerrprocess cgi 2c compms cs nhllmn ledIre 3EooheMonxlers W Cookies H I39I39P is stateless every request is independent Don t want user to keep having to enter password every time o A cookie is data that is stored on the browser s machine and sent to the web server when a matching page is visited cs n rquot mus ledIre cu Could Mondays Problems Left The database password is visible in plaintext in the Pythin code No way around this with UVa mysql server Anyone who can read UVa filesystem can access your database The password is transmitted unencrypted over the Internet next class Proving you can read an email account is not good enough to authenticate for important applications cs n rquot mus ledIre cu Could Mondays Lecture 4 I The Value of Everything 2 D 7 L EEEEE T l C8150 Computer Science 3 University of Virginia Computer Science David Evans httpwwwcsvirginiaeduevans Question 2 0 Without Evaluation Rules Question 2 was guesswork Once you know the Evaluation Rules you can answer Question 2 without any guessing A Lecture 4 Value of Everything 3 391quot computer Sc1ence u 11 JNlVIiRHl39Y gy VIRmNH Menu 0 Problem Set 1 0 Evaluation Rules Return Problem Set 1 at end of class today A I 39A Lecture 4 Value of Everythlng 2 2d 100 100 Evaluation Rule 3 Application a Evaluate all the subexpressions 100 ltprimitivegt 100 b Apply the value of the first subexpression to the values of all the other subexpressions Error 100 is not a procedure we only have apply rules for procedures A I 39A Lecture 4 Value of Everythlng 4 2h if not I39COOKieSquot quoteatquot quotstarvequot Q meel a Evaluate not quotcookiesquot Evaluation Rule 3 Application a Evaluate all the subexpressions lt primitive notgt cookies The quotes really matter here Without them what would cookies evaluate to b Apply the value of the first subexpression to the values of all the other subexpressions Application Rule 1 To apply a primitive just do it How do we evaluate an application of a primitive if we don t know its predefined meaning A Lecture 4 Value of Everythlng 5 Lecture 4 Value of Everything 6 quot C m t r Clence V O EVIV QSHTqullGlNLx Defining not library procedure not obj not returns t if objis false and returns f otherwise define not v if v f t 2h if not quotcookiesquot quoteatquot quotstarvequot Evaluate not quotcookiesquot gt f So value of if is value of Express0172 gt starve n mm o Vzlue mayle 7 gm westerns a mm a n Compucgg guitars brighter define brighter lambda color1 color2 if gt getred color1 getgreen color1 getblue color1 getred color2 getgreen color2 getblue color2 t 0 Is this correct Maybebut very hard to tell Your oode should appear in a way that reveals its structure define brighter lambda color1 color2 Eb getred color1 39i getgreen color1 getblue color1 getred color2 getgreen color2 getblue color2 mm o Vzlue mayle 9 E Wm Esme mm o Vzlue mayle Iffy Proposition if Expresson t f Expresson Is this always true if cookies t f Brighter brighter define brighter 1 da colorl colorz gt get red colorl get green colorl et blue colorl gs re co or get green colorz get blue color2 mm o Vzlue mayle 1 n Cmm f lema mum owzlueoIEnwlhnq 12 Brighter brighter define brightness lambda color get red color get green color get blue color define brighter lambda colorl color2 gt brightness colorl brightness color2 Believable brighter makecolor 255 255 0 makecolor 255 1 255 lecture 4 Value at Everylhln 14 i Computer Science Color Absorbed u Framnn m iighi absorbed hyeachlype nimne Eiue 4mm A nm 55mm Hip imam w ed 2 AkMomiimLwircoviESWLECHO Waveieriqih lecture 4 Value at Everylhln EEDnm Cognitive Scientist s Answer define brightness 1 color 0299 get red color 0587 get green color 0114 get blue color define brighter l a colorl color2 gt brightness colorl brightness color2 15 iii Compiimt ers quot cc closercolor Green Star version define closercolor sample colori color2 lt abs getred colori getred sample abs getblue colori getblue sample abs getgreen colori getgreen sample abs getred color2 getred sample abs getblue color2 getblue sample abs getgreen color2 getgreen sample lecture 4 Value at Everylhln i 3 lecture 4 Value at Everylhln 16 Elli mew sisrss abs getred colori getred sample abs getblue colori getblue sample abs getgreen colori getgreen sample define closercolor sample colori color2 lt abs getred color2 getred sample abs getblue color2 getblue sample abs getgreen color2 getgreen sample irrmm 16 Cnmwtc siwss i no lambda abs getredcolor1 getred sample abs getbluecolor1 getbluesample getgreencolor1 getgreensample define closercolor sample color1 color2 lt abs getred color2 getred sample abs getblue color2 getblue sample abs getgreen color2 getgreen sample is 2 a new 9 Compumr L w s define color difference lambda colora colorb abs getred colora getred colorb abs getbluecolora getblueoolorb abs getgreencolora getgreencolorb define closercolor sample color1 color2 color rence color2 sample red sample 0 l o o o blue sample abs getgreen color2 getgreen sample 22 e a mu 2n compu g de ne colordifference bda colora colorb getred colora getred colorb getgreen colora getg reen colorb getblue colora getblue colorb de ne closercolor sample colorl color2 lt colordifference colorl sample colordifference color2 sample What if you want to use square instead of abs mime MVzlueoIEvoylhnq 21 define colordifference lambda of lambda colora colorb of getred colora getred colorb of getgreen colora getgreen colorb of getblue colora getblue colorb define closercolor sample color1 color2 lt color fference color1 sample colordifference olor2 sample m CDmeEL r a new 22 Il III Cvmpw define colordifference lambda of lambda colora colorb Cl getred colora getred colorb cf getgreen colora getgreen colorb cf getblue colora getblue colorb define closercolor sample color1 color2 lt colordifference square color1 sample colordifference square color2 sample 22 e a new 23 n Comma or i rt int unsigned charmasi unsigned char image gtri gt int unsigned chargmasi unsigned char image gtgi b int unsigned charbmasi unsigned char insge gtbi result trtgtgtbtbt Your code should never look like lhisl Use newlines and 39ndeming 0 make ll easy 0 undersland lhe slruclure 0 your codei Nole unless you are wrmng a palenl Then lhe goal is l0 make u as hard l0 undersland as possibie mime MVzlueoIEvoylhnq 24 The Patented RGB RMS Method C5150 P5 Grading Scale rt Gold Star Excellent Work No Gold Stars on gt Ps1 bt bmasi image gtbi Green Star You got everything Iwanted result Hrt 9t9t b bt Blue Star Good Work You got most Patent reqUirememSi things on this PS but some answers could rmasi image gtri gmasi image gtgi new must not be previously available be better ancient Babylonians made mosaics 2 useful SIlver Star Some problems Make sure 3 nonobvious you understand the solutions on today s 14 of ou came u with this method slides most ofyrest used apbs instead which works as well PS1 Average39 lwvzi 25 cmlmgsrgm l 25 comlmgsge gs No upper limit What should you do if you can t 391 if Double Gold Star exceptional work get your code to work Better than I expected anyone would do Keep trying think of alternate 391 Triple Gold Star Better than I thought approaches pOSSIble mowemosalc for P51 G h I f h AC d t t t Quadruple Gold Star You have let 8391 romt e 5 an your broken important new ground in CS which C assrnfj es should be published in a majorjournal 0 But If Its too late for that t t t t t Quintuple God Star You deserve to In your submission explain what win a Turing Award a fast general way to d esnlt WWI aquotd a5 mUCh as You can make the best nonrepeating photomosaic on What You thlnk 395 quot939quot and wrong P51 or a proof that it is impossible Ledm lemlzvewthmq 27 Cumpuggf mum OVzlueoIEvewlhmq za Cumpgggggg go Primitive Expressions Expression PrMitiveEXJression PrMitiveEXJression Number PrMitiveEXJression t f PrMitiveEXJression Primitive Procedure Evaluation Rule 1 Primitive If the expression is a primitive it evaluates to its predefined value Evaluation Rules gtgt ltiimitivegt t Andean an m CumpulchcianLE My Name Expressions Expression iVameExpression iVameExpression Name Evaluation Rule 2 Name If the expression is a name it evaluates to the value associated with that name PM cm gt a h Mm a A Computer 5 Evaluation Rule 5 If Expression if ExpressionPredicate EpreSSIbHCOnsequent EpreSSIbHAIlzername Evaluation Rule 5 If To evaluate an if expression a Evaluate the predicate expressions b If it evaluates to f the value of the if expression is the value of alternate expression OthenNise the value of the if expression is the value of consequent expression mu hullenlivewlhmq 33 m CDmeEL g Rules for Application 1 Primitive If the procedure to apply is a primitive just do it 2 Constructed Procedure If the procedure is a constructed procedure evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value Definitions De nition define Name Expression Defi ition Rule A definition evaluates the Expression and associates the value of Expression with Name gt de gedumb 1 exjj e s39type mu 3 i st39afguinebgrgiyen gamma odqer argumenis were primidvegt gt dumb J ldonlifior39 dumb mam 32 aw m Application Expressions Expression AppIbs bn xpra39slbn 39 39 39 Expresson MoleEYpessions e Expresson MoreEYpessons Evaluation Rule 3 Application To evaluate an application expression a Evaluate all the subexpressions b Then apply the value of the first su bexpression to the values of the remaining subexpressions mu hullenlivewlhmq a4 m CDMPHESE camp in run hullenlivewlhmq 35 Constructing Procedures Lambda Expression ProcedureExpression ProcedureExpression lambda Parameters Expression Parameters a Name Parameters Evaluation Rule 4 Lambda Lambda expressions evaluate to a procedure that takes the given Parametersas inputs and has the Expressbn as its body mam a ew M u Lecture 6 Programming with Data s 150 Computer Science A University of Virglnla Computer Science Davi Evans my wa cs Virginia ecuems Ways to Design Programs 1 Think about what you want to do and turn that into code 2 Think about what you need to represent and design your code around that Which is better mm a n 2 Aj Com History of Scheme 0 Scheme Guy Steele amp Gerry Sussman 1975 Guy Steele codsigned Scheme and created the rst Scheme interpreter for his 4m year project More recendy Steele speci ed Java 1995 Conniverquot 1973 and Planner 1967 0 Based on LISP John McCarthy 1958 Based on Lambda Calculus LISP Lots of Insipid Silly Parentheses LISt Processing language Lisis are pretty important hard to write a useful Scheme program Making Lists Alonzo Church 19305 without mam Last few lectures in course Making a Pair gt cons 1 2 1 2 1 2 cons constructs a pair mm 5 Campuga s mum a In Splitting a Pair gt car cons 1 2 W W 1 gt cdr cons 1 2 2 1 2 car extracts first part of a pair cdr extracts second part of a pair team 6 D 7 Comp Implementing cons car and cdr de ne cons a b lambda w if w a b de ne car pair pair t de ne cdr pair pair f Scheme provides primitive implemenlations for oons car and cdr But we oould de ne lhem ourselves team em 9 gm CDme Esaerissr Triple A triple is just a pair where one of the parts is a pair define triple a b c cons a cons b c define t first t car t define t second t car cdr t define t third t cdr cdr t team em 11 m Comm Why car and cdr Original 19505 LISP on IBM 704 Stored cons pairs in memory registers car Contents of the Address part of the Register cdr Contents of the Decrement part of the Register coulder Doesn t matter unless you have an IBM 704 Think of them as first and rest define first ca r The DrScheme Pretty Big la nguage already de ne these but they are not part define rest cdr ofstandard Scheme team em a Comp u Pairs are fine but how do we make threesomes m m Cvmpvsgg sisrisst Quadruple A quadruple is a pair where the second part is a triple define quadruple a b c d cons a triple b c d define qfirst q car q define qsecond q t first cdr t define qthird t t second cdr t define qfourth t t third cdr t team em 12 m Comma Multuples a quadruple a quintuple a sextuple An octuple is group of octupi A quintuple is a pair where the second part is o A sextuple is a pair where the second part is o A septuple is a pair where the second part is o A is a pair where the second part is a mm m 13 gm Cmnusrgiews Lists List It39s hard to write this A listis either or empty List cons Element USO a pair where the second part is a list mm m 15 gm CDme Esaenss List Examples gt null gt cons 1 null gt list null gt list cons 12 gt list cons 1 null t mm m 17 Cum um ncncc M Rum Lists List cons Element USO A list is a pair where the second part is a list One big problem how do we stop This only allows infiniter long lists u g C 39 P 5 T WE Null List cons Element USO List null A listis either a pair where the second part is a list or empty null mm m 15 MMCDMPHESE 39 More List Examples gt list cons 1 cons 2 null t gt car cons 1 cons 2 mil 1 gt cdr cons 1 cons 2 mil 2 mm 5 IE Class 25 Undecidable Problems A 150 Computer Science E University or vrgma Computer Science David Evans http Www cs wgm eduevans Problem Classes if P NP Undecidable o halB7 0 nd proof Menu Review Undecidability Halting Problem How do we prove a problem is undecidable What do we do when faced with an undecidable problem 2 TL Cmnusrgie rs Halting Problem Define a procedure halts that takes a procedure and an input evaluates to t if the procedure would terminate on that input and to f if would not terminate define halts procedure input cs nhllmn m 25nnattabtmmtm a A 31511 F m m 25 mm W Campuggr s cc Informal Proof define contradicthalts x if halts contradicthalts null loopforever t If contradicthalts halts the if test is true and it evaluates to loopforever it doesn t halt If contradicthalts doesn t halt the if test if false and it evaluates to t It halts Proof by Contradiction Show Xis nonsensical Show that if you have A and Byou can make C NH w Show that you can make A Therefore Bmust not exist 4 X contradicthalts Scheme interpreter that follows the evaluation rules 5 halts 31511 F m m 25 mm 5 m comw x Science a Cump l f scicngg Evaluates to 3 Problem Input A procedure Pand input I Output true if evaluating PI would result in 3 false otherwise Is Evaluates to 3 decidable E31517 Fall m5 lulure 25 unduldzble Problems HelloWorld Problem Input A procedure Pand input I Output true if evaluating PI would print out Hello World false otherwise Is HelloWorld decidable memem 25x g mCumpusgr L Proof by Contradiction Show Xis nonsensical Show that if you have A and Byou can make C NH Show that you can make A Therefore Bmust not exist X halts A a Scheme interpreter that follows the evaluation rules 5 helloworld 00 Undecidability Proof Suppose we could define evaluatesto3 that decides it Then we could define halts de ne halts P I if evaluatesto3 begin P I 3 1 Since it evaluams to 3 we know P I must halt only way it could not evaluate to 3 is if P 1 doesn t halt Note assumes P I cannot produce 0 an error m5 F m m 2 mu 3 W computer Undecidability Proof Suppose we could define helloworld that decides it Then we could define halts define halts P I if helloworld begin removeprints P I print Hello World t 0 E31517 Fall m5 lulure 25 unduldzble Problems m m Owns n E comma E31517 Fall m5 lulure 25 unduldzble Problems From Paul Graham s Undergraduation My friend Robert learned a lot by writing network software he was an un ergrad One of his projects was to oonnect Harvard to the Arpanet it had been one of the original nodes but by 1984 the connection had died Not only was this work not fora class but because he spent all his time on it and neglected his studis he was kicked out of school fora yar en Robert got kicked out of grad school for writing the Internet worm of 1988 I envied him enormously for nding a way out without the stigma of ai ure It all evened out in the end and now he39s a professorat MIT But you39ll probably 39er ifyou don39t go to that extreme it cause i 39 3 years ofprobauorl 400 hours of communlty servlce 310000 ne 12 Cumpu E31517 Fall m5 lulure 25 unduldzble Problems Morris Internet Worm 1988 o P fingerd Program used to query user status Worm also attacked other programs 0 I nop400 pushl 68732f pushl 6e69622f movl spr10 pushl 0 pushl 0 pushl r10 pushl 3 movl spap chmk 3b isworm P I should evaluate to t Worm infected several thousand computers 10 of Internet in 1988 c5150 Fall 2005 Lecture 25 Undecidable Problems 13 m Computer Spence m r 7V l39lZRS l39 of Rulx Undecidability Proof Suppose we could define isworm Then define halts P I if isworm begin deworm P I wormcode 1 Since it isa worm we know wormcode was evaluated and Pmust halt I The wormcode would not evaluate so Pmust not halt Can we make deworm CS150 Fall 2005 Lecture 25 Undecidable Problems quot m llt 1 C1 II C 15 C0 Paer SNI F 139 VHSIXLJ Solving Undecidable Problems 0 No perfect solution exists Undecidable means there is no procedure that 1 Always gives the correct answer 2 Always terminates Must give up one of these to solve undecidable problems Giving up 2 is not acceptable in most cases Must give up 1 A CS150 Fall 2005 Lecture 25 Undecidable Problems 17 our quot26quot3139 3 Worm Detection Problem Input A program Pand input 139 Output true if evaluating P I would cause a remote computer to be infected Virus Detection Problem Input A program Pand input 139 Output true if evaluating P I would cause a file on the host computer to be infected A c5150 Fall 2005 Lecture 25 Undecidable Problems 14 WE Computer Spence ill I n V V 39I RS 1Y5 1 UN Conclusion AntiVirus programs cannot exist The A11 QFCOmfPUte JF Virus esea rch and efense 39 letr Szr Symante39c M esearch Ingefense Pelrr Scull A c5150 Fall 2005 Lecture 25 Undecidable Problems 15 mi Compu tg clechlen illr x quota t Actual isvirus Programs 0 Give the wrong answer sometimes False positive say P is a virus when it isn t False negative say P is safe when it is 0 Database of known viruses if P matches one of these it is a virus Clever virus authors can make viruses that change each time they propagate AV software finiteproof finding Emulate program for a limited number of steps if it doesn t do anything bad assume it is safe A cs15o Fall 2005 Lecture 25 Undecidable Problems 13 mi Compqtgr Iclegkclen rum 39 quot7 l Class 34 Computing with Life and the Chicken Flu C8150Conunuer8cmnce University of Virginia David Evans Computer Science httpwwwcsvirginiaeduevans DNA 0 Sequence of nucleotides adenine A guanine G cytosine C and thymine T 0 Two strands A must attach to T and G must attach to C A C5150 Fall 2005 Lecture 34 DNA Compute s and the Chickm Flu 2 Computgr SEICXICC mumu jl Wm Hamiltonian Path Problem 0 Input a graph stalt vertex and end veltex Output either a path from start to end that touches each vertex in the graph exactly once or frallcse indicating no such path exists start CH0 CH0 BWI end BWI ltgt How hard is the IAD Hamiltonian path Encoding The Graph 0 Make up a two random 4nucleotide sequences for each city CHO CHOl AC39I39I39 CHOZ gcag RIC RIC1 TCGG RICZ actg IAD IAD1 GGCT IAD2 atgt BWI BWI1 GATC BWI2 tcca o If there is a link between two cities A gtB create a nucleotide sequence AZBl CHO gtRIC gcagTCGG 321732339 mp www mllle ovgveseavcnnanolemhapgo odionidna mm problem u 3 am C mmtsg szrass C5150 Fall 2005 Lecture 34 DNA Compute s and the Chickm Flu A 4 Ill C mPHTSFW EEE EEH Encoding The Problem 0 Each city nucleotide sequence binds with its complement A lt gt T G lt gt C CHO CHOI ACTT CHOZ gcag CHO39 TGAA cgtc RIC TCGGactg RIC AGCCtgac IAD GGCTatgt IAD CCGAtaca BWI GATthca BWI CTAGaggt 0 Mix up all the link and complement DNA strands they will bind to show a path Path Binding CHO IAD RIC BWI TGAACgtcCCGAtacaAGCCtgacCTAGaggt llll llllllll llllllll llll gcagGGCTatgtTCGGactgGATC CHO gtIAD IAD gtRIC RIC gtBWI TCGGactg RIC CHO ltgt 3W1 ACTTgcag GATthca IAD GGCTatgt 5 gm C mRtlfF ElSll S A C5150 Fall 2005 Lecture 34 DNA Compute s and the Chickm Flu 5 m Comput t SCIEEIC Getting the Solution 0 Extract DNA strands starting with CH0 and ending with BWI Easy way is to remove all strands that do not start with CH0 and then remove all strands that do not end with BWI 0 Measure remaining strands to nd ones with the right weight 7 8 nucleotides 0 Read the sequence from one of these strands ESISEI Fall zuus Leemre a4 DNAcumputes am the Ehidltm Flu 7 gm Convilfsa sa arsa DNAEnhanced PC ESISEI Fall zuus Leemre a4 DNAcumputes am the Ehidltm Flu 9 M Compeyit 53 iisg Why don t we use DNA computers 0 Speed shaking up the DNA strands does 1014 operations per second 400M supercomputer does 1010 0 Memory we can store information in DNA at 1 bit per cubic nanometer o How much DNA would you need Volume of DNA needed grows exponentially with input size To solve 45 vertices you need 20M gallons A ESISU Fall 2nu5 teemre 34 DNAuemputes and the Ehidltm Flu 8 m Comput t c seg ca Complexity for DNA Computer 0 Complexity classes are different than for regular computers because a step is different Scales with amount of DNA you have 0 Turns time complexity into DNAamount complexity If you have an amount of DNA that is exponential in input size you can solve NP complete problems in P steps Computability for Quantum and DNA computers 0 DNA computers no change to what is computable only changes time it takes 0 Quantum computers They are so strange they even change what is computable Quantum physics provides true randomness something a Turing machine cannot do A ESISU Fall zuus Leemre a4 DNAcumputas and the Ehidltm Flu 10 W Compllf g c scg ccu Chicken Flu Some of these slides are from my USENIX 2004 talk What Biology an and an 2 Teach Us About Secun39 httpwwwcsvirginiaeduevansusenix04 ESISEI Fall zuus Leemre 34 DN A cuhputes am the Ehidltm Flu 1 1 m Computing gsgcg A ESISU Fall zuus Leemre a4 DNAcumputas and the Ehidltm Flu 12 W C0mpl gcg Viruses 0 Genetic material RNA with protective coat 0 Receptor binding proteins attach to cell 0 Injects genetic material into cell nucleus 0 Uses proteins in cell to 4 re roduce 200 m p Influenza Virus 0 Releases copies to Infect more cells calm Fall mus Letmre 34 DNAucnpumrs am the uhtkm Flu 13 E131 Compute Virus Binding 0 To bind to a cell virus receptor binding proteins must match cell membrane receptors Receptor 0 Virus is internalized by cell injects RNA into nucleus Hosthell calm Fall mus Letmre 34 DNA umpumrs am the uhtkm Immune Systems vs Virus Scanners 0 Standard AntiVirus software scans for known attacks compare code against a library of already known attacks 0 Approach doesn t work if new viruses emerge quicker than updates Internet Worms spread time 20 minutes Human genome updates 20 years I Need a way to detect and defeat previously unknown attacks calm Fall mus Letmre 34 DNA umpumrs am the uhtkm Flu 17 if39i Comtieta rci iist Influenza Virion Hemagluttin Envelope 400 spikes around virus protective shell built mg me was 3353 Ziggenjegemded proteins and 9 determines what It can bind v to red blood cells 39 7 CapSId F RN A Influenza Viruses Named according to the Neuraminidase surface proteins This demrmi s the shape Helps new V39nons escape antibodies must bind to h 5t ce Avian Flu H5N1 calm Fall mus Letmre 34 DNAupmpumrs am the uhtkenriu 14 m CompuM Virus Scanning Antibody Virions have a specific shape to bind to hosts so scanners antibodies can recognize that shape and block virions Receptor HostQell calm Fall mus Letmre 34 DNA umpumrs am the uhtken Pathogen Diversity Genetic drift random point mutations Some will be successful and multiply RNAbased viruses mutate very rapidly Genetic reassortment mixing u If two strains of influenza virus infect the same cell they mix up their genes Influenza A viruses have 8 segmenB Can mix and match segments If an organism is invaded by a strain that attacks a different species segmenis can jump to the new species by combining with other viruses calm Fall mus Letmre 34 DNAupmpumrs am the uhtkenriu 15 m Computel 549ng mat Receptor Diversity 0 Lymphocytes are white blood cells that have surface proteins to recognize intruders when stimulated by antigen they make antibodies Need to recognize all foreign intruders but DNA can t know about all 1016 possible intruders 0 Gene segments are randomly combined to form different receptors Create 107 new lymphocytes every day Lymphocytes that match intruders reproduce quickly build immunity 0 But need to ensure lymphocytes don t match self new Immune System Disorders False negatives are immune deficiencies False positives are autoimmune diseases Reject organ transplants Multiple Sclerosis motor nerve cells are antigens Rhumatoid Arthritis connective tissue is antigen 21 CumputhScscnc c cs m rquot mus mum cm on Eomvulers 2nd lhe chmquot Flu m Racing Parasites Parasites evolve quickly E Coli bacteria 1 hour per generation Influenza virus Offspring should be optimized for a dfferenl environment than their parents parasites have evolved Matt Ridley The Red Queen cs F m m it mm Pquot 23 m compt x Recognizing Self Major Histocompatibility Complex Surface molecules that are unique to individual on all cells except red blood ce ls Authenticate cell as self Diversity of MHC types protects a population Thymus gland Lymphocytes that match self molecules are Eirginated others are mature and enter 0 Y Computer Immunology 0 Forrest Hofmeyr and colleagues 1994 o Recognize computer intrusions 0 Generate library bitstrings that encode patterns of normal behavior system calls network connections etc 0 Generate random detectors keep ones that don t match the normal behavior 0 Recognize behaviors that are abnormal as possible intrusions cs n rquot mus mum cu DNAEomvulers 2nd lhe Achieving Diversity Natural selection reduces diversity Will select against inferior genes for particular current environment Sex maintains diversity Obtain multiple forms of a gene AB blood e Retain currently unfavored genes Opposites attract o Wedekind and Fun found that men and women are attracted to odoro members of opposite sex that have MHC genes most different from then39selves ES 1517 F m m 3 mum m m mm m 24 m Cumputcr Sc mm m Now playing JS Bach re Artoszgue Lectu re 8 Menu Recursmg Recurswely i 393 Recursive Procedures GEB Chapter V Fibonacci Returns Richardreizmarsvar 39 K RTNS parked outside the theater g g whereQED is playing I Alan Alda playing Richard Feynman in QED V C8150 Computer Science University of Virginia Computer Science David Evans httpwwwcsvirgin ia eduevans 3150 Fall 2005 Leciure a Recursing Recursively 2 m Comma sasrs Defining Recursive Procedures Example 1 Be optimistic De ne findclosest goal numbers that Assume you can solve it evaluates to the number in the list If you could how would you solve a bigger numbers list that is closest to goal problem 2 Think of the simplest version of the oqindclosest 200 list 101 110 120 201 340 588 problem something you can already 1findclosest 12 list 1 11 21 solve I39 his is the base case gt ndmsest 12 st 95 3 Combine them to solve the problem 95 A 3150 Fall 2005 Lecture 5 Recusing Recursively 3 ll Comput r Scxegc e 3150 Fall 2005 Lecrure a Recursing Recursively 4 m Computsr Seiervice Find Closest Number Find Best Match Be optimistic Strategy Assume you can de ne If the new number is better than the best ndclosestnumber goal numbers match with the other number use the new number Otherwise use the best match of the other numbers that finds the closest number to goal from the list of numbers What if there is one more number Can you write a function that finds the closest number to match from new number and numbers 3150 Fall 2005 Lecture 5 Recusing Recursively 5 Eli C mPltFS 3150 Fall 2005 Lecrure a Recursing Recursively 5 m Computgr S Optimistic Function define ndclosest goal numbers if lt abs goal first numbers a s goal findclosest goal rest numbers first numbers findclosest goal rest numbers cs 1517 rquot mus ledIre a serum9 myth 7 PM ComRuteI Science cumsquot The Base Case define findclosest goal numbers if 1 length numbers first numbers if lt abs goal first numbers abs goal findclosest goal rest numbers first numbers findclosest goal rest numbers Same as before cs 1517 rquot mus ledIre a serum9 myth Computer Science new Seen Anything Like This de ne ndsbestsmatch sample tiles colors if length tiles1 1f er irst til c0 mparator e is just one tile es at ile is the best mamh icksbetters match otherwise the b t sam le 39 39 first tiles findsbests match sample tiles Use picksbetmrsmatch rest tiles to determine which one is better colorscomparator colorscomparamr cs 1517 rquot mus ledIre a serum9 myth n cumputerseimce mmw n Defining Recursive Procedures 2 Think of the simplest version of the problem something you can already solve If there is only one number that is the best match cs 1517 rquot mus ledIre a serum9 myth a PM ComputeISc mums n define ndsclosest goal numbers i 1 length numbers first numbers if lt abs 7 goal first numbers abs 7 goal ndsclosest goal rest numbers Testl ng rst numbers ndsclosest goal gt ndclosestnumber 200 list 101 110 120 201 340 588 201 gt ndclosestnumber 0 list 1 1 gt ndclosestnumber 0 list rst expects argument of type ltnonempty istgt given ES 1511 r m team a Na Runway m m Computer Sc urer GEB Chapter V You could spend the rest of your life just studying things in this chapter 25 pages usic armon Stac s and Recursion D ogy Language Structure Number Sequences Fractals PS3 out today Quantum Electrodynamils late lecture DNA next to last lecture Samenessindifferentnss Gameplaying algorithn39s upooming lecture cs 1517 rquot mus ledIre a serum9 myth 12 Ecumpmse mm is Fibonacci s Problem Filius Bonacci 1202 in Pisa Suppose a newlyborn pair of rabbits one male one female are put in a field Rabbits mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits Suppose that our rabbits never die and that the female always produces one new pair one male one female every month from the second month on How many pairs will there be in one year Rabbits 38 m 88 1 quotn 8 8 2 888 88 3 88888888885 From ESISEI Fall zuus Letmre a Retursma Returslvely 13 Illllll C mIZ ES ElPS csrsu Fall zuus Letmre a Retursma Returslvely 14 mCmPiismeasrss Fibonacci Numbers GEB p 136 These numbers are best defined recursively by the pair of formulas FIBO n FIBO n 1 FIBO n 2 for n gt 2 FIBO 1 FIBO 2 1 Can we turn this into a Scheme procedure Note SICP defines Fib wiih Fib0 0 and Fib1 1 for base case Same function except for Fib0 is undefined in GEB version Defining Recursive Procedures Slide 3 Returns 1 Be optimistic Assume you can solve it If you could how would you solve a bigger problem 2 Think of the simplest version of the problem something you can already solve l39his is the base case 3 Combine them to solve the problem cslsu Fall zuus Letmre a Returslng Returslvely 15 mi Computer Screhce wt csrsu Fall zuus Letmre a Retursma Returslvely 6 m C W E El i f Defining F130 1 Be optimistic assume Defining fibo bo n evaluates to the nth Fibonacci you can solve It If you These numbers are best could how would you defined recursively by III number solve a bigger PFOblem the pair of formulas define fibo n 2 Think of the simplest FIBO n if or n 1 n 2 FIBO1 FIBO 2 1 version of the problem 13113001 71 something you can FIBO n 2 1 base case FIBO n already solve for n gt 2 bo n 1 FIBO r1 71 3 Combine them to solve FIBO 1 F130 2 1 FIBO r1 7 2 the problem bo 39 n 2 for n gt 2 ESl lel2 5LetmreEReturSInaReturswely 17 Compufgg v ES FallZ SuermreEReursingReursively la Comgw g Fibo Results gt fibo 2 gt bo 3 Why can t our 1Mx 2 Apollo Guidance gt fibo 4 Computer calculate fibo 100 gt fibo 10 55 gt fibo 100 5t7 work g a er4 hours To be mntinued Monday answer is in SICP 12 cs n rquot mus mum w Ec m quot5 5 ws Recursive Transition Networks ORNATE NOUN 4 begm ORNA TE NOUN NOUN cs n rquot mus mm a Mum9 mmer 21 mcvmvm Recursive Transition Networks ORNATE NOUN beam quot DRVA TENDUIV AR Tl EADfCTllENDUN DRVA TENDUIV AR Tl EADfCTVEADEETIVENDUN 23 Cumpytcr cicncE M m cs n rquot mus mum Recursive Transition Networks ORNATE NOUN LE ADJECTIVE NOUN 7 Can we describe this using Backus Naur Form 2n comwmgge gs cm s Recursive Transition Networks ORNATE NOUN ORNA TE NOUN NOUN ORNA TE NOUN AR 77le ADJEC 77 V5 NOUN mamam mm mmmmy 22 mCumpug uISc cc Recursive Transition Networks ORNATE NOUN DRVA TENDUIV AR Tl EADfCTllES NOUN ADfCTllES ADECTIVEADEETIVES ADfCTllES 24 Ecumpymsamgg M m Recursive Transition Networks ORNATE NOUN AW quot DPTARTIQEADfCTllESNDUN ADECTIVEADJECTIVES RTl E DP TAR Tl E cs n r inns m a Mmquot may 25 E Computer 5 39 Music Harmony Kehes Harmom39sches Labyrinm Little Harmonic Labyrinth Recursive Transition Networks ORNATE NOUN DRAM TENDLW AR TICLE ADJECTIVP39 NOUN Using extended BNF notation item item is optional 0 or 1 of them I39te 0 or more items Which notation is better cs n r inns m a Mmquot may 25 E Computer 5 39 Hey Jude John Lennon and Paul McCartney 1968 cs n r mus team 5 Wm Reunslvely cs n r mus team 5 Wm Reunslvely Hey Jude Tonic Hey Jude don39t make it v d take a sad song and make r Tonic better Ree iV member to iether into your Tonic heart then you can v start to make itbetr Tonic rter ielse 3 a Pain Hey Jude rer And Anyume you feei me 39 ielse V550 V550 ielse iese Better Coda iese 57er 57er Dadada ends on C F Eb Bb F Coda me e m m a we may 29 m campus me e m m a we may 3 m comp Class 22 Inheritance CS150 Computer Science David Evans University ofVirginia httpwwwcsvirginiaeduevans Computer Science Objects When we package state and procedures together we have an object 0 Programming with objects is objectoriented programming A c 39A C m r 1 nce 3 llll C5150 Fall 2005 Lecture 22 Inheritance Counter in Scheme using let define makeocounter let count 0 lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count Menu 0 Objects Review 0 ObjectOriented Programming 0 Inheritance C5150 Fall 2005 Lecture 22 Inheritance quot I Computer Science ul 1 ll lgtll n Vmuu N E Counter in Scheme define makeocounter lambda count lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count 0 C5150 Fall 2005 Lecture 22 Inheritance A 39 quot m t I 1 11C 4 gmgcquot UI Defining ask ask Object Met700 gt ask bcounter 39howmany 0 gt ask bcounter 39next gt ask bcounter 39howmany 1 define ask object message object message 39A Computer Science u m rmHm n39 ViRUIVi a C5150 Fall 2005 Lecture 22 Inheritance Com uter Science lquotquot pullquot UMHMsin u VmnH C5150 Fall 2005 Lecture 22 Inhen39tance makenumber define makenumber lambda n lambda message cond eq message 39value lambda self n eq message 39add lambda self other Wellsee Whylaiefioday ask self 39value ask other 39value 7 cbmpmexsrewe sle cs n lel mus ledIre 22 x de ne makemdmber global lambda n environment lambda message cond lambda self n eq7 message add lambda self other make quotumber39 ask self value ask other value gt de ne san makenumber 3 gt ask san 39value gt ask san 39add makenumber 4 parameters message body oond eq cs n lel mus ledIre 22 mum 9 m CDmP t i Simula Considered the first objectoriented programming language Language designed for simulation by Kristen Nygaard and OleJohan Dahl Norway 1962 Had special syntax for defining classes that packages state and procedures together cs n lel mus ledIre 22 x 11 Cumpx l f cicnc ask with arguments define ask object message object message The means take all the rest of the parameters and make them mfo a llst define ask object message fargs apply object message object args 3 Computer o39e of wlkl s ObjectOriented Programming mamas 1n mcbmpuggrse be Counter in Simula class counter integer count begin procedure reset count 0 end procedure next count count 1 end integer procedure howmany howmany count end 12 Cumpx l f sense XEROX Palo Alto Research Center PARC 19705 0 Bitmapped display 0 Graphical User Interface Steve Jobs paid 1M to visit and PARC and returned to make Apple LisaMac 0 Ethernet 0 First personal computer Alto o PostScript Printers Don t worry about What anybody else is g0ng to do The best way to predict the future is to in vent it Really smat people with reasonable funding can do quot just about anything that Dyn a bOOk 19 doesn t lioate too many of 7 III Just a model VEm ons Laws ObjectOriented Programmlng A an Kale 1971 C5150 Fall 2005 Lecture 22 Inheritance 13 Compgtgg c5150 Fall 2005 Lecture 22 Innentanue 14 r Commits Dynabook 1972 Tablet computer Intended as tool for learning 0 Kay wanted children to be able to program it also o Hallway argument Kay claims you could BYTE define the most powerful language in the Magazine world in a page of code A t Proof Smalltalk Ugus Scheme is as powerful but takes two pages 1981 c5150 Fall 2005Lecture221nheritanue 15 Compgrt Eleljl lem 15 Compgt a cleggsn Smalltalk Counter in Smalltalk Everything is an object class name counter Objects communicate by sending and instance variable names count receiving messages new count lt 0 o ObJects have their own state which may contain other objects neXt count lt count 1 o How do you do 3 4 howmany A count send the object 3 the message 4quot A I39l39fll39l Computer SCICHCC C5150 Fall 2005 Lecture 22 Inheritance 18 ldlAwUN l39l ullw mauler A c5150 Fall 2005 Lecture 22 Inheritance 17 Illlll Computer Scxence Aill UMl lvm l H Vm ll Inheritance cs F m 19 m computer saws mums makefraction de ne makefraction lambda numeratordenominator lambda message cond eq message 39value eq messa e a game as lambda self other makernumber e Nole ouradd lambda SEW numerator melhod eyaluales eq message getdenomlnator 0 a numben HO lambda self denominator W sccgl woi e D beller cs m rquot mus mm 22 Man 21 There are many kinds of numbers Whole Numbers 0 1 2 Integers 23 73 0 Fractions 12 78 Floating Point 23 00004 314159 But they can t all do the same things We can get the denominator of a fraction but not of an integer cs F m 2H m computer saws mums Why is redefining add a bad thing Cutandpaste is easy but There could be lots of number methods subtract multiply print etc Making the code bigger makes it harder to understand If we fix a problem in the number add method we have to remember to fix the copy in makefraction also and real complex float etc II Mm W consume 22 mcumpuggrs m makefraction define makefraction numer denom let su per makenumber f lambda message cond eq message 39value lambda self numer denom eq message 39get denominator lambda self denom eq message 39getnumerator lambda self numer e lse super messag e Using Fractions gt define half makefraction 1 2 gt ask half 39value gt ask half 39getdenominator gt ask half 39add makenumber1 3 2 gt ask half 39add half 1 consume 23 mcumpms H mm m cc consume 24 mcumpms m mm it v gt trace ask gt trace eq gt ask half 39add half ask 12ltproceduregt add 12ltproceduregt eq add Va ue I f ask ltprooeduregt value eq add getdenominator 5 Value Value eq add getnumerator i 12 ma ke n u m be r 5 ma kefra ction gt ask half 39add half ask ultproceduregt add 22ltproceduregt eq add va ue ask ltprooeduregt value f eq add getdenominator i KEEP value Value ask ltprooeduregt value eq add value i Keq Value Value I lift eq add add I 12 t 1 1 cs n r mus red 22 we 25 Comp uwt eyg injures Inheritance Inheritance is using the definition of one class to make another class makefraction uses makenumber to I39nreril the behaviors of number I lift add etnumerator i 1 I eq 9 ask ltprooeduregt value eq add value i He ll Value value I lift eq add add I 12 t 1 1 cs nhllmmLulumzzunhenlzme 2s Comp uwt gie 0 English A Fraction is a IrilrdofNumber o C Fraction is a alsivea cass whose base cbss is Num Java Fraction extends Number 0 Eiffel Fraction lhheliE from Number Noie people 0 Beta melimes draw lhis dillerenl Ways Fraction is a subpattem of Number 0 Smalltalk 72 and Squeak 05 Don39t have inheritance es r me 27 m cumpmer science new A measure 23 mcumpmerse Ce Mm m CS 150 Fraction inherits from Number Fraction is a subclass of Number The supercass of Fraction is Number Subtyping Subtyping is very important in statically typed languages like C C C Java Pascal where you have to explicitly declare a type for all variables method Numberadd Number n Because 01 subryprng ember a Number or 3 Fraction subtype 0 Number could be passed as be ergumem We won t cover subtyping although we will talk more about types later es r me 29 m computer Science new r es r me an m computer sc mm m Lecture 19 Programming with State C5150 Computer Science University of Virginia Computer Science David Evans httpwwwcsvirginiaeduevans From Lecture 3 Evaluation Rule 2 Names If the expression is a name it evaluates to the value associated with that name gt define two 2 gt two 2 Notices Today normal lab hours 4530pm Thursday s lab hours will be 57pm instead of normally scheduled times Spring Break there will not be normally scheduled lab hours or office hours between March 3 and March 10 Normal lab hours resume Sunday March 11 Exam 1 will be returned at end of today s class Schedule for rest of semester is updated on the course web wwwcsvirginiaeduschedule Lecture 1939 Mutalion 2 399 Com uter Science 39 M PM mm m 4 Wm Names and Places 0 A name is not just a value it is a place for storing a value 0 define creates a new place associates a name with that place and stores a value in that place define x 3 x 3 Lecture 19 Mutalion 3 39 39A m u 1 l n tutcquot PFS SSVEE Lecture 19 Mutation 4 399 Com uter Science gm PM Bang set set bangquot changes the value associated with a place gt define x 3 gtx 3 gt set x7 gtx 7 set should make you nervous gt define x 2 L t 19M tau39 39e Com uterScience ec ure u on 5 EM PM UVI39l K H Viuuin gt nextx 3 Before set all procedures gt nextx were functions except for 4 some with sideeffects gt X The value of f was the 4 same every time you evaluate it Now it might be different Lecture 19 Mutation 5 E Defining nextx define nextx set x x1 X 5definernegtlttx K lambda o syntacticsugarfox e I S Evaluation Rules gt define x 3 gt nextx x 0 or 10 DrScheme evaluats gt X neXtX application subexpressions left to right but Scheme evaluation rules allow any 5 7 m Compucgg idem mm m mum 3 gm Cmnusrgiews setcar and setcdr setcar 0 V Replaces the car of the cons pwith v setcdr 0 V Replaces the cdr of the cons pwith v These should scare you even more then set gt define pair cons 1 2 mm m mun g E 5355 Esamssl mm m mun 1n MMCDMPHESE 39 gt define pair cons 1 2 1 2 gt setcar pair 0 gt car pair 0 gt cdr pair gt setcdr pair 1 gt pair 0 1 map Functional Solution A procedure that takes a procedure of one argument and a list and returns a list of the results produced by applying the procedure to each element in the list define map proc lst if null lst null cons proc car lst map proc cdr lst 5 n Il II Comm mm m mun 12 amvrm 39 e map proc lst Imperathe null lst null cons proc car lst Solution map proc cdr Em A procedure that takes a procedure and list as arguments and replaces each element in the list with the value of the procedure applied to that element define map f lst if null lst void egln setcar lst f car lst map f cdr lst m 5 m 3 Computer 5658355 mums Mutation Changes Everything We can no longer talk about the value of an expressionquot The value of a give expression can change We need to talk about the value of an expression in an eXScutbn enw39ronment The order in which expressions are evaluated now matters mm 1 ml 15 Names and Places A name is a place for storing a value define creates a new place cons creates two new places the car and the cdr set name expr changes the value in the place name to the value of eXpr setcar pair expr changes the value in the car place of pairto the value of eXpr 5 17 Comm 5 Programming with Mutation gt map square intsto 4 gt define i4 intsto 4 gt map square i4 gt i4 1 4 9 16 eAueledull gt define i4 intsto 4 gt map square i4 1 4 9 16 gt i4 leuollounzl 1234 5 14 quot Computer 5658955 mums Why Substitution Fails gt define nextx set x x 1 x gt define x 0 gt lambda x x x nextx 2 Substitution model nextx negtlttgtlt begin set X X 1 X begin set X gtlt1gtlt begin set 0 0 1 0 begin set 0 0 1 0 0 0 mm 1 ml 15 Lambda and Places lambda x also creates a new place named x o The passed argument is put in that place How are these places different gt define X 3 gt lambda X X 4 4 m s m 1a m complm scig gg Location Location Location Places live in frames An environment is a frame and a pointer to a parent environment All environments except the global environment have exactly one parent environment global environment has no parent Application creates a new environment 5 lg compuwl gg Environments 1ltprimitivegt nu l7 1ltprimitivenullgt global environment The global environment points to the outermost frame It stans with all Scheme primitives gt define x 3 Evaluation Rule 2 Names A name expression evaluates to the value associated with that name To nd the value associated with a name look for the name in the frame associated with the evaluation environment If it contains a place with that name the value of the name expression is the value in that place If it doesn t the value of the name expression is the value of the name expression evaluated in the parent environment if the current environment has a parent Otherwise the name expression evaluates to an error the name is not de ned l 5 mm 21 Compu t c How to Draw a Procedure A procedure needs both code and an environment We ll see why soon We draw procedures like this Environment inter mm m mun 5 2 compuwl gg Procedures g1 0 environment null 22ltprimi ve gt define double lambda x x x mm m mun 22 How to Draw a Procedure for artists only Environment pointer xx Input paramemrs 39 Procedure B ody mm m mun 24 1 Pure I Luck Makeup lab hours 4306 today C8150 Com10uter Science Imiii University of Virginia David Evans Inquot computer SCience httpwwwcsvirginiaeduevans Pegboard Puzzle Lecture 1139 1 Luck 2 quot com 11th Science PM LlNlVlZRSI l3939 gquot VIRGINIA Solving the Pegboard Puzzle 0 How to represent the state of the board Which holes have pegs in them 0 How can we simulate a jump board state jump positions a board state 0 How can we generate a list of all possible jumps on a given board 0 How can we find a winning sequence of jumps Lecture 1139 1 Luck 3 quot Com uter Science I Pin1w UNWIiRSl39I39Y y3971RlNl Data Abstractions define makeboard rows holes cons rows holes define boardholes board cdr board define boardrows board car board define makeposition row col cons row col define getrow posn car posn define getcol posn cdr posn define sameposition posl p052 and getrow posl getrow posZ getcol posl getcol posZ Lecture 11 1 Luck 4 o quot Com uter Sc1ence Pquot M UNIVIiksI39I I39 gr39VIRUleA Removing a Peg removepeg evaluates to the board you get by removing a peg at posn from the passed board removing a peg adds a hole define removepeg board posn make board boardrows board cons posn boardholes board Lecture 11 1 Luck 5 Adding a Peg addpeg evaluates to the board you get by adding a peg at posn to board adding a peg removesa hole define addpeg board posn makeboard boardrows board removehole boardholes board posn 9 Computer Science duly UNlVlZRSI39IT afVIRGINlA Lecture 11 1 Luck 6 39A C m uter c1 nce O Pu adv UNIY Llt1TY SVIRGINIA if Wh m ist all eiemenis or wnion proc applied io inei eiemeni lS laise7 Remove Hole define removehole Ist posn sameposition car Ist posn dr Ist c cons car Ist removehole cdr Ist posn Could we define removehole using map No iengdn map r ist is always ine same as lmgdn lstj bui removerhole needs io remove elemenislrom ine liSl ei ii We had a procedure filmr proc ist inei removes iro lullquot u m lutk 7 Computer 5 39 no riull Filter Remove de ne iter proc ist if null7 ist it proc car ist proc is true keep it cons car st iter proc cdr iter pr i ist oc cdr ist proc is false drop it define removehole Ist posn filter lambda pos not sameposition pos posn Ist lullquot u m lutk oTrya oTrya Solving the Peg Board Game Try all possible moves on the board possible moves from the positions you get after each possible first move possible moves from the positions you get after trying each possible move from the positions you get after each possible first move lullquot u m lutk Filter define filter proc Ist if null Ist null if proc car Ist proc is true keep it cons car Ist filter proc cdr Ist filter proc cdr lst proc is false drop it gt lter lambda x gt x 0 list 1 4 3 2 1 4 2 mua a computers no move creams a list of three positions a start the posn that the jumping peg stars from a jump the posn that is being jumped over and end the p051 that the peg will end up in de ne makesmove start jump end iist smrt jump end s de ne getsend move third move executesmove evaluams to the board afmr making move move on board de ne executesmove board move dspeg removespeg removespeg board getsstart move getsjump move getsend move lullquot u m lutk Finding a Winning Strategy 65 How is winning 2person games e chess 9 poker different 12 lullquot u m lutk Peg board Puzzle 21 22 32 41 42 43 44 51 52 53 54 55 How do we find all possible jumps that land in a given target hole Pegboard Puzzle 11 31 32 33 51 52 53 54 55 How do we find all possible jumps that land in a given target hole A Lecture 11 1 Luck 13 A Lecture 11 1 Luck 14 M Computitgxlgvmssggggg Peg board Puzzle 11 21 22 312 41 42 43 44 52 63 54 How do we find all possible jumps that land in a given target hole All Moves into Target generatemoves evaluates to all possible moves that move a peg into the position target even if they are not contained on the board define generatemoves target map lambda hops let hop1 car hops hop2 cdr hops makemove makeposition getrow target car hop1 getcol target cdr hop1 makeposition getrow target car hop2 getcol target cdr hop2 target list cons cons 2 O cons 1 0 right of target hopping left cons cons 2 O cons 1 0 left of target hopping right cons cons O 2 cons O 1 below hopping up cons cons O 2 cons O 1 above hopping down cons cons 2 2 cons 1 1 above right hopping downleft cons cons 2 2 cons 1 1 above left hopping downright cons cons 2 2 cons 1 1 below right hopping up left cons cons 2 2 cons 1 1 below left hopping up right Lecture 11 1 Luck 15 A quot Com uter Selence P1 11y UNlYlZRSI I39Y 9f V1 RGlNlA Lecture 11 1 Luck 16 tut C0 All Possible Moves define allpossiblemoves board appendall map generatemoves boardholes holes define appendall lst if null lst null append car lst appendall cdr lst Butonly legal if start and end are positions on the board containing pegs Note could use apply append instead of appendall Legal Move define legalmove move A move is valid if o the start and end positions are on the board 0 there is a peg at the start position 0 there is a peg at the jump position 0 there is not a peg at the end position and onboard board getstart move onboard board getend move peg board getstart move peg board getjump move not peg board getend move A o Lecture 11 100 Luck 17 M Compgytgxa shc tgg g A Lecture 11 1 Luck 13 M Compgtgwsmc tegggm de ne degarmovev move A movers valrdrf ode smrtand erd poems are m the board map generatemoves othere is a peg at the srartposmon otherers apeg atihejump posium board boiesboies I mm em Wm de ne arroossr bi er moves board appendarr not peg board getrerrd move de ne legalmoves board lter legalmove allpossibIemoves board 19 Computer 39 mum team 11 m mk Winning Position How do we tell ifa board is in a winning position team 11 m mk Solve Pegboard de ne solvepegboard board nd rstwinner board legalmovs board de ne Fnd rstwinner board moves if null moves if iswinning sition board null Found winning game no moves needed f A losing position no more movs let result solvepegboard executemove board car moves if result winner not f 5 car movs result this move leads to winner nd rstwinner board cdr moves try rest m u m M 23 m compost Becoming a Genius Try all possible legal moves A C 2 c mmr 39 team 11 m mk iswinningposition de ne boardsquares board cou ntsq uares boa rd rows boa rd de ne countsquares nrows if nrows 1 1 nrows countsquares nrows 1 de ne iswinningposition board length boardholes board boardsquares board 1 team 11 m mk All Cracker Barrel Games starting with peg 2 1 missing J u st Plain Egnoramoose Leaving 1o pegs requires much more brilliance than leaving 11 Lecture 35 Cookie Monsters and SemiSecure Websites Secure Programming Honor Systemquot Programming All your users are nice and honest Nothing terribly bad happens if your program misbehaves c5150 Real Worldquot Programming Some users are mean and dishonat Bad things happen if your program es c5205 csm c S misbehav U ij i ggmfm David Evans Cariisms same 2 m CalIpussg gsg Buffer Overflows Buffer Overflow gt acc so bounds bounds c intmain mm gt how 5 mm 9i abcdefghijkl shsi s4i s is abodefgh X is 9 int main void H lt gtlt 9i char s4 9655 printf s is Sl l s printf x is mm x l c Program Stack 9255 gt bounds primer 5 is sri i 5 adeEfghijklm s i plrlff gtltls winnx s apeoerghuklmn gtltlS 1929715553 gt how 5 m 0X6d000009 Note your results vary av depe I is 3759 oxgooooog machine compiler gt ouh s what else is see a few thousand characters crashes shell running time of day etc Thi what does this kind of mistake S is what makes c fun look like in a popu ar server o Iquot Jain m Coiinniirr Scie Code Red Security in c5150 Can you have a Buffer Over ow vulnerability in Scheme Charme LazyCharme StaticCharme or Python n implementation Me on is m the interpreter so you don39t hale to allocate it or wonv about how much space you have No u less there is a bug n the underlying m anaged by Computer 5 Camping s eh is u cs Mm Web Application Security Malicious users can send bad input to your application Authentication most interesting applications need user logins Lecture 34 Cookie Monsters 7 CrossSite Scripting uva binpython Python Code gt Evaluate using Python gt interpreter send output to Client Output pages contain information provided by other users Python Interpreter Values A m r gm C PRFamp EESR E SQL Command Data ba se Lecture 34 Cookie Monsters 8 m Compgytgru c ea u CrossSite Scripting Demo user evans password 179756Fq4bhajanzIX12GPnLO Enter Review ltscript languagequotjavascriptquotgt function button while 1 alertquotI Own youquot ltscriptgt ltBODY onLoadquotbuttonquotgt Lecture 34 Cookie Monsters 9 Preventing CrossSite Scripting 0 Never never never ever trust users Everything you generate from user input needs to be checked and sanitized remove the tags For your ps9 websites you may assume all users are bound by the UVa Honor Code and won t do anything evil But don t forget how irresponsible it is to put something like this on the web A quot m 1 l n C0 PgrtgvnSkaVSVHEEH Lecture 34 Cookie Monsters 1o A quot m 139 1 n CO SVIEEH Authentication How do you authenticate Something you know Password 0 Something you have Physical key email account transparency Something you are Biometrics voiceprint fingerprint etc Serious authentication requires at least 2 kinds I 39 39A Lecture 34 Cookle Monsters 11 lull Compgtgg lc gg gn A A 12 tut Compare Lecture 34 Cookie Monsters Early Password Schemes UserID Password Login does direct I Fd password lookup a Y553 0 n oompan39son ben schemer dave LgtltLygtlt Log39in a39lyssa Password spot Fa39i39led 39log39in Guess aga39in mm M Cooke Monxlers 13 we gt Comp uter L Password Problems Need to store the passwords Dangerous to rely on database being Need to transmit password from user I to Dangerous to rely on Internet being confidential m M an MW 15 Hashing Manytoone maps a large number of values to a small number of hash values Even dislribulion for typical ata sets probability of Hx n lNw ere N is the number of hash values 1 n nOuN7 a H char s s0 7 3 mod 10 Ef cient Hx is easy to e M M an MW 17 run Comma Login Process Terminal Login aiyssa Password fido login sends Trusted Subsystem lt alyssa doquotgt 39 M M an MW 14 m compu g First Try Encrypt Passwords 0 Instead of storing password store password encrypted with secret IC 0 When user logs in encrypt entered password and compare to stored encrypted password UserID Problem if Kisn39t so secret decryptK encryptK H P mm M Cooke Monxlers 15 m CDMPHESE Cryptographic Hash Functions Oneway Given h it is hard to find x such that Hx h Collision resistance Given x it is hard to find y x such that Hy Hx M M an Mm 1a m cumpw Example OneWay Function Input two 100 digit numbers x and y Output the middle 100 digits ofx y Given x and y it is easy to calculate fxy select middle 100 digits xy Givenfxy hard to nd xand y A Better Hash Function 39 Hx encryth O Weak collision resistance Given 1 it should be hard to find 32 x such that Hy Hx Yes encryption is onetoone There is no such y o A good hash function No its output is as big as the message m M m MW 19 m Computer saws mums m M m Mm 2H m Computer saws mums Actual Hashing Algorithms Based on cipher block chaining Start by encrypting 0 with the first block Use the next block to encrypt the previous block SHA NIST95 512 bit blocks 160bit hash MD5 Rivest92 512 bit blocks produces 128bit hash This is what we use in HoosHungry It has been broken Hashed Passwords m M m MW 21 m cumpwg g quot 53 m M m MW 22 m cumpwg g quot 53 Dictionary Attacks Try a list of common passwords All 14 letter words List of common dog names Words from dictionary Phone numbers license plates All of the above in reverse Simple dictionary attacks retrieve most userselected passwords Precompute Hx for all dictionary entries m M m MW 23 m comptm was at least 86 of users are ASCII samecase letters lowercase letters in or names Salt of the Earth Python Code This is the standard UNIX password scheme We use the username as a IIsatII sinoe they must be unique 5 mdScryptencrypt pa sword user Salt 12 random bits enawtedpas UserID 1125 I user i password scheme lalyssa i9928ef0d7a0e4759ffefbadb8bc84228 levans ibafd72c60f450ed665a6eadc92b3647f DES m key salt is an encryption algorithm that is in a way t at epends o How much harder is the offline dictionary attack Computer 39 uusr teams M curiae Monsters Computer 39 uusr teams M curiae Monsters 25 Authenticating Users Registering for Account User enters email address User proves they are a worthwhile person bY haVing a legitimate email address Sent an email with a temporary password Not everyone who has an email address is md strhndom39randint OI 9999999 worthWh39le strrandomrandint 0 9999999 encmd mdScryptencrypt Its not too hard to snoop or intercept someone39s emai rnd strrandomrandint 0 99999 usersuserTabecreateUser user email rstnames astname md But provides much better authenticating From registerrprocess Cgl than just the honor system Do you trust Pythons random number generato 7 teams M curiae Monsters teams M curiae Monsters Users and Passwords Cookies reateUserself user email rsmames lasmame password d f 6 cc selfdb cursor 0 Sc d iNSmERTmiigggasg jng r rsuserislasmamepassword 39 Tl P 395 StatEIESS eVery request IS LUES 39 ir mames lasmame encpwd Independent C X MEWS Don t want user to keep having to enter seifdb commit 0 I defcheckPasswordself user password password every time c selfdb cursor 0 query SELECTpassword FROM users WHERE user user A cookie is data that is stored on the Efieiu er i e 0 browser s machine and sent to the web Ti nwiarse quotTn iggn is fig server when a matching page is visited se encpwd mdScryptencrypt password user d pwd return encpw sure M ensue musisrs 29 E commit teams M curiae Monsters Golden Ages Orde o CS150 Computer Science University of Virginia Computer Science Growth and Astrophysics David Evans httpwwwcsvirginiaeduevans 140 n 3 simulating 120 universe 100 80 6O 4O 20 simplesort 0 findbest 1 2 3 4 5 CS150 Fall 2005 Lecture 11 Golden Ages and Complexity 3 m Astrophysics If you re going to use your computer to simulate some phenomenon in the universe then it only becomes interesting if you change the scale of that phenomenon by at least a factor of 10 For a 3D simulation an increase by a factor of 10 in each of the three dimensions increases your volume by a factor of 1000quot o How much work is astrophysics simulation in notation When we double the size of the simulation the work octuples Just like oceanography octopi simulations A CS150 Fall 2005 Lecture 11 Golden Ages and Complexity 2 Computgr SCICPCC may Vivlzkwn lkMVH Orders of Growth 1400000 simulating universe 1200000 1000000 800000 600000 400000 200000 0 1 11 21 31 41 51 61 71 81 91 101 Astrophysics and Moore s Law Simulating universe is n3 Moore s law computing power doubles every 18 months Tyson to understand something new about the universe need to scale by 10X How long does it take to know twice as much about the universe A CS150 Fall 2005 Lecture 11 Golden Ages and Complexity 5 Computler SCICPCC um Mrlv iisll u 39llleMA CS150 Fall 2005 Lecture 11 Golden Ages and Complexity 4 39Q Computer Science may UVM lK H ry lkIll Knowledge of the Universe doubling every 18 months 1587 every 12 months define computingpower nyears if nyears O 1 1587 computingpower nyears 1 ii Simulation is 6n3 work de ne simulationwork scale scale scale scale de ne 390910 X 3909 X log 10 log is base 6 knowledge of the universe is log 10 the scale of universe we can simulate define knowledgeof universe scale log 10 scale CS150 Fall 2005 Lecture 11 Golden Ages and Complexity 6 i quotll Computer Sc1ence rum an39l39nsiri g Vimll of the Universe today can simulate size 10 universe 1000 work if gt simulationwork scale 1000 computingpower nyars scale 1 ndbiggestscale scale 1 knowledgeof universe ndbiggstscale 1 c3150 him1mm 11 Golden Age and complemiy 7 Comp ut e f goings Insert Sort de ne insertsort cf lst de ne insertel cf el lst if null lst if null lst nu list el insertel cf if cf el car lst car lst cons el lst insensort cf cons car lst cdr lst inser insertsort is nz msn F mus ledIre n and a i compleml Computer Science we Can we do better insertel lt 88 list 1 2 3 5 6 23 63 77 89 90 Suppose we had procedures firsthalf lst dhalf lst that quickly divided the list in two halves c3150 F m m m m ma 1 m Computer same mm gt ndsknowledgesofsuniverse 0 gt ndsknowledgesofsuniverse 1 1041392685158225 gt ndsknowledgesofsuniverse 2 433523068367 gt ndsknowledgesofsuniverse 5 1322219294733919 gt ndsknowledgesofsuniverse 10 and 39m not sure 7 155275783158 gt ndsknowledgesofsuniverse 15 am the forms Albert Einstein Only two Ii gs are in nite the uriierse and gt ndsknowledgesofsuniverse 30 300560944536028 gt ndsknowledgesofsuniverse so 366121349325 Vl ll there be any mystery gt ndsknowledgesofsuniverse so 395 quotI the Un39Verse When 6348717927935257 you dis c3150 F m m m m em a E Computer 5 mum Divide and Conquer Both simplesort and insertsort divide the problem of sorting a list of length n into Sorting a list of length n l Doing the right thing with one element Hence there are always n steps And since each step is 6n they are 6n2 To sort more efficiently we need to divide the problem more evenly each step c3150 F m m m m ma 1 m Computer s de ne insertsorth cf Ii Same as if null lst null lnserBort m c Insert Halves ca 5 insertelh insertsorth cf ch lst de ne insertelh cf el lst assumes lst is sorted by cf if null lst list el let 1 rsthalf lst sh secondhalf lst if cf el car 1 before rst half put at beginning append cons el 1 5 if null sh sh is null mans 1 has one element append 1 list el if cf el car sh append insertelh cf el 1 sh append 1 insertelh cfel sh c3150 F m m m m ma 2 m Computer 5 m n mum Evaluating insertelh lnsatelh lt 3 list 1 2 4 5 7 hseeh ltDmcaduel12cedltgt a 1245 7 l fge h 9 5 3 1 f lst el lt 3 s Get lh rstrhalf lst t sh seomdrlrlalf lst insetelh Diocemle flatmrltgt 3 1 2 4 it gel car lh 3 app l ld oors el fh sh 34 lfmll sh append lh list el or d el car sh 3 1 append ohsertelh f el m sh rt 3 2 append lh lrsertelh cfel sh if lt 3 2 f I 1223 Every time we call insertelh the length l12 34 of the list is approximately halved l02345 1 2 3 4 5 7 cs nhllmn team maiden Muhammad 13 Comp uwt e 5 39 How much work is insertelh Suppose firsthalf and secondhalf are 61 EBCh time we 93 I de ne lnsertelh cfel lst insertelh the size of whulw lst How much work is insertelh Suppose firsthalf and secondhalf are 61 Each time we ca 5 lst halvs So doubling the size of the list only increases the number of calls by 1 List Size Number of insertelh apphcauOhs 1 1 5 3 means is 2 2b 5 cs 1517 nu ma ledIre n Eolden he and Bumplexll Orders of Growth 19172533414953573313997105 emu F m team u at as u mum 17 m campus lst alvs So dou ling l fg ja m w 0 e if r a s the size of the list only SH secondrhawsw increases the nu er of it cfel car lh calls by 1 SD56 Saws WSW List Size Number of insertelh apphcauOhs 23W m New 1 1 it cf el car sh 2 2 append ihsertelh cf el lh sh 4 3 append lh 3 5 lnsertelh cfel sh mummiesteam maidenMuhammad 14 comm gs 39 e insertsorth Same as ll lSerBOrt except uses ll lserte define insertsorth cf lst We mse te m e 50 f if null7 lst if null lst listel n u let w rstrhalf lst sh secondrhalf lst lnsertelh cf lfcfel calm d l lh h car lst 5an 5 e S insertsorth append th listen if cf el car sh c appehd ihsertelh cf el lh sh D n Cd r 3950 lnsertelh cfel sh insertsorth would be n10g2 n ifwe have fast firsthal secondhalf cs 1517 nu ma ledIre n Eolden he and complemly 15 Is there a fast firsthalf procedure No To produce the first half of a list length n we need to cdr down the first nZ elements So rsthalf is 6n insertelh calls rsthalf every timeso o insertelh is 9a 9log2n 9n logzn o insensorth is 9a 9n logzn Soil logzn Vikes 39 sethanoursimplesorti cs 1517 nu ma ledIre n Eolden he and Bumplexll We ll nge out how m make a fast rstrhalfr like procedure Monday The Endless Golden Age Golden Age period in which knowledgequality of something doubles quickly At any point in history half of what is own about astrophysics was discovered in the previous 15 years her advances Short Golden Ages Golden Age period in which knowledgequality of something doubles quickly Endless golden age at any point in h39 ry the amount known is twice what u Short golden age knowledge doubles u m m r 9 hanged quelkeevev Dessheck rule Ame Goal per cm FIFA Wald Cup Endless Golden Age and Grade Inflation 0 Average student gets twice as smart and wellprepared even 15 years should it be today ifgrading standards didn39t change Grade In ation or De ation 200 average GPA in 1970 gentleman39s C 7 2 better studenis 19701988 3 admitting women nonwhites 1971 154 population increase wequot 19m moon 058 increase in enrollment WWW 7 anathema 11nnn l Studemznnz 13943 12595an Average GDAtoday shou d be 214 sisu has m the best ar the best students and anly the best 31 34 ar them staym m the nurse are P51 5a the average grade lri SISEI shauld he 21 4quot2quot2quot34l31 w 9 Mr v Class 20 Objects I invented the term Object Orentea and I can tell you I did not have C in mind Alan Kay c5150 camping 5mm David Evans UanE39S y afvwgmxa hm NW 5 virgins aduEvaris camp mg 5mm Menu A better counter Finishing Fish Programming with Objects ES 1517 F m m 2n mm 2 W Computer saws nextx from Class 18 de ne X 0 Elszim define nextx I H set x x 1 X gt nextx 1 gt set X 23 ministers gt neXt X begin set x x 1 X 24 cs n rquot mus mum 2n Dbyuls m A Better Counter 0 The place that keeps track of the count would be part of the counter not part of the global environment 0 Can we do this IL CDmP f i Esaerissl cs m rquot mus mum 2n Dbyuls 4 Recall from Lecture 19 Application Construct a new frame enclosed in the environment of this procedure Make places in that frame with the names of each parameter Put the values of the parameters in those places Evaluate the body in the new environment Iquot Iquot 9 5 mum alumina 5 mcompw was A Better Counter define makecounter lambda count lambda set count 1 count count 0 cs m rquot mus mum 2n Dbyuls s de ne makeoounter lambda count lambda set count 1 oount count gt de ne mycount ma eoounter gt mycount gt mycount gt mycount 3 parameters body lambda set oount m F m m 2 mm 7 E Computer sauvm wtk u Using Counter gt define bcounter makeocounter gt bcounter 39next gt bcounter 39next gt bcounter 39next gt bcounter 39howmany 3 gt bcounter 39reset gt bcounter 39howmany 0 An Even Better Counter define makeocounter Iambda count lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count 0 as F m m 2 mm a E Computer sauvm wtk u Objects oWhen we package state and procedures together we have an object 0 Programming with objects is objectoriented programming a as F m m 2 mm W Compuggrs m Emma Mammuk m mCompuggrs m Finishing Fish Recap through class 17 0 May 1941 Nazis start using Lorenz cipher to communicate between conquered European capitals Allies know Baudot code 2 sets of 5 wheels from test ges 0 August 1941 Operator retransmits message with abbreviations Allies learn one 4000character key by XORing intercepted messages and then guessing possible plaintexts to nd a pair that makes sense 0 Feb 1942 Bill Tutte determines structure of Lorenz machine by analyzing key Emma Mammuk n Cumputcrs m 1 1 Emma Mammuk 2 Cumputcrs m 1 1 Message is in German Double Delta 0 Combine two channels morelikelyfollowing A 21i XOR A 22I Lifir nm e m A M1XORA M2 gt 12 XOR A X1i XORA X2i V2 key XORA s1 iXORA s2i gt V2 I I Swheels only turn some of the time w en M wheel is 1 ProbA 21i XOR A 22i XOR A XL XOR A X2i 0 055 So if guess of initial con guration is correct generated X will have this property and we will see more Os than ls A 150 Fall 2005 Lecture 20 objects 13 mi Computer Science A in mm Using the Advantage o If the guess of X is correct should see higher than 12 of the double deltas are 0 0 Try guessing different configurations to find highest number of 0 double deltas 0 Problem of double delta operations to try one con g length of Z length ofX for 10000 letter message 12 M for each setting 7 XOR per double delta 89 M XOR operations c5150 Fall 2005 Lecture 20 objects 14 u C mwrt 93sass Dec 1942 Decide to build a machine to do these XORs quickly clue June 1943 Apr 1943 first Heath Robinson machine is delivered Intercepted ciphertext on tape 2000 characters per second 12 miles per hour Needed to perform 7 XOR operations each 392 ms M Wig Heath Robinson British Cartoonist 18721944 150 Fall 2005 Lecture 20 objects Colossus 0 Heath Robinson machines were too slow 0 Colossus designed and first built in Jan 1944 o Replaced keytext tape loop with electronic keytext generator 0 Speed up ciphertext tape 5000 chars per second 30 mph Perform 5 double deltas simultaneously Speedup 25X for faster tape 5X for parallelism ls gm Comretersaasrsrs numm A c3150 Fall 2005 Lecture 20 objects 15 m Commits Sctegtce O O ciphertext Tape Electronic Keytext Logic Tape Reader Generator 0 Position Counter Colossus o 10 Colossi machines operating by end of WWII o Decoded messages 63M letters total that enabled Allies to know German troop locations to plan DDay o Destroyed after war kept secret until 19705 documents released in late 90 A 150 Fall 2005 Lecture 20 objects 17 Comput r SCICEICC A c3150 Fall 2005 Lecture 20 objects 15 m Computcr Sciepcc ObjectOriented Programming mm m mm mm mm Simula o Considered the rst objectorientedquot programming language 0 Language designed for simulation by Kristen Nygaard and OleJohan Dahl Norway 1 62 0 Had special syntax for de ning classes that packages state and procedures together mm m mm mm mm Counter in Simula class oounter integer count begin procedure reset oount 0 end procedure next oount oount 1 end integer procedure howmany howmany count end end XEROX Palo Alto Research Center PARC 1970s 0 Bitmapped display 0 Graphical User Interface Steve Jobs paid 1M to visit and PARC and returned to make Apple LisaMac 0 Ethernet 0 First personal oomputer Alto o PostScript Printers 0 ObjectOriented Programming 22 0m 1 Wm about What anybody 555 5 gym a dc 777913951 Wayfo gamma rum 5 a wantr ReaW 19 doesn t voafe Dome7 of Aewtm 395 Laws quot iAan Kay 1971 Dynabook 1972 Just a model mm m mm mm mm Dynabook 1972 o Tablet computer 0 Intended as tool for learning 0 Kay wanted children to be able to program it also 0 Hallway argument Kay claims you oould de ne the most powerful language in the world in a page ofcodequot 0 Proof Smalltalk Scheme is as powerful but takes two pages 24 5H5 smal L39sysie 39 vato BYTE Magazine August 1981 Smalltalk 0 Everything is an object o Objects communicate by sending and receiving messages 0 Objects have their own state which may contain other objects 0 How do you do 3 4 send the object 3 the message 4 25 399 Com uter Science M PM aw a View C5150 Fall 2005 Lecture 20 Objects 26 m Camputgr Seleg e um Ivm r L lKr m Counter in Smalltalk class name counter instance variable names count new count lt 0 next count lt count 1 howmany A count Counter in Scheme de ne makeocounter lambda count lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count 0 A c5150 Fall 2005 Lecture 20 Objects 27 quot Computer Sctence Wimuuwvitw A quotI Computer Scrence rHv39 l lMl i bu H 17 Vim C5150 Fall 2005 Lecture 20 Objects 28 Counter in Scheme using let define makeocounter let count 0 lambda message if eq message 39reset set count 0 if eq message 39next set count 1 count if eq message 39howmany count Defining ask ask Object Method gt ask bcounter 39howmany 0 gt ask bcounter 39next gt ask bcounter 39howmany 1 define ask object message object message c5150 Fall 2005 Lecture 20 objects 29 I quotI Computer Science Mm UNH39IJNIV 1 VWURLL A c5150 Fall 2005 Lecture 20 Objects 30 IT Computgr Scxepcc mm H vm Lecture 30 CS 150 Computer Science University of Virginia DaVId Eva ns Computer Science httpwwwcsvirginiaeduevans Menu Finishing Charme Interpreter Application Lazy Evaluation A Lecture 30 Laziness 2 m Computer Selence 1W UN in ii vim Ni def mevalexpr env elif isLambdaeXpr return evaILambdaeXpr env elif isAppIicationeXpr return evalAppIicationeXpr env else evalEi rtii lquotlinlmown expression type in Sl x7 Lecture 30 Laziness 3 mi Computer Seience We Ibiv39iixs W View Implementing Procedures What do we need to record Environment pointer x x Input parameters in mouth Procedure Body Lecture 30 Laziness 4 Elil Computer Science NE Kim in n r mew Procedure Class class Procedure def initsef params body env selfparams params selfbody body selfenv env def getParamssef return selfparams def getBodysef return selfbody def getEnvironmentsef return selfenv A Lecture 30 Laziness 5 Elm Compukt r Scren ce Evaluating Lambda Expressions def evaILambdaeXprenv assert isLambdaeXpr if enexpr 3 evalError quotBad lambda expression 005quot strexpr return Procedureexpr1 expr2 env A Lecture 30 Laziness 5 m Computgr Sc rence Evaluating Applications def mevaegtltpr env evalApplication def evalApplicationegtltpr en To eva Lrate an application evaluate all the sutexpression ellf IsApplIcatIonegtltpr rv s da e return evalApplicationegtltpr env subex a then appl r 5 pr mevasexpr env expr y the value of the rst suhexpr39ession to the rest return mappysubexprvas0 subexprvas1 Tm 7 meval Mum 7 ComRuwt gigiencf Mum a comp ut gio maPP39Y Implemented Interpreter def mappyproc operands if isPrImitiveProoedureproc return procoperands elif isinstancepruc Procedure Whats m39ss39ng Reva paran39s procgetPararrs special for riewenv if begin set I V Primitive procedures fori in range0 enparan39s BJ pr strings lists etc E Mum g Cumpuggg cicnc Mum m cumpgggggg 5 Lazy Evaluation Don t evaluate expressions until their value is really needed 39 II 39 Surprise QUIZ We might save work this way since sometimes we don t need the value of an expression We might change the meaning of so expressions since the order of evaluation matters Not a wise policy for problem sets all answer values will always be needed m me mum cm m compm st mm it Lazy Examples Charmegt lambda x 3 2 2 LazyCharmegt lambda x 3 2 2 Charmegtlambda x 3 car 3 Rimmes extensions error car expects a pair applied to 3 mm p57 LazyCharmegt lambda x 3 car 3 3 Charmegt lambda x 3 loopforever no value loo 5 forever LazyCharmegt lambda x 3 loopforever 3 Laziness can be useful compute new mum cm m 13 How do we make our evaluation rules lazier Evaluation Rule 3 Application To evaluate an application b apply the value of the first subexpression to the values of the other su bexpressions a evaluate the rst subexpression and delay evaluating the operand subexprssions until their values are needed m cm 15 Computer sctcng mum Delaying Evaluation Need to record everything we will need to evaluate the expression later After evaluating the expression record the result for reuse m cm 17 Computer Science MM Ordinary men and women having the opportunity ofa happy life will become more kindly and less persecuting and less inclined to view others with suspicion The taste for war will die out partly for this reason and partly because it will involve long and severe work for all Good nature is ofall moral qualities the one that the world needs most and good nature is the result of ease and security not ofa life ofarduous struggle Modem methods of production have given us the possibility of overw oontinued to be as energetic as we were e ore there were machines in this we have been foolish but there is no reason to go on being foolish forever Bertrand Russell In Plaise ofklena s 1932 ooaut orof HIM lls Ma ema 39a proved wrong by Go39del39s proof Computer Sc ce musquot t mum cm m 14 Evaluation of Arguments Applicative Order eager evaluation Evaluate all subexpressions before apply Scheme original Charme Java Normal Order lazy evaluation Evaluate arguments when the value is needed Algol60 sort of Haskell Miranda LazyCharme Normal Scheme order is not Normal Order 15 cumptrcrsc mum I Thunk I Can class Thunk defiinitiself expr env selfexpr expr selfenv env selfevaluated False if not self evaluated selfvalue foroeevalself7expr selfenv selfevaluate True return selfvalue la cumpmsc mu m Lazy Application subexprvals land expresslon OPE Thunksexpr env expr1 What else needs to change Hint where do we need resvalues instmcl of Thunks 21 cmpms ldkw M Primitive Procedures def cleThunkexpr if isThunkexpr return exprvalue else return expr def mappyploc operands if isPrimitiveProoedureproc erancls map lambda op deThunkop operands return procoperancls em We need me deThunk procedure because Pymon39s lambda consbuct can only have an expression as lls body 23 computers Mrs r Forcing Evaluation Thunk def lnlt self expr env sel expr expr selfenv env selfevaluamd False def valueself if not selfevaluamd selmalue forceevalselfexpr selfenv selfeva ua ed True letum selfvalue def fo roeeVaKex39pr ens 2r commgs Primitive Procedures Option 1 redefine primitives to work on thun Option 2 assume primitives need values of all their arguments 22 cmpms ldkw M Conditionals We need to know the actual value of the predicate expression to know how to evaluate the rest of the conditional 24 computers Mrs r Class 13 Quicksort Problems and Procedures Queen s University Belfast Northern Ireland 08150 Computer Science David Evans UHIVCFSIty Of Vlrglnla httpwwwcsvirginiaeduevans Computer Selence Art of Computer Programming Donald E Knuth 0 Volume 1 1968 Fundamental Algorithms 0 Volume 2 Seminumerical Algorithms Random numbers arithmetic Volume 3 Sorting and Searching 0 Volume 4 Combinatorial Algorithms in preparation 2005 0 Volume 5 Syntactic Algorithms estimated for 2010 0 Volume 6 7 planned 777e rst nder of any error in my books receives 2 5 6 signi cant suggestions are also worm 0 32 each If you are really a careful reader you may be able to recoup more Man the cost of me books W5 way w C5150 Fall 2005 Lecture 13 Problems and Procedures A 39A C m ut r len gm 0 Can we do better 0 Making all those trees is a lot of work 0 Can we divide the problem in two halves without making trees Why are we spending so much time on sorting Reason 1 its important 0 Reason 2 it is a well THIS CLASSIC VORK NEWLY UPDA I39IED AND REVISED defined problem for TheArt of exploring algorithm Computer Programrmng deSIgn and complexnty analysis A sensible programmer rarely if ever writes DONALD 15 KNUT g their own code for sorting there are sont procedures provided by all major languages 800 pages long N C5150 Fall 2005 Lecture 13 Problems and Procedures 39m39 Computer Science u 1 Ui39i kii q ViRUNA Recap insertsorttree define inserteltree cf el tree if null tree maketree null el null if cf el getelement tree maketree inseIteItree cf el getleft tree getelement tree getrig ht tree maketree getleft tree getelement tree inseIteItree cf el getright tree n number of elements in tree 0log n n number of elements in lst define insentsonttree cf lst define insertsontworker cf lst if null lst null inserteltree cf car lst insentsontworker cf cdr lst n extractelements insentsont worker cf lst quot Computer Science uiv Lixii39iiiii 4 mexi h C5150 Fall 2005 Lecture 13 Problems and Procedures U1 C5150 Fall 2005 Lecture 13 Problems and Procedures Quicksort 0 Sir C A R Tony Hoare 1962 Divide the problem into Sorting all elements in the list where cf car list el is true it is lt the first element Sorting all other elements it is gt the first element Will this do better OI C5150 Fall 2005 Lecture 13 Problems and Procedures Computgr SCICQCC um Vn39rkqn39nr mumA 39 39A Com uter lence IIIII PM Quicksort define quicksort cf Ist if null Ist lst quicksort cf lter lambda el cf el car lst cdr lst list car Ist quicksort cf filter lambda el not cf el car lst cdr lst mamas1 apxww 7 EcomputeISL 1 le 3 de ne quicksort cf Ist if null Ist lst HOW much append uicksort cf Work IS filmr lambda el cf el car Ist cdr Ist list car Ist qurcksort mum lter lambda el not cf el car Ist son What it the input list is sorted Worst Case nz What it the input list is random Expected n log2 n mm hum1Napx mm a M Computer Comparing sorts gt teshgrowth insertsortrhee n 20 gt testgrowlh quicksort 250 time me 20 n 500 hme 80 t39m 562 n 64000time 16634 n 128000 me 165261 n 128000 time 35813 4 0 19 31 19 21 52 33 20 26 40 11 18 27 20 23 23 33 22 Both are n 10g2 n Absolute time of quicksort much faster 8 m rquot mus mm 1 mum 2nd Duneinn Good enough for VISA n 128000 time 35813 36 seconds to sort 128000 with quicksort n 10g2 n How long to sort 800M items gt log 4 13852943511198905 gt 128000 log 128000 1505252 549491 gt 128000 log 128000 35 128000 log 128000 41812 5 5 9997487578923 800000000 log 800000000 41812 5 9 228 5054130373 392000 seconds 45 days wvwv 9 mcvmpw t g 8 m rquot mus mm 1 mum 2nd Duneinn m m Cvmvnse Are there any procedures more complex than simulating the universe 9013 mm hum1Napx mm 1 m compth Permuted Sorting A possibly really dumb way to sort Find all possible orderings of the list permutations Check each permutation in order until you find one that is sorte Example sort 3 1 2 All permutations 312321213231132 lsrsonedV lsrsonedV lsrsonedV lsrsonedV lsrsonedV lS39SO cs F m w WM 2 m cumpw permutesort issorted define issorted cf Ist or null Ist 1 length Ist and cf car Ist cadr Ist issorted cf cdr st de ne permutesort cf Ist car filter lambda Ist issorted cf Ist allpermutations lst humMilanntmtw 13 quot Computetstattvtce humMilanntmtw 14 Ecomput e ISciqIce we we gt time permutesort lt randintlist 5 cpu time 10 real time 10 gc time 0 de ne allpermutations Ist 4 14 14 45 51 atone gt time permutesort lt randintlist 6 c u time 40 real time gc time 0 map lambda n if length Ist 1 list Ist The permutations of a are a map lambda oneperm 6 29 39 40 54 69 gt time permutesort lt randintlist 7 cpu time 261 real time 260 gc time 0 6 7 35 47 79 82 84 gt time permutesort lt randintlist 8 cpu time 3585 real time 3586 gc time 0 4 10 40 50 50 58 69 84 cons nth ISt n oneperm gt time permutesort lt randintlist 9 allpermutations exceptnth Ist n Crashesl intsto length lst 39 lm mm m ml 15 cumpwg n53 lm mm m ml 15 cumpwg n53 de ne permutesort cf Ist HOW mm at Number of permutations work is lter lambda Ist issorted cf Ist map allpermutations lst lambda n permutesort if length Ist 1 list Ist map lambda oneperm cons nth lst n oneperm allpermutations exceptnth lst n c We evaluated lssorted once for each intsto length lst PermUtatlon 0f ISt 0 There are n length Ist values in the rst map How much work is issorted77 for each possible rst element 0 Then we call allpermutations on the list without n that element length n7 o How many permutations of the list are There are quot 1 1 PermUtati ns there 0 Hence there are n lists to check nl Bataan m Miltmuntmtw 17 n ulcumputtts net Ewan m Miltmuntmtw la chumptht ts m mm it vl Are there any procedures more complex than simulating the universe 9013 Maybe this is the wrong question a F m Mm WM 19 m compiler ScietIce The Sorting Problem 0 Input a list and a comparison function 0 Output a list such that the elements are the same elements as the input list but in order so that the comparison function evaluates to true for any adjacent pair of elements cs n rquot mus mm 1 mum 2nd Duneinn Problems and Procedures Sorting problem is 0n10g n We know a procedure quicksort that solves sorting in n log n o Is the sorting problem n log n To know this we need to prove there is no procedure that solves the sorting problem with time complexity better than n log n samum Mikmu mdw 23 mcompwsc a Procedures and Problems So far we have been talking about procedures how much work is permute sort We can also talk about problems how much work is sorting A problem defines a desired output for a given input A solution to a problem is a procedure for finding the correct output for all possible inputs cs n rquot mus mm 1 mum 2nd Duneinn 2n Ecompumsgggm mums Problems and Procedures If we know a procedure that is that is fn that solves a problem then we know the problem is 0fn The sorting problem is 0n since we know a procedure permutesort that solves it in G n o Is the sorting problem is n No we would need to prove there is no better procedure 22 Cump cs n rquot mus mm 1 mum 2nd Duneinn Sorting problem is 001 log n c There are n possible orderings Each comparison can eliminate at best 12 of them So best possible sorting procedure is 910g2n Sterling s approximation n 201quot 50 best possible sorting procedure is Qlog n QM log n Recall iog rgumglivcauon is normal 3 it log m1 log m log n a F m Mm WM 24 m compt x saw LECtU re 17 Double Deltas and Banburismus i 7 We ll finish up the tree All C r 2 sorting next week Colossus Rebuilt Bletchley Park Summer 2004 IToday s notes online only links I David Evans httpwwwcsvirginiaeduevans C8150 Computer Science University of Virginia Computer Science Lorenz Wheels 12 wheels 501 pins total set to control wheels Work to break in pw so real Lorenz is 411253 1 quintillion 1018 times harder A Lecture 17 Double Deltas and Banburismus 3 Computsr Selence luv NlVlilislH39y 39lkmxu Two Time Pad 0 Allies have intercepted C1 M1 e K1 C2 M2 e K1 Same key used for both same starting configuration 0 Breaking message C1 e C2 M1 e K1 e M2 e K1 M1 e M2 e K1 e K1 M1 e M2 Lorenz A Lecture 17 Double Deltas and Banburismus 2 IT39SquotII Computgr Selence ulbc llk ll f mun39u Breaking Fish 0 GCHQ learned about first Fish link Tunny in May 1941 Intercepted unencrypted Baudotencoded test messages 0 August 30 1941 Big Break Operator retransmits failed message with same starting configuration Gets lazy and uses some abbreviations makes some mistakes o SPRUCHNUMMERSPRUCHNR Serial Number n Lecture 17 Double Deltas and Banburismus 4 39quotml Computgr Science HIY l39l Rgtl of IRUHH Cribs Know C1 C2 intercepted ciphertext C1 e C2 M1 e M2 Don t know M1 or M2 But can make some guesses cribs 0 SPRUCHNUMMER Sometimes allies moved ships sent out bombers to help the cryptographers get good cribs Given guess for M1 calculate M2 M2 c1 6 c2 6 M1 0 Once guesses that work for M1 and M2 K1M1ec1M2ec2 Lecture 17 Double Deltas and Banburismus 6 Lecture 17 Double Deltas and Banburismus 5 Computer Selence um Myrksin39y39 VIRmVH 39A in Compgrssniesass Reverse Engineering Lorenz 0 From the 2 intercepted messages Col John Tiltman worked on guessing cribs to find M1 and M2 4000 letter messages found 4000 letter key K1 0 Bill Tutte recent Chemistry graduate given task of determining machine structure Already knew it was 2 sets of 5 wheels and 2 wheels of unknown function Six months later new machine structure likely to generate K1 A Lecture 17 Double Deltas and Banburismus 7 I39m llquot COmPL lFSI39 Scream Breaking Traffic 0 Knew machine structure but a different initial configuration was used for each message 0 Need to determine wheel setting Initial position of each of the 12 wheels 1271 possible starting positions Needed to try them fast enough to decrypt message while it was still strategically valuable IThis is what you did for PS4 except with fewer wheels I Lecture 17 Double Deltas and Banburismus 9 M C mPHFSamp 33 Double Delta A Zn ch 9 Zci1 Combine two channels A Z1i A Zn A M1i A Mu gt 12 Yippee D A X1 43 A X2 12 key 43 A Slat 43 A 8231 gt 12 Yippee Why is A M1i ea A lei gt 12 Message is in German more likely following letter is a repetition than random Why is A Slli EB A SZIi gt 12 Swheels only turn when Mwheel is 1 Lecture 17 Double Deltas and Banburismus 11 COmPiiF Intercepting Traffic 0 Set up listening post to intercept traffic from 12 Lorenz Fish links Different links between conquered capitals Slightly different coding procedures and different con gurations 600 people worked on intercepting traffic A Lecture 17 Double Deltas and Banburismus s Inquot Computsr Sueyce Recognizing a Good Guess o Intercepted Message divided into 5 channels for each Baudot code bit ZC 2021222324252627 Zci mci EB Xci EB Sci Message Key parts from S wheels and rest 0 Look for statistical properties How many of the zcli s are 0 12 not useful How many of zcli1 ea zcli are 0 12 A Lecture 17 Double Deltas and Banburismus 10 rm Computgl Susana M WHIle mm Actual Advantage 0 Probability of repeating letters ProbA M1 eA M2 0 0614 33 of German digraphs are repeating 0 Probability of repeating Skeys ProbA seA 3 0 073 ProbA 217 e A 227 e A X1 A X2 0 0614 073 1 06141 073 AMandSareO AMandSarel 03955 if the wheel settings guess is correct 05 othenNise Lecture 17 Double Deltas and Banburismus 12 A gm COmPBtE Using the Advantage If the guess of X is correct should see higher than 12 of the double deltas are 0 0 Try guessing different configurations to find highest number of 0 double deltas Problem of double delta operations to try one config length of Z length of X for 10000 letter message 12 M for each setting 7 er per double delta 89 M G operations Heath Robinson Dec 1942 Decide to build a machine to do these 695 quickly due June 1943 0 Apr 1943 first Heath Robinson machine is delivered Intercepted ciphertext on tape 2000 characters per second 12 miles per hour Needed to perform 7 er operations each 12 ms mummmmwmwf Heath Robinson British Cartoonist 18721944 1 Lecture 17 Double Deltas and Banburismus 13 Solegcc firm I39Igt39R r l39l i39 f1 JRUIMH Colossus Heath Robinson machines were too slow Colossus designed and first built in Jan 1944 Replaced keytext tape loop with electronic keytext generator Lecture 17 Double Deltas and Banburismus 14 Selepce mm Kn39IiRsi39rtg Datum1 Colossus Desngn Ciphertempe Q 7 Q V Electronic Keytext Loic Tape Reader Generator V V Position Counter Counter V Printer Lecture 17 Double Deltas and Banburismus 16 Colossus History Kept secret after the war all machines destroyed A 1 RebUIICI Bletchley Park During WWII Summerzom Speed up ciphertext tape 5000 chars per second 30 mph Perform 5 double deltas simultaneously Speedup 25X for faster tape 5X for parallelism Lecture 17 Double Deltas and Banburismus 15 Impact on WWII 10 Colossus machines operated at Bletchley park Various improvements in speed Decoded 63 million letters in Nazi command messages Learned German troop locations to plan D Day knew the deception was working Lecture 17 Double Deltas and Banburismus 17 iii Lecture 17 Double Deltas and Banburismus 18 COIIIPIIESI SCl glCC mm Niwansrrtg lmtnx m How could the folks at Bletchley Park solve a problem 1 quintillion times harder than ps4 Lecture 17 Double Deltas and Banburismus 19 39ll Compu ttgr ScLelvlcel Motivation Helps Confronted with the prospect of defeat the Allied cryptanalysts had worked night and day to penetrate German ciphers It would appear that fear was the main driving force and that adversity is one of the foundations of successful codebreaking Simon Singh The Code Book A Lecture 17 Double Deltas and Banburismus 21 m Computgl Suegce Banburismus Given two Enigma encrypted messages how can we determine if they were encrypted starting with the same wheel settings II Thu IS unulllcr mtlhnd ulm39ll lhl irrmuns adopt in heir lmzmnn 39I39hrv mnkc uw ul lllc mllmn impulalmn in tzrdcr ln crczllc contusion and pump 391 hcv spruad false rumours untl IuL Illu lnMruClllan In mdcr to l mu is you should ubc th Second rulu which IS as I39UIIMWS 39E RL39MUURS AND DO 11 WHEN YOU RECEIVE i QLTI39I39I39C SURE THAT IT IS A TRUE ORDER AND NO I A FAKEI ORDER MOST 01 YOU KNOW YOUR POLICEMEN AND YOUR ARI WARDENS BY SIGII I39l YOU CAN TRUST THEM IF YOU KEEP YOUR HEADS YOU CAN ALSO V TELL WHETHER A MILITARY OFFICER IS REALLY BRITISH OR ONLY PRETENDING 39 TO BE SO IF 1N DOUBT ASK THE POLICE MAN OR TILE ARI WARDEN USE YOUR COMMON SENSE al Lu comes In m m 7 m m to noquot Poster in RAF Museum 7 7 L 39 Lecture 17 Double Deltas and Banburismus 20 mi Cornplatted y l 1 9 reI The Good News No problems on your exam are 1quintillionth as hard as breaking the Lorenz cipher Lecture 17 Double Deltas and Banburismus 22 Computgr Sclcgce Enigma Invented commercially 1923 German Navy Army Air Force About 50000 in use many were captured by Allies Modified throughout WWII Germans believed perfectly secure Kahn s Codebreakers 1967 didn t know it was broken Enigma maChine o Turing s 1940 Treatise on Enigma at B ztrihley declassified in 1996 Reverse Engineering Enigma This fictional movie about a fictional US submarine mission is followed by a mention in the end credits of those actual British missions Oh the British deciphered the Enigma code too Come to think of it they pretty much did everything in real life that the Americans do in this Simple Substitution Ciphers ABCDEFGH I JKLMNOPQRS TUVWXYZ quJaua decry t JIDKQACRSHLGWNFEXUZVTPMYOB CS gt DZ Lecture 17 Double Deltas and Banburismus 25 Inquot I Comput r Sqegce u mm o movie Roger Ebert s review of U571 Lecture 17 Double Deltas and Banburismus 25 m Compug g gica cvsu l39L Al lu Wm Elquot m V Simple Itllltn RUIN substitution L El Latch turns g h 1 next rotor once per rotation Lecture 17 Double Deltas and Banburismus 27 ComREJEJ n gsa sy Left Middle Right Re ecmr Rotor Rotor Rotor E v e c 39A a o f i e rt a so l c E g Right rotor g advanced as m c one p05ltlon E t H c A Lecture 17 Double Deltas and Banburismus 23 m Comping Schlerllcev Enigma s Rotating Substitutions Language is NonRandom 0 Random strings the probability of two letters in the two messages matching is 126 number of letters in alphabet o Sameencrypted strings the output letters will match when the input letters match This happens much more frequently because some letters eg e is 13 of all letters are more common A g ABCDEFGHIJKLMNOPQRSTUVWXYZ i g 22 ABCDEFGHI JKLMNOPQRSTUVWXYZ 323 C SQHL ZNYKXUWVJRDFBET IMOGACP 1 ABCDEFGHI JKLMNOPQRSTUVWXYZ EE EEWCBE SYHLZOQKXTIMNJWFP 3 Lecture 17 boublebeltasand Banburlsmus 29 Compuf gwsn ieaicaew Lecture 17 Double Deltas and Banburismus 30 I39llquot Comput r Seance WW Alan Turing s Solution M1 I GXCYBGDSLVWBDJLKWIPEHVYGQZWDTHRQXIKEESQS I M2 I YNSCFCCPVIPEMSGI ZWFLHESCIYSPVRXMCFQAXVXDVU I l 3 gal Man Lecture 17 Double Deltas and Banbulismus 31 mm Computer Science um UMvrllsm39g anlxu Lecture 17 Double Deltas and Banburismus 32 m Computer Suence m Uleller 1 VlllulNM Banburismus M1 I GXCYBGDSLVWBDJLKWIPEHVYGQZWDTHRQXIKEESQS I M2 IYNSCFCCPVIPEMSGI ZWFLHESCIYSPVRXMCFQAXVXDVU I Intercepted Message 1 Intercepted Message 2 NgtltgtltZltcamwomozz qummmmUnmv A B c D E F G H I J K L M N o P O R s T U v N X Y z Lecture 17 Double Deltas and Banbulismus 33 mi Computer Selence ml um munyum Intercepted Message 1 Ngtltgtlt2ltcamwomozszuHmmmmUnmv NgtltgtltZltICHMWOMOZZHNQHEGMMUOWJ A Lecture 17 Double Deltas and Banbulismus 35 m Computer Suence rmumnuuw anle Lecture 17 Double Deltas and Banburismus 34 m Computer Saence we Uml39mm 1 Vlwm Trying Possible Alignments Lectule 17 Double Deltas and Banbulisnus Class 30 Vocational Skills How and Why to Build a Dynamic Web Application C8150 Computer Science 1 University of Virginia DaVICI Evans Computer Science httpwwwcsvirginiaeduevans GhostinteM ft 7 I Trick or l 7 Trickers Challenge N Challenge N R H secret V R H secret A0 R H secret N TrickorTreat L r Trlckers R H secret Challenge quot Ch H a enge Valid T 39 I in rickers am Bu reau C5150 Fall 2005 Lecture 30 Dynamic Web Applications 2 m Who Invented the Internet A C5150 Fall 2005 Lecture 30 Dynamic Web Applications 4 Computgr 5C189C6 uni x139iRsrrrgr nuan sum A C5150 Fall 2005 Lecture 30 Dynamic Web Applications 3 COI I IPUtSI39 Selence iliiv r il39li l 39g IRUINM A collection of multiple networks connected together so messages can be transmitted between nodes on different networks The First internet 0 1800 Sweden and Denmark worried about Britain invading o Edelcrantz proposes link across strait separating Sweden and Denmark to connect their signaling telegraph networks 1801 British attack Copenhagen network transmit message to Sweden but they don t help 0 Denmark signs treaty with Britain and stops communications with Sweden A C5150 Fall 2005 Lecture 30 Dynamic Web Applications 5 Computgr Scmgce quot12 xn39Istlrs gf lRNl A C5150 Fall 2005 Lecture 30 Dynamic Web Applications 6 COI39IIPUItSI Selence mm Nl liR5139l39 g mama First Use of Internet 0 October 1969 First packets on the ARPANet from UCLA to Stanford Starts to send quotLOGINquot but it crashes on the G o 20 July 1969 Live video bw and audio transmitted from moon to Earth and to millions of televisions worldwide C5150 Fall 2005 Lecture 30 Dynamic Web Applications l A 39 394 C m r c1 nce O PPMFS1 HSK11 SVVRLIV The Modern Internet 0 Packet Switching Leonard Kleinrock UCLA thinks he did Donald Davies and Paul Baran Edelcrantz s signalling network 1809 sort of did it 0 Internet Protocol Vint Cerf Bob Kahn Vision Funding JCR Licklider Bob Taylor 0 Government Al Gore first politician to promote Internet 1986 act to connect government networks to form Interagency Network ll C5150 Fall 2005 Lecture 30 Dynamic Web Applications 8 quotll Computgr Sciegce 1 Ily 39 lgtl I 3 lltLll Kahn and Cerf s Answer Al Gore was the first political leader to recognize the importance of the Internet and to promote and support its development No one person or even small group of persons exclusively quotinventedquot the Internet It is the result of many years of ongoing collaboration among people in government and the university community But as the two people who designed the basic architecture and the core protocols that make the Internet work we would like to acknowledge VP Gore39s contributions as a Congressman Senator and as Vice President No other elected official to our knowledge has made a greater contribution over a longer period of time http wwwfirstmondayorgissuesissue510wiggins C5150 Fall 2005 Lecture 30 Dynamic Web Applications W quot Computer Science uw an39iwn aVlRUlMA Government and Networking Chappe wanted a commercial network The use of novel methods that modify established habits often hurts the interests of those who profit the most from the older methods Few people with the exception of the inventors are truly interested in helping projects succeed while their ultimate impact is still uncertain Those in power will normally make no effort to support a new invention unless it can help them to augment their power and even when they do support it their efforts are usually insufficient to allow the new ideas to be fully exploited Claude Chappe 1824 Anyone performing unauthorized transmissions of signals from one place to another with the aid of telegraphic machines or by any other means will be punished with an imprisonment of one month to one year and a fine of 1000 to 10000 Francs French Law passed in 1837 made private networking illegal A C5150 Fall 2005 Lecture 30 Dynamic Web Applications 10 Computgr Selepce mv xmizmi gr 39iknnu The World Wide Web 3 a l he Desk Wide Web 7 Memex Machine Vannevar Bush As We May Think LIFE 1945 C5150 Fall 2005 Lecture 30 Dynamic Web Applications 1 H r m r n CO Pgithl lZ SVSVHEH cs 150 Fall 2005 Lecture 30 Dynamic Web Applications 12 m Computer Selence at 39i39 Vll39l Rl I 39 p rllllll Licklider and Taylor s Vision Available within the network will be functions and services to which you subscribe on a regular basis and others that you call for when you need them In the former group will be investment guidance tax counseling selective dissemination of information in your field of specialization announcement of cultural sport and entertainment events that fit your interests etc In the latter group will be dictionaries encyclopedias indexes catalogues editing programs teaching programs testing programs programming systems data bases and most important communication display and modeling programs All these will be at some late date in the history of networking systematized and coherent you will be able to get along in one basic language up to the point at which you choose a specialized language for its power or terseness J C R Licklider and Robert W Taylor The Computer as a Communication De vice April 1968 The World Wide Web 0 Tim BernersLee CERN Switzerland 0 First web server and client 1990 Established a common language for sharing information on computers Lots of previous attempts Gopher WAIS Archie Xanadu etc CS150 Fall 2005 Lecture 30 Dynamic Vi Human 1 I39llquot vuruu yumw i39l ll39bf le IR51n EIKIRIXK1L c5150 Fall 2005 Lecture 30 Dynamic Web Applications 14 m Computgr Scmegce miu Nn39iiRsri i g39 mumi World Wide Web Success World Wide Web succeeded because it was simple Didn t attempt to maintain links just a common way to name things Uniform Resource Locators URL httpI wwwcsvirginiaedulcs150indexhtml service Hostname File Path HyperText Transfer Protocol HyperText Transfer Protocol A r ache HTTP SERVER PROjECT b El pay 2 515 I mini r Univesilyafwmimam l E mm 5 we ediIcs39ISB 23 Qcmbeymna l rid team reQUesi 53W l l GET cslSOindexhtml HTTP10 Server lthtmlgt ltheadgt Contents of file Client Browser HTML HyperText Markup Language A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 15 Computgr Selepce rm Nl i39liR39Sl39iqu mumsx A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 16 Computgr 5616966 miwr Nll liktil39l H39y39 mumi Popular Web Site Strategy 1 Static Authored Web Site Drawbacks oHave to do all the work yourself oThe world may already have enough Twinkleexperiment websites Web Programmer Content Producer Popular Web Site Strategy 2 Dynamic Web Applications Seed content and function WELCOME TO EBAY Content Producer AUCTIONWEB The most fun buying and selling on the Webl Take part in an excmng auction or put your own merchandise on auctwn all free for buyersl eBay in 1997 I9 httpwebarchiveorgweb19970614001443httpwwwebaycom A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 17 Computgr Suegce rifflir Kli l Rfill t af mmxn A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 18 Compqtgr Sometime m Nl lil l39lquoti39gf39 imami Popular Web Site Strategy 2 Dynamic Web Applications Seed content and function Eire Edit view Favorites Iools Help 1 a 391 7 eaack v 4 m 1 t I Search Favorites T v 7 Q v Lirils 39 Advantages 0 Users do most of the work 0 If you re lucky they might even pay you M for the privilege Disadvantages 0 Lose control over the content you might get sued for things your users do 0 Have to know how to program a web eBay in 2005 application A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 19 Computgr SCICPCB 4mm x1vitksi39i i39g iniiixu Vocational Skills Job listings at monstercom 2 Nov 2005 within 100 miles of Charlottesville SQL more than 1000 within 100 milesquot 80400K Python 20 within 100 miles 40150K HTML 427 3050K Scheme 5 400K Loan Officers Mortgage Brokers All Experience Levels Needed A performance based incentive scheme including company car Salary 400000year Note none for Scheme programming language Today s Aberrant Class 0 Covers topics primarily selected for their practical not intellectual value 0 Covers material found in For Dummies books but we it different 5 Viyirunn i a 1 455g li hdumml m If 5quot Vlslt us at f dummiuram Database a quot li Lazy he i I i 39 N t V 1 mtth te W17 1 A u i LIAM v quot aft HTML HyperText Markup Language 0 Language for controlling presentation of web pages 0 Uses formatting tags Enclosed between lt and gt 0 Not a universal programming language Proof no way to make an infinite loop A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 21 COIIlPlltSl SCICgJICC m Ni l RSl39iquotn39af39 iRGlXH HTML Grammar Excerpt Document 2 lthtmlgt Header Body lthtmlgt Header 2 ltheadgt HeadElements ltheadgt HeadElements 2 HeadElement HeadElements HeadElements 2 HeadElement 2 lttitlegt Element lttitlegt Body 2 ltb0dygt Elements lt bodygt Elements 2 Element Elements Elements 2 Element 2 ltpgt Element ltpgt Make Eementa paragraph Element 2 ltcentergt Element ltcentergt Center Element horizontally on the page Element 2 ltbgt Element ltbgt Display Eementin bold What is a HTML interpreter Element 2 Text A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 23 Computgr SCleglCB irzlii Xii39iiksi39i39i39g39 imiixm A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 22 COI PUtSl Selegce mlw Nl39lZRN39l39lquotn39 3f lRGlle t Dynamic Web Sites 0 Programs that run on the client s machine Java JavaScript Flash etc language must be supported by the client s browser so they are usually flaky and don t work for most visitors Used mostly to make annoying animations to make advertisements more noticeable Occasionally good reasons for this need a fancy interface on client side like Google Maps 0 Programs that run on the web server Can be written in any language just need a way to connect the web server to the program Program generates regular HTML works for everyone Almost Every useful web site does this A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 24 Computgr SCICPCC irm NlYlZRSl39l39Y 3139 finmxm Dynamic Web Site iniaed uNdsu9whooridesbrowsecgi am e o nnnnnn a Client Web Browser HTML Interpreter CS150 Fall 2005 Lecture 30 Dynamic Web Applications 25 m IJIr 39 SH 3914 71 v Processing a GET Request Python Code Evaluate using gt Python evaluator send output gt to client t0 Client Python Evaluator A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 27 Learning New Languages Syntax Where the etc all go If you can understand a BNF grammar this is easy Semantics What does it mean Learning the evaluation rules Harder but most programming languages have very similar evaluation rules 0 Style What are the idioms and customs of experienced programmers in that language Takes many years to learn 0 Need it to be a professional Python programmer but not to make a useful program Client Dynamic Web Site GET browsecgi l I Read publichtmlhooridesbrowsecgi I File Server Request quot Processor a browsecgi CS150 Fall 2005 Lecture 30 Dynamic Web Applications 26 m Python o A universal programming language Everything you can compute in Scheme you can compute in Python and vice versa Friday we will explain why more convincingly o Imperative Language Designed to support a programming where most of the work is done using assignment ObjectOriented Language Built in support for classes that package data and procedures A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 28 in 39 39 39g Python If Instruction if Expression Statements Evaluate Expression If it evaluates to true evaluate the Statements It is similar to if Expresson begin Statements Differences Indenting and new lines matter Changing the indentation changes meaning of code What true means Scheme anything that is not f Python anything that is not False None 0 and empty string or container A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 29 361651016H Ll l R ll 39f lk A CS150 Fall 2005 Lecture 30 Dynamic Web Applications 30 yum s 9 1quot i A 4 EiEEEWEQEESQE Hi Exam 1 o Handed out at end of Friday s class due at the beginning of Monday s class 0 Open nonhuman resources but no help from other people 0 Covers everything through today including Lectures 115 Book Chapters 27 PS 14 Chapter 8 out today is not covered but understanding it may help you prepare for exam 0 Review Session Weds 630 in Olsson 228E L t 15 39k s t39 39e Com uterScience ec quotre 39Qu39c er or mg 2 M PMUmvmnvavimxu Sorting Cost define bestfirstsort lst cf if null lst lst let best findbest lst cf cons best bestfirstsort delete lst best cf define findbest lst cf if 1 length lst car lst pickbetter cf car lst findbest cdr lst cf The running time of best rstsort is in n2 where n is the number of elements in the input list IAssuming the procedure passed as cf has constant running time I Lecture 15 Quicker Soltin 3 Com uter Science 9 gm PM Divide and Conquer sorting 0 Best first sort find the lowest in the list add it to the front of the result of sorting the list after deleting the lowest 0 Insertion sort insert the first element of the list in the right place in the sorted rest of the list Lecture 15 Quicker Sortin 4 Com uter Science 39 g PM an uusi w afViRUNH insertsort define insertsort lst cf if null lst null insertone car lst insertsort cdr lst cf cf Lecture 1539 Quicker Soltin quot Com uter Science 9 5 gm PM insertone define insertone el lst cf if null lst list el if cf el car lst cons el lst cons car lst insertone el cdr lst cf Lecture 15 Quicker Sortin 399 Com uter Science 9 6 M P How much work is insertsort de ne insertsort lst cf if null lst null insertone car lst insertsort cdr lst cf cf de ne insertone el lst cf if null lst list el if cf el car lst cons el lst car lst insertone el cdr lst cf How many times does insert sort evaluate insertone n times once for each element insertsort has running time in nz where n is the number of elements in the input list running time of insertone is in n mus 5 gum smug 7 Comm L we s gt insertsort lt revintsto 20 12345678910111213141516171819 20 Requirs 190 applications of lt gt insertsort lt inlsto 20 12345678910111213141516171819 20 Requirs 19 applications of lt gt insertsort lt randintlist 20 0 11 16 19 23 26 31 32 32 34 42 45 53 63 64 81 82 84 84 92 Requirs 104 applications of lt lullquot IE Qultler smug g m CDmeEL u bestfirstsort vs insertsort 0 Both are nz worst case reverse list 0 Both are nz when sorting a randomly ordered list But insertsort is about twice as fast 0 insertsort is n best case ordered input list Is insert Which is better sort faster than bestfirst sort lullquot IE Qultler smug a Comp uter we gt best rstsort lt intsto 20 12345678910111213141516171819 20 Requires 210 applications of lt gt best rstsort lt randintlist 20 4 4 16 18 19 20 23 32 36 51 53 59 67 69 73 75 82 82 8 Requires 210 applications of lt lullquot IE Qultler smug m mcvmvm inserton Suppose Can we do better elt88 list 1 2 3 5 6 23 63 77 89 90 we had procedures firsthalf lst secondha If lst that quickly divided the list in two halves lullquot IE Qultler smug n quotT lullquot IE Qultler smug 12 ewqu u u Class 15 P vs NP Smiley Puzzles and Curing Cancer CSISO Computer Science David Evans University of Virginia Computer Science httpwwwcsVIrglnIaeduevans Smileys Problem Input n square tiles m m Output Arrangement of the tiles in a square where the colors and l shapes match up or no its impossiblequot Not minequot Menu 0 Complexity Classes P and NP 0 Quiz Answers 0 Problem Reductions a c5150 Fall 2005 Lecture 15 P vs NP 2 Computer Sctence M M an39l m Vllmm39 Smileys Problem Input n square tiles Output Arrangement of the tiles in a square where the colors and l shapes match up or no its impossiblequot C5150 Fall 2005 Lecture 15 P vs NP A 3 ut Cornerst sisaga C5150 Fall 2005 Lecture 15 P vs NP A 4 tut Commrs ssass How much work is the Smiley s Problem 0 Upper bound 0 0n Try all possible permutations Lower bound 2 2 n Must at least look at every tile 0 Tight bound 9 No one knows NP Problems 0 Can be solved by just trying all possible answers until we find one that is right 0 Easy to quickly check if an answer is right Checking an answer is in P o The smileys problem is in NP We can easily try n different answers We can quickly check if a guess is correct check all n tiles c5150 Fall zoos Lecture 15 P vs NP 5 Computer Science 1 cumrumw 91mm 1 A c5150 Fall 2005 Lecture 15 P vs NP 5 m Computer Sctence lllll mimicryWWW Complexity Classes Class F problems that can be solved in polynomial time 0nk for some constant k Easy problems like simulating the universe are all in P Class NP problems that can be solved in polynomial time by a nondeterministic machine includes all problems in P and some problems possibly outside P like the Smileys puzzle cs n rquot mus mm 15 v V up ComputerScien Ce tuwm PNm Is P different from NP is there a problem in NP that is not also in P If there is one there are infinitely many Is the hardest problem in NP also in P If it is then every problem in NP is also in P o No one knows the answer The most famous unsolved problem in computer science and math Listed first on Millennium Prize Problems cs 1517 mm in 15 a w a m Computer Science mum Problem Classes Possible View Simulating UniVeIse 0n3 Find Best 9a How many problems are in me 0a class7 in inite How many problems are in P but not m the 0a ciass7 in nite How many problems gaming are in NP but not m P7 n log n Smileyy Either 0 or in nitel 0n and 9a cs n rquot mus mum 159mm 3 ComRuwt ggo Problem Classes if P at NP Simulating UniVeIse 0n3 Find Best 9a How many problems are in me 0a ciass7 in nite How many problems Suiting are in NP but not om lo n M f 1 g Smileys In lnl e 0n and 9a m cmpwg Problem Classes If P N P Simulating UniVeIse 0n3 Find Best 9a How many problems 900 are in line 0a ciass7 in nite How many problems are in P but not in line 0a ciass7 in nite How many pro Quiz Responses Partners for PS3 Only 4 groups worked as partners AII partner groups got Gold stars Only 8 out of 20 nonpartner PS got Gold stars 0 Your responses Learn more working alone 7 Finish faster working alone 5 Couldn t find anyone to work with 2 Wanted to work with PSlPSZ partner 4 Started too late 5 blems Suiting are in NP but not 9 lo n g n Smileys nquot cs n rquot mm m 15mm 11 comp f him 12 mcmpmsc mm m

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

#### "Selling my MCAT study guides and notes has been a great source of side revenue while I'm in school. Some months I'm making over $500! Plus, it makes me happy knowing that I'm helping future med students with their MCAT."

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.