• No results found

Tutorial 1 Data Structures ’10

N/A
N/A
Protected

Academic year: 2022

Share "Tutorial 1 Data Structures ’10"

Copied!
54
0
0

Loading.... (view fulltext now)

Full text

(1)

Tutorial 1

Data Structures ’10

Jonathan Cederberg <jonathan.cederberg@it.uu.se>

Friday, September 10th, 2010

(2)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(3)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(4)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

The set of natural numbers is defined inductively:

0 is a natural number.

if x is a natural number then x + 1 is a natural number.

To prove that a property P(n) holds for all natural numbers n, show the following:

Base Case: Show that P(0) holds.

Induction Step: Show that P(n) implies P(n + 1).

(5)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Example: Show that

n

X

i =0

i = n(n + 1)

2 for all n ≥ 0.

(6)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Base Case: n = 0

0

X

i =0

i = 0 0(0 + 1)

2 = 0

(7)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Induction Step Assume

n

X

i =0

i = n(n + 1) 2 Show

n+1

X

i =0

i = (n + 1)(n + 2) 2

(8)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

n+1

X

i =0

i =

n

X

i =0

i + (n + 1)

= n(n+1)2 + (n + 1)

= n22+n + (n + 1)

= n2+3n+22

= (n+1)(n+2)2

= (n+1)((n+1)+1) 2

(9)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(10)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

To prove that a property P(n) holds for all natural numbers n ≥ n0, we show the following:

Base Case: Prove that P(n0) holds.

Induction Step: Show that P(n) implies P(n + 1).

(11)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Example: Show that

n

X

i =0

(i − 3) ≥ n2

4 for all n ≥ 12.

Base Case: n = 12

12

X

i =0

(i −3) = −3−2−1+0+1+2+3+4+5+6+7+8+9 = 39

122

4 = 144

4 = 36 36 ≤ 39

(12)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Induction Step Assume

n

X

i =0

(i − 3) ≥ n2 4 Show

n+1

X

i =0

(i − 3) ≥ (n + 1)2 4

(13)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

n+1

X

i =0

(i − 3) =

n

X

i =0

(i − 3) + ((n + 1) − 3)

n42 + (n − 2)

= n2+4n−84

= n2+2n+1+2n−94

= (n+1)24+2n−9

(n+1)4 2

(14)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(15)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

An example of Asymptotic notation

Prove 5n2 − 3n − 6 = Θ(n2).

That is, find c1, c2, n0 such that for all n > n0 we have 0 ≤ c1· n2≤ 5n2 − 3n − 6 ≤ c2· n2

(16)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

What does 5n2 − 3n − 6 look like?

(17)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

What does 5n2 − 3n − 6 look like?

(18)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

What does 5n2 look like?

(19)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

What does 5n2 look like?

(20)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

What about c1= 4 and c2 = 6?

(21)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

What n0 do we want?

(22)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Prove 5n2 − 3n − 6 = Θ(n2).

Pick c1 = 4, c2 = 6, and n0= 6.

Show that

0 ≤ 4n2for all n ≥ 6 (trivial).

4n2 ≤ 5n2 − 3n − 6 for all n ≥ 6.

5n2 − 3n − 6 ≤ 6n2for all n ≥ 6.

(23)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

4n2 ≤ 5n2 − 3n − 6 for all n ≥ 6:

4n2 ≤ 5n2 − 3n − 6

0 ≤ n2 − 3n − 6 = n(n − 3) − 6

6 ≤ n

|{z}

≥6

(n − 3)

| {z }

≥3

| {z }

≥18

(24)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

5n2 − 3n − 6 ≤ 6n2 for all n ≥ 6:

5n2 − 3n − 6 ≤ 6n2

− 6 ≤ n2 + 3n = n

|{z}≥6

(n + 3)

| {z }

≥9

| {z }

≥54

(25)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(26)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Method 1 – Recursive Trees

Each node represents the cost of one subproblem.

The sum of all node costs within a level gives the total cost of that level.

The sum of all per-level costs gives the total cost.

(27)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Method 2 – Substitution Guess a solution.

Verify correctness of the solution using mathematical induction.

(28)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Example:

Consider the recurrence T (n) =

 1 if n = 1

2T n

2

 + 3n if n > 1 Show that T (n) = O(n lg n). (lg = log2).

(29)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Pick n0= 2 and c = 4. Show that T (n) ≤ 4 n lg n for all n ≥ 2.

Use induction on trees:

Base Case: Show that the property holds for one level of the tree.

Induction Step: Show that if the property holds for one level of the tree, then it will also hold for the next level.

(30)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

The base cases n = 2.

T (2) = 2T (1) + 3 · 2 = 2 · 1 + 6 = 8 2 · 4 · lg 2 = 8

8 ≤ 8.

n = 3.

