transcript - New Mexico Computer Science for All

advertisement
Algorithms
This presentation provides an introduction to algorithms. Algorithms are a
fundamental building block of all computer programs. [Slide 1] So what do these
things have in common, doing the laundry, going grocery shopping, and making
chocolate chip cookies? Well, each is a repeated action. Every time you have
laundry, you have to do it. Every time you need groceries, you have to go to the
grocery store. And every time you want fresh home baked cookies, you just have
to make them. And each of them follows the same steps every time you want to
do it. Each of these things can be thought of as an algorithm.
[Slide 2] An algorithm is just a set of instructions that can be used repeatedly to
solve the same problem or task. [Slide 3] Why do we use algorithms? Well, once
we have an algorithm we don’t have to think about the details of the problem
anymore. We already know how to solve it. Once you know how to go to the
grocery store, you don’t need to whip up the map every time you go. You just go.
All the information we need is in the algorithm. We just have to follow it. The
same algorithm can be used again and again. If it’s the right type of algorithm it
can be expanded to solve a bigger problem. Once you have the algorithm anyone
can follow it.
[Slide 4] Computers have a number of characteristics that make them uniquely
suited to solve algorithms. Their speed and accuracy and the fact that they’re
well suited to solve tedious problems like searching on a large telephone
directory or adding a large column of numbers. Actually computers are problem
solving machines. Once you’ve developed that technique for solving the
problem, once you’ve developed that algorithm, the computer can use the
algorithm and solve your problems quickly. But without an algorithm you don’t
have a solution. If your algorithm is broad enough it can solve many different
types of problems.
[Slide 5] So what are the characteristics of a good algorithm? Well, it has to
receive input or have initial conditions. And it has to produce a result or an
output. All algorithms contain steps that are well ordered, clearly defined, and
doable. There has to be a clear stopping point or condition. And algorithms are
general.
[Slide 6] The good news is you already know about algorithms because you’ve
already used them. One typical algorithm that you’ve already used is the wiggle
where you ask the turtles to turn right random 90 degrees and turn left random
90 degrees and move forward one. That’s an algorithm and you’ve used it more
than once. [Slide 7] There are many different types of algorithms, and you can
group them together in ways that highlight how you would apply them to a
problem, or how the solution could be used.
Algorithms
Page 1 of 3
Different ways you might group algorithms include implementation, so if the
algorithm is iterative or deterministic if it has one outcome, or the methodology
used, brute force or divide and conquer for instance, or the application if you’re
going to sort a list of numbers or search through a telephone directory, those are
different applications.
[Slide 8] We’re going to explore some algorithms based on their types of
applications. Some groups of algorithms might be counting if you need to count
a large group of items, sorting if you need to sort through a large list of items,
searching if you need to find an item in a list. Mapping or graphing, perhaps you
need to know the shortest distance between two points or the cheapest way of
getting there. Encryption, either encoding information or decoding information.
Packing, how do you make the most items fit in a given a space. And mazes, you
can use algorithms to create or solve mazes.
[Slide 9] When you develop an algorithm you follow the following steps. First you
figure out what the problem is. Then you need to know what the input for that
problem is and what you want out of the algorithm. Then you use abstraction
and decomposition. You break the problem into its parts and detail all the steps
that are required. Then you can write a pseudo code.
This is something written not in any particular programming language but in
plain English that helps you figure out how you’re going to write your code.
Then, you might refine the solution if necessary and write the code in the
programming language that you’re using. Of course, you’re going to have to
debug that code and test the code in different cases to make sure that it works.
And then you’re done.
[Slide 10] Let’s look at an example. Say you want to take the average of a group
of numbers. Well, we know that the input will be a list of numbers and the
output should be the average of that list. If we want to apply abstraction and
decomposition, in other words, if we want to know what the steps are, well we
know that we have to add up the numbers in the list to create a total and we
have to count how many numbers are in the list. Then the average is the total of
all the numbers in the list divided by how many numbers are in the list.
[Slide 11] The pseudo code for taking the average might look something like this.
First you would initialize the variables total and count to zero. Then for each
number in the list you would add that number to the total variable and you
would add one to the count variable. Once you are done with all the numbers in
the list then you would divide the total by count. If we needed to we could refine
a pseudo code but it seems pretty straightforward. Then we would write it in the
programming language that we’re using and then of course we’d have to debug
it.
Algorithms
Page 2 of 3
[Slide 12] In general algorithms need to be analyzed. That’s because there are
many ways to solve the same problem. And when you analyze an algorithm you
evaluate whether or not it’s suitable for your problem. Is it fast enough? How
much memory does it need? Then you can compare it with other possible
algorithms that could be used to solve the same problem. Also sometimes when
you analyze your algorithm you can find improvements for it. Algorithms tend to
become better when they’re analyzed. They become shorter, simpler, and more
elegant.
[Slide 13] One technique that’s used to analyze algorithms is the big O notation.
It’s used in computer science and it’s a measure of how your algorithm runs as
you increase the amount of data that your algorithm evaluates. As you increase
the input size, you evaluate how much that increases the processing time and
the memory used. Big O notation is another way of classifying algorithms. Some
common big O values are order n or order n log n or n squared. We’ll be talking
about this in a later video.
[Slide 14] In summary, algorithms are a set of instructions that can be used over
and over again to solve a particular problem or do a particular task. You use
algorithms because you don’t have to think about the details of the problem
anymore. All the information is contained in the algorithm and anyone can
follow it.
Algorithms have the following characteristics. They receive input and produce an
output. They contain steps that are well ordered, clearly defined, and doable.
Generally they stop by themselves or they stop after a certain criteria. They’re
general in their application. Algorithm analysis is important because it helps you
choose the best algorithm for your task. One of the methods used is the big O
notation. Thank you.
Algorithms
Page 3 of 3
Download