You are on page 1of 57

Problem Solving

Problem Solving in Daily Life


People make decisions every day to
solve problems that affect their lives
Problems may be as unimportant as
what to watch on television or as
important as choosing a new profession
If a bad decision is made, time and
resources are wasted, so its important
that people know how to make
decisions well
People solve problems daily at home,
or work, or wherever they go.

Problem Solving in Daily Life


Problems at home include such things as
what to cook for dinner, which movie to see
this evening, which car to buy, or how to
sell the house.
At work, the problems might involve
dealing with fellow employees, work
policies, management, or customers.
The better the decisions an employee can
make, the more valuable that person will
be to the company.
In each case, the six steps in problem
solving can be followed

Steps of Problem Solving


There are six steps in problem solving
Step 1 - Identify the problem
Classroom situation - Most problems
have been identified for you and given
to you in the form of written
assignments or problems out of a book.
Outside classroom Need to identify
the problem

Step 2
Understand the problem
- Must
understand what is involved in the
problem before you can continue
toward the solution
If you are setting up a solution for a
person, then you must know what that
person knows.
For example, to tell someone how to
find a restaurant in your city if he has a
limited knowledge of the city you can
only use the information known to him

Step 2
Knowing the knowledge base is very
important since you cannot use any
instructions outside this base
Must also know knowledge base of the
problem solver
Cannot solve a problem if the problem
solver do not know the subject
For example, to solve a problem
involving calculus, you must know
calculus; to solve a problem involving
accounting, you must know accounting.

Step 2
In the case of computer problems,
understanding the problem involves
identifying
Input available for the problem
Output to determined
Process in involved in achieving the output
from the given input

Step 3

Identify alternative ways to solve the


problem.
List should be as complete as possible.
Sometimes may need to talk to other people
to find other solutions than those you have
identified.
Alternative solutions must be acceptable ones.
To travel from Mumbai to Chennai Alternative
ways to travel through
Land (Bus, Train, Car)
Air (Flight)
Sea (Ship)

Step 3
But sea seems to be infeasible solution
for this problem

Step 4
Select the best way to solve the
problem from the list of alternative
solutions.
Need to identify and evaluate pros and
cons of each possible solution before
selecting best one.
In order to do this, you need to select
criteria for the evaluation.
These criteria will serve as the
guidelines for evaluating each solution.

Step 5

List instructions that enable you to


solve the problem using the selected
solution.
These
numbered,
step-by-step
instructions must fall within the
knowledge base set up in step 2.
No instruction can be used unless the
individual
or
the
machine
can
understand it.
This can be very limiting, especially
when working with computers.

Step 6
Evaluate the solution.
To evaluate or test a solution means to check
its result to see if it is correct, and to see if it
satisfies the needs of the person(s) with the
problem.
When a person needs a piece of furniture to
sleep on, buying her a cot may be a correct
solution, but it may not be very satisfactory.
If
the
result
is
either
incorrect
or
unsatisfactory, then the problem solver must
review the list of instructions to see that they
are correct or start the process all over again.

Example Problem - What to do this evening

1. Identify the problem. How do the


individuals wish to spend the evening?
2. Understand the problem.
Knowledge base of the participants must
be considered.
Solutions selected are ones that everyone
involved would know how to do.
Probably would not select playing a game
of chess as a possible solution if the
participants did not know how to play

Example Problem - What to do this evening

3. Identify alternatives.
Watch television
Invite friends over
Play video games
Go to the movies
Play miniature golf
Go to the amusement park
Go to a friends party.

List is complete only when you can


think of no more alternatives.

Example Problem - What to do this evening


4. Select the best way to solve the problem.
Weed
out
alternatives
that
are
not
acceptable, such as those that cost too much
money or do not interest one of the
individuals involved.
Specify the pros and cons of each remaining
alternative.
Weigh the pros and cons to make the final
decision.
This solution will be the best alternative if all
the other steps were completed well.

Example Problem - What to do this evening

5. Prepare a list of steps (instructions)


that will result in a fun evening.
6. Evaluate the solution. Are we having
fun yet? If nobody is having fun, then
the planner needs to review the steps
to have a fun evening to see whether
anything can be changed, if not then
the process must start again.

Types of Problems
Some
problems
have
straightforward
solution and others do not
Some problems, such as balancing a
checkbook or baking a cake, can be solved
with a series of actions
These are algorithmic solutions.
Once the alternatives have been eliminated,
for example, and once one has chosen the
best among several methods of balancing
the checkbook, the solution can be reached
by completing the actions in steps.
These steps are called the algorithm.

Types of Problems
Solutions of other problems, such as how
to buy the best stock or whether to expand
the company, are not so straightforward.
These solutions require reasoning built on
knowledge and experience, and a process
of trial and error.
Solutions that cannot be reached through
a direct set of steps are called heuristic
solutions.
Problem solver can use the six steps for
both algorithmic and heuristic solutions.