T (3) = 2T (1) + 3 · 3 = 2 · 1 + 9 = 11 3 · 4 · lg 3 = 12 · lg 3

11 ≤ 12 · lg 3.

(31)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

The Induction Step AssumeT n

2 ≤ 4 n2 lg n2

ShowT (n) ≥ 4n lg n

(32)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

T (n) = 2T n

2 + 3n

≤ 2 · 4 n

2 lg n2 + 3n

≤ 8 n2 lg n2 + 3n

= 4 n (lg n − lg 2) + 3n

= 4 n (lg n − 1) + 3n

= 4 n lg n − 4n + 3n

≤ 4 n lg n

(33)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(34)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

1. Asymptotic growth rates

Rank these functions in order of growth:

n 4lg n n! lg n 22n (n + 1)!  3 2

n

n lg n n3 2n n · 2n

(35)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Example

n lg n n (lg n)lg n

(36)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

2. Fibonacci

The golden cut 1+

5

2 is a ratio with a number of different interesting properties, and is often denoted φ. Its conjugate is φ =b 1−

5

2 . These ratios are connected with the Fibonacci series {Fn}n=0, defined recursively as

F0 = 0 F1 = 1

Fn= Fn−1+ Fn−2 for n ≥ 2

Prove by induction that for all n ≥ 0, we have

Fn=

φn− φbn

√5

(37)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

3. Tiling

How many different ways are there to tile a 2 × n rectangle with 1 × 2 tiles? Formulate as a recurrence, and solve it.

(38)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Outline

1 Mathematical Induction

2 Variant of Mathematical Induction

3 Example of Asymptotic notation

4 How to Solve Recurrence Equations

5 Assignment 1

6 Joint Exercises

(39)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Towers of Hanoi

You are given a tower of n disks initially stacked in decreasing order of diameter on one out of three pegs. The goal is to transfer the entire tower to one of the other pegs, moving one disk at a time and never placing a larger disk onto a smaller one. Devise a divide-and-conquer algorithm, analyze its running time and write and solve the recurrence.

(40)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

First thing: we should use divide-and-conquer. This means, put in words, that the way to move a big tower is by moving a smaller tower.

Algorithm: to move a tower of n disks from peg A to peg C , move the smaller tower consisting of n − 1 disks to peg B, move the nth disk to peg C , and move the tower consisting of n − 1 disks on peg B to peg C . If n = 1, skip the recursive step.

In pseudocode: Towers-Of-Hanoi(n)

1 if n = 1

2 thenmove disk to target peg 3 else Towers-Of-Hanoi(n − 1) 4 move nth disk to target peg 5 Towers-Of-Hanoi(n − 1)

(41)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

First thing: we should use divide-and-conquer. This means, put in words, that the way to move a big tower is by moving a smaller tower.

Algorithm: to move a tower of n disks from peg A to peg C , move the smaller tower consisting of n − 1 disks to peg B, move the nth disk to peg C , and move the tower consisting of n − 1 disks on peg B to peg C . If n = 1, skip the recursive step.

In pseudocode: Towers-Of-Hanoi(n)

1 if n = 1

2 thenmove disk to target peg 3 else Towers-Of-Hanoi(n − 1) 4 move nth disk to target peg 5 Towers-Of-Hanoi(n − 1)

(42)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

First thing: we should use divide-and-conquer. This means, put in words, that the way to move a big tower is by moving a smaller tower.

Algorithm: to move a tower of n disks from peg A to peg C , move the smaller tower consisting of n − 1 disks to peg B, move the nth disk to peg C , and move the tower consisting of n − 1 disks on peg B to peg C . If n = 1, skip the recursive step.

In pseudocode:

Towers-Of-Hanoi(n) 1 if n = 1

2 thenmove disk to target peg 3 else Towers-Of-Hanoi(n − 1) 4 move nth disk to target peg 5 Towers-Of-Hanoi(n − 1)

(43)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Complexity

What type of complexity?

So, how do we get the recurrence T (n)?

The call with n = 1 clearly requires 1 operation on line 2. A call with n > 1 requires T (n − 1) operations on line 3, 1 op on line 4 and T (n − 1) ops on line 5

T (n) =

 1 if n = 1

2T (n − 1) + 1 if n > 1

(44)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Complexity

What type of complexity?

So, how do we get the recurrence T (n)?

The call with n = 1 clearly requires 1 operation on line 2. A call with n > 1 requires T (n − 1) operations on line 3, 1 op on line 4 and T (n − 1) ops on line 5

T (n) =

 1 if n = 1

2T (n − 1) + 1 if n > 1

(45)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Complexity

What type of complexity?

So, how do we get the recurrence T (n)?

The call with n = 1 clearly requires 1 operation on line 2.

A call with n > 1 requires T (n − 1) operations on line 3, 1 op on line 4 and T (n − 1) ops on line 5

