CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook...

27
CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution ** --------------------------------------------------------------------------------------------------------------------- --------------------- 1. (0.5 points) Give an example of a positive function f(n) such that f(n) is neither O(n) nor (n). Explain both assertions. Solution: Let f(n) = n 2 * (1 + cos(n)) which has O(n 2 ) and (1). --------------------------------------------------------------------------------------------------------------------- --------------------- 2. (0.5 point) Suppose f(N) = O(h(N)) and g(N) = O(h(N)). Justify your answers. a) Is f(N) +g(N) = O(h(N))? Solution: Yes. f(N) = O(h(N)): This means that there are positive constants n 0 ’ and c’ such that g(N) = O(h(N)): This means that there are positive constants n 0 ” and c” such that Then we have where c = c’ + c” and n 0 = max(n 0 ’, n 0 ’’). Therefore, is O(h(N)). b) Is f(N) - g(N) = O(h(N))? Solution: Not always. Counter example: let f(N) = N + 2 and g(N) = N – 1 then f(N) – g(N) = N + 2 – N + 1 = 3 = O(1)

Transcript of CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook...

Page 1: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution **

------------------------------------------------------------------------------------------------------------------------------------------

1. (0.5 points) Give an example of a positive function f(n) such that f(n) is neither O(n) nor Ω(n). Explain both assertions.

Solution:

Let f(n) = n2 * (1 + cos(n)) which has O(n2) and Ω(1).

------------------------------------------------------------------------------------------------------------------------------------------

2. (0.5 point) Suppose f(N) = O(h(N)) and g(N) = O(h(N)). Justify your answers. a) Is f(N) +g(N) = O(h(N))? Solution: Yes. f(N) = O(h(N)): This means that there are positive constants n0’ and c’ such that

g(N) = O(h(N)): This means that there are positive constants n0” and c” such that

Then we have

where c = c’ + c” and n0= max(n0’, n0’’). Therefore, is O(h(N)). b) Is f(N) - g(N) = O(h(N))? Solution: Not always.

Counter example: let f(N) = N + 2 and g(N) = N – 1 then f(N) – g(N) = N + 2 – N + 1 = 3 = O(1)

Page 2: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

-----------------------------------------------------------------------------------------------------------------------------------------

3. (0.5 point) Explain why Logarithmic time does not depend on the base chosen for the logarithms that is, loga n is O(logb n) for any real numbers a > 1 and b > 1. Solution:

For any a > 0, b > 0, and a, b ≠ 1,

So and differ only by a constant multiplier K = which will be discarded in big-O.

Therefore they have the same growth rate. ------------------------------------------------------------------------------------------------------------------------------------------

4. (0.5 point) Please sort (4, 2, 9, 6, 1, 5) using a sorted sequence as Priority Queue. Illustrate all the steps

Solution:

Sequence S Priority Queue P Input (4, 2, 9, 6, 1, 5) ()

Phase 1

(2, 9, 6, 1, 5) (9, 6, 1, 5) (6, 1, 5) (1, 5) (5) ()

(4) (2, 4)

(2, 4, 9) (2, 4, 6, 9)

(1, 2, 4, 6, 9) (1, 2, 4, 5, 6, 9)

Phase 2

(1) (1, 2)

(1, 2, 4) (1, 2, 4, 5)

(1, 2, 4, 5, 6) (1, 2, 4, 5, 6, 9)

(2, 4, 5, 6, 9) (4, 5, 6, 9) (5, 6, 9) (6, 9) (9) ()

Page 3: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

5. (0.5 point) Please sort (4, 2, 9, 6, 1, 5) using an unsorted sequence as Priority Queue. Illustrate all the steps.

Solution:

Sequence S Priority Queue P Input (4, 2, 9, 6, 1, 5) ()

Phase 1

(2, 9, 6, 1, 5) (9, 6, 1, 5) (6, 1, 5) (1, 5) (5) ()

(4) (4, 2)

(4, 2, 9) (4, 2, 9, 6)

(4, 2, 9, 6, 1) (4, 2, 9, 6, 1, 5)

Phase 2

(1) (1, 2)

(1, 2, 4) (1, 2, 4, 5)

(1, 2, 4, 5, 6) (1, 2, 4, 5, 6, 9)

(4, 2, 9, 6, 5) (4, 9, 6, 5) (9, 6, 5) (9, 6) (9) ()

------------------------------------------------------------------------------------------------------------------------------------------

6. (0.5 point) Please sort (4, 2, 9, 6, 1, 5) using Heap-sort as Priority Queue. Illustrate all the steps using trees. Please Bottom-up Heap Construction to construct the initial tree, where 4 goes in the root position.

Solution I: (4, 2, 9, 6, 1, 5)

Phase 1:

4

2 9

5 6 1

Page 4: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

4

2 9

5 6 1

4

2 5

9 6 1

4

1 5

9 6 2

4

1 5

Page 5: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

1

4 5

9 6 2

1

4 5

