640 likes | 811 Views
For science!. Tower of Hanoi – Link 1 Tower of Hanoi – Link 2. ALGORITHMS. MMC. Radu Sorici. The University of Texas at Dallas. What are algorithms? . What are algorithms? . An algorithm is a procedure which takes some values ( inputs ) and produces another set of values ( outputs ).
E N D
For science! • Tower of Hanoi – Link 1 • Tower of Hanoi – Link 2
ALGORITHMS MMC RaduSorici The University of Texas at Dallas
What are algorithms? • An algorithm is a procedure which takes some values (inputs) and produces another set of values (outputs)
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)
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.
Inputs and outputs • So what are the inputs?
Inputs and outputs • So what are the inputs? • The inputs are a series of numbers • And the output?
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.
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.
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?
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!
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.
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.
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" ).
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.
Usage • What other problems are solved by algorithms?
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.
Usage • Let us not also forget about the Human Genome Project (1990-2003) which ended in a complete deciphering of the human DNA!
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.
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.
Problems and Examples • Problem: Create an algorithm that solves the sorting problem.
Problems and Examples • Problem: Create an algorithm that solves the sorting problem. • Solution: Bubble Sort!
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.
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.
Insertion Sort • Insertion Sort: • Chose a number from a given set and place it on a line
Insertion Sort • Insertion Sort: • Chose a number from a given set and place it on a line • 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
Insertion Sort • Insertion Sort: • Chose a number from a given set and place it on a line • 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 • 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
Insertion Sort • Insertion Sort • Why is insertion sorting correct?
efficiency • Efficiency • Of course the most widely used characteristic of efficiency is speed.
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!
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?
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.
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.
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.
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
Complexity • The complexity of the algorithm is the number it takes to transform the inputs into the outputs.
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.
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.
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).
Complexity • Problem: What is the complexity of the Insertion Sort?
Complexity • Problem: What is the complexity of the Insertion Sort? • Answer: O(n^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).
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
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.
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?
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.
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
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.