algorithms cse532, lecture 1

75
Andrew P. Black based in part on material by Mark P. Jones Department of Computer Science Portland State University Fall 2015 Lecture 2: Preliminaries, Asymptotic Notation CS 350 Algorithms and Complexity Thursday, 1 October 2015

Upload: vunhu

Post on 31-Dec-2016

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Algorithms CSE532, Lecture 1

Andrew P. Black

based in part on material by Mark P. Jones

Department of Computer Science

Portland State University

Fall 2015

Lecture 2: Preliminaries, Asymptotic Notation

CS 350 Algorithms and Complexity

Thursday, 1 October 2015

Page 2: Algorithms CSE532, Lecture 1

Form yourselves into groups of 4! Number yourselves 1, 2, 3 and 4! Roles:• Facilitator: gets discussion moving and keeps it moving,

e.g., by asking the other group members questions, sometimes about what they've just been saying.

• Summarizer: Every so often, provides a summary of the discussion for other students to approve or amend.

• Reflector: This student will listen to what others say and explain it back in his or her own words, asking the original speaker if the interpretation is correct.

• Elaborator: This person seeks connections between the current discussion and past topics or overall course themes.

2

Thursday, 1 October 2015

Page 3: Algorithms CSE532, Lecture 1

Here are your Roles:3. Facilitator: gets discussion moving and keeps it moving, e.g., by asking the other group members questions, sometimes about what they've just been saying.2. Summarizer: Every so often, provides a summary of the discussion for other students to approve or amend.4. Reflector: This student will listen to what others say and explain it back in his or her own words, asking the original speaker if the interpretation is correct.1. Elaborator: This person seeks connections between the current discussion and past topics or overall course themes.

3

Thursday, 1 October 2015

Page 4: Algorithms CSE532, Lecture 1

GCD (again!)! Find gcd(31415, 13205) using Euclid’s

algorithm! Estimate how man times faster that was,

compared to using the consecutive algorithm

4

Consecutive1. t ← min(m, n)2. r

1 ← m rem t.

3. if r1 = 0 then goto step 4 else goto step 6

4. r2 ← n rem t.

5. if r2 = 0 then return t

6. t ← t – 17. goto step 2

Euclid

Thursday, 1 October 2015

Page 5: Algorithms CSE532, Lecture 1

Old world PuzzleA peasant finds himself on a riverbank with a wolf, a goat, and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room for only the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage.

Solve this problem for the peasant or prove it has no solution. (Note: The peasant is a vegetarian but does not like cabbage and hence can eat neither the goat nor the cabbage to help him solve the problem. And it goes without saying that the wolf is a protected species.)

5

Thursday, 1 October 2015

Page 6: Algorithms CSE532, Lecture 1

What’s the first move?

6

p w g c

Thursday, 1 October 2015

Page 7: Algorithms CSE532, Lecture 1

What’s the first move?

7

A:

B:

C:

D:

E: none of the above

p

w g c

p

w g

c

p w

g c

p

w

g

c

Thursday, 1 October 2015

Page 8: Algorithms CSE532, Lecture 1

Solve this problem in your groups

8

p w g c

Thursday, 1 October 2015

Page 9: Algorithms CSE532, Lecture 1

What’s an algorithm?! Which of the following constitute an

algorithm for computing the area of a triangle, given positive numbers a, b, c representing the lengths of the sides?

9

Exercises 1.2

1. Old World puzzle A peasant finds himself on a riverbank with a wolf,a goat, and a head of cabbage. He needs to transport all three to the

other side of the river in his boat. However, the boat has room for only

the peasant himself and one other item (either the wolf, the goat, or the

cabbage). In his absence, the wolf would eat the goat, and the goat would

eat the cabbage. Solve this problem for the peasant or prove it has no

solution. (Note: The peasant is a vegetarian but does not like cabbage

and hence can eat neither the goat nor the cabbage to help him solve the

problem. And it goes without saying that the wolf is a protected species.)

2. New World puzzle There are four people who want to cross a rickety

bridge; they all begin on the same side. You have 17 minutes to get them

