• No results found

Depth-First Order

In document FE_Book_1 (Page 50-63)

In depth-first search,74 we start with the root and traverse from the left child and from leaves.

Depending on the timing when the nodes are traversed, it can be classified as shown in the following table.

Search method Order in which nodes are traversed Pre-order Parent, left child, right child, in this order In-order Left subtree, parent, right subtree, in this order Post-order Left subtree, right subtree, parent, in this order Below, the number at each node indicates the order in which the node is traversed.

Pre-order In-order Post-order

It is probably not very clear yet what the rules are for each of the search types, so let me add some explanation. In depth-first order, the search follows the order as shown below:

In pre-order, the node values are taken out whenever you traverse the left side of the nodes.

Hence, the order is “+ – a b / * c d e.” In in-order, the node values are taken out whenever you traverse under the nodes. Hence, the order is “a – b + c * d / e.” In post-order, the node values are accessed whenever you traverse the right side of the nodes. Hence, the order is “a b – c d * e / +.” 75

74 (FAQ) Depth-first order frequently appears on the exams. Understand well how the nodes are taken out in pre-order, in-order, and post-order.

75 (Note) Note the result of traversing the tree to obtain symbols and variables. In pre-order, the result is “+ – a b / * c d e,”

which is in Polish Notation. In in-order, the result is “a – b + c * d / e,” which is in standard mathematical notation. In post-order, the result is “a b – c d * e / +,” which is in Reverse Polish Notation.

Q1 In binary search, when the number of sorted data values is quadrupled, how much does the maximum number of comparisons increase by?

Q2 Explain the characteristics of each of the sorting methods: “shell sort,” “bubble sort,” “quick sort,” and “heap sort.”

A1 2 times

Since the number of data values becomes 4 times as much, substitute the “n” in the formula “log2

n + 1” (maximum number of comparisons) with “4n.”

log2 4n + 1 = (log2 4 + log2 n) + 1 = log2 22 + log2 n + 1

= 2 + log2 n + 1

= 2 + (log2 n + 1) A2

Shell sort: Elements of the array are picked at certain intervals first and are sorted; then, more elements are picked by reducing the intervals and are sorted.

Bubble sort: Adjacent elements are compared and exchanged if the order is not correct; this process is repeated.

Quick sort: An intermediate (median) reference value is chosen, and the array is divided into the elements larger than the reference value and those smaller than the value; within each portion, the same process is repeated.

Heap sort: The unsorted portion is expressed as a subtree, from which the largest (or the smallest) value is taken out and moved to the already sorted portion. This process is repeated to reduce the unsorted portion.

Quiz

Difficulty: ** Frequency: ***

Q1. There is a register which stores values in binary. After entering a positive integer x into this register, the operation “to shift the register value 2 bits to the left and to add x to the value” will be performed. How many times as large as x is the resulting register value? Here, assume that overflow due to shifting will not occur.

a) 3 b) 4 c) 5 d) 6

Correct Answer: c

In general, if there is no overflow, shifting n bits to the left multiplies the value by 2n while shifting n bits to the right multiples the value by 1/2n. Shifting 2 bits to the left is to multiply by 22, so if we let y be the calculation result, y is related to x by the following equation:

y = x

× 2

2

+

x = x

× ( 2

2

+ 1 )

= 5×x (y is 5 times x)

a) To make it 3 times as large, we would shift the register value 1 bit to the left and add x to it.

Shifting 1 bit to the left multiplies the value by 21, so the result would be as follows:

x

b) To make it 4 times as large, we would shift the register value 2 bits to the left, which multiplies the value by 22, and the result would be as follows:

x x

y

= × 2

2

= 4

d) To make it 6 times as large, we would shift the register value 2 bits to the left and add this result to the result obtained by shifting the original register value 1 bit to the left. Shifting 2 bits to the left multiplies the value by 22, and shifting 1 bit to the left multiplies the value by 21, so the following would result:

x

Difficulty: * Frequency: ***

Q2. Which of the following is an appropriate description concerning the cancellation of significant digits?

a) It means that the number of the significant digits is extremely reduced when a floating point number is subtracted by another whose value is almost equal.

b) It refers to an error which occurs because the calculation result exceeds the maximum numeric value that can be processed.

c) It refers to an error which occurs when rounding off (up or down) the numbers smaller than the lowest digit when the total number of digits in numerical representation is limited.

d) It refers to the omission of the low-order digit of an operand when adding floating point numbers.

Correct Answer:

a

