47
Becoming a Better Problem Solver: A CS Perspective Melvin Zhang melvinzhang.net http://www.slideshare.net/melvinzhang/ becoming-a-better-problem-solver-a-cs-perspective January 20, 2012

Becoming a better problem solver: a CS perspective

Embed Size (px)

DESCRIPTION

Slides for a talk given at NUS Hacker's Friday Hacks held on 20th Jan 2012

Citation preview

Page 1: Becoming a better problem solver: a CS perspective

Becoming a BetterProblem Solver:A CS Perspective

Melvin Zhangmelvinzhang.net

http://www.slideshare.net/melvinzhang/

becoming-a-better-problem-solver-a-cs-perspective

January 20, 2012

Page 2: Becoming a better problem solver: a CS perspective

Becoming a Better Problem Solver:A CS Perspective

Page 3: Becoming a better problem solver: a CS perspective

Outline

What is problem solving?

Strategies and tacticsGetting started (Strategies)Making progress (Tactics)

Summary

What I’m working on

Page 4: Becoming a better problem solver: a CS perspective

Polya’s Mouse

Page 5: Becoming a better problem solver: a CS perspective

Polya’s Mouse

A good problem solverdoesn’t give up easily, butdon’t keep banging yourhead on the same part ofthe wall.

The key is to vary eachattempt.

Page 6: Becoming a better problem solver: a CS perspective

References

Page 7: Becoming a better problem solver: a CS perspective

References

Page 8: Becoming a better problem solver: a CS perspective

What is problem solving?

Problem solving is the process of tacklingproblems in a systematic and rational way.

Page 9: Becoming a better problem solver: a CS perspective

Steps in problem solving

Understandingthe problem

Devising a plan

Carrying outthe plan

Looking back

Page 10: Becoming a better problem solver: a CS perspective

Outline

What is problem solving?

Strategies and tacticsGetting started (Strategies)Making progress (Tactics)

Summary

What I’m working on

Page 11: Becoming a better problem solver: a CS perspective

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.

Page 12: Becoming a better problem solver: a CS perspective

Outline

What is problem solving?

Strategies and tacticsGetting started (Strategies)Making progress (Tactics)

Summary

What I’m working on

Page 13: Becoming a better problem solver: a CS perspective

Strategy 1. Get your hands dirty

Page 14: Becoming a better problem solver: a CS perspective

Example: Generating Gray codesNamed after Frank Gray, a researcher from BellLabs. Refers to a special type of binary code inwhich adjacent codes different at only one position.

3-bit binary code000001010011100101110111

Page 15: Becoming a better problem solver: a CS perspective

Example: Generating Gray codes

1-bit01

2-bit00011110

3-bit000001011010110111101100

Page 16: Becoming a better problem solver: a CS perspective

Applications of Gray codes

Figure: Rotary encoder for angle-measuring devices

I Used in position encoder (see figure).

I Labelling the axis of Karnaugh maps.

I Designing error correcting codes.

Page 17: Becoming a better problem solver: a CS perspective

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 Inverse

Original Given a set of object, find an objectsatisfying some property P .

Inverse Find all of the objects which does NOTsatisfy P .

Page 18: Becoming a better problem solver: a CS perspective

Example: Invitation

You want to invite thelargest group of friends,so that each person knowat least k others at theparty.

Page 19: Becoming a better problem solver: a CS perspective

Invitation

Direct approach

1. For each subset of friends, check if everyoneknows at least k others.

2. Return the largest set of friends.

Looking backWorks but there are potentially 2n subsets to check,where n is the number of friends.

Page 20: Becoming a better problem solver: a CS perspective

Invitation

Find the InverseInstead of finding the largest group to invite, findthe smallest group that is left out.

ObservationA person with less than k friends must be left out.

Page 21: Becoming a better problem solver: a CS perspective

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.

Page 22: Becoming a better problem solver: a CS perspective

Example: Largest rectangle

Find the largest white rectangle in an n × n grid.There is an easy solution which checks allrectangles. There are

(n2

)×(n2

)≈ n4 rectangles.

Page 23: Becoming a better problem solver: a CS perspective

Example: Largest rectangle

