algorithms and problem solving-1 algorithms and problem solving

28
Algorithms and Problem Solving-1 Algorithms and Problem Solving

Post on 19-Dec-2015

267 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-1

Algorithms andProblem Solving

Page 2: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-2

Problem Solving

• Programming is a process of problem solving

• Problem solving techniques

– Analyze the problem

– Outline the problem requirements

– Design steps (algorithm) to solve the problem

• Algorithm:

– Step-by-step problem-solving process

– Solution achieved in finite amount of time

Page 3: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-3

Problem Solving Process

• Step 1 - Analyze the problem– Outline the problem and its requirements

– Design steps (algorithm) to solve the problem

• Step 2 - Implement the algorithm– Implement the algorithm in code

– Verify that the algorithm works

• Step 3 - Maintenance– Use and modify the program if the problem

domain changes

Page 4: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-4

Analyze the Problem

• Thoroughly understand the problem

• Understand problem requirements

– Does program require user interaction?

– Does program manipulate data?

– What is the output?

• If the problem is complex, divide it into subproblems

– Analyze each subproblem as above

Page 5: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-5

What is an algorithm?

• The idea behind the computer program• Stays the same independent of

– Which kind of hardware it is running on

– Which programming language it is written in

• Solves a well-specified problem in a general way• Is specified by

– Describing the set of instances (input) it must work on

– Describing the desired properties of the output

Page 6: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-6

Before a computer can perform a task, it musthave an algorithm that tells it what to do.

• Informally: “An algorithm is a set of steps that define how a task is performed.”

• Formally: “An algorithm is an ordered set of unambiguous executable steps, defining a terminating process.”– Ordered set of steps: structure!

– Executable steps: doable!

– Unambiguous steps: follow the directions!

– Terminating: must have an end!

Page 7: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-7

The definition of an algorithm

Page 8: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-8

Important Properties of Algorithms

• Correct– always returns the desired output for all legal

instances of the problem.• Unambiguous• Precise• Efficient

– Can be measured in terms of• Time• Space

– Time tends to be more important

Page 9: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-9

Algorithms can be represented indifferent ways:

• A single algorithm can be represented in many ways:– Formulas: F = (9/5)C + 32

– Words: Multiply the Celsius by 9/5 and add 32.

– Flow Charts.

– Pseudo-code.

• In each case, the algorithm stays the same; the implementation differs!

Page 10: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-10

A program is a representation of an algorithmdesigned for computer applications.

– Process: Activity of executing a program, or execute the algorithm represented by the program

Process: Activity of executing an algorithm.

Page 11: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-11

Expressing Algorithms

• English description

• Pseudo-code

• High-level programming language

More

preciseMore easily expressed

Page 12: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-12

Pseudocode is a notational system whereideas can be expressed in enough detail to berepresented as an algorithm.

• Pseudocode is like a programming language but its rules are less stringent.

• Written as a combination of English and programming constructs– Based on selection (if, switch) and iteration (while,

repeat) constructs in high-level programming languages

• Design using these high level primitives– Independent of actual programming language

Page 13: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-13

Example: The sequential search algorithm in pseudocode

Page 14: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-14

Algorithm discovery

• The Two Steps of Program Development:– 1. Discover the algorithm.

– 2. Represent the algorithm as a program.

• Step 2 is the easy step!• Step 1 can be very difficult!• To discover an algorithm is to solve the problem!

Page 15: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-15

Problem Solving is an art and a creativeprocess

• Problem solving techniques are not unique to Computer Science.

• The CS field has joined with other fields to try to solve problems better.

• Ideally, there should be an algorithm to find/develop algorithms.

• However, this is not the case as some problems do not have algorithmic solutions.

• Problem solving remains an art!

Page 16: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-16

Problem Solving Strategies

• Working backwards– Reverse-engineer– Once you know it can be done, it is much easier to do– What are some examples?

• Look for a related problem that has been solved before– Java design patterns– Sort a particular list such as: David, Alice, Carol and Bob

to find a general sorting algorithm • Stepwise Refinement

– Break the problem into several sub-problems– Solve each subproblem separately– Produces a modular structure

• K.I.S.S. = Keep It Simple Stupid!

Page 17: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-17

Stepwise Refinement

• Stepwise refinement is a top-down methodology in that it progresses from the general to the specific.

• Bottom-up methodologies progress from the specific to the general. – These approaches complement each other

• Solutions produced by stepwise refinement posses a natural modular structure - hence its popularity in algorithmic design.

Page 18: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-18

Object-Oriented Design Methodology• Four stages to the decomposition process

– Brainstorming

– Filtering

– Scenarios

– Responsibility algorithms

Page 19: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-19

Class-Responsibility-Collaboration (CRC) Cards

Page 20: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-20

Brainstorming

• A group problem-solving technique that involves the spontaneous contribution of ideas from all members of the group– All ideas are potential good ideas

– Think fast and furiously first, and ponder later

– A little humor can be a powerful force

• Brainstorming is designed to produce a list of candidate classes

Page 21: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-21

Filtering

• Determine which are the core classes in the problem solution

• There may be two classes in the list that have many common attributes and behaviors

• There may be classes that really don’t belong in the problem solution

Page 22: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-22

Scenarios

• Assign responsibilities to each class

• There are two types of responsibilities– What a class must know about itself (knowledge)

– What a class must be able to do (behavior)

• Encapsulation is the bundling of data and actions in such a way that the logical properties of the data and actions are separated from the implementation details

Page 23: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-23

Responsibility Algorithms

• The algorithms must be written for the responsibilities– Knowledge responsibilities usually just return the

contents of one of an object’s variables

– Action responsibilities are a little more complicated, often involving calculations

Page 24: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-24

Computer Example

• Let’s repeat the problem-solving process for creating an address list

• Brainstorming and filtering– Circling the nouns and underlining the verbs

Page 171

Page 25: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Computer Example

• First pass at a list of classes

Page 26: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Computer Example

• Filtered list

Page 27: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-27

CRC Cards

Page 28: Algorithms and Problem Solving-1 Algorithms and Problem Solving

Algorithms and Problem Solving-28

Responsibility Algorithms