14.6 Exercises

Factual

  1. Problem 2arrow-up-right from the Spring 2016 Midterm 2.

  2. Problem 1d arrow-up-rightfrom the Spring 2015 Midterm 2.

  3. Suppose we have the following WQU with path compression. What is the height of the tree after we call isConnected(8, 9)?

chevron-rightProblem 1hashtag

Solutionsarrow-up-right are linked here and on the course website.

chevron-rightProblem 2hashtag

Solutionsarrow-up-right are linked here and on the course website.

chevron-rightProblem 3hashtag

The resulting tree will have height 1. Every node along the path from 0 to 9 will now have parent 0, and similarly every node along the path from 0 to 8 will also have parent 0.

Conceptual

  1. Which of the following arrays could represent a valid weighted quick union structure?

chevron-rightProblem 1hashtag

Procedural

  1. Define a fully connected WQU as one where all elements are in the same set. What is the maximum and minimum height of a fully connected WQU with 6 elements?

  2. Suppose we have a WQU of height H. What is the minimum number of elements that must be in the WQU?

chevron-rightProblem 1hashtag

The minimum height is always 1 (all elements are connected to the root). The maximum height is 2 (we take the floor of log26\log_2 6).

chevron-rightProblem 2hashtag

We know that Hlog2NH \leq \log_2 N, where NN is the number of elements in the WQU. As such, N2HN \geq 2^H.

Metacognitive

  1. Problem 3arrow-up-right from the Spring 2017 Midterm 2.

  2. Suppose we create a WQU with NNitems, then we perform MCM_C union operations and MUM_U union operations. Using big O notation, what is the runtime of this sequence of operations?

  3. Using the same variables as problem 2, describe a sequence of operations that would result in a runtime of O(N+MU+MC)O(N + M_U + M_C).

  4. Write a int find(int p) method for the WQU with path compression. It should perform path compression as described in lecture: any node on the path from root to our target node should have its parent reset to the root. It takes in the target node p and returns the root of the tree p is in.

chevron-rightProblem 1hashtag

Solutionsarrow-up-right are linked here and on the course website.

chevron-rightProblem 2hashtag

O(N+(MU+MC)logN)O(N + (M_U + M_C)\log N). Each operation takes logN\log N time, and we need NN time to initialize an empty array of size NN.

chevron-rightProblem 3hashtag

Initialize the array as usual. Then, connect the items in sequence: connect(0, 1), connect(0, 2), connect(0, 3), up to connect(0, N). This results in a height-1 tree, on which connect and isConnected run in constant time.

chevron-rightProblem 4hashtag

Last updated