COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu 1
CHAPTER II COMPUTING
Chapter overview How computers work Hardware Software Algorithms and Heuristics Algorithmic thinking 3
HOW COMPUTERS WORK
programs with their data Overall organization MAIN MEMORY programs with their data CPU Hard disk User inputs and outputs
Inside the main memory Operate in user mode Operates in kernel mode Running programs: we call them "processes" KERNEL allocates memory to processes grants CPU to processes control disk accesses Operate in user mode Operates in kernel mode
The running programs Reside in main memory while they are running Include many background processes We do not see them Take space and often CPU time Having a large main memory allows us to run more programs at the same time
TIP If your computer becomes slow whenever you switch among programs You need more memory If your computer takes a lot of time to boot You could have a slow disk Your OS has a lot of things to load into main memory Useful and not so useful
The kernel Responsible for Managing the resources Which process should get the CPU How our files are stored … Enforcing security and preventing crashes
Security issues Must protect running programs from attempts to modify them by other programs Mostly programming issues Also viruses Must protect our data on disk Especially if computer is shared
Running a program OS creates a process Allocates memory space to the process Disk copy of program is brought from disk into main memory Process competes with other processes for CPU time Process is deleted when program terminates
Saving the results Normally done by saving the results in a file stored on disk Can print them later
What is inside a program? Instructions: Telling the CPU what to do Constants: Stable values Variables: Memory locations where results can be stored
ALGORITHMS AND HEURISTICS 14 14
HOW COMPUTERS WORK
What is an algorithm? “Effective method expressed as a finite list of well-defined instructions for calculating a function” Wikipedia
Three important points It must be an effective method Guaranteed to produce the result The instructions should be well-defined Anybody using the algorithm should obtain the same answer It should have a finite number of steps An algorithm must terminate after a finite number of finite steps
These are not algorithms On a shampoo bottle: Lather Rinse Repeat
These are not algorithms On a shampoo bottle: Lather Rinse Repeat How many times?
These are not algorithms On fuel tank cap: Turn until three o'clock
These are not algorithms On fuel tank cap: Turn until three o'clock That could be a long time!
Example: Converting C into F If you travel outside of the US, temperatures are likely to be given in Celsius not Fahrenheit. How the scales differ: In Fahrenheit: Water freezes at 32 F and boils at 212 F In Celsius: Water freezes at 0 C and boils at 100 C
Example: Converting C into F Read Celsius temperature x Multiply by 1.8 Add 32 to obtain Fahrenheit temperature y
Example: Converting C into F Another way to do it: Read Celsius temperature x Fahrenheit temperature y = 1.8×x +32
Counter-example (I) British weatherman's rule of thumb: Multiply C temperature by 2 Add 30 Very good for temperatures in 41-59 F range During a Texas summer, better use: Add 25
Counter-example (II) These two rules are heuristics, not algorithms Do not always give the right conversion Still useful Double and add 25 rule converts 30 C into 85 F Right answer is 86 F
A program is not algorithm It is the expression of an algorithm in a programming language Picking the right algorithm is the most important task After that, we just have to code!
Example Finding a name in a table Naïve solution is sequential search Binary search is much faster
Sequential search (I) We look for search_name in list list Start at beginning of list If list is empty stop and return NOT FOUND If search_name matches name of first list entry stop and return list entry If we have reached the end of the list stop and return NOT FOUND
Sequential search (II) Look for next list entry If search_name matches name of next list entry stop and return list entry Go to step 4
Binary search (I) We look for search_name in list list If list is empty stop and return NOT FOUND Find entry exactly in middle of list If search_name matches the name of that entry stop and return list entry
Binary search (II) If search_name goes before the name of entry restart search for first half of list If search_name goes after the name of entry restart search for second half of list
Example of binary search List contains Alan Alice Barbara Emily Francis Gina Peter
Example of binary search We look for Charles in a sorted list of names Alan Alice Barbara Emily Francis Gina Peter
Example of binary search We compare search name with entry exactly in the middle of the list (Emily) Alan Alice Barbara Emily Francis Gina Peter
Example of binary search Since Charles comes before Emily we can eliminate second half of list Alan Alice Barbara
Example of binary search We compare search name with entry exactly in the middle of the list (Alice) Alan Alice Barbara
Example of binary search Since Charles comes after Alice we can eliminate the first half of list Barbara
Example of binary search We compare search name with entry exactly in the middle of the list (Barbara) Barbara
Example of binary search Since Charles comes after Alice we can eliminate the first half of the list Barbara
Example of binary search Since Charles comes before Barbara we can eliminate one half of the list
Example of binary search Since list to be searched is now empty we conclude that the list does not contain Charles
Comparing performances List with 1024 entries Sequential search: Maximum number of steps: 1024 Average number of steps: 512 (one half) Binary search: Number of steps: 10 (= log2 1024)
Heuristics (I) Many problems have no practical algorithmic solution Algorithm will take too long Example: Finding the absolute best price for an item Should check everywhere Not cost-effective
Heuristics (I) Heuristics provide solutions That are not guaranteed to work in all cases That provide a good approximation of the correct solution Example: When we want to buy an item, we focus on the stores that are likely to sell the item at a good price
An example Finding the maximum of a curve Start at any given point Move on the curve following the upward direction Stop when the curve reaches a start going downward
It works
It does not always work
Which one is the most useful? ALGORITHM Always provides the right answer Can be very slow HEURISTICS Normally provide a good approximation of the right answer Relatively fast
Algorithmic thinking Way to analyze problems and come with one or more algorithmic solutions that fully describe the solution handle all special cases can implemented on a computer system will run at a reasonable cost Most important skill for a programmer Can be learned
Algorithmic thinking basics Basic concepts Order matters Must make choices Repeat the same task Over different data To get better results
Order matters (I) You want to bake something Preheat oven Set up oven timer and temperature Put item in oven Turn off oven Take item from oven Can invert steps 2 and 3 Maybe 5 and 4, not the other steps
Order matters (II) The formulas for the surface of a circle s = pi*r2 if you are given d, you must compute in that order!
Order matters (III) The general rule is Cannot compute anything if your data are not ready To compute s = pi*r2 Must know the values of pi and r
Must make choices If average >= 90 : Give an A Else if average >= 88 : Give an A- Else if …
Repeat the same task Over different data For all exams turned in: For all pages in exam For each question on page Grade question Compute page total Compute exam total
First refinement For all exams turned in: For all pages in exam For each question on page Grade question Add grade score to page total Add page total to exam total Will abysmally fail!
Why? Violated basic rule: Cannot compute anything if your data are not ready Did not initialize exam total and page total
Second refinement For all exams turned in: Reset exam total to zero Reset page total to zero For all pages in exam For each question on page Grade question Add grade score to page total Add page total to exam total Will produce grade inflation!
The right solution For all exams turned in: Reset exam total to zero For all pages in exam Reset page total to zero For each question on page Grade question Add grade score to page total Add page total to exam total
Repeat the same task To get a more accurate result Before the era of pocket calculators Taught an insane method to compute square roots Nobody used Much older and simpler Babylonian method
Babylonian method If x is an overestimate to the square root of a non-negative real number S then S/x will be an underestimate (and vice versa) (x + S/x)/ 2 will provide a better approximation Why? Because x* S/x = S
Computing the square root of 2 Start with x = 2 S/x = 1 and (x +S/x)/2 = 1.5 Set new value of x to 1.5 S/x = 1.33333 and (x +S/x)/2 = 1.416666667 … Stable value x = 1.414213562 after 5 steps
Computing the square root of 99 Start with x = 99 S/x = 1 and (x +S/x)/2 = 50 Set new value of x to 50 S/x = 1.8 and (x +S/x) /2=25.99 … Stable value x = 9.949874371 after 8 steps Use a spreadsheet to try it at home!
But Quite boring to go though all this steps True but Algorithm is very simple Computers do not complain!