Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Tower of Hanoi – Link 1 Tower of Hanoi – Link 1  Tower of Hanoi – Link 2 Tower of Hanoi – Link 2.

Similar presentations


Presentation on theme: " Tower of Hanoi – Link 1 Tower of Hanoi – Link 1  Tower of Hanoi – Link 2 Tower of Hanoi – Link 2."— Presentation transcript:

1  Tower of Hanoi – Link 1 Tower of Hanoi – Link 1  Tower of Hanoi – Link 2 Tower of Hanoi – Link 2

2 Radu Sorici The University of Texas at Dallas

3

4  An algorithm is a procedure which takes some values (inputs) and produces another set of values (outputs)

5  Algorithms appear as early as computational problems. They are nothing but a set of rules by which some values are calculated from some inputs (and as such can be thought of as algorithms)

6  Example: Someone needs to find the maximum value out of a series of recorded values. This problem may arise, for example, when we look for the highest temperature during a period of time.

7  So what are the inputs?

8  The inputs are a series of numbers  And the output?

9  The output is a the number m, such that  For example if the inputs are the numbers 1, 2, 3, 1000, 2^10 then the maximum is 1024.

10  And the output?  The output is a the number m, such that  For example if the inputs are the numbers 1, 2, 3, 1000, 2^10 then the maximum is 1024.  Such an input is called an instance of the maximum problem.

11  And the output?  The output is a the number m, such that  For example if the inputs are the numbers 1, 2, 3, 1000, 2^10 then the maximum is 1024.  Such an input is called an instance of the maximum problem.  What are some other computational problems?

12  And the output?  The output is a the number m, such that  For example if the inputs are the numbers 1, 2, 3, 1000, 2^10 then the maximum is 1024.  Such an input is called an instance of the maximum problem.  What are some other computational problems?  Do not forget about sorting!

13  We will call an algorithm correct if it halts with correct outputs. If an algorithm is correct then it solves the problem.

14  An algorithm which is incorrect can (and usually does) produce incorrect output. It may also halt or not depending on the inputs.

15  We will call an algorithm correct if it halts with correct outputs. If an algorithm is correct then it solves the problem.  An algorithm which is incorrect can (and usually does) produce incorrect output. It may also halt or not depending on the inputs.  An incorrect algorithm does not necessarily mean failure. Sometimes a partially correct answer is quite satisfactory. It is called a heuristic (Greek: "Ε ὑ ρίσκω", "find" or "discover" ).

16  In modern days heuristics are used in anti- viruses. They check for known viruses and attempt to guess new ones according to some rules.

17  What other problems are solved by algorithms?

18  Wal-Mart uses an advanced algorithm which enables them to have very precise shipping and restocking schedule. They use statistical variables to predict the demand for certain items.  The Internet! It uses multiple algorithms in order to manage data transfer and authentication. If we think of a computer as a node in a graph it suddenly becomes a graph theory problem! In addition, we can see more and more cloud services which require complex algorithms to manage virtually unlimited amounts of information.

19  Let us not also forget about the Human Genome Project (1990-2003) which ended in a complete deciphering of the human DNA!

20  In the end approximately 3.3 billion base- pairs were mapped. Not sure what that means but it sounds very complicated.

21  Let us not also forget about the Human Genome Project (1990-2003) which ended in a complete deciphering of the human DNA!  In the end approximately 3.3 billion base- pairs were mapped. Not sure what that means but it sounds very complicated.  Last but not least cryptography. Our data is encrypted based on number theory and numerical manipulations.

22  Problem: Create an algorithm that solves the sorting problem.

23  Solution: Bubble Sort!

24  Problem: Create an algorithm that solves the sorting problem.  Solution: Bubble Sort! Even though it is my favorite algorithm to solve the sorting problem it is far from efficient. A far more efficient algorithm is the insertion sort.

25  Problem: Create an algorithm that solves the sorting problem.  Solution: Bubble Sort! Even though it is my favorite algorithm to solve the sorting problem it is far from efficient. A far more efficient algorithm is the insertion sort.  Usually the best way to describe an algorithm is in English. It is also advisable to be as concise as possible.

26  Insertion Sort: 1. Chose a number from a given set and place it on a line

27  Insertion Sort: 1. Chose a number from a given set and place it on a line 2. Chose another number and place it on the same line but in the right position. This is either to the right if it is larger or equal or to the left if it is smaller

28  Insertion Sort: 1. Chose a number from a given set and place it on a line 2. Chose another number and place it on the same line but in the right position. This is either to the right if it is larger or equal or to the left if it is smaller 3. Repeat step two until all of the numbers are in ascending order (note that if the actions in step two are reversed we would have obtained a descending sequence). Also note that at every step the number on the line are always sorted

29  Insertion Sort  Why is insertion sorting correct?

30  Efficiency  Of course the most widely used characteristic of efficiency is speed.

31  Efficiency  Of course the most widely used characteristic of efficiency is speed  Since we are interested in computer science as well, speed may refer to the capability of the computer processor to execute commands. Everyone likes a computer that can run the newest games!

32  Efficiency  Of course the most widely used characteristic of efficiency is speed  Since we are interested in computer science as well, speed may refer to the capability of the computer processor to execute commands. Everyone likes a computer that can run the newest games!  What are other characteristics that we might look at if we are interested in efficiency and in general why are we interested in efficiency?

33  Case Study: The Bubble Sort! The basic idea is that if we have n, numbers we arrange them in a line. Then we compare any two numbers and change their location depending on which one is larger or smaller.

