Title: CS1371 Introduction to Computing for Engineers
1CS1371Introduction to Computing for Engineers
- Problem Solving and Abstraction
2Problem Solving and Abstraction
- Solving Problems
- Taxonomy of Operations on Collections
- Working forward from the data
- Working backward from the answer
- Designing the Algorithm
- Implementing the Code
Learning Objectives Understanding how to solve
problems Understanding the value of modularity
3Problem Solving in General
- Problem solving is a fundamentally creative skill
- This skill is learned by practice solving
problems of different types. - Different types of problem require different
problem solving skills - Solving Physics problems frequently involves
identifying the appropriate principles and
formulas based on the given input data - Algebra problems require an algebraic
representation of the given information followed
by manipulating equations and axioms until the
answer is derived - Geometry problems are similar, but usually
involve graphical representations of the situation
4Problem Solving in Computer Science
- The end result of a computer science problem is a
program that produces the right behavior. - As with problem solving in general, there is a
wide spectrum of approaches to problem-solving
depending on the nature of the problem - Numeric computation (solving equations)
- User interaction (games, graphics)
- Modeling and simulation (Object-Oriented
Programming) more on this later
5Computation Problems
- This type of problem will present you with some
data and ask you to compute some result(s). - With simple problems, you may be able to find the
solution directly. - With more complex problems, you have to work in
two directions - Forward from the data, and
- Backwards from the answer
data
answer
6Possible Operations
- There are only 6 different operations you can
possibly perform on a collection of like data
items. - Each operation requires a function to specify how
that operation should be performed - Any algorithm can be constructed by applying
these operations in some order. - The operations are
- Build create the collection from some source
- Traverse touch each item and maybe summarize
- Map change the values in each item
- Filter remove some items from the collection
- Search determine whether a specific item exists
- Sort re-order the collection
7Data Collections
- There are at least 5 ways to collect data items.
- Not all operations make sense for all
collections. - The collection types are
- Vector 1-dimensional collection of identical
types accessed by index (can be ordered to
facilitate search) arrays are just vectors of
vectors - Structure tree of mixed types accessed by field
name - Linked List dynamic collection of identical
types accessed from the head - Tree dynamic collection of identical types
accessed from the root (can be ordered to
facilitate search) - Graph general assembly of data nodes and links
8Combining Operations and Collections
- The solution to any computational problem can be
viewed as a combination of one or more primitive
operations on one or more data types. - E.g. copying data from one form to another is a
combination of traversing the source and building
the destination - Some actual data may be organized as a
combination of the primitive data types - E.g. cell array of structures
9Total an example of Traverse
- Given any collection of data from which you can
derive some numerical value - Set the sum to zero
- Traverse the collection
- For each item in the collection, add its value
to the sum - Counting is a special case of totaling where the
value is 1 for each item. - Extracting the value might itself require
traversal of, or access to, an inner collection,
like a structure.
10Copy an example of Build and Traverse
- You are given a linked list of data items and
want to create a vector of data. - Traverse the list, counting the items
- Create an empty vector of that size
- Set the vector index to 1
- Traverse the list again
- For each item on the list,
- insert the item into the new vector
- Increase the index
11Map giving employees a raise
- Given a cell array of employee records, produce a
new cell array with revised salaries according to
some given formula - Traverse the cell array
- At each entry, replace the salary slot with the
new value.
12Filter removing students who withdraw
- Given a cell array of student records
- Traverse the array
- For each student,
- If the student matches the criterion for removal,
replace A(I) with
13Search find a CD
- Given a cell array of CD records
- Traverse the array.
- For each item
- Extract a CD
- If this matches the search criterion, return
true, or the record, or whatever
14Sort find the top ten
- Given a collection of arbitrary length containing
items you can compare, you want to know the ten
best. - For example, sales of specific albums last week.
- In general, you could sort this whole collection
and keep the ten at the top of the resulting
list. - Specifically, you could insert each in order into
an array of size 10, removing any item pushed off
the end of the array. - For a large number of items (gt 1000), this would
be significantly faster than the general case. - For less items, you wouldnt care about
efficiency anyway.
15Computation Problem Example
- Given the baseball statistics for a collection of
players, find the batting champion(s) for the
year must have more than 100 plate appearances.
data
answer
- Traverse
- Search
- Insert
- Sort
- Select on a criterion
- Find the max of a collection
- Sort a collection and take the top values
- Optimal theory
- Einsteins Theory
- Geometric mini-max
Collection of player names
Collection of player statistics
- Sort by batting average
- Keep those at the top with the same average
- Select those data with more than 100 plate
appearances - Discard those lt 100
16Draw a Picture, Work Examples
Sort by Avg
Sort by AB
17The Sub-Problems
- Sorting a collection of statistics
- Generalize the solution to sort by plate
appearances or batting average - May need utility function to extract data from a
statistics record - Discarding some entries in a collection based on
a threshold - Probably could use the same utility function to
decide on statistics to discard - Needs a second item to be logically correct
(i.e., consider multiple criteria)
18Emerging Abstraction
- We keep identifying things we could do, and leave
the details until later (a good definition of
Abstraction). - Making a collection of statistics
- Sorting a collection
- Choosing parts of that collection
- Extracting data from a statistics record
- We continue abstracting our components until we
can find code to do it, or can visualize a simple
program to do it. - Write the modules we have to write
- Test each component to ensure that it meets our
expectations. - Integrate the components to solve the problem
- Test the complete answer.
19Six Steps for Writing a Module
- State the problem clearly and concisely
- Gather the information
- Describe the input and output information
- Define a set of tests, including trivial cases
- Develop the theory
- Work the problem by hand for simple cases
- Write each step in English
- Generalize and simplify
- Code a solution by expanding the outline
- Evaluate the code for all the test cases.
20Example Removing an Item from a Collection
- This kind of module is well expressed as a flow
chart - Each element in the flowchart has a
characteristic shape which indicates its general
function - Each process entry may need further decomposition
before being coded
21User Interaction Problems
- Programs requiring user interaction usually have
a common structure expressed as a top-level flow
chart as follows
- Each process block in this chart may be a
significant computation sub-problem that must be
solved
22Example User Interaction Tic-Tac-Toe
Significant computation here
and here
and here
23Abstraction Lets you Isolate Decisions
- How will you store the board?
- 3 3 array?
- numeric or non-numeric?
- How will you show the board to the user?
- Text printout?
- Graphical picture?
- How will you retrieve user input?
- Text input?
- GUI buttons?
- What algorithm will decide if a player won?
- What algorithm for choosing the computers next
move?
24Goal of this Class
- Understand some techniques of problem solving
- Know some different collections of data and what
methods can be applied to those collections - Be able to design, implement and test these
methods - Be confident in your ability to integrate
sub-problem solutions into the solutions of
larger, more practical problems
25Questions?
26(No Transcript)