Types of Problems
However, in step 6, evaluating the solution,
the correctness and appropriateness of
heuristic solutions are far less certain.
Its easy to tell if your completed checkbook
balance is correct and satisfactory, but its
hard to tell if you have bought the best
stock.
With heuristic solutions, the problem solver
will often need to follow the six steps more
than once, carefully evaluating each possible
solution before deciding which is best.

Problems to Discuss in Class


For each of the following tasks, write a set of
numbered, step-by-step instructions (a
solution) so complete that another person
could perform the task without asking
questions. Define the knowledge base of this
person by listing what you expect the person
to know in order to follow your directions. For
example, for task a (below), make a cup of
cocoa, the knowledge base might include
such things as knowledge of milk or water, a
refrigerator, pan, spoon, cocoa, cup, range
top or microwave, and so forth.

Problems to Discuss in Class


a. Make a cup of cocoa
b. Sharpen a pencil
c. Walk from the classroom to the
student lounge, your dorm, or the
cafeteria
d. Start a car (include directions
regarding what to do if the car doesnt
start)
e. Get a glass of water from your kitchen
f. Start your computer

More about Problem


Problem - puzzle that requires logical thought or
mathematics to solve
Puzzle - could be a set of questions on a
scenario which consists of description of
reality and set of constraints about
scenario
Scenario: VIT Chennai campus has a library.
The librarian issues books only to VIT
employees.
Description of reality : There is a library in VIT
chennai campus and there is a librarian in the
library.
Constraint : Librarian issues books only to VIT
employees

Case study - Discussion

Have you ever observed this scenario?


Yes!!! What are the problems in the

Examples of Computational Problem

Process of Computational
Problem Solving
Computational problem solving does not simply
involve the act of computer programming.
It is a process, with programming being only one
of the steps.
Before a program is written, a design for the
program must be developed.
And before a design can be developed, the
problem to be solved must be well understood.
Once written, the program must be thoroughly
tested

What Problem Can Be Solved By


Computer
When it is possible to produce solution of the
problem by a set of step-by-step procedures
or actions
This step-by-step action is called an
algorithm.
The algorithm will process some inputs and
produced output.
Solving problem by computer undergo two
phases:
Phase 1:
Organizing the problem or pre-programming phase.

Phase 2:
Programming phase.

Stuff to look at
Problem Solving Basics and Computer
Programming, by By Ronald A. Pasko
http://www.cs.iit.edu/~cs100/ProblemSolving.
pdf

Problem Description
Given the 3 dimensions of a box (length,
width, and height), multiply them together to
determine the volume

Understanding the Problem


Decompose the problem description

Eg. perform syntactic analysis on the

description in 4 steps
Identify all of the nouns in the sentence

Given the 3 dimensions of a box (length,


width, and height), calculate the volume.

Segregate Input - Output


Once these nouns are identified, they should
be grouped into one of two categories:

Input (items I either already know or am


getting from the user)

Output (items that I find out by manipulating


the input)

Eliminate redundant information


There may be some information in the problem

description that made it into our input/output chart


that we really dont need to solve the problem (that is,
not all of thenouns may be relevant)
Eliminate the most general item

Process
Identify all of the verbs in the sentence

Verbs in the problem specification identify

what actions your program will need to take


These actions, known as processing are the

steps between your input and your output

Processing for Output


Link you inputs, processes, and output

This step is as simple as drawing lines between the

relevant information in your chart. Your lines show what


inputs need to be processed to get the desired output
Take our length, width, and height and multiply them, to

give us our desired volume

PRE-PROGRAMMING PHASE
Analyzing the problem.
Developing the Hierarchy Input
Process Output (HIPO) chart or
Interactivity Chart (IC).
Developing the Input-ProcessOutput (IPO) Chart.
Drawing the Program flowcharts.
Writing the algorithms.

Analyzing the Problem


Identify the following:
Data requirement
Processing requirement or
procedures that will be needed to
solve the problem
The output

PAC for Payroll Problem


Calculate the salary of an employee who works
by hourly basis, formula to be used is Salary =
Hour works * Pay rate
Data
Hours work,
Pay rate

Processing
Salary =
Hours work *
payrate

Output
Salary

Solution
Alternatives
1. Define the
hours worked
and pay rate
as constants.
2. Define the
hours worked
and pay rate
as input
values.

PAC
to
Convert
Miles
to
Kms
Convert the distance in miles to
kilometers where 1.609 kilometers per
mile.

Data

Distance in
miles

Processing

Output

Solution
Alternatives

Kilometers =
1.609 x miles

Distance in
kilometers

1. Define the
miles as
constants.
2. Define the
miles as
input values.

