questions - highcliffe school€¦  · web viewa finite state machine (fsm) consists of a set of...

40
Questions Topic 1.2 - Comparing Algorithms 1) Why are algorithms studied? 2) If C(n) is the number of times an algorithm’s basic operation needs to be executed, how much longer will each of the following take to run if its input size is doubled? a) C(n) = ½ n b) C(n) = ¼ n 3 c) C(n) = 2 n 3) Why are the estimated running times of algorithms not measured in seconds or milliseconds? 4) Explain how to estimate the running time of an algorithm. 5) What is meant by: a) The computational complexity of an algorithm? b) The computational complexity of a problem? 6) What is meant by: a) The time complexity of an algorithm? b) The space complexity of an algorithm? 7) The following algorithm determines whether all the elements in a given array, A, of n elements are distinct. Algorithm DistinctElements(A[1..n], n) //Determines whether all elements in A are distinct //Inputs: Array A and n, number of elements n ≥ 2 //Output: Returns ″True″ if all the elements are distinct // otherwise returns ″False″ Result ← True For i ← 1 To n – 1 Do 1 © Dr K R Bond 2009

Upload: others

Post on 11-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 1.2 - Comparing Algorithms

1) Why are algorithms studied?2) If C(n) is the number of times an algorithm’s basic operation needs to be

executed, how much longer will each of the following take to run if its input size is doubled?a) C(n) = ½ nb) C(n) = ¼ n3

c) C(n) = 2n

3) Why are the estimated running times of algorithms not measured in seconds or milliseconds?

4) Explain how to estimate the running time of an algorithm.5) What is meant by:

a) The computational complexity of an algorithm?b) The computational complexity of a problem?

6) What is meant by:a) The time complexity of an algorithm?b) The space complexity of an algorithm?

7) The following algorithm determines whether all the elements in a given array, A, of n elements are distinct.

Algorithm DistinctElements(A[1..n], n)//Determines whether all elements in A are distinct//Inputs: Array A and n, number of elements n ≥ 2 //Output: Returns ″True″ if all the elements are distinct// otherwise returns ″False″ Result ← TrueFor i ← 1 To n – 1 Do For j ← i + 1 To n Do If A[i] = A[j] Then Result ← FalseReturn Result

a) By tracing this algorithm confirm that C(n) =b) How will C(n) behave for large values of n ?

1© Dr K R Bond 2009

(n-1)n2

Page 2: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 1.2

1. Algorithms are considered to be procedural solutions to problems and problem solving is a human activity. Designing algorithms is a problem solving activity that can be useful regardless of whether a computer is involved. Computer programs would not exist without algorithms. Therefore, a study of algorithms is considered to be a cornerstone of computer science. There may be more than one algorithm for solving a particular problem and algorithms for solving a particular problem may do so with dramatically different speeds and also with different memory requirements.

2.a. Twice as longb. Eight times as longc. 2n times as long

3. Using a standard unit of time measurement such as a second to estimate the running time of a program implementing the algorithm would not be sensible. The estimate would be dependent on the speed of the computer, the quality of the program implementing the algorithm and the compiler used in generating the machine code.

4. Compute the number of times the basic operation is executed on inputs of size n. The basic operation is the operation in the algorithm contributing the most to the total running time.

5.a. The computational complexity of an algorithm measures how economical

the algorithm is with time and space.

b. Computational complexity of a problem is taken to be the worst-case complexity of the most efficient algorithm which solves the problem.

6.a. Time-complexity of an algorithm indicates how fast an algorithm runsb. Space-efficiency of an algorithm indicates how much memory an algorithm

needs

2© Dr K R Bond 2009

Page 3: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

7. (a) Input size is number of elements in the array, i.e., n. The innermost loop contains a single operation, the comparison of two elements. Consider this as the algorithm’s basic operation. In the worst case scenario, the input is an array for which the number of element comparisons is the largest amongst all arrays of size n. There are two cases for this worst case scenario:

1. arrays with no equal elements2. arrays in which the last two elements are the only pairs of

equal elementsFor such inputs, one comparison is made for each repetition of the innermost loop, i.e. for each value of the loop’s variable j between limits i + 1 and n. This is repeated for each value of the outer loop, i.e. for each value of the loop’s variable i between its limits 1 and n - 1. Consider the case when n = 6.

