Uploaded by Sachin Tiwari

hw2-sol

advertisement
Question 1
Given a matrix A = (anm ), suppose that each row is sorted into increasing
order and then each column is sorted into increasing order. Do the rows
remain sorted into increasing order? Justify your answer.
———————
After the rows are sorted we have the property:
ai,j < ai,j+1 for all i, j 1≤ i, j, ≤ m

A=





a11 a12
a21 a22
..
..
.
.
an1 an2
. . . a1m
. . . a2m
..
...
.
. . . anm






When sorting the columns, consider only the first column. When we sort
this by column, the rows in the column will be sorted, since there is only 1
element in each row.

A=





a11
a21
..
.






an1
Clearly each row in this previous matrix is sorted, and we now have the
property:
ai,j < ai+1,j for all i, j 1≤ i, j, ≤ n
Now consider the next column.

A=





a11 a21
a21 a22
..
..
.
.
an1 an2






The first element in the second column, a21 , will be the smallest in the
column. The first element in the first column, a11 , will also be the smallest
in its column. Since the rows were sorted in the beginning, there must exist
at least one element in the first column which is less than or equal to a21 ,
therefore a21 must be greater than or equal to a11 . Similarly, there must exist
at least 2 elements less than or equal to a22 , one element that came before
it in it’s original row, and since a22 ≥ a21 , a22 ≥ a11 . It follows that for any
element a2k , 1 ≤ k ≤ n there are k elements in the first column that are less
than or equal to it. Therefore since both columns are sorted a2k must be
≥ a1k , and each row in the two column matrix is sorted.
This can be extended to m columns, which shows that the rows remain in
sorted order.
Question 3
Suppose that instead of selecting the k th largest name, we are interested in k
largest names, but not their relative order. Can this be done in linear time?
———————
This can be done in linear time.
If we run the Worst-case linear-time Select(i, n) algorithm on for i = (n-k+1)
we can get the k th largest name in linear time.
After we have this name we can then use it as a pivot to partition the rest of
the array. We can compare each element against this name. If the element
is greater than the name we output it. This can be done in O(n).
The output will be the k th largest names, and the overall running time is
linear.
Question 4
18.2-1
Show the results of inserting the keys
F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z ,E
in order to an empty B-tree with minimum degree 2.
———————
1. Insert F, Q, S
FQS
2. Insert K, C
Q
CFK S
3. Insert L, H, T, V
FQ
C
HKL
STV
4. Insert W
FQT
C
HKL
S
VW
5. Insert R, M, N
Q
FK
C
H LMN
T
RS
VW
6. Insert P, A, B, X
Q
FKM
ABC H
T
L
NP
RS VWX
7. Insert Y
Q
FKM
ABC H
TW
L
NP
RS
V XY
8. Insert D, Z, E FINAL CONFIGURATION
KQ
BF
A
CDE H
M
L
NP
TW
RS
V
XYZ
18.2-3
Explain how to find the minimum key stored in a B-tree and how to find the
predecessor of a given key stored in a B-tree.
———————
Minimum Key:
Start from the root, traverse down the left most child until you reach a leaf.
Take the left most element as the minimum key.
Predecessor:
There are a few cases we have to consider when attempting to find an element’s predecessor:
1. The element we selected is in a leaf
• If the element is not the left most element in the node, return the
element directly to the left.
• If the element is the left most element in the node, go up the tree
until you find an element in the parent node to the left of the link
and return this element as the predecessor. If you reach the root
and find no such parent then you attempted to find the predecessor
of the minimum element, which does not exist, so return null.
2. The element we selected is in an inner node
• Traverse to the left child node of the element. If you are in a leaf,
return the right-most element in the leaf, otherwise traverse down
rest of the tree by the right child until you reach a leaf node and
then return the right-most element.
Download