T (n) =

 1 if n = 1

2T (n − 1) + 1 if n > 1

(46)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Complexity

What type of complexity?

So, how do we get the recurrence T (n)?

The call with n = 1 clearly requires 1 operation on line 2.

A call with n > 1 requires T (n − 1) operations on line 3, 1 op on line 4 and T (n − 1) ops on line 5

T (n) =

 1 if n = 1

2T (n − 1) + 1 if n > 1

(47)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Complexity

What type of complexity?

So, how do we get the recurrence T (n)?

The call with n = 1 clearly requires 1 operation on line 2.

A call with n > 1 requires T (n − 1) operations on line 3, 1 op on line 4 and T (n − 1) ops on line 5

T (n) =

 1 if n = 1

2T (n − 1) + 1 if n > 1

(48)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

How to solve it

Start by some unfolding, and compute for small values.

Guess closed form solution: Tc(n) = 2n− 1 Prove it by induction:

Basecase (n=1): T (1) = 1 = 21− 1 = Tc(1)

Induction step: assume T (n) = Tc(n).

Then T (n + 1) = 2T (n) + 1 = 2 · (2n− 1) + 1 = 2n+1− 2 + 1 = 2n+1− 1 = Tc(n)

(49)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

How to solve it

Start by some unfolding, and compute for small values.

Guess closed form solution: Tc(n) = 2n− 1

Prove it by induction:

Basecase (n=1): T (1) = 1 = 21− 1 = Tc(1)

Induction step: assume T (n) = Tc(n).

Then T (n + 1) = 2T (n) + 1 = 2 · (2n− 1) + 1 = 2n+1− 2 + 1 = 2n+1− 1 = Tc(n)

(50)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

How to solve it

Start by some unfolding, and compute for small values.

Guess closed form solution: Tc(n) = 2n− 1 Prove it by induction:

Basecase (n=1): T (1) = 1 = 21− 1 = Tc(1)

Induction step: assume T (n) = Tc(n).

Then T (n + 1) = 2T (n) + 1 = 2 · (2n− 1) + 1 = 2n+1− 2 + 1 = 2n+1− 1 = Tc(n)

(51)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

How to solve it

Start by some unfolding, and compute for small values.

Guess closed form solution: Tc(n) = 2n− 1 Prove it by induction:

Basecase (n=1): T (1) = 1 = 21− 1 = Tc(1)

Induction step: assume T (n) = Tc(n).

Then T (n + 1) = 2T (n) + 1 = 2 · (2n− 1) + 1 = 2n+1− 2 + 1 = 2n+1− 1 = Tc(n)

(52)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Pancake sorting

Pankcake sorting is a sorting problem where prefix reversal is the only means with which you can alter the list to sort.

Devise and analyse a simple algorithm for pancake sorting. Do an exact worst case analysis in terms of the number of prefix reversals, and give an asymptotic upper bound, given that the reverse-prefix-operation takes O(n) time.

(53)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Pancake sorting

Divide-and-conquer approach:

Pancake-Sort(s) 1 if size(s) ≤ 1 2 then return

3 else i ← indexOfMax(s) 4 Flip(s[0, . . . , i ])

5 Flip(s)

6 Pancake-Sort(s[0, . . . , size(s) − 1])

(54)

Mathematical Induction Variant of Mathemati- cal Induction

Example of Asymptotic notation How to Solve Recurrence Equations Assignment 1 Joint Exercises

Pancake sorting

Iterative approach:

Pancake-Sort(s)

1 for i ← size(s) − 1 down to 1 2 doj ← indexOfMax(s) 3 Flip(s[0, . . . , j ]) 4 Flip(s[0, . . . , i ])

References

Outline

Related documents

To choose a solution offered by traditional security companies, in this paper called the firewall solution (Figure 6), is today one of the most common, Identity management market

According to Lo (2012), in the same sense “it points to the starting point of the learning journey rather than to the end of the learning process”. In this study the object

In summary, we have in the appended papers shown that teaching problem- solving strategies could be integrated in the mathematics teaching practice to improve students

In the study by Maries et al., (2016) shot gathers from the profile showed that the velocity of the direct P-waves varied between 400-500 m/s, whereas the refracted P- waves

The notion of a Baltic Region that would unite Russia, the Baltic States and Poland with the Scandinavian countries and Germany can be traced back to the Soviet political idea

I Assistenten under fliken dokumentbibliotek, lärandenämnden finns skriftlig information: Budget, vidaredelegering, styrkedja, nämndsplan, riktlinjer för placering i

We furthermore write the equations in potential form and use the pure hodograph transformation and corresponding recursion operators to construct hierarchies of autonomous

It is useful to be able to read people’s body language, but it is equally useful to learn how to get your body to send the right signals and eliminate the wrong signals.. See