Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSc 142 2005/6 Week nine 1 CSc142 Algorithms and efficiency Week 9 Pete Bagnall Elizabeth Phillips

Similar presentations


Presentation on theme: "CSc 142 2005/6 Week nine 1 CSc142 Algorithms and efficiency Week 9 Pete Bagnall Elizabeth Phillips"— Presentation transcript:

1 CSc 142 2005/6 Week nine 1 CSc142 Algorithms and efficiency Week 9 Pete Bagnall bagnall@comp.lancs.ac.uk Elizabeth Phillips e.m.phillips@lancaster.ac.uk

2 CSc 142 2005/6 Week nine 2 Week 9 topics Research in complexity Starring: Towers of Hanoi Tile Puzzle Travelling Salesman

3 CSc 142 2005/6 Week nine 3 Lower and Upper Bounds Each problem has some level of complexity. It is not always obvious what this is. The discovery of a new ’best’ algorithm places an upper bound on the problem’s complexity. Analysis of the problem at hand can yield a proof that no algorithm exists that can solve the problem in less than, say O(N 2 ). Such a proof establishes a lower bound.

4 CSc 142 2005/6 Week nine 4 Lower and Upper Bounds Open Problems: Any problem where we don’t have an algorithm that achieves the lower bound. Problems where the gap between lower and upper bounds is large are the subject of intense research. Closed Problems: Problems for which there exists at least one algorithm at the lower bound are called closed problems.

5 CSc 142 2005/6 Week nine 5 Inefficiency and intractability We have seen many examples of algorithms whose complexity class is exponential or factorial. The recursive Fibonacci function was merely inefficient. Without pre-computation, a lower bound for Fibonacci appears to be O(N). Some problems whose algorithmic lower bound is exponential or factorial cannot be computed in any useful time at all, and are called unreasonable, or intractable.

6 CSc 142 2005/6 Week nine 6 Intractable problems A big favourite is the Towers of Hanoi. Eastern monks are the keepers of 3 towers. On the first tower is a pile of golden rings. They aim to move all the rings onto the next tower. They move one ring a day Never onto a smaller ring They have 64 rings, and claim the world will end when they finish Should we be worried? Actually, it was invented by the French mathematician Edouard Lucas in 1883.

7 CSc 142 2005/6 Week nine 7 Recursive Hanoi void moveHanoi(int n, int from, int to, int spare) { if (n == 1) { System.out.println(from + "->" + to); } else { moveHanoi(n - 1, from, spare, to); System.out.println(from + "->" + to); moveHanoi(n - 1, spare, to, from); }

8 CSc 142 2005/6 Week nine 8 Hanoi complexity A recursive function - take the heavier side of the branch. Each visit to the function might recursively call two more. Recursive Hanoi is O(2N). When is the end of the world if the rings can be moved Once a nanosecond?

9 CSc 142 2005/6 Week nine 9 P, NP and Tile Puzzle Remember the Tile Puzzle example? If you put the tiles down at random, what’s the probability the game is finishable? Or … how many of the possible arrangements can be completed? Exactly (N 2 )! starting arrangements for bad randomising method.

10 CSc 142 2005/6 Week nine 10 P, NP and Tile Puzzle To decide whether a badly randomised game is finishable, generate all move sequences from start arrangement, to around N 3 moves. Each move has up to 4 next moves = around O(4^N 3 ) move sequences. After move, apply O(N 2 ) finish checking How many are finishable? This algorithm is O((N 2 )! x 4^N 3 x N 2 )

11 CSc 142 2005/6 Week nine 11 P, NP and Tile Puzzle Generating a solution is O((N 2 )! x 4^N 3 ) Checking a solution is O(N 2 ) We introduce a non-deterministic Turing Machine here, which is able to always guess the best next move. For the ’is this finishable’ problem, the non-deterministic algorithm now finishes in N 3 polynomial time. For very many algorithms, a non-deterministic Turing Machine can compute in polynomial time, and we call the class these algorithms belong to non-deterministic polynomial or NP for short.

12 CSc 142 2005/6 Week nine 12 P = NP? Algorithms that execute in polynomial time all belong to class P Problems in NP are intimately related, and can be converted into other NP problems by polynomial-time reduction If a polynomial-time solution can be found for an NP problem, all NP problems have polynomial-time solutions. We haven’t managed it yet! We believe P != NP. $1,000,000 to anyone who can prove this (or disprove it) P NP

13 CSc 142 2005/6 Week nine 13 Some NP problems Travelling salesman Given N towns with inter-town distances, devise shortest all-town tour Packing problem Given a set of shapes (or solid objects), can they be put together to fit in a rectangle (or rectangular container). Who cares? Freight companies: the perfect distribution network and perfectly packed container is impossible to find. But better solutions to both problems are valuable.

14 CSc 142 2005/6 Week nine 14 The last slide We’ve covered: Inefficiency and Intractability The P=NP question Towers of Hanoi The Travelling Salesman Packing Problem Next week: A note on optimisation Case study More examples


Download ppt "CSc 142 2005/6 Week nine 1 CSc142 Algorithms and efficiency Week 9 Pete Bagnall Elizabeth Phillips"

Similar presentations


Ads by Google