Element No No of Comparisons Element in first column Compared with elements1 5 2, 3, 4, 5, 62 4 3, 4, 5, 63 3 4, 5, 64 2 5, 65 1 6

In general, the number of comparisons becomes 1 + 2 + 3 + 4 + 5 + … + nThis has the sum

(b) For large values of n this becomes as (n - 1)n = n2 - n and for large n, n2 dominates

3© Dr K R Bond 2009

(n-1)n2

n2

2

Page 4: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 1.2 - Comparing Algorithms

8) What is meant by the asymptotic behaviour of a function?2) What is meant by big-O notation?3) Place the following in ascending order of complexity from the least complex to

the most complexO(2n), O(n3), O(n), O(n!), O(n2), O(log2 n), O(nlog2 n )

11) Explain the meaning of the followinga) Exponential growthb) Polynomial growthc) A polynomial time algorithmd) An exponential time algorithm.e) A linear time algorithm

4© Dr K R Bond 2009

Page 5: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 1.2

8. Asymptotic behaviour of a function is the behaviour of the function for very large values of n.

9. Given a function g(n) then big O of g, represents the class of functions that grow no faster than the function g

10.

f = O(log2 n)f = O(n)f = O(nlog2 n )f = O(n2)f = O(n3)f = O(2n)f = O(n!)

11.a. Exponential growth exhibits growth rates that vary according to the

form kn, e.g. 2n where k = 2 and n = 1, 2, 3, etc.b. Polynomial growth exhibits growth rates that vary according to the

form nk, e.g. n3 where k = 3 and n = 1, 2, 3, 4, etc.c. An exponential time algorithm is one whose execution time grows

exponentially with input size.d. A polynomial time algorithm is one whose execution time grows as a

polynomial of input size.e. A linear time algorithm is a polynomial time algorithm that executes in

O(n) time

5© Dr K R Bond 2009

Page 6: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 1.3 - Finite State Machines

1) What is a finite state machine?2) What is the output Y from the FSM shown in Figure 01 when the input X is

001011? Assume that the bits are submitted to the FSM in the order of most significant bit through to least significant bit.

Figure 01. FSM with output for an alphabet of {0, 1}

3) What is the output Y from the FSM shown in Figure 02 when the input X is 00101100? Assume that the bits are submitted to the FSM in the order of most significant bit through to least significant bit. What is the final state of the machine when all the input has been processed?

Figure 02. FSM with one input X and one output Y

6© Dr K R Bond 2009

S0 S110

00 10

01

FSMX Y

S010

S1 S200

S310

00 10

0000

FSMX Y

10

Page 7: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

4) Table 1 shows the transition table for a finite state machine with outputs. The alphabet is {0, 1}. Draw the equivalent state transition diagram.

Table 01. Finite state machine with outputs, alphabet {0, 1}

5) Figure 3 is a FSM with an input alphabet of {0, 1}.and an output alphabet of {0, 1, =, c}. It has three halting states, S2, S4 and S5. What is the machines output for the following inputs:

a) 00b) 01c) 10d) 11?

6) What function is performed by the machine in question 3?

Figure 03. FSM with outputs

1) Explain the difference between a finite state machine with outputs and a finite state automaton.

7© Dr K R Bond 2009

Current state S0 S0 S1 S1

Input symbol 0 1 0 1Next state S1 S0 S0 S1

Output symbol 1 0 1 0

S0

S10=

S3

S2

S5

S6

1=

00

01

10c1

S411

Page 8: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

2) Draw the state transition diagram for a DFA for each of the following languages:a) All strings over {a,b} that contain bba but not aabb) All strings over {a,b} that contain neither bab nor aabc) All strings over {a,b} that contain both bab and aabd) All strings over {a,b} that contain an even number of b’se) All strings over {a,b} that contain an odd number of b’s and an even number

of a’s3) Explain the difference between a deterministic and a nondeterministic finite state

automaton.4) Explain the role of an accepting state in a finite state automaton.

8© Dr K R Bond 2009

Page 9: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

5) State what strings the DFA in Figure 5 accepts.

Figure 04. State diagram for a finite state automata