34  Take the first number and compare it to the rest and change the order when necessary. This takes n-1 steps.

35  Case Study: The Bubble Sort! The basic idea is that if we have n, numbers we arrange them in a line. Then we compare any two numbers and change their location depending on which one is larger or smaller.  Take the first number and compare it to the rest and change the order when necessary. This takes n-1 steps.  Thus in total we make at most (n-1)*(n- 1)=n^2-2n+1 steps.

36  Now suppose that a computer can execute 1,000,000 commands per second. If we are given 10,000 numbers to sort we will need approximately

37  The complexity of the algorithm is the number it takes to transform the inputs into the outputs.

38  The algorithms we are looking at have polynomial complexities which means that the number of steps required to solve it can be represented as a polynomial.

39  The complexity of the algorithm is the number it takes to transform the inputs into the outputs.  The algorithms we are looking at have polynomial complexities which means that the number of steps required to solve it can be represented as a polynomial.  Very often we will represent this complexity with a big O notation which in simple terms represents the rate of growth of a function.

40  With polynomials we are used to thinking in terms of degrees. Similarly the complexities of algorithms can be represented as classes of polynomial degrees. For example all problems that can be solved in a linear amount of steps (ax+b – steps) has complexity O(n). Then Bubble Sort has complexity O(n^2).

41  Problem: What is the complexity of the Insertion Sort?

42  Answer: O(n^2).

43  Of course there are algorithms which are not solved in polynomial time ( for example: find all perfect squares less than a given number).

44  There are a number of complexity classes. A famous picture circulating in most textbooks relating complexity classes is the following

45

46  P: In simple terms a problem that can be solved in polynomial time  NP: These are problems that can be solved in non-deterministic polynomial time.

47  P: In simple terms a problem that can be solved in polynomial time  NP: These are problems that can be solved in non-deterministic polynomial time.  P=NP?

48  P: In simple terms a problem that can be solved in polynomial time  NP: These are problems that can be solved in non-deterministic polynomial time.  P=NP?  The travelling salesman problem is in the NP class.

49  P: In simple terms a problem that can be solved in polynomial time  NP: These are problems that can be solved in non-deterministic polynomial time.  P=NP?  The travelling salesman problem is in the NP class  A solution can be verified in polynomial time

50  So far we have seen algorithms which are called incremental. These are algorithms are basically recursion which repeat a certain step in order to get us closer to the output.

51  Another famous approach and the most important in my opinion is the divide and conquer.

52  The main goal of this technique is to divide a problem into multiple similar problems with smaller inputs.

53  Another famous approach and the most important in my opinion is the divide and conquer.  The main goal of this technique is to divide a problem into multiple similar problems with smaller inputs.  It consists of three steps 1. Divide the problem into similar sub problems 2. Conquer the sub problems by solving them in the same way 3. Combine the results together to get the final output

54  Use the divide and conquer technique to solve the sorting problem

55  The analysis of the complexities yielded by divide and conquer algorithms is very interesting but unfortunately is beyond the scope of this lecture. It is enough to say that they produce far better results then other techniques

56  Use the divide and conquer technique to solve the sorting problem  The analysis of the complexities yielded by divide and conquer algorithms is very interesting but unfortunately is beyond the scope of this lecture. It is enough to say that they produce far better results then other techniques  Propose a problem that might benefit from the divide and conquer technique

57  Alternating between the smallest and the next- smallest disks, follow the steps for the appropriate case:  For an even number of disks:  make the legal move between pegs A and B  make the legal move between pegs A and C  make the legal move between pegs B and C  repeat until complete  For an odd number of disks:  make the legal move between pegs A and C  make the legal move between pegs A and B  make the legal move between pegs B and C  repeat until complete

58  Recursive solution  A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. The following procedure demonstrates this approach.  label the pegs A, B, C—these labels may move at different steps  let n be the total number of discs  number the discs from 1 (smallest, topmost) to n (largest, bottommost)  To move n discs from peg A to peg C:  move n−1 discs from A to B. This leaves disc n alone on peg A  move disc n from A to C  move n−1 discs from B to C so they sit on disc n

59  For a set of points X in the plane find the minimal convex set containing X.  For a set of points X in the plane find a polygon that contains all of the points from X.  Four numbers are written on a circle. The final goal is to move the numbers to the right, by one position, without moving the circle. The only operations available are 1. Assignment and recording of one value 2. Assignment, addition, and subtraction 3. Assignment, multiplication, and division

60  Let f be a function such that f(0)=0, f(1)=1, f(2n)=n, f(2n+1)=f(n)+f(n+1). Devise an algorithm that computes f(N) for some N.

61  Let f be a function such that f(0)=0, f(1)=1, f(2n)=n, f(2n+1)=f(n)+f(n+1). Devise an algorithm that computes f(N) for some N. (Recursion)  Consider a set of 10 numbers. Find an algorithm that extracts subsets out of these 10 numbers such that they are divisible by a) 2 b) 3 c) 5 d) 7

62  Consider a matrix of size m x n with numbers written in it. A number is called amazing if it is at the same time the smallest number in its row and the largest in its column. Devise an algorithm that finds all such numbers in a given matrix.

63  Check if a given points is located inside a given triangle (generalize to convex polygon).

64


Download ppt " Tower of Hanoi – Link 1 Tower of Hanoi – Link 1  Tower of Hanoi – Link 2 Tower of Hanoi – Link 2."

Similar presentations


Ads by Google