8/9/2019 Better Problem Solver
1/44
Becoming a Better
Problem Solver:
A CS Perspective
Zhang Zhiyong Melvinhttp://www.comp.nus.edu.sg/~melvin
School of Computing,National University of Singapore
May 27, 2009
http://www.comp.nus.edu.sg/~melvinhttp://www.comp.nus.edu.sg/~melvinhttp://www.comp.nus.edu.sg/~melvinhttp://www.comp.nus.edu.sg/~melvin8/9/2019 Better Problem Solver
2/44
Becoming a Better Problem Solver:
A CS Perspective
8/9/2019 Better Problem Solver
3/44
Outline
What is problem solving?
Strategies and tacticsGetting started (Strategies)Making progress (Tactics)
Summary
8/9/2019 Better Problem Solver
4/44
Polyas Mouse
8/9/2019 Better Problem Solver
5/44
Polyas Mouse
A good problem solverdoesnt give up easily, butdont keep banging your
head on the same part ofthe wall.
The key is to vary eachattempt.
8/9/2019 Better Problem Solver
6/44
References
8/9/2019 Better Problem Solver
7/44
References
8/9/2019 Better Problem Solver
8/44
What is problem solving?
Problem solving is the process of tacklingproblems in a systematic and rational way.
8/9/2019 Better Problem Solver
9/44
Steps in problem solving
Understandingthe problem
Devising a plan
Carrying outthe plan
Looking back
8/9/2019 Better Problem Solver
10/44
Outline
What is problem solving?
Strategies and tacticsGetting started (Strategies)Making progress (Tactics)
Summary
8/9/2019 Better Problem Solver
11/44
Strategies, tactics and tools
StrategiesGeneral approaches and psychological hints forstarting and pursuing problems.
TacticsDiverse methods that work in many differentsettings.
ToolsNarrowly focused techniques and tricks forspecific situations.
8/9/2019 Better Problem Solver
12/44
Outline
What is problem solving?
Strategies and tacticsGetting started (Strategies)Making progress (Tactics)
Summary
8/9/2019 Better Problem Solver
13/44
Strategy 1. Get your hands dirty
8/9/2019 Better Problem Solver
14/44
Example: Generating Gray codesNamed after Frank Gray, a researcher from Bell
Labs. Refers to a special type of binary code inwhich adjacent codes different at only one position.
3-bit binary code
000001010011
100101110111
8/9/2019 Better Problem Solver
15/44
Example: Generating Gray codes
1-bit01
2-bit0001
1110
3-bit000001
011010110111101100
8/9/2019 Better Problem Solver
16/44
Applications of Gray codes
Figure: Rotary encoder for angle-measuring devices
Used in position encoder (see figure).
Labelling the axis of Karnaugh maps.
Designing error correcting codes.
8/9/2019 Better Problem Solver
17/44
Strategy 2. Restate the problem
The problem as it is stated may not have an obvioussolution. Try to restate the problem in a differentway.
Find the InverseOriginal Given a set of object, find an object
satisfying some property P.
Inverse Find all of the objects which does NOTsatisfy P.
8/9/2019 Better Problem Solver
18/44
Example: Invitation
You want to invite the
largest group of friends,so that each person knowat least k others at theparty.
8/9/2019 Better Problem Solver
19/44
Invitation
Direct approach
1. For each subset of friends, check if everyoneknows at least k others.
2. Return the largest set of friends.
Looking back
Works but there are potentially 2n subsets to check,where n is the number of friends.
I
8/9/2019 Better Problem Solver
20/44
Invitation
Find the InverseInstead of finding the largest group to invite, find
the smallest group that is left out.
ObservationA person with less than k friends must be left out.
S 3 Wi hf l hi ki
8/9/2019 Better Problem Solver
21/44
Strategy 3. Wishful thinking
Make the problem simpler by removing the sourceof difficulty!
1. Identify what makes the problem difficult.
2. Remove or reduce the difficulty.
E l L l
8/9/2019 Better Problem Solver
22/44
Example: Largest rectangle
Find the largest white rectangle in an n n grid.
There is an easy solution which checks allrectangles. There are
n
2
n
2
n4 rectangles.
E l L l
8/9/2019 Better Problem Solver
23/44
Example: Largest rectangle
2D seems to be difficult, how about 1D?
There aren
2
segments in a row, but we can find
the longest white segment using a single scan of the
row. What is that so?
E l N t G d
8/9/2019 Better Problem Solver
24/44
Example: Next Gray code
Given an n-bit Gray code, find the next code.
3-bit Gray code000001011010110
111101100
E l N t G d
8/9/2019 Better Problem Solver
25/44
Example: Next Gray code
Gray code is tough! What if we worked in binary?
Binary code
101
110
Gray code
111
101
O tli
8/9/2019 Better Problem Solver
26/44
Outline
What is problem solving?
Strategies and tacticsGetting started (Strategies)Making progress (Tactics)
Summary
Maki g og ess
8/9/2019 Better Problem Solver
27/44
Making progress
Record your progressAny form of progress is good, record your workingsand keep track of interesting ideas/observations.
Sometimes, you might
have to sleep on it.
Story of RSA
8/9/2019 Better Problem Solver
28/44
Story of RSA
Figure: Left to right: Adi Shamir, Ron Rivest, Len Adleman
Tactic 1 Extremal principle
8/9/2019 Better Problem Solver
29/44
Tactic 1. Extremal principle
Given a choice, it is useful to consider items whichare extreme.
Tallest/shortest Leftmost/rightmost
Largest/smallest
Example: Activity selection
8/9/2019 Better Problem Solver
30/44
Example: Activity selection
Each bar represents an activity with a particularstart and end time. Find the largest set of activitieswith no overlap.
Example: Activity selection
8/9/2019 Better Problem Solver
31/44
Example: Activity selection
An intuitive approach is to repeatedly pick theleftmost activity.
Does this produce the largest set of activities?
Example: Activity selection
8/9/2019 Better Problem Solver
32/44
Example: Activity selection
This method may be fooled! Consider the following:
Example: Activity selection
8/9/2019 Better Problem Solver
33/44
Example: Activity selection
How would you normally pick among a set of tasks?Do the one with the earliest deadline first!
Tactic 2 Symmetry
8/9/2019 Better Problem Solver
34/44
Tactic 2. Symmetry
Example: Gray code to binary code
8/9/2019 Better Problem Solver
35/44
Example: Gray code to binary code3-bit Gray code 3-bit binary code
000 000001 001011 010010 011
110 100111 101101 110100 111
Some observations: The leftmost column is always the same. After a column of ones, the order flips
(reflection).
Example: Gray code to binary code
8/9/2019 Better Problem Solver
36/44
Example: Gray code to binary code
3-bit Gray code000001
011010110111
101100
Order 0 1 0
1 0 1Gray code 1 1 0
Binary code 1 0 0
Tactic 3 Space/time trade-off
8/9/2019 Better Problem Solver
37/44
Tactic 3. Space/time trade off
Instead of always computing an answer from
scratch, it may be worthwhile to precompute somepartial results.
Example: Computing segment sums
8/9/2019 Better Problem Solver
38/44
Example: Computing segment sums
Given an array A of integers, compute the sum ofany segment A[i,j] efficiently.
6 4 -3 0 5 1 8 7
For example,
A[1, 3] = 6 + 4 +3 = 7
A[3, 7] = 3 + 0 + 5 + 1 + 8 = 11
Example: Computing segment sums
8/9/2019 Better Problem Solver
39/44
Example: Computing segment sums
Wishful thinking Computing for any segment A[i,
j]is difficult, what if we consider onlysegments of the form A[1,j]?
Space/time trade-off Sums for A[1,j] can beprecomputed and stored in a anotherarray P
A 6 4 -3 0 5 1 8 7P 6 10 7 7 12 13 24 31
Example: Computing segment sums
8/9/2019 Better Problem Solver
40/44
Example: Computing segment sums
A 6 4 -3 0 5 1 8 7P 6 10 7 7 12 13 24 31
ObservationThe sum for A[i,j] can be computed asP
[j
] P
[i] +
A[i].
Example: Computing segment sums
8/9/2019 Better Problem Solver
41/44
p p g g
Looking back
What is special about sum? Does this work with max/min? If not, can the
idea be adapted?
Outline
8/9/2019 Better Problem Solver
42/44
What is problem solving?
Strategies and tacticsGetting started (Strategies)Making progress (Tactics)
Summary
Strategies and tactics
8/9/2019 Better Problem Solver
43/44
g
Strategies
1. Get your hands dirty2. Restate the problem
3. Wishful thinking
Tactics
1. Extremal principle2. Symmetry
3. Space/time trade-off
Duality of Problem and Solution
8/9/2019 Better Problem Solver
44/44
y
If we really understand the problem, the
answer will come out of it, because the
answer is not separate from the problem.
J. Krishnamurti