Thinking Like a Computer - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Thinking Like a Computer

Description:

The input to our problem is the set of birthdates. ... Once we've crossed off all the birthdates, then our frequency table tells us ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 21
Provided by: usersEc3
Category:

less

Transcript and Presenter's Notes

Title: Thinking Like a Computer


1
Thinking Like a Computer
2
Beginnings
  • The computer is dumber than you are.
  • If you cant solve the problem, youll never get
    the computer to solve it.
  • The first step in writing a computer program is
    developing a mechanical, step-by-step process
    that a human could use to solve the problem.

3
An Example Problem
  • What is the most popular birthdate among the
    students in this class?
  • As humans we think we know how to solve this
    problem.
  • Can we describe all the details?
  • Can we define a MECHANICAL process to solve the
    problem?

4
Step 0 Understand the Problem
  • In this case, the problem is pretty
    straightforward.
  • Birthdate defined as the month and day (not
    year) of a persons birth.
  • Most popular each of the 366 possible birth
    dates occurs zero or more times as a birthdate
    for students in EE312. The most popular date is
    the date which occurs as a birthdate more times
    than any other date.
  • If two or more dates are equally the most
    popular, then the answer to our problem can be
    any of these dates.

5
Step 1a Understand your Input
  • The input to our problem is the set of
    birthdates.
  • Initially, this information is stored in the
    brains of students sitting in the classroom (hard
    to access the information from this location).
  • Lets assume weve transferred the input
    information onto a large sheet of paper.
  • Our input consists of a large table, with one
    column, each entry is a birthdate (month/day).
  • If we were writing a computer program to solve
    this problem, wed need to specify how the
    computer accesses the input. In this case, we
    can assume that our human processor can read the
    paper.

6
Input Keys
  • Volume of information
  • How many distinct types of information?
  • How much data of each type?
  • Format of information
  • Are there delimiters? How do you know where one
    datum ends and the next begins?
  • How do you know where the beginning and end are
    for tables and sets?
  • What is the legal range of input?
  • Procedures to access information
  • What subroutines do we call or variables do we
    use?

7
Step 1b Output Information
  • In this case, our output is pretty trivial
  • The most popular date
  • The number of students that have that birthdate.
  • The same keys for input information apply to
    output information.

8
Step 1c Intermediate Information
  • Its frequently hard to identify all the
    intermediate information well need until weve
    finished step two. Be we can make some guesses
    now and come back later.
  • For now, Im going to guess that well need a
    second table.
  • There will be 366 entries in the table (one for
    each possible birthdate).
  • The information stored in each entry will be the
    number of students that have that birthdate.

9
Step 2 Select or Develop and an Algorithm
  • Well spend the next several slides on this step.
  • In some cases, someone else may have solved a
    very similar problem. Youll be able to use
    their solution (with a few small modifications.
  • In other cases, youll need to develop a solution
    from scratch.
  • The solution will need to be step-by-step and
    mechanical.
  • The computer is incredibly stupid.
  • You must reduce the algorithm to the computers
    level.

10
Algorithm 1
  • Start at the beginning of the input table
  • Cross off birthdates as we progress
  • Each time we cross off a birthdate, increment the
    corresponding entry in the frequency table
  • Once weve crossed off all the birthdates, then
    our frequency table tells us exactly how many
    students have each birthday.
  • Ooops! We need to ensure that the entries in the
    frequency table are initially zero.
  • OK, now all w need to do is find the maximum in
    the frequency table.

11
Algorithm 1 Cont.
  • To find the maximum, well again start at the
    beginning and cross off entries.
  • We need some additional intermediate
    information. Well have two scalars
  • most_popular
  • how_popular
  • Each time we cross off an entry, well compare
    the value to how_popular. If the next entry is
    greater, well change how_popular to that value.
    Well also set most_popular to the date
    corresponding to the entry.

12
Lessons
  • An algorithm is extremely specific, with every
    step described precisely.
  • No And then a miracle occurs
  • We will need to go back and forth among the
    steps.
  • Frequently, we dont know all the intermediate
    information until were done with the algorithm.
  • Sometimes, we wont recognize all the special
    cases for input or output until were halfway
    done with the algorithm.

13
More Lessons Decomposition
  • Algorithm 1 is really two algorithms
  • Histogram (build the frequency table)
  • Find Max (select most popular birthdate).
  • Decomposition can be either top down or bottom
    up.
  • Top down means that you were certain that if
    you could solve each small problem, the big
    problem would be solved (but youre not sure yet
    you can solve these small problems).
  • Bottom up means that you know you can solve at
    least this one small problem (and hope that turns
    out to be useful).

14
More Lessons Focus on Information
  • The information is at the center of everything in
    programming
  • Data types (int, float)
  • Data structures (tables, sets, queues, maps,
    etc.)
  • As you identify the information in your problem
    (input, output or intermediate).
  • First determine quantity (e.g., scalar or table).
  • Then determine type (e.g., integer or birthdate)
  • Assign a variable name be sure to always have
    precise meaning for each variable!
  • Determine an initial value for all variables.

15
Algorithm 2 Top Down Decomposition
  • Small problem 1 Take input and sort it.
  • I dont know how to do this, but maybe a miracle
    will occur and Ill figure it out later.
  • Small problem 2 Count consecutive birthdates
    and remember which birthdate appeared the most
    times consecutively.
  • This step should give me the correct answer,
    provided the input was sorted.

16
Small Problem 2
  • Start at the beginning of (sorted) input. Cross
    off each date in sequence.
  • Create four intermediate scalars
  • most_popular same as before
  • how_popular same as before
  • current_birthdate the birthdate that we crossed
    off last time. Initialized to December 32nd.
  • current_frequency the number of times weve
    crossed off the same birthdate in a row.

17
Small Problem 2 Cont.
  • Each time we cross of the same birthdate,
    increment current_frequency.
  • Each time we cross off a new birthdate
  • first, compare current_frequency to how_popular.
  • if current_frequency is higher, update
    how_popular and most_popular.
  • then, set current_frequency to zero, set
    current_birthdate to the new birthdate.

18
Small Problem 1, Sorting
  • Well have to wait until April for that miracle.
  • Lessons
  • Sometimes, top down decomposition can lead us to
    a dead end. We just dont know how to solve the
    corresponding small problems.
  • In other cases (like this one), one of our small
    problems may be a classic, well-defined problem,
    and we can use a program that someone else has
    already written. (e.g., enter all the input into
    Excel, and use the sort function).

19
Comparing Algorithm 1 and 2
  • Algorithm 2 used less memory (we replaced the
    entire frequency table with two scalars).
  • Algorithm 1 is almost certainly going to be
    faster!
  • Sorting, even though its easy to understand, is
    relatively slow.

20
Algorithm 3
  • Left as an exercise for the student.
  • Combine the two steps from Algorithm 1.
  • We dont really need the whole frequency table,
    all we really want to know is whats the max
    frequency.
  • We ought to be able to find a way to solve this
    problem without sorting and without using a whole
    table of intermediate information.
  • Left as an exercise for the TA
  • Based on all your experience, what is the best
    algorithm for this problem. Note that you may
    not know a priori how sparsely distributed the
    birthdates will be.
Write a Comment
User Comments (0)
About PowerShow.com