Logic
Definition : A method of human thought
that involves thinking in a linear, step by
step manner about how a problem can be
solved
Logic is a language for reasoning. It is a
collection of rules we use when doing reasoning.
Example
John's mum has four children.
The first child is called April.
The second May.
The third June.
What is the name of the fourth child?

Importance of Logic in problem solving

Determine whether a given number is


primeProcess
or not? Outp Solution Alternatives
Data
ing
Numbe Check if
r, N
there is
a factor
for N

ut
Print
Prime
or Not
Prime

1. Divide N by numbers from


2 to N and if for all the
division
operations,
the
reminder is non zero, the
number is prime otherwise it
is not prime
2. Same as 1 but divide the N
from 2 to N/2
3. Same as Logic 1 but divide
N from 2 to square root of N

Importance of Logic in problem solving

In a fun game, MXM grid is given with full of coins. The


player has to give a number 'N' of his choice. If N is
lesser than M then he is out of game and doesn't gain
any points. Otherwise he has to place all coins in the
MXM grid in the NXN grid and he gains points equal to
the number of free cells in the N X N grid.
Data
Process Outp Solution Alternatives
ing
ut
Numbe If N is
Numb 1. Compute N2 - M2 as NXN
rs
M less than er of MXM
and N M
points
Points = gaine 2. Compute (N + M) X (N M)
d
0
(Number of multiplication is
Otherwis
reduced)
e
Comput

Problem Description
Write a problem analysis chart (PAC)
that asks a user to enter the distance
of a trip in miles, the miles per gallon
estimate for the users car, and the
average cost of a gallon of gas.
Calculate and display the number of
gallons of gas needed and the
estimated cost of the trip.

PAC for the Problem


Data

Processing

Output

distance,
miles per
gallon,
cost per
gallon

gas needed =
distance / miles
per gallon.

Display gas
needed

estimated cost =
cost per gallon x
gas needed

Display
estimated cost

Hierarchy Input Process Output


(HIPO) or Interactivity Chart
Developed when the problem is normally
big and complex
Processing can be divided into subtasks
called modules
These modules are connected to each
other to show the interaction of processing
between the modules
Main/control module controls the flow all
other modules.
The IC is developed using top-downmethod: top to down left to right order

HIPO
Interaction will form a hierarchy,
called Hierarchy Input Process Output
Chart (HIPO) or Interactivity Chart
(IC).
Main
Module

Module 1

Module 4

Module 2

Module 3

Module 5

Module 6

HIPO for Payroll


PAYROLL

READ

CALCULA
TE

PRINT

Extended Payroll Problem


You are required to write a program
to calculate both the gross pay and
the net pay of every employee of
your company. To determine the
gross pay, you have to multiply the
accumulated total hours worked by
the employee, by the appropriate
pay rate. The program should print
the cheque that tells the total net
pay. The net pay is calculated by
subtracting the gross pay with any
deductions that may be incurred by

HIPO for Extended Payroll


Problem
Payroll

Calculate
Gross Pay

Accumulate
Hourly
Worked

Determine
Pay rate

Calculate
Net Pay

Calculate
Deduction
s

Write
Cheque

Cost Estimation Problem


Write a PAC and Hierarchy Input
Process Output (HIPO) that asks a
user to enter the distance of a trip in
miles, the miles per gallon estimate
for the users car, and the average
cost of a gallon of gas. Calculate and
display the number of gallons of gas
needed and the estimated cost of the
trip.

PAC for Cost Estimation


Problem
Input
- Distance in
miles
- Miles per
gallon
- Cost gas per
gallon

Processing
- Enter distance
- Enter miles per gallon
- Calculate total gas
needed
- Enter cost gas per gallon
- Calculate estimated cost
- Display total gas and
estimated cost
- End

Output
-Total gas
needed
- Estimated
cost

Calculate gas

Estimation

Life Cycle of Example Problem


Determine whether a given number is
prime or not?

Identifying Input / Output of Problem

Input Data

Processing

Output

Number, N

Check if there is Print Prime or Not


a factor for N
Prime

Solution Alternatives
1. Divide N by numbers from 2 to N and if for
all the division operations, the reminder is non
zero, the number is prime otherwise it is not
prime
2. Same as 1 but divide the N from 2 to N/2
3. Same as Logic 1 but divide N from 2 to
square root of N

Flow Chart

Implementation
n = int(input())
x = n // 2
# For some y > 1
counter = 2
while counter <= x:
if n % counter == 0:
#
Remainder
print(The given number
is Composite)
break # Skip else
counter += 1
else:
# Normal exit
print(The given number is prime')

Debugging
Correct Syntactical / logical errors

Testing
Generate Test cases with boundary
conditions:
A prime number Eg: 19
A composite number Eg: 24
Boundary Conditions: 0,1, 2.3
For 1 Print the number is neither prime nor
composite
For 0 Invalid input
For 2.3 Invalid input