140 likes | 243 Views
CSc142 Algorithms and efficiency Week 9. Pete Bagnall bagnall@comp.lancs.ac.uk Elizabeth Phillips e.m.phillips@lancaster.ac.uk. Week 9 topics. Research in complexity Starring: Towers of Hanoi Tile Puzzle Travelling Salesman. Lower and Upper Bounds.
E N D
CSc142Algorithms and efficiencyWeek 9 Pete Bagnall bagnall@comp.lancs.ac.uk Elizabeth Phillips e.m.phillips@lancaster.ac.uk
Week 9 topics • Research in complexity • Starring: • Towers of Hanoi • Tile Puzzle • Travelling Salesman
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(N2). Such a proof establishes a lower bound.
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.
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.
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.
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); • } • }
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?
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 (N2)! starting arrangements for bad randomising method.
P, NP and Tile Puzzle • To decide whether a badly randomised game is finishable, generate all move sequences from start arrangement, to around N3 moves. • Each move has up to 4 next moves = around O(4^N3) move sequences. • After move, apply O(N2) finish checking • How many are finishable? This algorithm is O((N2)! x 4^N3 x N2)
P, NP and Tile Puzzle • Generating a solution is O((N2)! x 4^N3) • Checking a solution is O(N2) • 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 N3 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.
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
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.
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