Problem Solving for Computer Science

advertisement
Getting Started with a Problem
• “Eighty percent of success is showing up.”
– Woody Allen
• “Success is 1% inspiration and 99% perspiration.”
– Thomas Edison.
• To successfully solve any problem, the most important
step is to get actively involved.
– The Principle of Intimate Engagement: You must commit to the
problem
– “Roll up your sleeves”
– “Get your hands dirty.”
1
Easy vs. Hard Problems
• Exercises: (e.g. compute 10! without a calculator)
• Easy problems: See the answer
• Medium problems: See the answer once you
engage
• Hard problems: You need strategies for coming
up with a potential solution, sometimes for even
getting started. Open-ended problems are
often like this. Often, multiple possible solutions,
you need a strategy to choose “the best” one.
(e.g. Estimate 2710! Why is this one hard? You can use a
calculator… )
http://en.wikipedia.org/wiki/Stirling%27s_approximation
2
Effective vs. Ineffective
Problem Solvers
Effective: Believe that problems can be solved through the
use of heuristics and careful persistent analysis
Ineffective: Believe ``You either know it or you don't.''
Effective: Active in the problem-solving process: draw
figures, make sketches, ask questions of themselves
and others.
Ineffective: Don't seem to understand the level of personal
effort needed to solve the problem.
Effective: Take great care to understand all the facts and
relationships accurately.
Ineffective: Make judgments without checking for accuracy
3
Mental Toughness
• Need the attributes of confidence and
concentration
– Confidence comes with practice
– Attack a new problem with an optimistic
attitude
• Unfortunately, it takes time
– You can’t turn it on and off at will
– Need to develop a life-long habit
4
Engagers vs. Dismissers
• Engagers typically have a history of success
with problem solving.
• Dismissers have a history of failure.
• You might be an engager for one type of
problem, and a dismisser for another.
• You can “intervene with yourself” to change
your attitude of dismissal
5
The Mental Block
• Many students do significant problem solving for
recreation
– Sodoku, computer games, recreational puzzles.
• These same students might dismiss math and
analytical computer science problems due to a
historical lack of success (the mental block)
And the other way around: dismissing verbal
problems.
• To be successful in life you will need to find
ways to get over any mental blocks you have
• Learn to transfer successful problem-solving
strategies from one part of your life to other
parts.
– Example: Writing is a lot like programming
6
Example Problem
• Connect each box with its same-letter
mate without letting the lines cross or
leaving the large box. (Actual problem used in software
company job interview)
B
A
C
C
B
A
7
Strategy (my favorite): solve a
simpler problem first.
B
A
C
C
A
C
B
A
8
Heuristic: Wishful Thinking
B
A
C
B
B
C
C
A
C
B
A
B
A
B
A
A
C
C
C
B
A
C
B
A
Engagement Example
• Cryptoarithmetic problem (base 10)
AD
+DI
--------DID
Need to start somewhere,
find a chink in the problem’s armor
10
The 9 coin problem
9 coins that look alike. One is
fake, can be heavier or lighter (not
known). Using a simple balance
scale and 3 weighings, single out
the fake one.
Hint: solve a simpler problem first. Which one?
The
solution
for
3
coins:
the weighings are:
1 against 2
1 against 3
Both of these can have three outcomes: fall to the left (l), fall to the
right (r), or balance (b). The following table gives the answer for each
of these outcomes:
outcome fake coin # why:
----------------------ll
1
too heavy
lb
2
too light
lr
(not possible)
bl
3
too light
bb
no fake coin
br
3
too heavy
rl
(not possible)
rb
2
too heavy
rr
1
too light
The solution for 9 coins:
Step 1. Divide 9 coins into 3 piles of 3 coins each.
Use the 3-coin
strategy to weigh:
pile 1 against pile 2
pile 1 against pile 3
From Step 1, you will determine: a) which pile contains the fake
and
b) if the fake is heavier or lighter.
Step 2: Weigh 2 coins from the pile that contains fake.
Total # of weighings: 2+1 = 3.
More: The 12 coins puzzle
the 9 coin interactive
Let’s get closer to the real world:
In “Gulliver’s Travels”, by Swift, Gulliver travels to distant parts of the Earth
where he meets liliputians who are mere 6 inch tall, and giants who are
72 feet tall. These people are made of the same kind of flesh, muscle
and bone; and look pretty much like like Gulliver who is 6 feet tall.
Let’s get closer to the real world:
In “Gulliver’s Travels”, by Swift, Gulliver travels to distant parts of the Earth
where he meets liliputians who are mere 6 inch tall, and giants who are
72 feet tall. These people are made of the same kind of flesh, muscle
and bone; and look pretty much like like Gulliver who is 6 feet tall.
Problem 1: It takes one full liliputian glass of their good and strong wine for
the liliputian to start feeling joyous. How many such glasses does Gulliver need
for the same effect?
Problem 2: How many liliputian suites needs to be cut and re-stitched to make
a suit to fit Gulliver?
Problem 3: According to Gulliver’s story, the giants could walk, run and jump
just like Gulliver. Why is this not really possible?
Key steps:
• Problem 1. The larger the volume of the person, the more wine.
Number of glasses ~ volume. Need to find ratio of volumes
Gulliver/Lilliputian.
• Key simplification for problems 1-2. Assume human to be a cube of
side a. Then V = a3. When a increased by 12, V=a3 goes up by 123.
• For #2, we need surface area, SA = 6a2. Gulliver’s area ~ 122 x
SA(liliputian).
a
• For problem #3 needs a more sophisticated simplification.
Body
Now, the mass of the body that the bone/leg/muscle has to support
Leg
is proportional to size a. While the strength of the bone/muscle is
proportional to its cross-sections, that is a2. As a grows, the ratio of
Mass to strength becomes smaller. So, the giants will crush their bones
if they tried to stand up. By the same argument, an ant can lift 40 times its own weight,
16
while a human can only lift about as much as his own weight only.
Chuck Norris vs. Bruce Lee
• http://www.youtube.com/watch?v=gpbXCj
2RqBw
17
Download