all across to the other side. It is night, and they have one flashlight. Amaximum of two people can cross the bridge at one time. Any party that

crosses, either one or two people, must have the flashlight with them. Theflashlight must be walked back and forth; it cannot be thrown, for example.Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes,

person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must

walk together at the rate of the slower person’s pace. (Note: According to

a rumor on the Internet, interviewers at a well-known software company

located near Seattle have given this problem to interviewees.)

3. Which of the following formulas can be considered an algorithm for com-

puting the area of a triangle whose side lengths are given positive numbers

d, e, and f?

a. V =ps(s� d)(s� e)(s� f)> where s = (d+ e+ f)@2

b. V = 12ef sinD> where D is the angle between sides e and f

c. V = 12dkd> where kd is the height to base d

4. Write pseudocode for an algorithm for finding real roots of equation d{2+e{+ f = 0 for arbitrary real coe!cients d> e> and f= (You may assume theavailability of the square root function vtuw({)=)

5. Describe the standard algorithm for finding the binary representation ofa positive decimal integer

a. in English.

b. in pseudocode.

6. Describe the algorithm used by your favorite ATM machine in dispensing

cash. (You may give your description in either English or pseudocode,

whichever you find more convenient.)

7

Thursday, 1 October 2015

Page 10: Algorithms CSE532, Lecture 1

Basic Data Structures! Array: sequence of n items, stored

contiguously in memory" element access: constant time

! Linked List: sequence of n nodes, each containing a pointer and an item" access to element k: time proportional to k" insert an element: constant time" delete an element: constant time

10

Thursday, 1 October 2015

Page 11: Algorithms CSE532, Lecture 1

! How can we insert and delete at index k in an array?

! How long do these operations take?

A. constant time?B. time proportional to insertion position k ?C. time proportional to size of the array n ?

11

Thursday, 1 October 2015

Page 12: Algorithms CSE532, Lecture 1

Group Problem—Unsorted Array! Suppose that you have an (unsorted)

array of size n.

! How can you delete the ith element of the array so that the time taken does not depend on n? (1 ≤ i ≤ n)

12

Thursday, 1 October 2015

Page 13: Algorithms CSE532, Lecture 1

Group Problem—Unsorted Array! Suppose that you have an (unsorted)

array of size n.

! How can you delete the ith element of the array so that the time taken does not depend on n? (1 ≤ i ≤ n)

! Is element access still constant time?

13

Thursday, 1 October 2015

Page 14: Algorithms CSE532, Lecture 1

Group Problem—Sorted Array! Suppose that you have a sorted array of

size n.

! How can you delete the ith element of the array so that the time taken does not depend on n? (1 ≤ i ≤ n). Yes, the array must remain sorted.

14

Thursday, 1 October 2015

Page 15: Algorithms CSE532, Lecture 1

Trees! Levitin: free tree ≡ connected acyclic graph

15

a b

e

g

h

j

c

d

f

Thursday, 1 October 2015

Page 16: Algorithms CSE532, Lecture 1

Trees! Levitin: forest ≡ connected acyclic graph

16

a b

e

g

h

j

c

d

f

Thursday, 1 October 2015

Page 17: Algorithms CSE532, Lecture 1

Rooted Tree! In a tree, ∃ a unique path from one node

to another! So we can pick an arbitrary node as the

"root"

17

a

g

h

j

Thursday, 1 October 2015

Page 18: Algorithms CSE532, Lecture 1

Rooted Tree! In a tree, ∃ a unique path from one node

to another! So we can pick an arbitrary node as the

"root"

17

a

g

h

j

Thursday, 1 October 2015

Page 19: Algorithms CSE532, Lecture 1

Rooted Tree! In a tree, ∃ a unique path from one node

to another! So we can pick an arbitrary node as the

"root"

17

a

g

h

j

a

g

h

j

Thursday, 1 October 2015

Page 20: Algorithms CSE532, Lecture 1

Rooted Tree! In a tree, ∃ a unique path from one node

to another! So we can pick an arbitrary node as the

"root"

17

a

g

h

j

Thursday, 1 October 2015

Page 21: Algorithms CSE532, Lecture 1

Rooted Tree! In a tree, ∃ a unique path from one node

to another! So we can pick an arbitrary node as the

"root"

17

a

g

h

j

Thursday, 1 October 2015

Page 22: Algorithms CSE532, Lecture 1

Rooted Tree! In a tree, ∃ a unique path from one node

to another! So we can pick an arbitrary node as the

"root"

17

a

g

h

j

a

gh

j

Thursday, 1 October 2015

Page 23: Algorithms CSE532, Lecture 1

Tree Depth & Tree Height! depth =

length of path to root

! height = maximum depth

18

a

b

e

g

h

j

c

df

depth 0

depth 1

depth 2

depth 3

depth 4

= number of levels – 1

Thursday, 1 October 2015

Page 24: Algorithms CSE532, Lecture 1

Ordered Tree ! children are

ordered (left to right)

19

6

5

9

4

7

3

7

42

Thursday, 1 October 2015

Page 25: Algorithms CSE532, Lecture 1

Search Tree ! children are

ordered (left to right)

! left children ≤ parent < right children

20

6

7

9

5

4

4

2

3 7

Thursday, 1 October 2015

Page 26: Algorithms CSE532, Lecture 1

Size of Numbers! How many bits are there in the binary

representation of a decimal number n?

21

A. log10 n

B. lg n

C. blg nc

D. dlg ne

E. none of the above

Thursday, 1 October 2015

Page 27: Algorithms CSE532, Lecture 1

A. C(n) D. tm / C(n)B. O(C(n)) E. C(n) / tm

C. tm C(n) F. none of the above

Basic operationAn algorithm has multiplication as its basic operation. A multiplication takes time tm, on average. On an input of size n, the algorithm performs its basic operation C(n) times.

What’s the approximate run time T(n) of the algorithm?

22

Thursday, 1 October 2015

Page 28: Algorithms CSE532, Lecture 1

Running Time! Suppose T(n) = 4n3 seconds! So T(10) = 4000 s (4000s ≈ 1.1 hour)! What’s T(1 000)?

A. 10 000 s (≈ 2.8 hours)B. 40 000 s (≈ 11 hours)C. 4 000 000 s (≈ 6.6 weeks)D. 1 000 000 000 s (31 years)E. 4 000 000 000 s (127 years)

23

Thursday, 1 October 2015

Page 29: Algorithms CSE532, Lecture 1

4

N

f(n)

C g(n)

there exists a C such that,for all n > N, f(n) ≤ C g(n).

upper bounds

Thursday, 1 October 2015

Page 30: Algorithms CSE532, Lecture 1

4N

f(n)

C g(n)

there exists a C such that,for all n ≥ N, f(n) ≤ C g(n).

upper bounds

What’s the relationship between f(n) and g(n)?

A. f(n) ∈ O(g(n))B. f(n) ∈ 𝛀(g(n))C. f(n) ∈ Θ(g(n))D. f(n) < C(g(n))E. none of the above

Thursday, 1 October 2015

Page 31: Algorithms CSE532, Lecture 1

4

f(n) ∈ O(g(n))

f(n)

C g(n)

there exists a C such that,for all n ≥ N, f(n) ≤ C g(n).

Big Ohupper bound

N

Thursday, 1 October 2015

Page 32: Algorithms CSE532, Lecture 1

3

Formalizing Asymptotic Notation:! A function f(n) is said to be O(g(n)) if

there are constants c>0 and N>0 such that:

f(n) ≤ c g(n) for all n ≥ N

! In other words: for large enough input n, f(n) is no more than a constant multiple of g(n).

! Big Oh is used for stating upper bounds.

“Big Oh”

Thursday, 1 October 2015

Page 33: Algorithms CSE532, Lecture 1

28

Which of the following is true:

A. 3n2 + 500 2 O(n)

B. 3n2 + 500 2 O(n2)

C. 3n2 + 500 2 O(n3)

D. A & B

E. B & C

F. none of the above

Thursday, 1 October 2015

Page 34: Algorithms CSE532, Lecture 1

Which of the following is true:

29

A. 3n3/500 2 O(n)

B. 3n3/500 2 O(n2)

C. 3n3/500 2 O(n3)

D. A & B

E. B & C

F. none of the above

Thursday, 1 October 2015

Page 35: Algorithms CSE532, Lecture 1

5

Examples:! 4n2 + 3 ∈ O(n2)

! 4n3 + 3 ∈ O(n3)

! n2/1000 + 3000n ∈ O(n2)

In general:! Can ignore all but the highest power! Can ignore coefficients

Thursday, 1 October 2015

Page 36: Algorithms CSE532, Lecture 1

Logarithms! Which of the following is true?

31

A. O(lnn) = O(log10 n)

B. O(lg n) = O(lnn)

C. lg n = lnn

D. all of the above are true

E. none of the above is true

F. A and B are true

G. B and C are true

Thursday, 1 October 2015

Page 37: Algorithms CSE532, Lecture 1

Powers! Which of the following is true

32

A. O(4n) = O(2n)

B. O(2⇥ 2n) = O(10⇥ 2n)

C. both of the above are true

D. neither of the above is true

Thursday, 1 October 2015

Page 38: Algorithms CSE532, Lecture 1

More Examples:Logarithms:! Can ignore base because:

logab = logcb/logca.

! Thus O(log2n) is the same as O(log10n).

Exponents:! Can ignore non-exponential terms! Base of exponentiation is important; for

example, O(4n) is bigger than O(2n).33

Thursday, 1 October 2015

Page 39: Algorithms CSE532, Lecture 1

More Properties of Big Oh:

! O notation is additive and multiplicative:

If f(n) ∈ O(s(n)) and g(n) ∈ O(t(n)), then:" f(n) + g(n) ∈ O(s(n) + t(n));" f(n)g(n) ∈ O(s(n)t(n)).

! O notation is transitive:

If f(n) ∈ O(g(n)), and g(n) ∈ O(h(n)), then

f(n) ∈ O(h(n)).

34

Thursday, 1 October 2015

Page 40: Algorithms CSE532, Lecture 1

Classes of Algorithm:There are standard names for some of the most common complexity classes:

✦ Constant: O(1)

✦ Logarithmic: O(log n)

✦ Linear: O(n)

✦ Linearithmic: O(n log n)

✦ Quadratic: O(n2)

✦ Exponential: O(2n)

✦ Double Exponential: O(22n)35

Thursday, 1 October 2015

Page 41: Algorithms CSE532, Lecture 1

Polynomial Algorithms:! An algorithm is said to be polynomial if it

is O(np) for some integer p.

! Terminology:

" Problems with polynomial algorithms are generally considered to be tractable.

" Problems for which no polynomial algorithm has been found are often considered intractable.

36

Thursday, 1 October 2015

Page 42: Algorithms CSE532, Lecture 1

f(n)

C g(n)

there exists a C such thatfor all n > N, f(n) ≥ C g(n).

Lower Bounds

37N

Thursday, 1 October 2015

Page 43: Algorithms CSE532, Lecture 1

f(n)

C g(n)

there exists a C such thatfor all n > N, f(n) ≥ C g(n).

Lower Bounds

What’s the relationshipbetween f(n) and g(n)?

A. f(n) ∈ O(g(n))B. f(n) ∈ 𝛀(g(n))C. f(n) ∈ Θ(g(n))D. f(n) > C(g(n))E. none of the above

37N

Thursday, 1 October 2015

Page 44: Algorithms CSE532, Lecture 1

f(n) ∈ Ω(g(n))

N

f(n)

C g(n)

there exists a C such thatfor all n ≥ N, f(n) ≥ C g(n).

Omega, Ωlower bound

38

Thursday, 1 October 2015

Page 45: Algorithms CSE532, Lecture 1

Dealing with Lower Bounds:! “This algorithm takes at least …”

! A function f(n) is said to be in Ω(g(n)) if there are constants c>0 and N>0 such that:

f(n) ≥ c g(n) for all n ≥ N

! Note that f(n) ∈ Ω(g(n)) if and only if g(n) ∈ O(f(n)).

Omega

39

Thursday, 1 October 2015

Page 46: Algorithms CSE532, Lecture 1

Mnemonics! Big Oh is really a Capital greek letter

Omicron; pronounce it O-micron. Pronounce 𝛀 O-mega.

! Read as f is O-smaller-than g! Read as f is O-larger-than g

" The large O (O, 𝛀) says: f may be equal to g" The small o (𝞸, 𝜔) says: f will be unequal to g

40

f(n) 2 ⌦(g(n))

f(n) 2 O(g(n))

Thursday, 1 October 2015

Page 47: Algorithms CSE532, Lecture 1

Tight Bounds:

! A function f(n) is said to be in Θ(g(n)) if it is in both O(g(n)) and Ω(g(n)).

" If f(n) ∈ Θ(g(n)), then it is eventually “sandwiched” between constant multiples of g(n).

! f(n) ∈ Θ(g(n)) if and only if limn!1

g(n)

f(n)= c

41

Thursday, 1 October 2015

Page 48: Algorithms CSE532, Lecture 1

C2 g(n)

C1 g(n)

N

f(n)

f(n) ∈ Θ(g(n))

there exist C1 and C2 such that, for all n ≥ N,C1 g(n) ≤ f(n) ≤ C2 g(n).

Theta, Θtight bound

42

Thursday, 1 October 2015

Page 49: Algorithms CSE532, Lecture 1

Simple laws of Θ(..) notation:! Addition:

Θ(f(n) + g(n)) = Θ(f(n)) + Θ(g(n))

! Scaling: for any constant c>0,

Θ(cf(n)) = c Θ(f(n)) = Θ(f(n))

43

Thursday, 1 October 2015

Page 50: Algorithms CSE532, Lecture 1

True or False! You have two sorting algorithms:

B is O(n2), while Q is O(n lg n).

! True or false: Q is always faster than B" A. True" B. False

44

Thursday, 1 October 2015

Page 51: Algorithms CSE532, Lecture 1

Beware Constant Factors!! Use complexity measures with care!

! A Θ(n2) algorithm might actually be faster than a Θ(n) algorithm for all values of n encountered in some real application!

45

Thursday, 1 October 2015

Page 52: Algorithms CSE532, Lecture 1

Θ(n2)

Θ(n)

n

T(n)

The Θ(n2) algorithm is faster than the Θ(n) alternative if we’re working within this particular range …

46

Thursday, 1 October 2015

Page 53: Algorithms CSE532, Lecture 1

Beware Constant Factors!! Use complexity measures with care!

! A Θ(n2) algorithm might actually be faster than a Θ(n) algorithm for all values of n encountered in some real application!

! How would you find out?

A. more careful analysis for different nB. measure the implementation for different nC. neither of the above

47

Thursday, 1 October 2015

Page 54: Algorithms CSE532, Lecture 1

Little-oh! Not much used in analysis of algorithms! Compare little-oh and big-oh

48

f(n) 2 O(g(n)) i↵ 9c > 0, 9N > 0

such that 0 f(n) cg(n), 8n � N (1)

f(n) 2 o(g(n)) i↵ 8c > 0, 9N > 0

such that 0 f(n) < cg(n), 8n � N (2)

Notice that there are two di↵erences. First, little-ouses a strict less-than rather than the less-than-or-equal-to in the condition on f(n). Second, little-o uses the uni-versal quantifier 8 in place of the existential 9. So whereaswith big-O you get to pick the constant, with little-o, theinequality must be true for all constants.

How can one thing always be less than another, re-gardless of what constant it’s multiplied by? It has to bezero. Intuitively in o-notation, the function f(n) becomesinsignificantly small, relative to g(n), as n approaches in-finity. In symbols:

limn!1f(n)g(n) = 0

This is actually the definition that was given in Section5.5.2 of Hein’s book on Discrete Structures.

Thursday, 1 October 2015

Page 55: Algorithms CSE532, Lecture 1

! Two differences:" change of quantifier" change of comparator

! How can f always be less than g, regardless of the constant c?

49

f(n) 2 O(g(n)) i↵ 9c > 0, 9N > 0

such that 0 f(n) cg(n), 8n � N (1)

f(n) 2 o(g(n)) i↵ 8c > 0, 9N > 0

such that 0 f(n) < cg(n), 8n � N (2)

Notice that there are two di↵erences. First, little-ouses a strict less-than rather than the less-than-or-equal-to in the condition on f(n). Second, little-o uses the uni-versal quantifier 8 in place of the existential 9. So whereaswith big-O you get to pick the constant, with little-o, theinequality must be true for all constants.

How can one thing always be less than another, re-gardless of what constant it’s multiplied by? It has to bezero. Intuitively in o-notation, the function f(n) becomesinsignificantly small, relative to g(n), as n approaches in-finity. In symbols:

limn!1f(n)g(n) = 0

This is actually the definition that was given in Section5.5.2 of Hein’s book on Discrete Structures.

Thursday, 1 October 2015

Page 56: Algorithms CSE532, Lecture 1

! Two differences:" change of quantifier" change of comparator

! How can f always be less than g, regardless of the constant c?

49

f(n) 2 O(g(n)) i↵ 9c > 0, 9N > 0

such that 0 f(n) cg(n), 8n � N (1)

f(n) 2 o(g(n)) i↵ 8c > 0, 9N > 0

such that 0 f(n) < cg(n), 8n � N (2)

Notice that there are two di↵erences. First, little-ouses a strict less-than rather than the less-than-or-equal-to in the condition on f(n). Second, little-o uses the uni-versal quantifier 8 in place of the existential 9. So whereaswith big-O you get to pick the constant, with little-o, theinequality must be true for all constants.

How can one thing always be less than another, re-gardless of what constant it’s multiplied by? It has to bezero. Intuitively in o-notation, the function f(n) becomesinsignificantly small, relative to g(n), as n approaches in-finity. In symbols:

limn!1f(n)g(n) = 0

This is actually the definition that was given in Section5.5.2 of Hein’s book on Discrete Structures.

Thursday, 1 October 2015

Page 57: Algorithms CSE532, Lecture 1

! Two differences:" change of quantifier" change of comparator

! How can f always be less than g, regardless of the constant c?

49

f(n) 2 O(g(n)) i↵ 9c > 0, 9N > 0

such that 0 f(n) cg(n), 8n � N (1)

f(n) 2 o(g(n)) i↵ 8c > 0, 9N > 0

such that 0 f(n) < cg(n), 8n � N (2)

Notice that there are two di↵erences. First, little-ouses a strict less-than rather than the less-than-or-equal-to in the condition on f(n). Second, little-o uses the uni-versal quantifier 8 in place of the existential 9. So whereaswith big-O you get to pick the constant, with little-o, theinequality must be true for all constants.

How can one thing always be less than another, re-gardless of what constant it’s multiplied by? It has to bezero. Intuitively in o-notation, the function f(n) becomesinsignificantly small, relative to g(n), as n approaches in-finity. In symbols:

limn!1f(n)g(n) = 0

This is actually the definition that was given in Section5.5.2 of Hein’s book on Discrete Structures.

Thursday, 1 October 2015

Page 58: Algorithms CSE532, Lecture 1

limn!1

t(n)

g(n)=

8><

>:

0 ) t(n) has a smaller order of growth than g(n)

c > 0 ) t(n) has the same order of growth as g(n)

1 ) t(n) has a larger order of growth than g(n)

