UNIVERSITY AT BUFFALO, THE STATE UNIVERSITY OF NEW YORK
The Department of Computer Science & Engineering
cse@buffalo
CSE 111: Great Ideas in Computer Science

Introduction to Computer Science and Algorithms

What Is A Computer
From the Oxford English Dictionary:
  1. A person who makes calculations or computations; a calculator, a reckoner; spec. a person employed to make calculations in an observatory, in surveying, etc.
  2. A device or machine for performing or facilitating calculation.
  3. An electronic device (or system of devices) which is used to store, manipulate, and communicate information, perform complex calculations, or control or regulate other devices or machines, and is capable of receiving information (data) and of processing it in accordance with variable procedural instructions (programs or software); esp. a small, self-contained one for individual use in the home or workplace, used esp. for handling text, images, music, and video, accessing and using the Internet, communicating with other people (e.g. by means of email), and playing games.

A very incomplete history of computers and computing

1834 - Regarded as the first computer is Charles Babbage's Analytical Engine, which was never built but incorproated everything needed to do general purpose computation mechanically! Babbage is considered to be the "father of the computer" as he was the first to design and attempt to build a mechanical computer. Ada Lovelace is often considered to be the first computer programmer, having written an algorithm intended for use on the Analytical Engine (this is not without controversy though, as a working Analytical Engine wasn't built until 1991, not within her lifetime).

1936 - Alan Turing published his paper "On Computable Numbers, with an Application to the Entscheidungsproblem" which defines the model of computation we still use today.

During WWII, great advances in computation occured - some of which was because of efforts in cryptoanalysis to crack the Enigma code used by the Germans. Alan Turing once again was instrumental in this effort.

1943 - Thomas Watson (from IBM) says "I think there is a world market for maybe five computers."

1950 - UNIVAC does the US census. It also predicts Eisenhower will win the presidential election in 1952. Also Alan Turing publishes his paper on the Turing Test, asking the question of whether or not computers can think.

1957 - FORTRAN, to become and extremely popular programming language, released.

1972 - First computers join ARPANET, which will become the Internet.

1977 - Apple II released.

1981 - IBM PC released.

1984 - Macintosh released. The Internet contains ~1000 hosts.

1991 - Tim Berners-Lee "invents" the World Wide Web.

1997- IBM's Deep Blue beats world chess champion Kasparov.

1998 - Google launched.

1999 - Napster created.

2011 - Watson wins on Jeopardy.

There are many more complete timelines of computer history on the web. A couple good ones are the ones by the Computer History Museum, and the more complete but less detailed one here. There's a pretty neat article from IEEE Spectrum about some items from the Computer History Museum. There's also a really neat training video set about a Navy mechanical targeting computer from the 50's on YouTube (there's 7 parts - this links to part 1).

What Isn't Computer Science


What Is Computer Science
The study of computation. That is, what can and cannot be computed, and we know something can be computed if there exists an algorithm for computing it. Moreover, we're interested in what can be efficiently computed, and how we might do that computation. (Note: Usually when we talk about efficiency we mean time efficiency - doing a computation in as little time as is possible - but we can also mean space efficiency, and more recently power efficiency)

What Are Algorithms

Informally, an algorithm is like a recipe which tells you (or a computer!) how to do something.

It might sound silly, but it’s a simple algorithm for getting water!


“Boil water” is what we might call a named procedure. When we wish to perfrom an operation many times or in many situations, instead of enumerating each of its steps every time we give a certain sequence of steps a name. These named procedures require certain things to be true before they start (eg. you own a pan, water is accessable), and gurantee a certain outcome when they are done (ie. you now have boiling water).


Algorithms have to be specific. You can’t rely on “common sense” since everyone has their own (and computers have none)!


What Is Computation

We can view a computer as a black box which we give some information to, and get back something else by "turning a crank." First we have to start by having some way to specify a problem we're looking at. One way to do this is by looking at functions (note that a function itself doesn't do anything, we'll talk about methods for computing functions in a bit).

A function is a set of input-output pairs, <input, output>, such that no two have the same input. That is, there may not be more than one output for a single input. The successor function:
successor = <0, 1>,<1, 2>,<2, 3>, ...
We could alternatively write this as
successor(0) = 1; successor(1) = 2; successor(2) = 3; ...

Functions can be finite, that is, have a number of outcomes which is not infinite.
answer = <’Hi’,print ’Hello there!’>;<’Bye’, print ’Goodbye!’>;< input != ’Hi’ & input != ’Bye’, print ’Hmm, interesting’>

They also can be partial, meaning they are undefined for some values.
sqrt = < 1; 1 >,< 4; 2 >,< 9; 3 >, ...

Note that functions don't do anything - they are simply a mapping from input to output. What we're really interested in is the mechanics which let us compute a function. That is, given an input, we should get the output that we expect.

Therefore, we say a function f is computable if there exists an algorithm which computes f. This should give you the clue that there are some functions which are not computable! We'll get there later in the course.

An algorithm, more formally then, is a finite procedure for computing f that:

  1. Has the same input/output behavior as f
  2. Specifies the relationship between the input and output of f using well defined steps (ie. not magic).


How Can We Write An Algorithm?
An algorithm for computing some procedure P can contain any combination of three things:
These three things together are what we call the flow of the algorithm.

Later in the course we’ll prove these three are all you need, but for now we’ll just learn about what they are and how to use them.

Sequence
Doing a finite number of things in order.

Consider the steps required to start a car:
  1. Open car door
  2. Sit in drivers seat
  3. Fasten seatbelt
  4. Insert key in ignition
  5. Turn key
There are 5 steps here, which must be peformed in precisely this order. Any step out of place or missed will result in not completing the task.

In general:
1. Perform operation x
2. Perform operation y
.
.
n. Perform operation z

Selection
Choosing which path to take based on some condition.

For example, if the light is red then stop, else go. In general:
1. If <something is true> then perform operation x [else perform operation y]

Where square brackets represent an optional part of the expression.

Selection is a lot like a decision tree... Consider the following analysis of the "30 second rule" (via sfweekly):


This is the same thing - just a lot of yes/no questions!

Loop
There are several general forms of the loop... here are a few:

There are only so many for convenience - any one can perform the operations of the other two! We'll concern ourselves mostly with the "For each" loop, as it's most natural to think about.

Let's look at an example having to do with looking through a stack of papers for a specific one:

for each paper in papers
if paper == one_looking_for then return

Here's something more complex:

for each x in range(5)
for each y in range(5)
print '*'
print endline

Note, range(5) is meant to generate the list (0, 1, 2, 3, 4).

In-Class Exercise: What does this output?


Two More Things...
We also use variables and assignment statements. A variable is what you are used to in algebra, except it can be a number, piece of text, an image, etc... It represents some value. We use assignment statements to assign a specific value to a variable. Example:

var x        
 #Here we’ve defined a variable x.

x = 1        
#Here we’ve assigned the value of x to be 1. We pronounce this "x gets 1."

x = x + 1  
#Now we’ve said the value of x should be the current value of x + 1 (so, 2 in this case).

An Example Algorithm: Addition, naively
var ans = top-number
var temp = 0
while temp < bottom-number
ans = ans + 1
temp = temp + 1

Class Exercise: Design the addition algorithm for the grade school method of addition.



Copyright © 2011 Daniel R. Schlegel