{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# a3sol - McGill University COMP251 Assignment 3 Solution...

This preview shows pages 1–4. Sign up to view the full content.

McGill University COMP251: Assignment 3 Solution Question 1 Idea : A number x in { 0 , 1 ,...,n 3 1 } can be expressed in the form a 2 n 2 + a 1 n + a 0 where 0 a 2 ,a 1 ,a 0 n 1. More precisely, a 2 = x n 2 ,a 1 = x n ⌋− a 2 n,a 0 = x n ( a 2 n + a 1 ) In other words, a 2 ,a 1 ,a 0 are the digits of x written in base n : x = a 2 a 1 a 0 n . Two numbers written in base n are compared just as in base 10: Suppose that x = a 2 a 1 a 0 n and y = b 2 b 1 b 0 n then x > y if and only if a 2 > b 2 or a 2 = b 2 and a 1 > b 1 or a 2 = b 2 and a 1 = b 1 and a 0 > b 0 We can now use Radix sorting. Here the range of each digit is k = n , and each number has 3 digits. The running time for Radix sorting is Θ( d ( n + k )) = Θ( n ) ( d is the number of digits, here d = 3). The algorithm : The algorithm Some-linear-sort below takes as input an array X of integers between 0 and n 3 1, sorts it in non-decreasing order, and store the result in X . The algorithm consists of 3 main parts: Preparing for radix-sort (lines 1 to 9): here we compute the base- n representation for each element in X . Each X [ i ] is represented as row A [ i ] in a 2-dimensional array A . Radix sort the base- n representations (lines 10 to 37): There are 3 digits (thus the for-loop on line 11). Now convert the sorted base- n representations back and store in X (lines 38-41). Some-linear-sort( X ) 1. % initialization 2. n length ( X ) 3. A : an 2-dimensional array of size n × 3 4. % first convert the numbers into base n and store the results in A . Suppose X [ i ] = ( a 2 a 1 a 0 ) n , then A [ i ][2] = a 2 ,A [ i ][1] = a 1 ,A [ i ][0] = a 0 5. for i from 1 to n do 6. A [ i ][2] ←⌊ X [ i ] n 2 7. A [ i ][1] ←⌊ X [ i ] n ⌋− nA [ i ][2] 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8. A [ i ][0] X [ i ] n ( nA [ i ][2] + A [ i ][1]) 9. end for 10. % now radix sort 11. for digit from 0 to 2 do % counting sort on the digit -th digit 12. % initializes counting array C : 13. for j from 0 to n 1 do 14. C [ j ] 0 15. end for 16. % next make each C [ j ] be the number of i such that A [ i ][ digit ] = j : 17. for i from 1 to n do 18. C [ A [ i ][ digit ]] C [ A [ i ][ digit ]] + 1 19. end for 20. % sum up: each C [ j ] will be the number of i such that A [ i ][ digit ] j : 21. for j from 1 to n 1 do 22. C [ j ] C [ j ] + C [ j 1] 23. end for 24. % now put each row A [ i ] into the right place (in B ): 25. for i from n down to 1 do 26. B [ C [ A [ i ]][0]] A [ i ][0] 27. B [ C [ A [ i ]][1]] A [ i ][1] 28. B [ C [ A [ i ]][2]] A [ i ][2] 29. C [ A [ i ]] C [ A [ i ]] 1 30. end for 31. % copy the array B back to A : 32. for i from 1 to n do 33. A [ i ][0] B [ i ][0] 34. A [ i ][1] B [ i ][1] 35. A [ i ][2] B [ i ][2] 2
36. end for 37. end for 38. % now get back the values from the base- n representations and output in X 39. for i from 1 to n do 40. X [ i ] n 2 A [ i ][2] + nA [ i ][1] + A [ i ][0] 41. end for Question 2 (a) Data structure : We are using a data structure for unbounded branching tree. Each node in the tree has the following fields (here we don’t need the parent): 1. left-child: pointer to its leftmost child (NIL if this node is a leaf) 2. right-sibling: pointer to its right sibling (NIL if this node the the rightmost child of its parent) 3. constant: Boolean value, TRUE if this is a leaf and contains a constant value (as oppose to a variable) 4. value: value of the subformula rooted at this node 5. index: if this node is represents a variable (i.e., it is a leaf and constant = FALSE), then the

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}