
タイトル：
0107 Case Study

概説：

Now, a lot of computer science students bristle a little bit when they take an algorithms class,

because of the heavy emphasis on mathematics.

A lot of us got involved in computer science because we really like doing stuff

and not necessarily doing math.

But nevertheless, I think there's a very strong case to be made for the importance

of mathematics in computer science and in algorithm design in particular.

I'd argue that there are three natural ways that theory stuff or math can really help.

One is to just get you thinking clearly about what it is that you're trying to accomplish.

It's very easy when you're in the depths of writing code to lose track

of what it is that you want the code to actually do.

Just thinking formally about what you're doing is something

that using your mathematical background can help with.

Another thing that it can be very helpful with is analyzing the efficiency of what you've produced.

You can actually know where there are spots where you could be doing a better job

and have the code be running more effectively and more efficiently

without producing incorrect code.

Just taking a moment to think a little mathematically can be a huge win

and save you tremendous amounts of time.

It sounds very important, right?

Now, this notion of efficiency is actually very important to think about.

What is it that you want your program to do efficiently.

Do you want it to be fast in terms of time?

Do you want it to be efficient in terms of the amount of memory

on the computer that it uses, so it does its work with as little memory as possible?

These days more and more people are worrying about energy usage.

Are there ways of organizing your computation so that it is efficient

in terms of the amount of power that it uses.

The tools that we develop in this course are going to be useful across the board here,

but we're going to mainly focus on issues of time.

To get you thinking about algorithms and how they work and what makes them correct

and how to make them more efficient, let's go through an example together.

Here is a little bit of Python code that I wrote.

It's a routine called naïve, because I'm not telling you yet what it actually is doing.

What it takes in as input are two integervalued variables

that are none negative, and then it does some assignments

and recalculations and a while loop.

It runs for a little bit and then it returns z.

What I'd like you to do is take a look at this. It's not very long.

It's probably not completely obvious to you right away what it is that it's doing.

I encourage you to run this in Python.

Give it some example inputs and outputs.

See if you can come up with a pattern for what it is that it's doing,

and then convince yourself why.

Once you have a hypothesis as to what it's doing,

see if you can figure out how you can convince yourself that this program actually

is computing what you think it's computing.

Then I want you to fill out the following quiz.

You can take this code, and you can run it for any particular value of a and b,

but I want you to think about what it does in general as a function of a and b.

When you run naive(a, b) does it return whichever of a or b is the larger one?

Does it calculate a  b, or for that matter, does it calculate b  a?

Does it calculate the sum of a and b?

Does it calculate the product of a and b?