Lecture 1 Grade school algorithms Sept. 8, 2017 1 What is an - - PowerPoint PPT Presentation

lecture 1
SMART_READER_LITE
LIVE PREVIEW

Lecture 1 Grade school algorithms Sept. 8, 2017 1 What is an - - PowerPoint PPT Presentation

Lecture 1 Grade school algorithms Sept. 8, 2017 1 What is an algorithm? An algorithm is a sequence of instructions or rules or operations for manipulating data to produce some result. Think of an algorithm as a recipe. In CS, the recipe


slide-1
SLIDE 1

Lecture 1

Grade school algorithms

  • Sept. 8, 2017

1

slide-2
SLIDE 2

What is an algorithm?

An algorithm is a sequence of instructions or rules or

  • perations for manipulating data to produce some

result. Think of an algorithm as a recipe. In CS, the recipe works with digital information such as numbers, text strings, images, sounds,….

See Khan Academy course on Algorithms for a good intro

2

slide-3
SLIDE 3

Today: grade school arithmetic

  • addition
  • subtraction
  • multiplication
  • division

You learned algorithms for performing these operations!

3

slide-4
SLIDE 4

Grade school addition

4

You needed to memorize single digit sums to do this. (Remember how you learned single digit sums?)

slide-5
SLIDE 5

What is the algorithm for addition? Let’s use an array for a, b, and the result r.

5

slide-6
SLIDE 6

Grade School Addition

6

For each column i { compute single digit sum a[i] + b[i] and add the carry value from previous column determine the result r[i] for that column determine the carry value for the next column }

slide-7
SLIDE 7

Grade School Addition (“pseudocode”)

7

(To be explained on next slides.)

slide-8
SLIDE 8

Grade School Addition (“pseudocode”)

8

“mod”

compute single digit sum a[i] + b[i] and add the carry value from previous column determine the result r[i] for that column

slide-9
SLIDE 9

Grade School Addition (“pseudocode”)

9

“mod” Integer division (ignore remaider)

determine the carry value for the next column

slide-10
SLIDE 10

The grade school addition algorithm is non-trivial. It makes use of a good number representation: it represents each number as sum of powers of 10.

(Hindu-Arabic system invented ~2000 years ago)

Do you understand how it works ?

10

slide-11
SLIDE 11

Imagine an addition algorithm that is based on Roman numerals: It would be rather awkward!

11

slide-12
SLIDE 12

Grade school subtraction

924 352 572

_

How to write an algorithm for doing this?

12

slide-13
SLIDE 13

Grade school subtraction

924 352 572

_

How to write an algorithm for doing this? How to describe the “borrowing” step? (You will implement this in Assignment 1.)

13

8

1

slide-14
SLIDE 14

Multiplication

Q: What do we mean by a * b ? (assuming integers)

14

slide-15
SLIDE 15

Multiplication

Q: What do we mean by a * b ? (assuming integers) A: (a + a + ….. + a), b times

15

a is the “multiplicand” b is the “multiplier”

slide-16
SLIDE 16

Multiplication

Q: What do we mean by a * b ? (assuming integers) A: (a + a + ….. + a), b times

  • r (b + b + … + b), a times

16

slide-17
SLIDE 17

The definition of multiplication suggests a slow algorithm: You learned a much faster algorithm in grade school.

17

slide-18
SLIDE 18

Grade school multiplication

18

“multiplicand” “multiplier”

slide-19
SLIDE 19

Grade school multiplication

Step 1: make 2D table tmp [ ][ ]

19

slide-20
SLIDE 20

Grade school multiplication

Step 2: for each column in table, sum up the rows

// column // row

20

slide-21
SLIDE 21

21

Grade school multiplication specifies that we build a temporary 2D array of size N*N. (the jaggy shape) In Assignment 1, you will implement an algorithm that does not use such a 2D array.

slide-22
SLIDE 22

Division

Q: What do we mean by a / b ? (assuming integers, and a > b)

22

slide-23
SLIDE 23

Division

Q: What do we mean by a / b ? (assuming integers, and a > b) A: We mean: “How many times can we subtract b from a before our answer is between 0 and the remainder ?”

23

slide-24
SLIDE 24

Division

Q: What do we mean by a / b ? (assuming integers, and a > b) A: a = q*b + r, 0 <= r < b q is quotient, r is remainder

24

slide-25
SLIDE 25

Slow division algorithm

To compute a / b, repeatedly subtract b from a until the result is less than b. You learned a much faster algorithm in grade school.

25

slide-26
SLIDE 26

5 ... ______________ 723 41672542996 3615

  • 552 ...etc

Grade school division (“long division”)

How would you write out the algorithm? (You will do it in Assignment 1.)

26

slide-27
SLIDE 27

Computational Complexity

What do we mean by ‘fast’ and ‘slow’? Suppose we want to perform arithmetic operations

  • n two integers a, b which have N digits each.

How many ‘steps’ does each algorithm take ?

27

slide-28
SLIDE 28

Grade School Addition

1 N 1

We mean that each part of the program is executed 1 or N times.

28

slide-29
SLIDE 29

Grade School Addition

c1 c2 * N c3

The time it takes is c1 + c3 + c2*N for some unspecified constants. When we analyze algorithms, we often ignore these constants. 29

slide-30
SLIDE 30

Grade School Multiplication

N N^2 N 1 N N^2 N

30

slide-31
SLIDE 31

Computational Complexity

We say… Grade school addition takes time O( ). Grade school multiplication takes time O( ). We will see a formal definition of O( … ) in a few weeks.

31

slide-32
SLIDE 32

TODO

  • Install Eclipse. Tutorial next week

(Wed for Sec. 001 and Thurs for Sec. 002)

  • MATH 240 issue for ECSE students

32