9 6 2 1

2 5

9 6 4

Page 6: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Phase 2: (4, 2, 9, 6, 1, 5)

S = (1)

S = (1)

2 5

9 6 4

9

2 5

6 4

9

2 5

6 4

Page 7: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1)

S = (1)

S = (1)

2

9

5

6 4

2

4

5

6 9

4

5

6 9

Page 8: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2)

S = (1, 2)

S = (1, 2)

S = (1, 2)

9

4

5

6

9

4

5

6

4

9

5

6

Page 9: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2)

S = (1, 2, 4)

S = (1, 2, 4)

S = (1, 2, 4)

4

6

5

9

6

5

9

9

6

5

5

6

9

Page 10: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2, 4, 5)

S = (1, 2, 4, 5)

S = (1, 2, 4, 5)

S = (1, 2, 4, 5, 6)

6

9

9

6

6

9

9

9

Page 11: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2, 4, 5, 6)

S = (1, 2, 4, 5, 6, 9)

Solution II: (it is not correct with the given condition, but as it is the method in the lecture note, we accept it with minus 0.1 mark.)

Phase 1:

5

1 6

4 9 2

5

1 6

4 9 2

Page 12: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

5

1 4

6 9 2

5

1 4

6 9 2

1

5 4

6 9 2

Page 13: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Phase 2:

S = (1)

1

2 4

6 9 5

2 4

6 9 5

6

2 4

9 5

Page 14: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1)

S = (1)

S = (1)

2

6

4

9 5

2

5

4

9 6

5

4

9 6

Page 15: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2)

S = (1, 2)

S = (1, 2)

6

5

4

9

4

5

6

9

5

6

9

Page 16: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2, 4)

S = (1, 2, 4)

S = (1, 2, 4)

S = (1, 2, 4, 5)

9

5

6

5

9

6

9

6

6

9

Page 17: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

S = (1, 2, 4, 5)

S = (1, 2, 4, 5, 6)

S = (1, 2, 4, 5, 6)

S = (1, 2, 4, 5, 6, 9)

7. (0.5 point) Please use removeMin() from the given heap below. Illustrate all the steps using trees.

9

9

Page 18: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Solution:

Page 19: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

8. (0.5 point) Below is a binary tree T’. Please construct a general tree T corresponding to T’.

6

10

8

12 21 92

102

20

Page 20: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Solution:

9. (0.5 point) Please use (i) Pre-order traversal (ii) Post-order traversal (iii) In-order traversal for the below tree.

Page 21: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Solution:

(i) Pre-order: A B D H E I J C F G K

(ii) Post-order: H D I J E B F K G C A

(iii) In-order: H D B I E J A F C K G

------------------------------------------------------------------------------------------------------------------------------------------

10. (0.5 point) Draw the result AVL tree B of insertion of an element with key 54 in the AVL tree A given below. Indicate x, y, z, a, b, c, T0, T1, T2, T3

Page 22: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Solution:

Phase 1: Insertion

a = y, b = x, c = z

Phase 2: Rebalancing

(EXERCISE section) Solution

44

17 62

78 50 32

47 54 88

T0 T1

T2

T3

y

x

z

47 62

88

44

17 78

50 32

54

T0

T1

T2

T3

y

z

x

Page 23: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

1. (0 mark) Below is a tree T. Please construct a binary tree T’ corresponding to T.

Solution:

Page 24: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

2. (0 mark) A (2, 4) tree is given below. Please insert 13 and draw all the steps to obtain the final (2, 4) tree containing 13. Please use the node that PRECEDES the node in inorder traversal if needed.

Solution:

Insert(13)

3 4

5 7 15

8 11 12 13 14 16 17

3 4

5 7 13 15

8 11 12 16 17 14

Page 25: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

------------------------------------------------------------------------------------------------------------------------------------------

3. (0 mark) A (2, 4) tree is given below. Please remove 10 and draw all the steps to obtain the final (2, 4) tree without containing 10. Please use the node that PRECEDES the node in inorder traversal if needed.

Solution:

Node 8 precedes node 10 in in-order traversal:

3 4

5 7

8 11 12 16 17 14

13

15

8

6 13 15

10 5 11 14 17

Page 26: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

Fusion: Transfer:

8

5 6

11 14 17

8

13 15

5 6

11 14 17

15

13

8

13 15

5 11 14 17

6

Page 27: CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook ...flocchin/CSI2510/SoluExercices.pdf · CSI2110 – Theory Assignment 1 – Fall 2010, Prof. WonSook Lee ** Solution

4. (0 marks) Please use removeMin() from the given heap below. Illustrate all the steps in array-based implementation.

Solution:

[3, 6, 8, 10, 21, 9, 10, 12, 20] [20, 6, 8, 10, 21, 9, 10, 12] [6, 20, 8, 10, 21, 9, 10, 12]

[6, 10, 8, 20, 21, 9, 10, 12] [6, 10, 8, 12, 21, 9, 10, 20]