代写COSC 1107/1105 Computing Theory代做Java语言

Computing Theory
COSC 1107/1105
Sample Exercise 2 Answers
1 Assessment details
1. Consider the grammar derivations below.
S ⇒ aSb⇒ aaSbb⇒ 
aacSdbb⇒ aacdbb
S ⇒ A⇒ xAy ⇒ xxAyy ⇒ xxB@yy ⇒ xxxB@yy ⇒ xxxx@yy
S ⇒ A⇒ @C ⇒ @Cy ⇒ @Cyy ⇒ @yyy
(a) From the above derivations, construct rules that must exist in any context-free grammar G
for which these derivations are correct.
Answer: From the first derivation we can see that the rules must include S → aSb,
S → cSd and S → λ. From the second derivation we can add the rules S → A, A → xAy,
A → B@, B → xB and B → x. From the third derivation we can add the rules A → @C,
C → Cy and C → y.
As this covers all the derivation steps in all three derivations above, we get the rules below.
S → aSb | cSd | A | λ
A→ xAy | B@ | @C
B → xB | x
C → Cy | y
(b) Assuming that these are all the rules in G, give L(G) in set notation.
Answer: L(G) = {wxi@yj(bd(w))Rorw(bd(w))R | i 6= j, i, j ≥ 0, w ∈ {a, c}∗} where bd(w)
is w with all a’s replaced by b’s and all c’c replaced by d’s, and wR is the reverse of w.
This may seem a little complicated but consider a string like aabax@yycdcc. If we replaced
all d’s in the grammar with c’s and b’s with a’s, then the language would be {wxi@yjwR|i 6=
j, i, j ≥ 0, w1 ∈ {a, c}∗}. So all we need to do to get the language of the original grammar is
replace the a’s and c’s in wR with b’s and d’s respectively.
Another point to note is that this language is not the same as the one below.
{w1xi@yjw2 | i 6= j, i, j ≥ 0, w1 ∈ {a, c}∗, w2 ∈ {b, d}∗, |w1| = |w2|}
Note that this latter language contains strings such as aabxx@yddd, which cannot be derived
by the grammar.
where na(w) is the number of a’s in w, and similarly for b, c and d.
(c) Is there a regular grammar for L(G)? Explain your answer.
Answer: There is no regular grammar for L(G). This language is context-free but not
regular because there is a need to count the number of x’s and y’s to make sure that they
are different, as well as counting the number of a’s or c’s and making sure there is an equal
number of b’s or d’s.
(d) Construct a context-free grammar for the language below.
L = {xi w1@w2 yj | i 6= 2j, i, j ≥ 0, |w1| = |w2|, w1 ∈ {a, c}∗, w2 ∈ {b, d}∗}
Answer: It is best to split this up into two cases and then combine the two grammars. So
let L = L1 ∪ L2 where
L1 = {xi w1@w2 yj | i < 2j, i, j ≥ 0, |w1| = |w2|, w1 ∈ {a, c}∗, w2 ∈ {b, d}∗}
and L2 = {xi w1@w2 yj | i > 2j, i, j ≥ 0, |w1| = |w2|, w1 ∈ {a, c}∗, w2 ∈ {b, d}∗}
For a constraint like i < 2j it can be helpful to use a table like the one below.
i j 2j
0 1 2
1 1 2
2 2 4
3 2 4
4 3 6
5 3 6
So for L1 we get G1 below.
S → xxSy | XA
A→ Ay | By
X → x | λ
B → aBb | aBd | cBb | cBd | @
For L2 with the constraint i > 2j the corresponding table is below.
i j 2j
1 0 0
3 1 2
5 2 4
7 3 6
This leads us to the grammar G2 below.
S → xxSy | C
C → xC | xD
D → aDb| | aDd | cDb | cDd | @
We can then combine these together for a grammar G for L = L1 ∪ L2.
S → T | U
T → xxTy | XA
A→ Ay | By
X → x | λ
B → aBb| | aBd | cBb | cBd | @ U → xxUy | C
C → Cy | Dy
D → aDb | aDd | cDb | cDd | @
There are ways this could be simplified, but that is not required. Constructing the grammar
this way gives confidence in its correctness, which is less obvious otherwise.
2. Drogo the Dreary, a distant relative of Thorin Oakenshield, has written the following discussion
of intractability. There are 5 incorrect statements in the paragraph below. Identify all 5 incorrect
statements and justify each of your answers.
“There are a number of problems which can be solved in principle, but in practice can be very
difficult to solve. These problems are often referred to as NP-complete problems, and include
the Travelling Salesperson problem, 3-SAT, factorisation and vertex cover. These problems are
certainly intractable, i.e. all algorithms for these problems have exponential running times. This
means that they can be solved for small instances, but the rate of growth of their complexity is so
fast that they cannot be solved in practice for any reasonable size. For example, the best known
algorithm for the Travelling Salesperson problem can take up to 2n10 +7n2 operations for a graph
of size n. This means it is in the class O(n10) and is thus intractable. Fortunately it is possible to
use approximation and heuristic algorithms to find some kind of solutions to these problems, either
by removing the guarantee that an optimal solution will be found, or by removing the constraint
that the running time will be polynomial or less (or removing both). There are also some similar
problems, such as the Hamiltonian circuit problem, which are known to be simpler to solve than
the Travelling Salesperson problem and are tractable. The name NP-complete problems comes
from the property that such problems have run in at most polynomial-time on a nondeterministic
Turing machine.”
Answer:
(a) Factorisation is probably intractable, but it is not known to be NP-complete. So it is incorrect
to list it as an NP-complete problem.
(b) NP-complete problems are almost certainly intractable, but it is incorrect to say that these
are certainly intractable.
(c) The best known algorithm for the Travelling Salesperson problem is exponential, and so the
statement of the running time here is certainly incorrect.
(d) Algorithms with a running time of O(n10) are in the polynomial class, and hence are con-
sidered tractable.
(e) The Hamiltonian Circuit problem is NP-complete, as is the Travelling Salesperson problem.
So these are either both intractable or both tractable, i.e. in the same complexity class.
3. The generalised Platypus game with Gandalf the White is played as follows (we will abbreviate
the name of this to GPGGW). There are three machines, with two being the usual platypus
machines (as in the generalised Platypus game from Assignment 2), with the third machine being
Gandalf the White (which we will abbreviate to GW ), which has the transition table as below.
For simplicity we assume all three machines have the same alphabet Σ. The tape is infinite in
both directions, and is initially blank.
q0 blank blank R q1
q0 X X R q1
q1 blank blank L q0
q1 X X L q0
where X denotes any non-blank symbol in Σ.
(a) Show that the halting problem for the GPGGW is undecidable. You may use any reduction
you like. Note that you may assume that the generalised Platypus problem from Assignment
2 is undecidable if you would find that helpful.
Answer: The simplest proof of this will be to reduce the generalised Platypus problem
from Assignment 2 (which you can assume is undecidable) to this problem. The key obser-
vation is that the Gandalf the White machine never changes any cell on the tape, and never
terminates. This means that the generalised Platypus game with Gandalf the White halts
iff the generalised Platypus game (from Assignment 2) halts. In terms of machines, consider
the diagram below. Note that the machine the GPGGW only needs the machines M1 and
M2 as input.
It is also possible to use a reduction from the blank tape problem to the GPGGW problem
as follows. Let M be the machine we want to analyse for the blank tape problem. Then
M will halt on the blank tape iff the generalised Platypus problem with Gandalf the White
halts for M and GW .
In terms of machines, consider the diagram below.
(b) Suppose the GPGGW is played on a Turing machine with a finite tape (making the halting
problem decidable), and also that there is a decidable problem A for which there is a reduction
from A to the GPGGW. This information could be used as an argument that the GPGGW
is NP-complete, provided that some further information is known. What further information
is needed? Explain your answer.
Answer: To show that a problem is NP-complete, we need to show that the problem is
in NP, and that the problem is NP-hard, i.e. that there is a polynomial-time reduction to it
from every other problem in NP. The simplest way to show the latter property is to find a
polynomial-time reduction from a known NP-complete problem to it.
So given the information above, we also need to know the following.
i. That GPGGW is in NP.
ii. That the problem A is NP-complete.
iii. That the reduction from A to the GPGGW is polynomial-time.
(c) Freddo the optimistic Frog likes playing Platypus tournaments. He particularly likes the 3-
player version, for which a tournament of n machines will require n(n+1)(n+2)/6 matches.
He ran a tournament for 100 machines which took 42.42 seconds on the family desktop
computer. Encouraged with his success, he attempts to run a tournament with 10,000
machines, but when it was discovered the computer took well over a day without coming
close to finishing, he was given a strict limit of 8 hours for all such tournament play (so that
tournaments could be run at night when all the other frogs were asleep). What is the largest
tournament size that Freddo can play within this limit? Show your working. We will call
this number n1.
Answer: A tournament of 100 machines will require 100×101×102/6 = 171, 100 matches.
Doing this in 42.42 seconds means that this takes 0.000247059 seconds per match. An 8-hour
limit gives Freddo 8 × 60 × 60 = 28, 800 seconds, and hence 8 × 60 × 60/(0.000247059) =
116, 571, 345 matches. When n = 886, the numebr of matches is 116, 310, 536, and for
n = 887 it is 116,704,364. So n1 = 886, i.e. Freddo can play a tournament of up to 886
machines.
(d) Having despaired of realising his dream of a complete 3-player tournament, Freddo hears
of a similar tournament game, known as Krazy Koalas. His friend Choco tells him that he
can also run a 100-machine tournament in 42.42 seconds, but the Koala tournament “only”
requires n6/(1000000) matches. Given Freddo’s time limit of 8 hours, what is the largest
Koala tournament he can run? Show your working. We will call this number n2.
Answer: From the previous answer we know that Freddo can run up to 116, 571, 345
matches. When n = 221, we have n6/(1000000) = (221)6/(1000000) = 116, 507, 435, and
when n = 222, it is 119, 706, 531. So n2 = 221, i.e. Freddo can play a tournament of up to
221 machines.
(e) Freddo, being an optimist, decides he wants to investigate the two types of tournament a
little further. Given he knows it takes just under 8 hours to run a Platypus tournament with
n1 machines and a Koala tournament of n2 machines, how long will it take to run a Platypus
tournament with n2 machines? And how long will it take to run a Koala tournament with
n1 machines? Show your working in each case.
Answer: A Platypus tournament with n2 = 221 machines will take 1,823,471 matches,
which will take 450.5 seconds, i.e. 7.5 minutes. A Koala tournament with n1 = 886 machines
will take 483,729,230,338 matches which will take 119, 509, 574.55 seconds, i.e. 3.78 years.
(f) Freddo’s activities attract the attention of a spambot (secretly installed on the family desk-
top), and gets an unsolicited offer from Hammy Spam Solutions to provide a host server
for running his tournaments, at a cost of $(1.01)n for a Platypus tournament of n machines,
where n could be as high as 10,000. After a small amount of thought, Freddo deletes the offer
and tells all his family and friends to avoid Hammy Spam Solutions at all times. Explain
why Freddo did this, with particular reference to the cost for a tournaments involving 100,
1,000 and 10,000 machines.
Answer: The cost is exponential, and sooner or later will become far too expensive. Freddo
knows he can run a Platypus tournament of 886 machines at no cost (apart from 8 hours on
the familiy desktop). Consider the table below.
Machines Cost ($s)
100 2.70
1,000 20,959.16
2,000 439286205.05
3,000 9207067941189.81
4,000 192972369947324000.00
5,000 4044537935523770000000.00
6,000 84770100073685200000000000.00
7,000 1776709720877430000000000000000.00
8,000 37238335563086900000000000000000000.00
9,000 780484070759879000000000000000000000000.00
10,000 163,58,287,111,890,900,000,000,000,000,000,000,000,000,000.00
Enough said!
4. (a) Construct a Turing machine M1 which recognises your student number. This machine should
accept only your student number; it should reject any string of length 6 or less, and any
string of length 8 or more. The only string of length 7 which it should accept is your student
number.
Answer: We will assume your student number is 7654321. A Turing machine which
recognises this is below. There are others of course, but note that this machine rejects any
string of length 6 or less, as well as rejecting any string of lenth 8 or more.
(b) Which of the following machines could also be used to recognise your student number, as
above? Briefly justify each of your answers.
ˆ A non-deterministic PDA
ˆ A deterministic PDA
ˆ A non-deterministic finite state automaton (NFA)
ˆ A deterministic finite state automaton (DFA)
ˆ A linear-bounded automaton
Answer: All of these can be used to recognise your student number. There is no memory
required, and it is simple to write a DFA for it. This means that all other types of automata
can be used as well.
(c) Consider the following machine M2, where q2 is the first state of your machine M1 above (so
the states q0 and q1 below are added to your M1, with machine constructed this way starting
in q0).
Explain why M2 on input xxxxxxx will always eventually terminate with success, no matter
what your student number is.
Answer: This machine nondeterministically replaces every x on the input tape with one
of the digits 0-9. Given the input of xxxxxx to the machine, this has the effect of guessing
a 7-digit number, no matter what that number is.
(d) Given an input of xn (i.e. n consecutive x’s), calculate the maximum time it will take M2
to terminate, assuming that it can process 1 transition from the above machine in 3× 10−5
seconds. Show your working and explain your reasoning.
Show your answers for n = 7, 10, 15 and 20 in the table below. Use the most appropriate
units of time in each case.
n Transitions Time
7
10
15
20
Answer: As the deterministic execution of M2 may take up to n× 10n transitions to guess
the correct n-digit number, the maximum time for it to terminate will be n×10n×3×10−5 =
3n× 10n−5 seconds. Note that each number takes n transitions.
n Transitions Time
7 70,000,000 2100s = 35 minutes
10 1011 34.7 days
15 1.5× 1016 14,259 years
20 2× 1021 1,901,285,269 years

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图