CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf ·...

16
1 CS/SE 2C03. Sample solutions to the assignment 3. Total of this assignment is 192 pts. Each assignment is worth 7%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. The deadline for a complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the graph below apply Ford-Fulkerson algorithm. Show all steps. Solution:

Transcript of CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf ·...

Page 1: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

1

CS/SE 2C03. Sample solutions to the assignment 3. Total of this assignment is 192 pts. Each assignment is worth 7%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. The deadline for a complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the graph below apply Ford-Fulkerson algorithm. Show all steps.

Solution:

Page 2: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

2

2.[10] For the graph above apply Edmonds-Karp algorithm. Show all steps. Solution:

3.[10] Exercise 2 on page 415 of the course text.

Page 3: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

3

4.[10] Exercise 5 on page 416 of the course text. Solution: NO

5.[6] a.[3] Show the binary search tree that results if we insert into an empty set,

represented as a binary search tree, the elements 9,10, 2,12,8, 7,1, 13, 4, 5, 6, 3, 11. Give all steps.

9 / \ 2 10 / \ \ 1 8 12 / / \ 7 11 13 / 4 / \ 3 5 \ 6

b.[3] What is the result of 6 successive DELETE(MIN) operations on the tree from (a)? Give all steps.

Page 4: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

4

9 / \ 8 10 / \ 7 12 / \ 11 13 6.[10] a.[5] Show the 2-3 tree that results if we insert into an empty set, represented as a 2-3 tree, the elements 7, 11, 15, 9, 10, 14, 1,12, 3, 4, 8, 2, 13, 0, 6, 5, 56. Give all steps. Steps not included in this solution

4 11

2 - 7 9 14 - 1 - 3 - 5 6 8 - 10 - 12 13 15 56 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 5 b.[5] Show the result of deleting 9, 1 and 11 from 2-3 tree that results from (1a). Give all steps.

Page 5: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

5

Page 6: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

6

7.[20] Redo 4 for Red-Black trees. a[10]

Page 7: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

7

b.[10]

8.[15] Show (step by step) the AVL trees formed by inserting the numbers 9, 8, 12, 11,

7, 6, 1, 2, 5, 4, 10, 3, 13. 9 9 9 9 9 insert(9) insert(8) insert(12) insert(11) insert(7) 8 8 12 8 12 8 12 11 7 11 9 9 9 9 9 insert(6) repair insert(1) insert(2) repair 8 12 7 12 7 12 7 12 7 12 7 11 6 8 11 6 8 11 6 8 11 2 8 11 6 1 1 1 6 2

Page 8: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

8

9 9 9 insert(5) repair insert(4)

7 12 6 12 6 12

2 8 11 2 7 11 2 7 11 1 6 1 5 8 1 5 8 5 4 6 6 6 repair insert(10) repair

2 9 2 9 2 9 1 5 7 12 1 5 7 12 1 5 7 11

4 8 11 4 8 11 4 8 10 12 10

6 6 6 insert(3) repair insert(13)

2 9 2 9 2 9 1 5 7 11 1 4 7 11 1 4 7 11

4 8 10 12 3 5 8 10 12 3 5 8 10 12 3 13

Page 9: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

9

9.[10] Consider the following modification of the ADT Dynamic Set: we have now the

following operations: MEMBER(x), DELETEMIN, INSERT(x), DELETEMAX, and AVERAGE, which is a function returning the average value of the elements in set S.

What model(s) would you recommend and why? Provide big O estimations of the ADT operations for each implementation.

We need to have two integer variables, sum and counter, and any efficient representation of a dynamic set, for example AVL tree, 2-3 tree, or Red- Black tree. The idea is to keep changing the sum and size of the set when doing insertion or deletion. It only adds some constant time to both operations. Therefore the running time is determined by the choice of dynamic set representation, i.e. O(log |S|). AVG(S) = sum/counter, so it is O(1). 10.[30] Given a set S of n positive integers and a positive number W, we want to produce

the maximal number of pairs {a,b} from S such that a+b ≤W. (No member of S may be used twice). For example, if S = {8,9,6,1,4,7} and W=10, an optimal solution would be {{1,7},{4,6}}. Design an O(n log n) algorithm for this problem that uses the following greedy strategy - repeatedly pick a (any way you like) and pair it with the largest element b such that a+b ≤W. Part of your task is to decide exactly how the choices of a will be made. Prove your algorithm finds an optimal solution.

Solution:

Page 10: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

10

Page 11: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

11

Page 12: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

12

11.[10] Sort the following numbers using Quick Sort: 10 21 13 30 16 5 10 25 3 20 8 40 0 7 30 16 5 11 12 10

Use the following algorithm for choosing pivots: the largest from the first two different values of A[i], starting from A[1]. Illustrate all the steps.

Solution:

10 21 13 30 16 5 10 25 3 20 8 40 0 7 30 16 5 11 12 10 partition v = 21 10 10 13 12 16 5 10 11 3 20 8 5 0 7 16 30 40 25 30 21

10 10 13 12 16 5 10 11 3 20 8 5 0 7 16 30 40 25 30 21

partition v = 10 partition v = 40

7 0 5 8 3 5 10 11 16 20 12 13 10 10 16 30 21 25 30 40

7 0 5 8 3 5 10 11 16 20 12 13 10 10 16 30 21 25 30 40

v = 7 v=11 v=30 5 0 5 3 8 7 10 10 10 20 12 13 16 11 16 25 21 30 30

5 0 5 3 8 7 10 10 10 20 12 13 16 11 16 25 21 30 30 v=5 v=8 v=20 v=25 3 0 5 5 7 8 16 12 13 16 11 20 21 25 3 0 5 5 7 8 16 12 13 16 11 20 21 25 v=3 v=16 0 3 11 12 13 16 16 0 3 11 12 13 16 16

Page 13: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

13

v=12 11 12 13 11 12 13 v=13 12 13 12 13 result: 0, 3, 5, 5, 7, 8, 10, 10, 10, 11, 12, 13, 16, 16, 20, 21, 25, 30, 30, 40 12.[15] Redo 5 for B-trees with: a.[5] minimum degree t=2 b.[5] minimum degree t=3 c.[5] minimum degree t=4

a

Page 14: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

14

b.

Page 15: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

15

c.

Page 16: CS/SE 2C03. Sample solutions to the assignment 3 ... - McMaster …se2c03/Assignments/as4sol.pdf · complaint is 2 weeks after the assignment is marked and returned. 1.[10] For the

16

13. [3] Demonstrate the insertion of keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a hash table with

collisions resolved by chaining. Let the table have 9 slots, and the hash function be h(k) = k mod 9.

0 →€ 1 →28→19→10→€ 2 →20→€ 3 →12→€ 4 →€ 5 →5→€ 6 →15→33→€ 7 →€ 8 →17→€

14. [3] Consider a hash table of size m=1000 and a corresponding hash function ( ) ( mod1)h k m kA

for ( 5 1) / 2A . Compute the locations to which the keys 61, 62, 63, 64, and 65 are mapped.

61 62 63 64 65 700 318 936 554 172

15.[30] The solutions to programming exercises will not be posted.