Comparing Orders of Growth! If you need to compare the rates of

growth of two functions, t and g, the easiest way is often to take limits:

18

Thursday, 1 October 2015

Page 59: Algorithms CSE532, Lecture 1

limn!1

t(n)

g(n)=

8><

>:

0 ) t(n) has a smaller order of growth than g(n)

c > 0 ) t(n) has the same order of growth as g(n)

1 ) t(n) has a larger order of growth than g(n)

Comparing Orders of Growth! If you need to compare the rates of growth

of two functions, t and g, the easiest way is often to take limits:

! Which function goes on top of the limit?A. The one you think grows slowerB. The one you think grows fasterC. It doesn’t matter

18

Thursday, 1 October 2015

Page 60: Algorithms CSE532, Lecture 1

Example! Prove that the functions and have

different orders of growth if

19

Thursday, 1 October 2015

Page 61: Algorithms CSE532, Lecture 1

Example! Prove that the functions and have

different orders of growth if

19

Thursday, 1 October 2015

Page 62: Algorithms CSE532, Lecture 1

Example! Prove that the functions and have

different orders of growth if

19

Thursday, 1 October 2015

Page 63: Algorithms CSE532, Lecture 1

20

Summary:

! Asymptotic notation using O, Ω, and Θ" O(f (n)) is an upper bound " Ω(f (n)) is a lower bound" Θ(f (n)) sets tight bounds

