csc142 algorithms and efficiency week 7

19
1 CSc 142 2005/6 W e e k W e e k s e v e n s e v e n CSc142 CSc142 Algorithms and efficiency Algorithms and efficiency Week 7 Week 7 Pete Bagnall [email protected] Elizabeth Phillips [email protected]

Upload: cameron-gaines

Post on 31-Dec-2015

14 views

Category:

Documents


1 download

DESCRIPTION

CSc142 Algorithms and efficiency Week 7. Pete Bagnall [email protected] Elizabeth Phillips [email protected]. Week 7 topics. Popular algorithms Factorial Fibonacci A coursework example. Maths: Factorial. n! is known as “n factorial” 0! = 1 1! = 1 2! = 2 x 1 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSc142 Algorithms and efficiency Week 7

1

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

CSc142CSc142Algorithms and efficiencyAlgorithms and efficiency

Week 7Week 7

CSc142CSc142Algorithms and efficiencyAlgorithms and efficiency

Week 7Week 7Pete Bagnall

[email protected]

Elizabeth Phillips

[email protected]

Page 2: CSc142 Algorithms and efficiency Week 7

2

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Week 7 topicsWeek 7 topicsWeek 7 topicsWeek 7 topics

Popular algorithms

Factorial

Fibonacci

A coursework example

Page 3: CSc142 Algorithms and efficiency Week 7

3

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Maths: FactorialMaths: FactorialMaths: FactorialMaths: Factorial

n! is known as “n factorial”

0! = 1

1! = 1

2! = 2 x 1

3! = 3 x 2 x 1

4! = 4 x 3 x 2 x 1

5! = … and so on.

Generally n! = n x (n-1) x (n-2) x … x 3 x 2 x 1

How many ways can you order 3 things?

Page 4: CSc142 Algorithms and efficiency Week 7

4

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

PermutationsPermutationsPermutationsPermutations

How many ways can you pick k objects from a set of n objects?

Pick 3 objects from a set of 5 objects.

perm(n, k) = fact(n) / fact(n-k)

2 letter permutations from C, A, T:CA, CT, AC, AT, TC, TA

5 x 4 x 3 x 2 x 1

2 x 1

5!

2!

n!

(n-k)!nPk5 x 4 x 3 = = = =

Page 5: CSc142 Algorithms and efficiency Week 7

5

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Permutations - what use?Permutations - what use?Permutations - what use?Permutations - what use?

Page 6: CSc142 Algorithms and efficiency Week 7

6

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Factorials - Simpson's Tile Factorials - Simpson's Tile PuzzlePuzzle

Factorials - Simpson's Tile Factorials - Simpson's Tile PuzzlePuzzle

You might ask: How many start positions are there?

perm(9, 9) = 9! / 0! (0! = 1)

= 9!

= 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1

= 362,880

Note that not all of these start positions can be reached from the finish positions.

Page 7: CSc142 Algorithms and efficiency Week 7

7

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

A factorial functionA factorial functionA factorial functionA factorial function

int factorial(int n) {

if (n <= 1) {

return 1;

} else {

return n * factorial(n - 1);

}

}

A recursive function (it calls itself).

Looks simple, with some hidden complexity!

No initialisation?

No loop?

Page 8: CSc142 Algorithms and efficiency Week 7

8

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Recursive factorial functionRecursive factorial functionRecursive factorial functionRecursive factorial function

int factorial(int n) {

if (n <= 1) {

return 1;

} else {

return n * factorial(n - 1);

}

}

Call with factorial(3) if (3<=1);

return 3 * factorial(3 - 1);

if (2<=1);

return 2 * factorial(2 - 1);

if (1<=1);

return 1;

Page 9: CSc142 Algorithms and efficiency Week 7

9

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Recursive factorial functionRecursive factorial functionRecursive factorial functionRecursive factorial function

Time for factorial(n) depends on depth of recursion - which depends on n. It’s O(N)

int factorial(int n) {

if (n <= 1) {

return 1;

} else {

return n * factorial(n - 1);

}

}

Executes ‘long’ branch (n - 1) times

Executes the short branch once

Page 10: CSc142 Algorithms and efficiency Week 7

10

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

What recursion hidesWhat recursion hidesWhat recursion hidesWhat recursion hides

Functions operate in a ‘context’ that defines

where they were called from

what arguments they received

their local variables

their return value

