Title: Structured Problem Solving
1StructuredProblem Solving
- Level C
- 2006-2007
- George Herterich
2Lets Tighten up a Little
- We are heading towards learning how to program a
computer. - Compooters are knott intellygent lyke wot we
rrrr. - Computer programming languages have strict rules
of grammar. - This set of rules constitutes the syntax of that
particular language
Pages 51 to 60
3Lets Tighten up a Little
- So far, we have laid down fairly strict rules
governing the programming constructs and how they
should be set out - We have given guidelines for choice of variable
names - All of the algorithms developed so far have been
fairly lax in the choice of phraseology
4Lets Tighten up a Little
- A bridge between algorithmic design and actual
computer programs which run correctly - Specify the rules
- Specify the syntax
- Specify the problem
- Test the solution
5This Will Certainly Help
6StacksA Specification
- The Environment
- The Rules
- The Syntax for the allowed operations
7The Environment
- A box containing a pile of books
- The number of books in the box is unknown
- (The box is labelled Stack1)
- A store of similar empty boxes labelled Stack2,
Stack3. - A tray which can hold 1 book only.
- (This is labelled Temp)
8The Rules
- A book may only be taken from the top of a Stack
- An attempt to take a book off an empty Stack is
illegal - A book can only be moved from a Stack to a Tray
or vice versa - A Stack will never become full
9The Syntax for the Allowed Operations
- Get Stackn
- Gets an empty box labelled Stackn from the store
of boxes - Pop(Stackn, Temp)
- Moves a book from the top of Stackn to the Tray
- Push(Temp, Stackn)
- Moves a book from the Tray to Stackn
10Specify the Problem
- Generate a Stack of books, labelled Stack2, which
are in the reverse order to those in Stack1
11Top Level Solution
Reverse(Stack1, Stack2) This could be a shorthand
way of specifying the problem Remember Stack1 is
given, but Stack2 is still stored away.
12A Better Solution Would Be
Get Stack2 Reverse(Stack1, Stack2)
13Reverse(Stack1, Stack2)
Let us do it manually using books and boxes or
the equivalent substitutes. This will give a feel
for any sequence, selection or repetition
14Next Level Solution
Repetition Move a book from Stack1 to Stack2 When
does this terminate ? When Stack1 is
empty (remember Stacks never become full) What is
the opposite of this When Stack1 is not
empty (Can be written as Stack1 not empty)
15The Algorithm So Far
Get Stack2 while Stack1 not empty begin move a
book from Stack1 to Stack2 end
16move a book from Stack1 to Stack2
We must follow the rules of the game. How did we
do it manually? Stack1 Tray Stack2
Pop
Push
17The Solution
Get Stack2 while Stack1 not empty begin
Pop(Stack1, Temp) Push(Temp, Stack2) end
Test the solution This can be done manually
18Next Problem
- Given
- A store of empty boxes, labelled Stack3, Stack4
- Stack1 and Stack2, each containing an unknown
number of books - A tray labelled Temp
- Rules
- As before
- Problem Specification
- Generate a Stack of books, labelled Stack3,
comprising the books from Stack1 merged with the
books from Stack2
19Do You Understand the Problem ?
20Understanding the Problem
Before the Merge
21Understanding the Problem
After the Merge
22Top Level Solution
Obvious from previous example
Get Stack3 Merge(Stack1, Stack2, Stack3)
Possible way of writing problem
23Top Level Solution
Get Stack3 No more refinement
needed Merge(Stack1, Stack2, Stack3) Try it
manually as before It seems to be a repetition of
Stack1
First
Second
Tray
Stack3
Stack2
Third
24A Repetition of What ?
A repetition of Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3)
25We Now Have
Get Stack3 while not a clue yet but am working on
it begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
26What is the Problem ?
The major problem is that the initial states of
the 2 Stacks are unknown. Both may be empty. One
may be empty. One may contain 99 books and the
other 46 books etc. We want the algorithm to work
for any two Stacks
27Let us Focus on the while Test
When do we stop merging the 2 stacks ? When any
further action would be illegal. What action is
defined as illegal ? An attempt to take a book
off an empty stack. Solution Stop merging as
soon as one stack becomes empty Then what? Fill
up stack3 with the remaining books
28Seems EasyBut What is the Problem ?
Unknown starting states, as before. Let us look
at some possible solutions and test them.
29Solution A
Get Stack3 while EITHER Stack1 OR Stack2 is not
empty begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
Test this solution for 2 or 3 different pairs of
stacks of different sizes. Revise the OR truth
table
30Solution B
Get Stack3 while BOTH Stack1 AND Stack2 are not
empty begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
Test this solution for 2 or 3 different pairs of
stacks of different sizes. Revise the AND truth
table
31Solution C
Get Stack3 while BOTH Stack1 AND Stack2 are not
empty begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
At this stage, one of the stacks is empty, but
we do not know which !!!!
32Solution C Continued
while Stack1 not empty begin Pop(Stack1,
Temp) Push(Temp, Stack3) end while Stack2 not
empty begin Pop(Stack2, Temp) Push(Temp,
Stack3) end
This will empty Stack1 if it is not empty. If it
is already empty, this section of code is
leapfrogged over.
This will empty Stack2 if it is not empty. If it
is already empty, this section of code is
leapfrogged over.
33We have certainly merged the two Stacks, but the
resulting Stack, Stack3, does not quite fit the
problem specification. Why Not ???
34Very Nearly There
We have merged the two stacks, but at the moment
Stack3 is upside down. It needs reversing. The
solution is at hand. We have already created a
procedure reverse. The syntax we chose was
reverse(Stacka, Stackb) We now have a procedure
merge which merges two stacks, but produces the
correct result in the reverse order. The syntax
for this was merge(Stackp, Stackq, Stackr)
35At LastThe Solution
Get Stack3 Get Stack4 merge(Stack1, Stack2,
Stack4) reverse(Stack4, Stack3)
Procedure Calls
Parameters
36Question
Using procedure calls and appropriate parameters,
write down the solution to the following
problem. Given 3 stacks, Stackx, Stacky and
Stackz containing an unknown number of
books. Problem Merge Stackx with Stacky so that
the contents of the resulting Stack, Stackw, are
in the correct order. Merge Stackw with Stackz so
that the contents of the resulting stack, Stackn,
are in the correct order.
37Solution
Get Stackw Get Stackm Get Stackn merge(Stackx,
Stacky, Stackn) reverse(Stackn,
Stackw) merge(Stackw, Stackz, Stackm) reverse(
Stackm, Stackn)
38This is a valid solution which involves
generating (getting) 3 empty Stacks (Stackw,
Stackm Stackn). There is a better solution
which only generates 2 empty Stacks. Have a go !
39Get Stackw Get Stackn merge(Stackx, Stacky,
Stackn) Stackx, Stacky now empty reverse(Stackn,
Stackw) Stackn now empty merge(Stackw, Stackz,
Stackx) reverse( Stackx, Stackn)