Title: COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
1COSC 1306COMPUTER SCIENCE AND PROGRAMMING
- Jehan-François Pâris
- jfparis_at_uh.edu
2CHAPTER IIIAlGORITHMS
3Chapter overview
- How computers work
- Hardware
- Software
- Algorithms and Heuristics
- Algorithmic thinking
4HOW COMPUTERS WORK
5Overall organization
MAIN MEMORY programs with their data
CPU
User inputs and outputs
Hard disk
6Inside the main memory
Operate inusermode
Operatesin kernelmode
7The 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
8TIP
- 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
9The kernel
- Responsible for
- Managing the resources
- Which process should get the CPU
- How our files are stored
-
- Enforcing security and preventing crashes
10Security 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
11Running 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
12Saving the results
- Normally done by saving the results in a file
stored on disk - Can print them later
13What is inside a program?
- Instructions
- Telling the CPU what to do
- Constants
- Stable values
- Variables
- Memory locations where results can be stored
14CHAPTER III ALGORITHMS
- Work in progress
- Will still add new materials
15What is an algorithm?
- Effective method expressed as afinite list of
well-defined instructionsfor calculating a
function - Wikipedia
16Three 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
17These are not algorithms
- On a shampoo bottle
- Lather
- Rinse
- Repeat
-
18These are not algorithms
- On a shampoo bottle
- Lather
- Rinse
- Repeat
- How many times?
-
19These are not algorithms
- On fuel tank cap
- Turn until three o'clock
-
20These are not algorithms
- On fuel tank cap
- Turn until three o'clock
- That could be a long time!
-
21Example 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 FahrenheitWater freezes at 32 F and boils at
212 F - In CelsiusWater freezes at 0 C and boils at 100
C
22Example Converting C into F
- Read Celsius temperature x
- Multiply by 1.8
- Add 32 to obtain Fahrenheit temperature y
23Example Converting C into F
- Another way to do it
- Read Celsius temperature x
- Fahrenheit temperature y 1.8x 32
24Counter-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
- Multiply C temperature by 2
- Add 25
25Counter-example (II)
- These two rules are heuristics, not algorithms
- Do not always give the right conversion
- Still useful
- Double and add 25 rule converts30 C into 85 F
- Right answer is 86 F
26A 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!
27Example
- Finding a name in a table
- Naïve solution is sequential search
- Binary search is much faster
28Sequential 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 liststop and
return NOT FOUND
29Sequential 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
30Binary 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
31Binary 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
32Example of binary search
- List containsAlanAliceBarbaraEmilyFrancisGi
naPeter
33Example of binary search
- We look for Charles in a sorted list of names
- AlanAliceBarbaraEmilyFrancisGinaPeter
34Example of binary search
- We compare search name with entry exactly in the
middle of the list (Emily)AlanAliceBarbaraEmi
lyFrancisGinaPeter
35Example of binary search
- Since Charles comes before Emilywe can eliminate
second half of listAlanAliceBarbara
36Example of binary search
- We compare search name with entry exactly in the
middle of the list (Alice)AlanAliceBarbara
37Example of binary search
- Since Charles comes after Alicewe can eliminate
the first half of listBarbara
38Example of binary search
- We compare search name with entry exactly in the
middle of the list (Barbara)Barbara
39Example of binary search
- Since Charles comes after Alicewe can eliminate
the first half of the listBarbara
40Example of binary search
- Since Charles comes before Barbara we can
eliminate one half of the list
41Example of binary search
- Since list to be searched is now emptywe
conclude that the list does not contain Charles
42Comparing 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)
43Heuristics (I)
- Many problems have nopractical algorithmic
solution - Algorithm will take too long
- Example
- Finding the absolute best price for an item
- Should check everywhere
- Not cost-effective
44Heuristics (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
45An 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
46It works
47It does not always work
48Which 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
49Algorithmic 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
50Typicalalgorithmicpatterns
51Sequences
- Almost trivial
- Specifies steps that must be taken in sequence
- Default for most programming languages
- Read Celsius temperature x
- Fahrenheit temperature y 1.8x 32
52Alternatives
- Can be
- Simple
- Double
- Multiple
53Simple alternative
- Describes an action that will be taken if some
condition is true - Other choice is doing nothing
- If item is taxable tax item_value
tax_rate
54Double alternative
- Describes two actions that can be taken depending
on a Boolean condition - One of them will always be executed
- if a lt b max aelse max b
55Typical newbie mistakes
- if a lt b max aif b gt a replace by
else max b - if a lt b max aelif b gt a replace
by else max b
56Multiple choices
- Describes several actions that can be taken
depending on multiple conditions - Can end with an elif or an else
- if average gt 90 grade 'A'elif average
gt 87 grade 'A-'elif average gt 84
grade 'B'elif
57A typical newbie mistake
- Checking for was already checked
- if average gt 90 grade 'A'elif average
gt 87 and average lt 90 grade 'A-'elif
average gt 84 and average lt 87 grade
'B'elif
58Loops
- Can be
- based on a condition
- based on a range
59Condition-based loops
- condition Truewhile condition
condition False last loop iteration - while True break immediate
exit - Can combine both!
60Iterative algorithms
- An important special case of loop algorithms
- Idea is to compute a numerical result by
successively computing better and better
approximations - Will stop when the value is accurate enough
- So many decimals
61Computing the square root (I)
- Want a simple algorithm to compute vs of a
positive number. - If x vs then x2 s and x s/x
- Consider an approximation a of vs
- If a lt vs then s/a gtvs
- If a gt vs then s/a ltvs
- In either case, vs is between a and s/a
STEM Students only
62Computing the square root (II)
If a gt s
x is inside
0
s/a
a
- The average of a and s/a (a s/a)/2is a better
estimate of vs
63Babylonian method to compute square roots
- s float(input("Enter a number "))
- old 1 anything but 0 or s
- new (s1)/2
- while abs(old - new) gt 1E-5
- old new
- new (old s/old)/2
- print("Square root of .5f is .5f." (s,
new))
64How does it work? (I)
For your information
- Recall
- (a b)2 a2 2ab b2
- We can write it
- a b v(a2 2ab b2)
- In our case
- s a2 2ab b2
- old a
- error b
65How does it work? (II)
For your information
- We try to evaluate the error
- a2 2ab b2 s
- b(2a b) s a2
- b (s a2)/(2ab) (s a2)/2a (s/a a)/2
- if b is small enough
- The new approximation will be
- a b a (s/a a)/2 ( 2a s/a a)/2
(a s/a)/2
66Advantages/Disadvantages
- Simpler to program than non-iterative methods
- Each step typically performs a relatively simple
computation - Number of steps required to achieve the required
accuracy level can be a critical issue - Try to pick an algorithm that converges fast
67Range-based loops
- for i in range (k, m)
- executes loop for i k, k1, , m 1
- for i in range (0, m)
- executes loop m times
- for i 0, , m 1
- for i in range (1, m 1)
- executes loop m times
- for i 1, , m
68Example
- for i in range(0,99)
- print("I will not talk in class.")
69Loops over strings (and lists)
- Loop goes through all the elements of
- a string
- for x in anystring
- Example
- mystring input("Enter a string ")
- for ch in mystring
- print(ch)