9© Dr K R Bond 2009

a or b

1a a

ba

2

3

4

a or b

b

Page 10: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 1.3

1. A finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output symbols (output symbol alphabet), a finite set of states and a transition function that maps a state-symbol pair (current state and input symbol) to a state (next state) and possibly generates an output (output symbol) depending on the type of FSM.

2. 0001000

3. 00000000, S0

4.

5.a. = 0b. = 1c. = 1d. = 0c1

6. One-bit binary adder

7. An FSM without output is known as a finite state automaton (FSA) or Finite Automaton, plural Finite State Automata. FSA are restricted to decision problems, i.e. problems that are solved by answering YES or NO. Therefore, FSA do not write anything at all whilst processing input whereas a finite state machine with output does.

10© Dr K R Bond 2009

S001

S1

10 10

01

Page 11: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

8.a.

b.

c.

11© Dr K R Bond 2009

S0 S1 S2 S3

S4

S5

S6

b b a

ba

a

b

aa b

a

b

a b

a b

S0

S2

S4

a | b

b

a

b

S3

aS1

a

S0 S1 S2 S3

S9S6 S7 S8

b a b a

a b

a b

a

b

b

S5

bb

S4

a

b

a

a

a

Page 12: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

d.

e.

9. A finite state automaton is deterministic if every state has exactly one transition or edge leaving it for each of the symbols in the alphabet. As there is exactly one edge for each symbol, an input word will cause the exact same behaviour every time a deterministic finite automaton is run on the word. A finite automaton can be nondeterministic if it does not have exactly one edge for each symbol of the alphabet. A state can have no edges for a symbol or it can have multiple edges for each symbol of the alphabet.

10. On reaching the end of the input if the FSA is in an accepting state then the FSA outputs a YES response

11. aa|b(a|b)*

12© Dr K R Bond 2009

S0 S1

S2 S3

a

b

b

b

a

b

a

a

S0

a

S3

b

S1 S2

a

a b

b

a

S4ba

b

Page 13: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 1.4 - Turing Machines

1) Show the state of the tape after the Turing machine is executed on a tape containing four 1s. The read/write head starts on the leftmost 1. The symbol → means move read/write head one square to the right.

6) The following Turing machines are expressed as sets of transition rules. Draw the state transition diagram for each. Assume that the starting state for each is state 1.a) δ(1, b) = (1, a, →) δ(1, a) = (1,b, →) δ(1, ) = (Stop, , ←)b) δ(1, 1) = (1, x, →) δ(1, ) = (2, x, ←) δ(2, x) = (3, 1, →)

δ(3, 1) = (3, 1, →) δ(3, ) = (2, 1, ← ) δ(2, 1) = (2, 1, ← ) δ(2, ) = (Stop, , ← )

4) Explain what the Turing machines in question 1 do.

5) What is a Turing machine?6) What is the principle of universality?7) Explain what is meant by the Universal Turing machine.8) This question is about universality. Which one of the following does not belong?

Supercomputer Coffee makerJava programming languageMS Excel Mobile phone DNA computer

9) Explain why programs can be treated as data.10) Explain one consequence of the principle of universality for microprocessor

systems which are used as microcontrollers or embedded computers.11) What is meant by computability?

13© Dr K R Bond 2009

11

11Stop 2 1

1 1 1 1 1

Page 14: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 1.4

1. 11111

2.a.

b.

3. Adds 1 to the number on the tape

14© Dr K R Bond 2009

1 Stopa | b

b | a

□ | □

1 2 3

Stop

□ | x x | 1

1 | x 1 | 1

□ | 1 □ | □

1 | 1

Page 15: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

4. A Turing machine (TM) is a finite state machine that controls one or more tapes, where at least one tape is of unbounded length (i.e. infinitely long).

5. Principle of universality: a universal machine is a machine capable of simulating any other machine.

6. A universal Turing Machine, U, is an interpreter that reads the description <M> of any arbitrary Turing Machine M and faithfully executes operations on data D precisely as M does. For single-tape Turing Machines, imagine that <M> is written at the beginning of the tape, followed by D.

7. Coffee maker

