CITS1401 Problem Solving and Programming
description
Transcript of CITS1401 Problem Solving and Programming
![Page 1: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/1.jpg)
CITS1401 Problem Solving and Programming
Problem-solving Techniques
Semester 1, 2014
A/Prof Lyndon WhileSchool of Computer Science & Software Engineering
The University of Western Australia
![Page 2: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/2.jpg)
CITS1401 problem-solving techniques 2
CITS1401
• CITS1401 covers– many important problem-solving techniques used
widely in Computer Science and in programming– writing basic programs in Python, a modern
high-level programming language– an introduction to software engineering
• Problem-solving techniques have been covered mostly via lab work– in this lecture we will review the techniques covered
![Page 3: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/3.jpg)
CITS1401 problem-solving techniques 3
Techniques discussed
• Morphological analysis and testing• Reduction and analogy• Enumeration and search• Abstraction• Divide-and-conquer• Backtracking
![Page 4: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/4.jpg)
CITS1401 problem-solving techniques 4
Morphological analysis and testing
• Lab 2 • “morphology” is the study of patterns or forms
– occurs widely in many branches of science • In CS/SE/IT, it occurs mostly in two contexts
– classification of inputs for processing – classification of inputs for testing
![Page 5: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/5.jpg)
CITS1401 problem-solving techniques 5
Classification of inputs for processing
• A term in a polynomial is defined by its coefficient and its exponent– what if we want to turn the term into a string?
• In the “easy case”:
(–6, 4) → “–6x^4”
![Page 6: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/6.jpg)
CITS1401 problem-solving techniques 6
Classification of inputs for processing
• But what if the coefficient == 0? (0, 4) → “0” > 0? (6, 4) → “6x^4”== 1? (1, 4) → “x^4”== –1? (–1, 4) → “–x^4”
• And what if the exponent == 0? (–6, 0) → “–6”== 1? (–6, 1) → “–6x”< 0? (–6, –4) → “–6/x^4”== –1? (–6, –1) → “–6/x”
![Page 7: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/7.jpg)
CITS1401 problem-solving techniques 7
Classification of inputs for testing
• In optional preferential voting, the voter can rank any subset of the candidates, from one of them up to all of them– so any sequence of non-repeating integers
increasing from 1 is a valid vote • If we write a function to parse OPV votes,
what would be a good set of test data? • Assume an election with three candidates
– a vote is represented by a string containing three characters
![Page 8: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/8.jpg)
CITS1401 problem-solving techniques 8
Testing example: classifying votes
• Intentionally ranking all three candidates: – i.e. permutations of 123 – 6 of these– 123, 132, 213, 231, 312, 321
• Intentionally ranking only two candidates: – i.e. permutations of 12<sp> – 6 of these – 12<sp>, 1<sp>2, <sp>12, 21<sp>, 2<sp>1, <sp>21
![Page 9: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/9.jpg)
CITS1401 problem-solving techniques 9
Testing example: classifying votes contd.
• Intentionally ranking only one candidate: – i.e. a 1 with two spaces– 3 of these – 1<sp><sp>, <sp>1<sp>, <sp><sp>1
• Accidentally ranking only two candidates: – i.e. some non-space instead of the 3
• permutations of 124? – 6 of these – 124, 142, 214, 241, 412, 421
![Page 10: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/10.jpg)
CITS1401 problem-solving techniques 10
Testing example: classifying votes contd.
• Accidentally ranking only one candidate: – omitting the 2
• 6 permutations of 134?– duplicating the 2
• 3 permutations of 122 • Accidentally ranking no candidates:
– omitting the 1• 6 permutations of 234?
– replicating the 1• 4 possibilities: 11<sp>, 1<sp>1, <sp>11, 111
![Page 11: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/11.jpg)
CITS1401 problem-solving techniques 11
Testing example: classifying votes contd.
• All spaces: – <sp><sp><sp>
• Over-length: – anything with more than three characters
• Under-length:– anything with fewer than three characters
• Already we have 43 test cases! • Every time we change the function,
we should re-run all tests– clearly we need a testing program!
![Page 12: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/12.jpg)
CITS1401 problem-solving techniques 12
Reduction and analogy
• Lab 3• Reduction is solving a new problem
by converting it into another problem for which we already have a solution – e.g. the problem of finding your way around
an unknown city can be reduced to the problem of finding a map of the city• assuming you can read maps!
• That problem can be reduced to the problem of finding a shop that sells maps– which can be reduced to the problem of
reading the information at the airport…
![Page 13: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/13.jpg)
CITS1401 problem-solving techniques 13
Reduction example: building tables
• Assume that we have written a function buildEvenTable that works for even n
• Now we need to write the function buildTable that works for all n
• It would be huge mistake to duplicate the code • Instead define buildTable by reducing the problem
to buildEvenTable – plus stripDummy and some other logic
![Page 14: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/14.jpg)
CITS1401 problem-solving techniques 14
Reduction example: running programs
• Imagine we have a Python interpreter that can run while-loops
• Then someone says “Let’s add for-loops to Python” • We could change the interpreter
– but that might be a lot of work, especially if it was originally written by someone else
• Or we could use reduction– replace each for-loop with an equivalent while-loop
for k in range(n): <statements>
k = 0while k < n: <statements> k += 1
becomes
![Page 15: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/15.jpg)
• Then someone says “Let’s add list comprehensions to Python”
• Use reduction again: replace each list comprehension with an equivalent for-loop
• Note the hierarchical approach
CITS1401 problem-solving techniques 15
Reduction example: programs contd.
zs = [f(x) for k in range(n) if p(k)]
zs = []for k in range(n): if p(k): zs.append(f(x))
becomes
![Page 16: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/16.jpg)
CITS1401 problem-solving techniques 16
Enumeration and search
• Lab 4• Very simple idea: generate all possible solutions
to the problem, and then check each one to see if it’s correct/good
• Used widely in – cryptography – artificial intelligence – game playing
![Page 17: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/17.jpg)
CITS1401 problem-solving techniques 17
Enumeration example: verbal arithmetic
• SEND + MORE = MONEY– consistently replace each letter with a digit
from 0, 1, …, 9 so that the arithmetic is correct• By enumeration, we could create all 10 x 9 x … x 3
= 1,814,400 possible assignments of digits to letters, and check each one– notionally very easy – computationally very expensive
![Page 18: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/18.jpg)
CITS1401 problem-solving techniques 18
Enumeration issues
• Often “all possible solutions” is way too many! – especially if there’s an infinite number of them…
• Often advantageous to – rank potential solutions
• likely to find a correct/good one sooner – use known correct/good solutions to develop
new (improved) possibilities• e.g. hill-climbing algorithms or genetic algorithms
– randomness helps sometimes!
![Page 19: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/19.jpg)
CITS1401 problem-solving techniques 19
Enumeration example: cryptography
• You are given a coded English message that you know was derived using a substitution cipher– i.e. each letter in the original was consistently
replaced by a different letter• Using naïve enumeration gives 26 x 25 x … x 1 =
403,291,461,126,605,635,584,000,000 possibilities• So use tricks like:
– ‘e’ probably occurs very often (& ‘a’, ‘r’, ‘t’, ‘n’, etc.)– the sequence ‘jx’ probably never occurs (& ‘zq’, etc.)– most occurrences of ‘q’ will be followed by a ‘u’
![Page 20: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/20.jpg)
CITS1401 problem-solving techniques 20
Enumeration example: missionaries & cannibals
• On one side of a river are three missionaries, three cannibals, and a canoe that can carry one or two people– any time on either side of the river, if the number of
cannibals exceeds the number of missionaries, something unpleasant happens
• Can you come up with a sequence of canoe trips that gets everyone safely across the river?
• e.g. the first trip could be – M crosses: no! – MM cross: no! – C crosses: ok, but the next trip must be just him coming back– CC cross, or MC cross: maybe…
![Page 21: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/21.jpg)
CITS1401 problem-solving techniques 21
Enumeration example: missionaries & cannibals
• By enumeration, we could create all possible sequences– but we need to check for loops – and it’ll be a lot
• Instead just apply the rule “always maximise the number of people on the far bank”– leads almost directly to a solution! – these sorts of guidelines are called heuristics
![Page 22: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/22.jpg)
CITS1401 problem-solving techniques 22
Abstraction
• Lab 5• Abstraction means simplifying a problem
as much as possible before solving it • Examples of this principle include
– operate on models instead of the “real world” – ignore some details to focus on others – discretise space and/or time (and other dimensions) – prefer simple data reps. (e.g. integers vs. dates)
• Abstraction can lead to more-general solutions
![Page 23: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/23.jpg)
CITS1401 problem-solving techniques 23
Abstraction examples
• Graph problems – entities as nodes, connections as arcs – focus is on the topology
• Dates as integers – faster, simpler, and more flexible
• Database construction– store only relevant data – but of course “relevant” is context-dependent…
![Page 24: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/24.jpg)
CITS1401 problem-solving techniques 24
Abstraction
• Einstein’s Constraint: “Everything should be made as simple as possible, but not simpler.” – omit all details that don’t contribute to a solution – allows you to focus on the “important bits” – but don’t omit any important bits!
![Page 25: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/25.jpg)
CITS1401 problem-solving techniques 25
Divide-and-conquer
• Lab 6• Divide-and-conquer means:
– divide a problem instance into several smaller pieces – solve each of the pieces separately – combine their solutions to solve the original problem
• Very widely-used technique, especially for processing data structures
• Often leads to very efficient algorithms
![Page 26: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/26.jpg)
CITS1401 problem-solving techniques 26
Divide-and-conquer example: mergesort
• Given a list of n numbers – [8, 0, 3, 6, 1, 7, 4, 2, 9, 5]
• Split the list down the middle – [8, 0, 3, 6, 1] and [7, 4, 2, 9, 5]
• Separately sort these two lists – [0, 1, 3, 6, 8] and [2, 4, 5, 7, 9]
• Merge the two sorted lists – [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] – need only (repeatedly) compare the heads
![Page 27: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/27.jpg)
CITS1401 problem-solving techniques 27
Divide-and-conquer example: quicksort
• Given a list of n numbers – [8, 0, 3, 6, 1, 7, 4, 2, 9, 5]
• Choose a pivot (say 5) and partition the list – [0, 3, 1, 4, 2] and [8, 6, 7, 9] – elements smaller than the pivot in the first list
• Separately sort these two lists – [0, 1, 2, 3, 4] and [6, 7, 8, 9]
• Append the two sorted lists and re-insert the pivot – [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
![Page 28: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/28.jpg)
CITS1401 problem-solving techniques 28
Divide-and-conquer issues
• Dividing up the data equally gives the best performance – e.g. quicksort
• the best pivot leaves two lists with n/2 elements • the worst pivot leaves one list with n–1, plus [ ]
• Auxiliary operations should be cheap– e.g. merge, partition
• Larger base cases may improve performance• Sometimes identical sub-problems arise
![Page 29: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/29.jpg)
CITS1401 problem-solving techniques 29
Backtracking
• Lab 7• Backtracking is a major enhancement to
enumeration and search • Enumeration & search:
– generate all possible complete solutions, then check each one for correctness
• Backtracking: – build up partial solutions bit by bit,
checking for correctness at each stage
![Page 30: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/30.jpg)
CITS1401 problem-solving techniques 30
Backtracking example: verbal arithmetic
• SEND MORE + ----------- MONEY– consistently replace each letter with a digit
from 0, 1, …, 9 so that the arithmetic is correct
• e.g. D + E = Y– or D + E = Y + 10
• e.g. N + R = E– more generally, N + R [+ 1] = E [+ 10]
![Page 31: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/31.jpg)
CITS1401 problem-solving techniques 31
Verbal arithmetic with Enumeration & Search
• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 7, Y = 8: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 7, Y = 9: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 7, Y = 0: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 8, Y = 7: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 8, Y = 9: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 8, Y = 0: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 9, Y = 7: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 9, Y = 8: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 9, Y = 0: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 0, Y = 7: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 0, Y = 8: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 6, M = 0, Y = 9: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 7, M = 6, Y = 8: NO• D = 1, E = 2, Y = 3, N = 4, R = 5, S = 7, M = 6, Y = 9: NO• etc. • 1,814,400 possible solutions to check
![Page 32: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/32.jpg)
CITS1401 problem-solving techniques 32
Backtracking example: verbal arithmetic
• D = 1– E = 2
• Y = 0: NO – 2,520 possibilities discarded • Y = 9, 8, 7, 6, 5, 4: NO – 15,120 more discarded • Y = 3
– N = 4» R = 5: NO – 60 more discarded » etc.
– E = 3– etc.
• D = 2 • etc.
![Page 33: CITS1401 Problem Solving and Programming](https://reader033.fdocuments.in/reader033/viewer/2022061612/56816668550346895dd9ff6c/html5/thumbnails/33.jpg)
CITS1401 problem-solving techniques 33
Backtracking issues
• Design a representation that allows building, checking, and discarding of partial solutions
• Discard partial solutions as early as possible • Incorporate all available clues!