1 / 14

CSc142 Algorithms and efficiency Week 9

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.

gail-brooks
Download Presentation

CSc142 Algorithms and efficiency Week 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSc142Algorithms and efficiencyWeek 9 Pete Bagnall bagnall@comp.lancs.ac.uk Elizabeth Phillips e.m.phillips@lancaster.ac.uk

  2. Week 9 topics • Research in complexity • Starring: • Towers of Hanoi • Tile Puzzle • Travelling Salesman

  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(N2). Such a proof establishes a lower bound.

  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. 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. 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. 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. 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. 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.

  10. 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)

  11. 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.

  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. 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. 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

More Related