Cancellation of significant digits is a phenomenon in which higher-order significant digits are lost in subtraction involving two values of the same sign which are close and in addition involving two values of the opposite signs whose absolute values are close. It occurs because computers process all numbers with only a finite number of digits. For instance, it occurs in the following calculation:

123.4567 – 123.4556 0.0011

Here, the higher-order digits become 0, reducing the number of significant digits drastically.

b) This describes an overflow.

c) This describes a rounding error.

d) This describes a loss of trailing digits.

Answer 2

Question 2

Difficulty: *** Frequency: ***

Q3. The truth table below shows the results of logical operation “x @ y.” Which of the following expressions is equivalent to this operation?

x y x @ y

True True False

True False False

False True True

False False False

a) x OR (NOT y) b) (NOT x) AND y

c) (NOT x) AND (NOT y) d) (NOT x) OR (NOT y)

Correct Answer: b

In logic operations, we assign “1” for “true” and “0” for “false.” It is easier to use familiar notation, so we shall use the following symbols:

x AND y Æ x

y (logical product) x OR y Æx+ y (logical sum) NOT x Æ x (logical negation)

Then, the logical expressions in the answer group can be rewritten as follows:

a) x OR (NOT y) x

+

y

b) (NOT x) AND y x

y

c) (NOT x) AND (NOT y) x

y d) (NOT x) OR (NOT y) x

+

y

Then we check to see which of the expressions in the answer group matches (has the identical results with) the given logic operation:

a) b) c) d)

x y x y x

+

y x

y x

y x

+

y x @ y

1 1 0 0 1 0 0 0 0

1 0 0 1 1 0 0 1 0

0 1 1 0 0 1 0 1 1

0 0 1 1 1 0 1 1 0

Hence, the operation whose results match those of x @ y is x

y.

Answer 3

Question 3

Difficulty: ** Frequency: **

Q4. When the syntax for numerical values is defined as shown below, which of the following expressions is treated as <numerical value>?

<numerical value> ::= <numerical string>|<numerical string>E<numerical string>|

<numerical string>E<sign><numerical string>

<numerical string> ::= <numeral>|<numeral string> <numeral>

<numeral> ::= 0│1│2│3│4│5│6│7│8│9

<sign> ::= +│-

a) –12 b) 12E–10 c +12E–10 d) +12E10

Correct Answer: b

This answer conforms to the third form (<numerical string> E <sign> <numerical string>) of the definition of <numerical value>.

This type of definition is called BNF notation (Backus-Naur Form). BNF notation is used as a way to formally denote the syntax of a programming language.

Overview of BNF notation is as follows:

α::=β The left-hand side α is defined as the right-hand side β. In other words, α = β.

< α > → This denotes the variable α. < > can be omitted.

| This means “or.” “α::=β | γ” means “α::=β” or “γ.”

“::=” can simply be written “=.”

a) By the definition of <numerical value>, “–” (<sign>) must follow “E.” The underlined part does not satisfy the definition. –12

c) By the definition of <numerical value>, “+” (<sign>) must follow “E.” The underlined part does not satisfy the definition. +12E – 10

d) By the definition of <numerical value>, “+” (<sign>) must follow “E.” The underlined part does not satisfy the definition. +12E10

Answer 4

Question 4

Difficulty: ** Frequency: **

Q5. A key is composed of 3 alphabetic characters. When the hash value h is decided with the following expression, which of the following collides with the key “SEP”? Here, “a mod b”

represents the remainder when a is divided by b.

h = (Sum of positions for each alphabetic character in the key) mod 27 Alphabetic

A hash value is the result of converting the key by a hash function, which is used for hashing.

The term “hashing” refers to a process of performing some sort of calculation on the key to convert it to an address value in order to obtain the storage address of the record in a direct organization file, for example. Here, the function used to obtain the address is called a hash function. If hashing generates the same hash value for two or more different keys, it is called a collision. Records that came in later when a collision occurred are called synonyms.

Calculating the hash value for “SEP” by means of the given hash function, we can obtain the following:

h = (sum of positions for each alphabetic character used in the key) mod 27 = (19 + 5 + 16) mod 27

Difficulty: ** Frequency: **

Q6. In the heap shown below, the value of a parent node is less than the values of child nodes.

When inserting a node into this heap, an element is added at the very end. If that element is less than the parent node, the parent and child are exchanged with each other. If element 7 is added to the heap at the position marked by the asterisk (*), what element will end up at position A?

9

A

11 14

24 25 19 28

