Tower of Hanoi Link 1 Tower of Hanoi Link 1 Tower of Hanoi Link 2 Tower of Hanoi Link 2.

64
FOR SCIENCE! Tower of Hanoi – Link 1 Tower of Hanoi – Link 2

Transcript of Tower of Hanoi Link 1 Tower of Hanoi Link 1 Tower of Hanoi Link 2 Tower of Hanoi Link 2.

Page 1: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

FOR SCIENCE! Tower of Hanoi – Link 1 Tower of Hanoi – Link 2

Page 2: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

ALGORITHMSRadu Sorici

The University of Texas at Dallas

MMC

Page 3: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

Page 4: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

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

Page 5: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

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

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)

Page 6: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

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.

Page 7: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INPUTS AND OUTPUTS So what are the inputs?

Page 8: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INPUTS AND OUTPUTS So what are the inputs? The inputs are a series of numbers

And the output?

Page 9: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INPUTS AND OUTPUTS 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.

Page 10: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INPUTS AND OUTPUTS 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.

Page 11: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INPUTS AND OUTPUTS 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?

Page 12: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

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!

Page 13: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

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

Page 14: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHAT ARE ALGORITHMS?

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.

Page 15: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

CORRECTNESS 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" ).

Page 16: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

CORRECTNESS 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" ).

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

Page 17: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

USAGE What other problems are solved by

algorithms?

Page 18: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

USAGE What other problems are solved by algorithms? 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.

Page 19: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

USAGE Let us not also forget about the Human

Genome Project (1990-2003) which ended in a complete deciphering of the human DNA!

Page 20: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 21: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 22: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS AND EXAMPLES Problem: Create an algorithm that

solves the sorting problem.

Page 23: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS AND EXAMPLES Problem: Create an algorithm that

solves the sorting problem.

Solution: Bubble Sort!

Page 24: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS AND EXAMPLES 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.

Page 25: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS AND EXAMPLES 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.

Page 26: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INSERTION SORT Insertion Sort:1. Chose a number from a given set and

place it on a line

Page 27: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INSERTION SORT 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

Page 28: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INSERTION SORT 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

Page 29: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

INSERTION SORT Insertion Sort Why is insertion sorting correct?

Page 30: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

EFFICIENCY Efficiency Of course the most widely used

characteristic of efficiency is speed.

Page 31: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

EFFICIENCY 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!

Page 32: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

EFFICIENCY 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?

Page 33: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 34: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 35: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 36: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

EFFICIENCY Thus in total we make at most (n-1)*(n-

1)=n^2-2n+1 steps. 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

Page 37: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY The complexity of the algorithm is the

number it takes to transform the inputs into the outputs.

Page 38: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 39: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 40: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY Very often we will represent this

complexity with a big O notation which in simple terms represents the rate of growth of a function.

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

Page 41: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY Problem: What is the complexity of the

Insertion Sort?

Page 42: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY Problem: What is the complexity of the

Insertion Sort? Answer: O(n^2).

Page 43: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY Of course there are algorithms which

are not solved in polynomial time ( for example: find all perfect squares less than a given number).

Page 44: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY Of course there are algorithms which

are not solved in polynomial time ( for example: find all perfect squares less than a given number).

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

Page 45: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY

Page 46: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 47: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY 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?

Page 48: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 49: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

COMPLEXITY 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

Page 50: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHEWWW, MOVING ON 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.

Page 51: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHEWWW, MOVING ON 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.

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

Page 52: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHEWWW, MOVING ON 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.

Page 53: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

WHEWWW, MOVING ON 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 steps1. Divide the problem into similar sub problems2. Conquer the sub problems by solving them

in the same way 3. Combine the results together to get the final

output

Page 54: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

DIVIDE AND CONQUER Use the divide and conquer technique to

solve the sorting problem

Page 55: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

DIVIDE AND CONQUER 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

Page 56: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

DIVIDE AND CONQUER 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

Page 57: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

TOWER OF HANOI 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

Page 58: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

TOWER OF HANOI 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

Page 59: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS 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 value2. Assignment, addition, and subtraction3. Assignment, multiplication, and division

Page 60: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 61: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS 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) 2b) 3c) 5d) 7

Page 62: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

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

Page 63: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS Check if a given points is located inside

a given triangle (generalize to convex polygon).

Page 64: Tower of Hanoi  Link 1 Tower of Hanoi  Link 1  Tower of Hanoi  Link 2 Tower of Hanoi  Link 2.

PROBLEMS Check if a given points is located inside

a given triangle (generalize to convex polygon).