8. A consequence of a Universal Turing machine is that programs and data are really the same thing. A program is just a sequence of symbols that looks like any other piece of input but when fed to a universal machine this input wakes up and begins to compute. A computer downloads Java applets, e-mail viruses as data but can then proceed to execute them as programs.

9. A microprocessor is a universal machine. An embedded computer with a microprocessor at its core can be programmed to perform as a microcontroller by choosing the right set of program instructions. The program or programs are changed when a different functionality is required.

10. A task is computable if and only if it can be computed by a Turing machine.

15© Dr K R Bond 2009

Page 16: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 1.5 - Intractable Problems

1) A person who shaves another is labelled a barber. A certain village has just one barber who is male. This barber shaves everyone who does not shave himself and no one else. a) Who shaves the barber? b) How would you classify this problem?

1) Using an exponent calculator (http://www.webwinder.com/wwhtmbin/jexpont.html) calculate the following:

(i) 210 (ii) 220 (iii) 230 (iv) 240 (v) 250 (vi) 260 (vii) 2120

3) A courtier presented the Persian king with a beautiful, hand-made chessboard. The king asked what he would like in return for his gift and the courtier surprised the king by asking for two grains of rice on the first square, four grains on the second, eight grains on the third, et cetera. The king readily agreed and asked for the rice to be brought.

a) How many grains of rice were placed on the :(i) fourth square,(ii) tenth square,(iii) sixty fourth square?(iv) ind out how many atoms, roughly, there are in the world.

b) How would you classify this problem?

4) What is meant by in the context of computation:a) Tractabilityb) Intractability?c) A young person is offered two choices for increasing her weekly allowance:

the first option begins at one pence and doubles each week, while the second option begins at £1 and increases by £1 each week. What is the young person's weekly allowance in each case after:

(i) ten weeks (ii) twenty weeks (iii) fifty two weeks?

5) Imagine having a pond with water lily leaves floating on the surface. The lily doubles in size every day and if left unchecked will smother the pond in 30 days, killing all the other living things in the water. Day after day the plant seems small and so it is decided to leave it to grow until it half-covers the pond, before cutting it back. On what day will this occur?

6) Suppose that you are organising three hundred students into groups of thirty. To complicate matters you have been provided with a list of pairs of incompatible

16© Dr K R Bond 2009

Page 17: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

students and requested that no pair from this list appear together in any group. The number of ways that thirty students can be chosen from three hundred students is about 6 x 1068. It is not possible to build a computer capable of solving this problem by brute-force. However, it is relatively easy to check if a given choice of thirty students is satisfactory, i.e. no pair within the thirty also appears on the forbidden list. How would you classify this problem?

7) Is it possible to write a program that reads in another program and its inputs and decides whether or not it goes into an infinite loop (an infinite loop often signifies a bug)?

17© Dr K R Bond 2009

Page 18: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 1.5

1.a. The answer is not decidableb. Undecidable or noncomputable

2.a. 1024b. 1048576c. 1073741824d. 1099511627776e. 1125899906842624f. 1152921504606847000g. 1.329227995784916 x 10+36

3.a.

i. 24 = 16ii. 210 = 1024

iii. 18446744073709552000iv. 1.33 x 1050

b. Intractable

4.a. Tractable means that a problem has a reasonable (polynomial) time solution

as the size of the input increasesb. Intractable means that no reasonable (polynomial) time solution has yet been

foundc. f

i. first option = £10.24 and second option = £11ii. first option = £10485.76 and second option = £21iii. first option = £45035996273704.96 and second option = £53

5. 15 days

6. NP-type problem

7. No

18© Dr K R Bond 2009

Page 19: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 1.6 - Regular Expressions, BNF and RPN

Regular Expressions

1) A language L is defined by the alphabet {a, b, c} together with the regular expression (a|c)+ bb.a) Explain in English what constitutes a valid string in L.b) Give two examples of valid strings in L

2) The + symbol is not strictly necessary. Show that this is not necessary by rewriting the regular expression for L.

3) Here are some informal descriptions of languages. Write down a regular expression to define each language formally. The alphabet for the languages is {0, 1}. Note any ambiguity in the informal descriptions.a) All possible binary stringsb) Strings in the language consist entirely of 1’s and do not include the empty