29 34 *

a) 7 b) 11 c) 24 d) 25

Correct Answer: b

Add the element to the given position and then repeat the procedure to exchange the child and the parent when the child element has a value smaller than the parent value. “7” is the added element here.

Exchange

Exchange

Exchange

Answer 6

Question 6

Difficulty: * Frequency: ***

Q7. Which of the following terms expresses a characteristic of stack operations?

a) FIFO b) LIFO c) LILO d) LRU

Correct Answer: b

A stack is a data structure of the type known as Last-In First-Out, where data stored last will be the first data to be taken out. The operation of inserting data into a stack is called a “push,” and the operation of taking data out of a stack is called a “pop.”

a) FIFO (First-In First-out) is the data structure of a queue, where data stored first will be the first data to be taken out.

c) LILO (LInux LOader) is a boot loader (program to load the OS into memory) that allows PCs to read Linux.

Translator’s note: It seems natural that LILO means “Last-In Last-Out” in this question.

d) LRU (Least Recently Used) means “least accessed in recent history” and is used as the page-replacing algorithm in a virtual memory system. This is the method of paging-out which discards the least recently accessed page.

Difficulty: ** Frequency: **

Q8. The decision table below shows the conditions for creating reports from employee files. Which of the following can be concluded from this decision table?

Under age 30 Y Y N N

Male Y N Y N

Married N Y Y N

Output Report 1

X

– –

Output Report 2

– – –

X

Output Report 3 X

– – –

Output Report 4

– –

X

a) Report 1 contains the contents of Report 4 except for data on men age 30 and over.

b) Report 2 contains all unmarried men.

c) Men in Report 3 are also included in Report 2.

d) Persons included in Report 4 are not included in any of the other reports.

Answer 7 Question 7

Question 8

Correct Answer: d

Let the negation of “married” be “unmarried” and the negation of “male” be “female.” Now, read the answer group descriptions carefully. In the following explanation, the underlined parts indicate negation (N).

a) The output conditions for Report 1 are “under 30, not male, married.”

→This is “under 30, female, married.”

The output conditions for Report 4 are “not under 30, male, married.”

→This is “at least 30, male, married.”

So, Report 1 contains females only. Report 4 contains males only, and removing those

“males, at least 30” from Report 4 causes it to be the empty set. Hence, this description is wrong.

b) The output conditions for Report 2 are “not under 30, not male, not married.”

→This is “at least 30, female, unmarried.”

So, report 2 contains females only, so it is not true that “all unmarried men” are included.

Hence, this description is wrong.

c) The output conditions for Report 3 are “under 30, male, not married.”

This is “under 30, male, unmarried.”

The output conditions for Report 2 are “not under 30, not male, not married.”

→This is “at least 30, female, unmarried.”

So, Report 3 contains males only while Report 2 contains females only. There is no intersection. Hence, this description is wrong.

d) By elimination this must be the correct answer, but let us check it. Organizing all of the output criteria for all of the reports from a), b), and c) in the answer group, we get the following:

Report 1: “under 30, female, married” (from “a”) Report 2: “at least 30, female, unmarried” (from “b”) Report 3: “under 30, male, unmarried” (from “c”) Report 4: “at least 30, male, married” (from “a”)

The condition “at least 30” for Report 4 is also for Report 2, but the other conditions are in negation of each other, so no person is included in both. Further, the condition “male”

is also for Report 3, but the other conditions are in negation of each other also. Similarly, the condition “married” is also for Report 1, but again the other conditions are in negation of each other. Therefore, no other reports contain those persons contained in Report 4. This is the correct description.

Answer 8

Difficulty: ** Frequency: ***

Q9. The flowchart below illustrates the Euclidean algorithm for obtaining the greatest common divisor of values “A” and “B,” by repeated subtraction. When “A” is 876 and “B” is 204, how many comparisons are required for completion of this process?

a) 4 b) 9 c) 10 d) 11

Correct Answer: d

The Euclidean algorithm is an algorithm to obtain the greatest common divisor of two integers A and B. However, you need not know this algorithm; all you have to do is to track how the data changes. First, by “AÆL” and “BÆS,” the values for which the greatest common divisor is to be obtained are rewritten as L and S. The algorithm then determines which is greater and subtracts the smaller from the larger. Then, in case of “L=S,” the algorithm stops.

Since initially A=876 and B=204, we subtract B (=S) from A (=L) as many times as possible.

Note that the values must be compared first before the subtraction takes place.