Thursday, 1 October 2015

Page 64: Algorithms CSE532, Lecture 1

Square roots! Write pseudocode for an algorithm that

computes ⌊√n⌋ for any positive integer n. Besides assignment and comparison, your algorithm may use only the four basic arithmetic operations.

21

Thursday, 1 October 2015

Page 65: Algorithms CSE532, Lecture 1

Euclid’s Euclid! Euclid’s algorithm, as presented in Euclid’s

treatise, uses subtractions rather than mod. Write pseudocode for this version of Euclid’s algorithm.

23

Thursday, 1 October 2015

Page 66: Algorithms CSE532, Lecture 1

Door in a Wall

56

copyrighted Soni Alcorn-Hender

Thursday, 1 October 2015

Page 67: Algorithms CSE532, Lecture 1

! You are facing a wall that stretches infinitely in both directions. There is a door in the wall, but you know neither how far away, nor in which direction. You can see the door only when you are right next to it.

! Design an algorithm that enables you to reach the door.

! Write an expression for the number of steps that your algorithm will take. Your expression should be in terms of n, the (unknown to you) number of steps between your initial position and the door.

57

Thursday, 1 October 2015

Page 68: Algorithms CSE532, Lecture 1

58

?

Thursday, 1 October 2015

Page 69: Algorithms CSE532, Lecture 1

