Docsity

CMPS 214 Problem Set 4: Data Structures and Algorithms, Exercises of Algorithms and Programming

Toggle sidebar
bg1
CMPS 214 Problem Set 4
Style guide and expectations:
Please see the Homework part of the syllabus for guidance
on what we are looking for in homework solutions. We will grade according to these standards.
You should cite all sources you used outside of the course material.
What we expect:
Make sure to look at the
We are expecting
blocks below each problem
to see what we will be grading for in each problem!
Exercises
We strongly suggest you do these on your own. As with any homework problem, though, you
may ask the course sta for help.
1.
For each of the following examples, if the nodes can be colored red or black to make a
legitimate red-black tree, then give such a coloring. If not, then say that they cannot.
[
We are expecting:
For each tree, either an image of a colored-in red-black tree or a
statement No such red-black tree. No justication is required.]
Solution:
Solution for the rst tree:
There is no such red-black tree for the second tree.
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CMPS 214 Problem Set 4: Data Structures and Algorithms and more Exercises Algorithms and Programming in PDF only on Docsity!

CMPS 214 Problem Set 4

Style guide and expectations: Please see the Homework part of the syllabus for guidance on what we are looking for in homework solutions. We will grade according to these standards. You should cite all sources you used outside of the course material.

What we expect: Make sure to look at the We are expecting blocks below each problem to see what we will be grading for in each problem!

Exercises

We strongly suggest you do these on your own. As with any homework problem, though, you may ask the course sta for help.

  1. For each of the following examples, if the nodes can be colored red or black to make a legitimate red-black tree, then give such a coloring. If not, then say that they cannot.

[We are expecting: For each tree, either an image of a colored-in red-black tree or a statement No such red-black tree. No justication is required.]

Solution: Solution for the rst tree:

There is no such red-black tree for the second tree.

  1. Word representations. As described in class, radix sort runs in O(d(n + r )) where n is the number of elements, r is the base, and d is the max length of the elements. For this problem, assume that radix sort uses bucket sort, which does in fact run in O(n +r ) time. [Note: The question looks long but it is in fact very straightforward.] (a) Pretend we want to use radix sort to sort a list of words W in lexicographical (alphabetical) order. All words are padded with space characters (assume space comes before 'a' lexicographically) to make them the same length. For the fol- lowing W , describe what the three variables d, n, and r refer to (your explanation should include some reference to W or its elements) and what their values would be for this problem. (For example, if the formula included a variable x that referred to the length of the rst word in the list of items being sorted, you might say x is the length of the rst word in the list, and is equal to 3 for list W .) W = [the, quick, brown, fox, jumps, over, the, lazy, dog]

[We are expecting: values and descriptions for d, n, and r .] Solution: In the context of sorting W in lexicographical order the radix sort runtime variables are equal to the following: ˆ d is the max length of a word in the list, and is equal to 5 in W. (Examples being quick, brown, and jumps) ˆ n is the number of words in the list, and is equal to 9 in the list W.

ˆ r is the number of lower case letters in the alphabet + the white space char- acter, and is equal to 27. (b) Now suppose we convert each of the strings in W to their ASCII representations (8-bit binary strings, with space mapping to 00100000, so the word 'the' becomes 40 digits long8 digits per character plus 8 digits per padding space to make it as long as the longest word in W ). We still want to use radix sort, and we want to treat these bit strings as literal strings (i.e., do not try to interpret the 8 bit strings into decimal numbers). Now what are the values for d, n, and r?

[We are expecting: values and descriptions for d, n, and r .] Solution: In the context of sorting W represented in ASCII in lexicographical order the radix sort runtime variables are equal to the following: ˆ d is the max bit length of a word in the list, and is equal to 5  8 = 40. ˆ n is the number of words in the list, and is equal to 9 in the list W. ˆ r is the size of the binary alphabet, and is equal to 2.

(e) Not wanting to mess with binary conversions, you decide instead to represent the words in your vocabulary V with "one-hot" vectors (vectors of length n with all 0's except for a single '1' in a position corresponding to a particular word. For example, in W , the word 'the' would be represented as '10000000', since there are eight unique words in the list). Give the new worst-case time complexity of radix sort on the list V , again simplifying as much as possible and explaining (informally) why that is the correct complexity. [We are expecting: a runtime, and a brief justication.] Solution: ˆ d is the max bit length of a word in the list, and is equal to jV j = n, because of the one hot encoding. ˆ n = jV j which is the number of words in the list V. ˆ r is the size of the binary alphabet, and is equal to 2.

So radix sort runs in O(d(n + r )) = O(n(n + 2)) = O(n^2 )

Problems

ˆ Try the problems on your own before you ask questions. ˆ Write up your answers yourself, in your own words. You should never share your typed- up solutions.

  1. [OPTIONAL] The impossible job interview You're interviewing for your dream job at an ecological ethical tech company with healthy snacks. You already passed 28 stages of interviews, and your nal interviewer asks you to design a binary search tree data structure that performs INSERT operations in O(

p log n) time using a comparison-based algorithm. Design such a data structure or prove that this is impossible.