stringc) Strings in the language start with a single 1 which may be followed by zero

or more 0’s.d) Strings in the language always start with either 110 or 011. The first three

symbols may be followed by zero or more 0’s or 1’s in any order.e) Any non-empty string in the language as long as it ends with a 0.f) Any non-empty string that that starts and ends with two 1’s is a valid string

in the language.4) Write a regular expression to describe strings over the alphabet {x, y, z} that are

in sorted order.5) Which of the following strings match the regular expression, be[ea]n

a) benb) beenc) beand) beean?

6) Which of the following strings match the regular expression, be?ada) bedb) badc) beadd) be?ad?

7) Which of the following strings match the regular expression 10(1)*01a) 1001b) 100101c) 10101d) 1011101?

19© Dr K R Bond 2009

Page 20: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

8) Which of the following strings match the regular expression \d+(-|\s)\d+ for stringsa) 01296-433006b) 01296--433006c) 01793 234589d) 01793 234589e) 01793234589?

9) Create a regular expression to validate web site addresses which always begin with ‘www.’ such as www.educational-computing.co.uk.

10) The word licence has been spelt in the following ways in a piece of text: licence license lisence license

Write a regular expression that will find all instances of the word licence spelt in these different ways.

BNF

11) Draw a syntax diagram which defines the Roman numerals from 1 to 10, i.e. I, II, III, IV, V, VI, VII, VIII, IX, X. Your diagram should not consist merely of ten alternatives.

1) An identifier in a programming language consists of a letter followed by zero or more letters or digits. Draw a syntax diagram for valid identifiers in this language.

2) A UserID for logging into a computer system consists of two digits followed by one of the following abbreviations: H, L, D, R, Pa, Ph; followed by a surname consisting of upper case letters A..Z only followed by a single initial chosen from the set of upper case letters A..Z. Draw a syntax diagram for UserIDs to this system.

RPN

14) Convert the following expressions involving the variables x and y into Reverse Polish notationa) x + yb) (x + y) / 2c) (x + y) * (x - 4) d) x ↑ y e) x + y ↑ 2 f) (x + y) / (x - y)

15) Convert the following expressions into infix notationa) x y *b) x y + 6 *

20© Dr K R Bond 2009

Page 21: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

c) x y + x y - /d) 2 x * 3 y * + 8 /e) x y + 2 ↑ 6 x - *

21© Dr K R Bond 2009

Page 22: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 1.6

1.a. A valid string consists of one or more as or one or more cs followed by two

bsb. aaccbb, cacccbb

2. (a|c)a*c*bb3.

a. (0|1)*b. 1+c. 10*d. (110|011)(0|1)*e. (0|1)+0f. 11(0|1)*11

4. x*y*z*5. beean6. bead7. 101018. 01296-433006 or 01793 2345899. www.([_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+)10. li(c|s)en(c|s)e11. Roma Numerals

22© Dr K R Bond 2009

V

I

X

I

I

V

I

Page 23: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

12. Identifier

13. UserID

14.a. xy+b. xy+2/c. xy+x4-/d. xy↑e. xy+2↑f. xy+xy-/

15.a. x * yb. (x + y) * 6c. (x + y) / (x - y)d. (2 * x + 3 * y) /8e. ((x +y) ↑ 2) * (6 - x)

23© Dr K R Bond 2009

Letter

Digit

Digit HDigit

L

D

R

Pa

Ph

A..Z A..Z

Page 24: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 2.5 Graphs and Trees

1) What does the degree of a vertex in the Hollywood graph represent?2) Draw a directed graph for a round-robin tournament involving three teams, A, B

and C3) What is a graph?4) Two utilities, water, W and electricity, E, are to be supplied to three houses, A,

B and C in the same road. Draw a graph for the connections that avoids the pipe work and cables crossing each other in the same plane.

5) Different species of animal A, B, C, D, E need to be assigned holding cages so that the following rules are observed:

A must not be in same cage as B and CB must not be in same cage as D and EC must not be in same cage as A and DD must not be in the same cage as A, B and CE must not be in the same cage as A, B and D

(a) Represent this problem as a graph.(b) What is the minimum number of cages that will be required?

6 ) Represent the undirected graph shown in Figure 5 asa) an adjacency matrixb) an adjacency list.

