problem solving and algorithm design. 2 problem solving problem solving the act of finding a...

23
Problem Solving and Algorithm Design

Upload: juliet-baynard

Post on 31-Mar-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

Problem Solving and Algorithm Design

Page 2: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

2

Problem Solving

Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled question.

Page 3: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

3

Problem Solving

G. Polya wrote How to Solve It: A New Aspect of Mathematical Method

His “How to Solve It” list is quite general. – It’s written in the context of solving

mathematical problems.– With simple wording changes the list becomes

applicable to all types of problems.

Page 4: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

4

1 - Ask Questions...

…to understand the problem:

– What do I know about the problem?

– What is the information that I have to process in order the find the solution?

– What does the solution look like?

– What sort of special cases exist?

– How will I recognize that I have found the solution?

Page 5: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

5

2 - Look for Familiar Things

You should never “reinvent the wheel”.

In computing, you see certain problems again and again in different guises.

A good programmer sees a task, or perhaps part of a task (a subtask), that has been solved before and plugs in the solution.

Page 6: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

6

3 - Divide and Conquer

Break up a large problem into smaller units that can be handled more easily.

– Abstraction plays an important role.

– The divide-and-conquer approach can be applied over and over again until each subtask is manageable.

Page 7: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

7

5

Abstraction (n.) - A mental model that removes complex details.

This is a key concept. Abstraction will reappear throughout the course – be sure

you understand it!

Abstraction

Page 8: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

8

Algorithms

Algorithm:A set of instructions for solving a problem or subproblem in a finite amount of time using a finite amount of data.

The instructions must be unambiguous.

Page 9: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

9

Example of an Algorithm

Preparing a Hollandaise sauce

Page 10: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

10

Pseudocode

… uses a mixture of English and formatting to make the steps in the solution explicit.

Page 11: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

11

Developing an Algorithm

Since implementing the algorithm involves making it computer-readable, the plan must be in a suitable form.

The methodology (set of working methods) used to make the plan must begin with a problem statement and conclude with a plan that can be easily coded.

Page 12: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

12

Developing an Algorithm

• Two methodologies are currently used, both based on the divide-and-conquer strategy:

– Top-Down Design• (functional decomposition)

– Object-Oriented Design• (OOD)

Page 13: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

13

Top-Down Design

… consists of breaking the problem into a set of subproblems called modules.

It creates a hierarchical structure of problems and subproblems (modules).

This process continues for as many levels as it takes to expand every task to the smallest details.

A step that needs to be expanded is an abstract step.

One that doesn’t need expansion is a concrete step.

Page 14: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

14

Top-Down Design

Page 15: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

15

Object-Oriented Design

Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem.

An object is an exemplar of a class.

Page 16: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

16

Object-Oriented Design

A class defines the abstract characteristics of a thing (object), including it's properties and the things it can do, or methods.

One might say that a class is a blueprint that describes the nature of something.

Page 17: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

17

Object-Oriented Design

Assume a class called DOG, which defines all possible dogs by listing the characteristics and behaviours they have.

The object Lassie is one particular dog, with particular versions of the characteristics.A Dog has fur; Lassie has fur.

Page 18: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

18

Object-Oriented Design

Notice that fur is also an object.It has characteristics of its own. Lassie’s is long, and brown and white.

But fur is also a class!- a blueprint for anything that can be called fur.

There is no heirarchical organisation of the Object Oriented concepts.

Page 19: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

19

Object-Oriented Design

Programming techniques include features such as:• information hiding,

• data abstraction,

• encapsulation,

• modularity,

• polymorphism,

• and inheritance.

We will see examples of many of these in the exercises.

Page 20: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

20

Testing the Algorithm

Regardless of the design methodology, the algorithm itself must be tested.

Testing at the algorithm development phase involves looking at each part of the design independently, and at how they are coupled.

Page 21: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

21

Testing the Algorithm

Large scale programming is conducted in teams. Divide-and-Conquer strategies makes it possible to assign subtasks to team members, who independently develop solutions.

In this environment, design testing is often done in team meetings, using the method of Inspection.

Inspection: One person (not the designer) reads the design (handed out in advance) line by line while the others point out problems.

Page 22: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

22

Testing the Algorithm

Modern languages have been designed to reduce the time it takes use them, but any powerful language requires significant time to become an effective programmer.

VB allows us to use a prototype approach –to try code segments and see how they work, without a lot of Desk Checking of algorithms.

Desk checking: Working through a design at a desk with a pencil and paper.

Page 23: Problem Solving and Algorithm Design. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled

23

Testing the Algorithm

Unfortnately, our prototypes don’t always work the first time. So even though it’s easy to get started, we often need to test our designs to find their errors.

A useful technique here is the Walk-through.

Walk-through: Manual simulation of the design, taking sample data values and tracing them through its steps to see what actually happens.