[We are expecting: If possible: An English description of the algorithm and a running time analysis. If impossible: A formal proof that this is impossible.] Solution: If it was possible to design a binary search tree data structure that performs INSERT operations in O(

log(n)) time using a comparison-based algorithm then it would be possible to sort n numbers in O(n

log(n)) by inserting them into this data structure and outputting the result as the in order traversal of the tree. This should be impossible since the lower bound of a comparison sorting algorithms is (n log(n)), so clearly such a data structure with an INSERT operation with a O(

log(n)) running time is impossible.

Goose! A goose comes to you with the following claim. They say that they have come up with a new kind of binary search tree, called gooseTree, even better than red-black trees! More precisely, gooseTree is a data structure that stores comparable elements in a bi- nary search tree. It might also store other auxiliary information, but the goose won't tell you how it works. The goose claims that gooseTree supports the following operations: ˆ gooseInsert(k) inserts an item with key k into the gooseTree, maintaining the BST property. It does not return anything. It runs in time O(1). ˆ gooseSearch(k) nds and returns a pointer to node with key k, if it exists in the tree. It runs in time O(log(n)). ˆ gooseDelete(k) removes and returns a pointer to an item with key k, if it exists in the tree, maintaining the BST property. It runs in time O(log(n)). Above, n is the number of items stored in the gooseTree. The goose says that all these operations are deterministic, and that gooseTree can handle arbitrary comparable objects.

You think the goose's logic is a bit loosey-goosey. How do you know the goose is wrong?

Notes: ˆ You may use results or algorithms that we have seen in class without further justication. ˆ Since the gooseTree is still a kind of binary search tree, you can access the root of gooseTree by calling gooseTree.root(). [We are expecting: Formally prove that the goose is wrong by showing that we can solve an algorithmic problem that we know the lower bound for with this data structure.] Solution: This is a similar issue to problem 1. Assume it was possible to design a binary search tree data structure, in this case called gooseTree, that performs gooseInsert(k) in O(1) time, which inserts an element into its correct position in the binary search tree. Since the paradigm of the gooseTree structure and its operations fall under the paradigm of comparisons in order to achieve its results then it would be possible to sort n numbers in O(n) by inserting them into this data structure and outputting the result as the in order traversal of the tree (we can traverse the tree because we can access its elements, as stated in the notes). This should be impossible since the lower bound of a comparison sorting algorithms is (n log(n)), so clearly the gooseInsert(k) operation with a O(1) running time is impossible.

  1. Prove that any comparison-based algorithm for merging two sorted lists of length m and n (m  n), requires (n log (1 + mn )) comparisons. We will be using the decision tree method to prove this lower bound. (a) What is the number of leaves in the decision tree? (i.e. How many possible ways are there to merge two lists of size m; n?) [We are expecting: A value in terms of m; n, AND a brief justication of how you derived this number] Solution: There are

(n+m n

ways to take two sorted lists of n and m elements respectively and generate a sorted list of n + m elements. So there are

(n+m n

= (^) n!((nn++mm)!n)! = (n n+!mm!)! leaves in the decision tree. One way to see how to derive this value is to phrase the problem slightly dierently: In how many ways can you arrange a list with n "L"'s and m "R"'s. Answering such a problem combinatorically will yield the desired value. (b) Prove that merging requires (n log (1 + mn )) comparisons. [We are expecting: Mathematical derivation of the nal result. Explicitly write all the steps.] Solution: A decision tree with leaves

(m+n m

must have a height of at least log 2

((m+n m

, so:

h  log 2

(n + m)! n!  m!

h =

log 2

(n + m)! n!  m!

h = (log 2 ((n + m)!) log 2 (n!) log 2 (m!))

h =

(n + m) log 2

n + m e

n log 2

(n e

m log 2

(m e

Stirling's approximation

h = ((n + m)(log 2 (n + m) log 2 (e)) n(log 2 (n) log 2 (e)) m(log 2 (m) log 2 (e))) h = (n log 2 (n + m) + m log 2 (n + m) n log 2 (n) m log 2 (m))

h =

n log 2

n + m n

  • m log 2

n + m m

h =

n log 2

m n

  • m log 2

n m

h =

n log 2

m n

  • m

) (^) n m

< 1 since n < m

h =

n log 2

m n

So merging requires

n log 2

1 + mn

comparisons.