Which way do you walk?A. To the right

B. To the left

C. It doesn't matter

59

Thursday, 1 October 2015

Page 70: Algorithms CSE532, Lecture 1

How far do you go?A. 1 step

B. 2 steps

C. Until you are in front of the door

D. k steps, for some fixed kE. The lesser of C and D

60

Thursday, 1 October 2015

Page 71: Algorithms CSE532, Lecture 1

What do you do then?

61

Thursday, 1 October 2015

Page 72: Algorithms CSE532, Lecture 1

Crossing the Bridge! Four people (named A, B, V and X) want to

cross a bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, for example. A takes 1 minute to cross the bridge, B takes 2 minutes, V takes 5 minutes, and X takes 10 minutes. A pair must walk together at the rate of the slower person’s pace.

25

Thursday, 1 October 2015

Page 73: Algorithms CSE532, Lecture 1

Crossing the Bridge, continued! Try to solve this problem, and then

choose the correct answer from the list:A. The mission is impossibleB. The first move is for A and X to cross with

the flashlightC. The first move is for A and B to cross with

the flashlightD. The first move is for A and C to cross with

the flashlightE. Some other first move is necessary to solve

the problem26

Thursday, 1 October 2015

Page 74: Algorithms CSE532, Lecture 1

Binary Representations! The number of bits b in the binary

representation of a number n is given by

b = ⌊log2 n⌋ + 1

Using this formula, how many bits are required to represent the number 230?

27

A. 29 C. 31

B. 30 D. none of these

Thursday, 1 October 2015

Page 75: Algorithms CSE532, Lecture 1

Binary Representations! The number of bits b in the binary

representation of a number n is given by

b = ⌊log2 n⌋ + 1

Using this formula, how many bits are required to represent the number (230 - 1)?

27

A. 29 C. 31

B. 30 D. none of these

Thursday, 1 October 2015