Figure 05. Undirected graph

24© Dr K R Bond 2009

1

2 4

3

5

Page 25: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

7) Represent the directed graph shown in Figure 6 as:a) an adjacency matrixb) an adjacency list

Figure 06 Directed graph

8) Represent the weighted directed graph shown in Figure 7 as:

a) an adjacency matrixb) an adjacency list.

Figure 07 Weighted directed simple graph

9) When would it be appropriate to use and why?

a) an adjacency matrixb) an adjacency list?

10) There are five unlabelled trees with four or fewer vertices. Draw these.11) Explain why a tree with n vertices has n - 1 edges.12) What is the difference between a tree and a rooted tree?

25© Dr K R Bond 2009

1

2 4

3

5

01

2 4

3

520 -3

30

5

88154

Page 26: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

13) Show how the graph in Figure 08 will be traversed using a depth-first traversal algorithm.

Figure 08 Undirected graph

26© Dr K R Bond 2009

D

E

F

G

C

B

A

Page 27: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 2.5

1. The number of actors that the actor at the vertex has starred with in movies. 2.

3. A graph is a diagram consisting of circles, called vertices joined by lines called edges or arcs, each edge joins exactly two vertices.

4.

5. a.

b. 3 cages: one for A, one for D and one for B, C and E

6.a.

b.Vertex Adjacent

Vertices1 2, 3, 52 1, 4, 53 1, 4, 54 2, 3, 55 1, 2, 3, 4

27© Dr K R Bond 2009

Vertex 1 2 3 4 51 0 1 1 0 12 1 0 0 1 13 1 0 0 1 14 0 1 1 0 15 1 1 1 1 0

A B

C

A B C

W

E

A

E

C

B

D

Page 28: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

7.a.

8.a.

b.

9.a. When many vertex pairs are connected by edges then the adjacency matrix is

best because it doesn't waste much space, and it indicates whether an edge exists with one access (rather than following a list).

b. When the graph is sparse, i.e. not many of its vertex pairs have edges between them, the adjacency list is best.

10.

28© Dr K R Bond 2009

Vertex 1 2 3 4 51 0 0 1 0 02 1 0 0 0 03 0 0 0 0 14 0 1 1 0 05 1 1 0 1 0

Vertex Adjacent Vertices

1 32 13 54 2, 35 1, 2, 4

Vertex 1 2 3 4 51 ∞ ∞ 1 ∞ ∞2 20 ∞ ∞ ∞ ∞3 ∞ ∞ ∞ ∞ 814 ∞ 30 -3 ∞ ∞5 5 8 ∞ 54 ∞

Vertex Adjacent Vertices1 3 3 02 1 1 203 5 5 814 2, 3 2 30 3 -35 1, 2, 4 1 5 2 8 4 54

Page 29: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

11. Imagine a graph of four vertices connected by four edges to create a cycle as follows:

If one edge is removed then the graph becomes a tree - a tree is a connected graph with no cycles. Generalising, a tree of n vertices must have n-1 edges.

12. A tree is a connected undirected graph with no cycles. A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root

13. Vertices visited in the following order EGFDABC

29© Dr K R Bond 2009

Page 30: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

QuestionsTopic 2.7 Simulation

1.1. Boats arrive at a wharf with one unloading crane. a.a. What are the entities and their attributes in this system? b.b. What are the possible states, events and activities of this system? c.c. Draw a life cycle diagram for the boats and the unloading wharf. d.d. What are the possible variables in this system?

2.2. Use the system from question 1. Assume between the hours of 0600 and 1200 a boat arrives every 1 hour and it takes 2 hours to unload. Using a table with suitable headings, show the state of the system from 0600 hours until all boats have been unloaded.

3.3. Adapt the system of question 2 to work with two unloading cranes. Using a table with suitable headings, show the state of the system from 0600 hours until all the boats have been unloaded. Are there times when the second crane is underused?

30© Dr K R Bond 2009

Page 31: Questions - Highcliffe School€¦  · Web viewA finite state machine (FSM) consists of a set of input symbols (input symbol alphabet) and if it produces output, a set of output

Solutions

Topic 2.6

31© Dr K R Bond 2009