This context is implemented as a stack frame.

A stack frame is created on the stack when function is called.

The stack frame is destroyed when function returns.

Page 11: CSc142 Algorithms and efficiency Week 7

11

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Function context in stack Function context in stack frameframe

Function context in stack Function context in stack frameframe

Calling code

Creates stack frame

Fills in return address and argument values.

Jumps to start of function.

Called code

Fills in return value

Jumps to return address

Calling code

Retrieves return value

Destroys stack frame

Page 12: CSc142 Algorithms and efficiency Week 7

12

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Another O(N) factorialAnother O(N) factorialAnother O(N) factorialAnother O(N) factorial

int iterativeFactorial(int n) {

int result = 1;

for (int i = 2; i <= n; i++) {

result *= i;

}

return result;

}

No recursion, slightly more code.

Appears to have worse secondary efficiency…

But remember - method calls take time

need to set up and tear down the stack frame

Page 13: CSc142 Algorithms and efficiency Week 7

13

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

An O(1) factorialAn O(1) factorialAn O(1) factorialAn O(1) factorial

How many int factorials are there?

We said n! rises fast.

A Java int is 32 bits

values from -2,147,483,648 to 2,147,483,647

(Java only has signed integers, unlike C, C++)

2,147,483,647 max +ve int

12! = 479,001,600 ok

13! = 6,227,020,800 not ok!

Page 14: CSc142 Algorithms and efficiency Week 7

14

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

An O(1) factorialAn O(1) factorialAn O(1) factorialAn O(1) factorial

final static int INT_FACTORIAL[] =

new int[] {1, 1, 2, 6, 24, 120, 720,

5040, 40320, 362880, 3628800,

39916800, 479001600};

int instantFactorial(int n) {

return INT_FACTORIAL[n];

}

Question: How many items in the array for the long version of this program?

Page 15: CSc142 Algorithms and efficiency Week 7

15

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

FibonacciFibonacciFibonacciFibonacci

The Fibonacci series is

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89…

Fib(0) = 1

Fib(1) = 1

Fib(i) = Fib(i-1) + Fib(i-2) A recursive definition!

int fib(int n) { if (n <= 1) { return 1; } else { return fib(n - 2) + fib(n - 1);

}}

Page 16: CSc142 Algorithms and efficiency Week 7

16

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Fibonacci complexityFibonacci complexityFibonacci complexityFibonacci complexity

Not as straightforward as factorial

Look at the number of times fib() is called…

00 11fib(0) fib(0) 1 1

fib(1) fib(1) 1 1

fib(2) fib(2) 3 3

fib(3) fib(3) 5 5

fib(4) fib(4) 9 9

fib(5) fib(5) 15 15

33

11

44

00 11

2222

11

55

00 11

22

33

Tfib(n) = Tfib(n-1) + Tfib(n-2) + 1 fib(n)

Page 17: CSc142 Algorithms and efficiency Week 7

17

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

Fibonacci optimisationsFibonacci optimisationsFibonacci optimisationsFibonacci optimisations

Fibonacci series soon produces very large numbers.fib(46) = 1,836,311,903 (getting close to limit of 32-bit int)

fib(46) takes a long time to compute! O(Tfib(N))

Use iterative code like… It’s O(N)!

int iterativeFib(int n) { int[] fib = new int[n + 1]; fib[0]=1; fib[1]=1; for (int i=2; i<=n; i++) { fib[i] = fib[i-1] + fib[i-2]; } return fib[n];}

Page 18: CSc142 Algorithms and efficiency Week 7

18

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

A coursework exampleA coursework exampleA coursework exampleA coursework example

Some code:int X(int[] Y) {

int Z = 0;for (int i = 0; i < Y.length; i++)Z = Z + Y[i] / Y.length;return Z;

}

What does this function do?

What is its time complexity O()?

What optimisations can you make? Re-write the code.

Can you improve primary efficiency of X? Why not?

X() has a serious problem which may be fixed in your version. What is the problem?

What potential error have you introduced?

Page 19: CSc142 Algorithms and efficiency Week 7

19

CS

c 14

22005/6

We e

k W

e ek

sev

ense

ven

The last slideThe last slideThe last slideThe last slide

We've covered:

Two famous recursive algorithms

Factorial

Fibonacci

An example coursework question

Next week:

Popular algorithms

`Styles' of algorithm

Coursework examples

Some questions from the notes