(1) Under the condition of L=876 and S=204, repeat subtraction until L<S. Since 4

204

876÷ = with remainder 60, the subtraction and replacement “L – S Æ L” can be executed 4 times before “L < S” is satisfied. Hence, the comparison (L:S) occurs 4 times.

(2) Under the condition of L=60 and S=204, repeat subtraction until L>S. Since 3

60

204÷ = with remainder 24, the subtraction and replacement “S – L Æ S” can be executed 3 times before “L > S” is satisfied. Hence, the comparison (L:S) occurs 3 times here.

(3) Under the condition of L=60 and S=24, repeat subtraction until L<S. Since 2

24

60÷ = with remainder 12, the subtraction and replacement “L – S Æ L” can be executed 2 times before “L < S” is satisfied. Hence, the comparison (L:S) occurs 2 times here.

(4) Under the condition of L=12 and S=24, repeat subtraction until L=S. Since

2

12

24 ÷ =

with remainder 0, the subtraction and replacement “S – L Æ S” can be executed 2 times before “L = S” is satisfied. Hence, the comparison (L:S) occurs 2 times here.

(5) The number of times of comparison for “L:S” is calculated as follows:

We can now add the numbers from (1) through (4).

Total number of times of comparison = 4 + 3 + 2 + 2 = 11 (times)

Difficulty: *** Frequency: ***

Q10. When the algorithms described by the two flowcharts below are performed on a positive integer M, which of the following conditions needs to be inserted in the box below so that the same value x can be obtained?

a) n > M b)

n > M + 1

c) n > M -1 d)

n < M

Correct Answer: a

The notation “n: M, -1, 1” at the loop limit means, as explained in the question, the following:

let the initial value of n be M, add “– 1” (subtract 1) each time the loop is executed, and stop when the final value “1” is reached. This means that the value of n changes from M, M – 1, M – 2, …, 2, and 1.

Let us follow the flowchart on the left first. Starting with n = M and decreasing the value by 1 each time the loop is executed until the value gets to 1 (n = M, M – 1, M – 2, …, 2, 1), the following operation is going on since “x

×

n

x” is executed within the loop. As “1 Æ x”

suggests, the initial value for x is 1. Let us track how the value of x changes as n changes:

n=M: x×n=1×M =Mx=M (The value of x changes to M.) n=M – 1 : x

×

n

=

M

× (

M

− 1 ) =

M

(

M

− 1 ) →

x

=

M

(

M

− 1 )

(The value of x changes to M(M – 1).) Note: The repetition specification

in the loop limit denotes the following.

Variable name: initial value, increment, final value

Start Start

Operation

End Operation

End

Answer 10

Question 10

This is calculating M

⋅ (

M

− 1 ) ⋅ (

M

− 2 ) ⋅ ... ⋅ 2 ⋅ 1

. For an integer value M, the product

1

2 ...

) 2 ( ) 1

( − ⋅ − ⋅ ⋅ ⋅

M M

M is called M! (M factorial).

On the other hand, consider the flowchart on the right. n changes from 1, 2, …, M, and the process

x

×

n

x” is repeated in the loop. This is also factorial calculation, beginning with 1.

Let us now specifically track how x changes with respect to the value of n. As in the flowchart on the left, the initial value of x is 1. Further, each time the loop is executed, the value of n increases by 1. The underlined part in each line below is the previous value of x:

n=1: x×n=1×1=1→x (x = 1) n=2: x×n=1×2→x (x =

1 × 2

)

n=3: x×n=1×2×3→x (x = 1×2×3) n=4: x×n=1×2×3×4→x (x = 1×2×3×4)

Let us consider how large n should be in order to make the result identical to the result of the flowchart on the left. The flowchart on the left repeats “x

×

n

x” to execute the following calculation:

Flowchart on the left = M

⋅ (

M

− 1 ) ⋅ (

M

− 2 ) ⋅ ... ⋅ 2 ⋅ 1

The multiplication begins with M here; on the right, the multiplication begins with 1. Hence, as shown below, if the multiplication continues until M, the results of the two flowcharts will be identical:

Flowchart on the right = 1×2×3×...×M

Therefore, we are to repeat “x

×

n

x” until n = M. Following the flowchart, after the command

x

×

n

x,” the program executes “n+1→n,” so after n=M is multiplied, we will have n = (M+1). This means that the program should flow to the “end” branch when “n=M+1” is satisfied.

Among the options in the answer group, this condition is “n>M.”

In document FE_Book_1 (Page 50-63)

Related documents