2D seems to be difficult, how about 1D?

There are(n2

)segments in a row, but we can find

the longest white segment using a single scan of therow. What is that so?

Page 24: Becoming a better problem solver: a CS perspective

Example: Next Gray code

Given an n-bit Gray code, find the next code.

3-bit Gray code000001011010110111101100

Page 25: Becoming a better problem solver: a CS perspective

Example: Next Gray code

Gray code is tough! What if we worked in binary?

Binary code

101

110

Gray code

111

101

Page 26: Becoming a better problem solver: a CS perspective

Outline

What is problem solving?

Strategies and tacticsGetting started (Strategies)Making progress (Tactics)

Summary

What I’m working on

Page 27: Becoming a better problem solver: a CS perspective

Making progress

Record your progressAny form of progress is good, record your workingsand keep track of interesting ideas/observations.

Sometimes, you mighthave to sleep on it.

Page 28: Becoming a better problem solver: a CS perspective

Story of RSA

Figure: Left to right: Adi Shamir, Ron Rivest, Len Adleman

Page 29: Becoming a better problem solver: a CS perspective

Tactic 1. Extremal principle

Given a choice, it is useful to consider items whichare extreme.

I Tallest/shortest

I Leftmost/rightmost

I Largest/smallest

Page 30: Becoming a better problem solver: a CS perspective

Example: Activity selection

Each bar represents an activity with a particularstart and end time. Find the largest set of activitieswith no overlap.

Page 31: Becoming a better problem solver: a CS perspective

Example: Activity selection

An intuitive approach is to repeatedly pick theleftmost activity.

Does this produce the largest set of activities?

Page 32: Becoming a better problem solver: a CS perspective

Example: Activity selection

This method may be fooled! Consider the following:

Page 33: Becoming a better problem solver: a CS perspective

Example: Activity selection

How would you normally pick among a set of tasks?Do the one with the earliest deadline first!

Page 34: Becoming a better problem solver: a CS perspective

Tactic 2. Exploit symmetry

Page 35: Becoming a better problem solver: a CS perspective

Example: Gray code to binary code3-bit Gray code 3-bit binary code

000 000001 001011 010010 011110 100111 101101 110100 111

Some observations:I The leftmost column is always the same.I After a column of ones, the order flips

(reflection).

Page 36: Becoming a better problem solver: a CS perspective

Example: Gray code to binary code

3-bit Gray code000001011010110111101100

Order 0 1 01 0 1

Gray code 1 1 0Binary code 1 0 0

Page 37: Becoming a better problem solver: a CS perspective

Tactic 3. Space-time tradeoff

Trading space for time: lookup tables, cachingTrading time for space: recalculation

Page 38: Becoming a better problem solver: a CS perspective

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,

I A[1, 3] = 6 + 4 + −3 = 7

I A[3, 7] = −3 + 0 + 5 + 1 + 8 = 11

Page 39: Becoming a better problem solver: a CS perspective

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 tradeoff 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

Page 40: Becoming a better problem solver: a CS perspective

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 ].

Page 41: Becoming a better problem solver: a CS perspective

Example: Computing segment sums

Looking back

I What is special about sum?

I Does this work with max/min? If not, can theidea be adapted?

Page 42: Becoming a better problem solver: a CS perspective

Outline

What is problem solving?

Strategies and tacticsGetting started (Strategies)Making progress (Tactics)

Summary

What I’m working on

Page 43: Becoming a better problem solver: a CS perspective

Strategies and tactics

Strategies

1. Get your hands dirty

2. Restate the problem

3. Wishful thinking

Tactics

1. Extremal principle

2. Exploit symmetry

3. Space-time tradeoff

Page 44: Becoming a better problem solver: a CS perspective

If there is a problem you can’t solve, thenthere is an easier problem you can solve:find it.

George Polya

Page 45: Becoming a better problem solver: a CS perspective

Outline

What is problem solving?

Strategies and tacticsGetting started (Strategies)Making progress (Tactics)

Summary

What I’m working on

Page 46: Becoming a better problem solver: a CS perspective

developer.hoiio.com

Page 47: Becoming a better problem solver: a CS perspective

magarena.googlecode.com