Sei sulla pagina 1di 3

T10KT - Introduction to Design of Algorithms

Solutions To Selected Problems


Week 2
April/May 2015
Solution to Question 3:
Part 3a: Let the tree be T. The first node on the pre-order list the root of T. Hence, root = M.
Pre-order:

In-order:

This divides the nodes of T in two sets in the in-order list. The nodes on left comes from the left sub-tree,
left(T), of T. The nodes on right comes from the right sub-tree, right(T), of T. Hence,
left(T) with root in it marked:
Pre-order:

In-order:
A C B D
So E is the left child of M.

right(T) with root in it marked:


G

In-order:
K F G J
So L is the right child of M.

Pre-order:

We proceed recursively further to finally construct the following tree T:

Part 3b: The post-order list is:


Pre-order:

In-order:

Post-order:

Part 3c: Can be formulated from the strategy outlined in Part 3a.
Part 3d: Here is a counter-example that shows that for the same pre-order and post-order lists there can be
two different trees. In-order list, however, resolves between them.

Solution to Question 7:
If you could create the convex-hull in o(n log n) time, this would imply, that you can sort n numbers in o(n log n)
time.
You do this with contradiction. You have n numbers (x1 , x2 , , xn ), which you would like to sort. From these
numbers, you create points P in 2D space in some fashion. For example pi = (xi , x2i ).
On these newly constructed points P you call the algorithm for convex hull, which returns you the actual
convex hull.
After that, you simultaneously traverse upper and lower hull, from the lower left-most point in (which you find
in O(n) time), to the upper right-most point. With this traversal you actually sort the numbers. Let pi and
pj be the next points on lower and upper hull, then if xi < xj , you move one step ahead on the lower hull and
add xi to the end of the sorted array (else you take one step ahead on the upper hull and add xj to the array).
You then finish when the traversals meet in the upper right-most point of the convex hull.
The algorithm that I described is run in O(n) time. So if the convex-hull algorithm would be o(n log n), the
sorting of n numbers could be also done in o(n log n) time, which is a contradiction since we know that sorting
n numbers takes at least (n log n) time.
Solution to Question 10:
Lets denote arr[0..N ) to mean the elements of the array from index 0 (inclusive) to N (exclusive).
Sort arr[0..2k). Now we know that arr[0..k) are in their final sorted positions, but arr[k..2k) may still be
misplaced by k.
Sort arr[k..3k). Now we know that arr[k..2k) are in their final sorted positions, but arr[2k..3k) may still
be misplaced by k.
Sort arr[2k..4k).

Until we sort arr[ik..N ), where i = bN/kc, then we are done!

This final step may be cheaper than the other steps when we have less than 2k elements left.
In each step, we sort at most 2k elements in O(k log k), putting at least k elements in their final sorted positions
at the end of each step. There are O(N/k) steps, so the overall complexity is O(N log k).
Solution to Question 15
a Since a domino is two adjacent squares, no domino can cover two squares that are diagonally across each
other. We construct a bipartite graph, where the black and white squares (as shown below) represent the
vertices of two partite sets. A white vertex is adjacent to the neighbouring black vertices. For example,
the vertex corresponding to the white square Y is adjacent to the vertices corresponding to the black
squares A, B, C and D.

A perfect matching in the bipartite graph so constructed is a tiling of the checker-board with dominoes.
b There is no perfect matching because the number of white neighbours of the 11 black squares is 10.

Potrebbero piacerti anche