Solved Problems for LAB: Data Structures - Problem Set 7 | CS 230, Assignments of Data Structures and Algorithms

Material Type: Assignment; Class: LAB: Data Structures; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-2ba
koofers-user-2ba 🇺🇸

5

(1)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS230 Data Structures Handout # 24
Prof. Lyn Turbak Friday, April 30
Wellesley College
Problem Set 7
Due: Friday, May 7
Overview: In this problem set, you will get experience with recurrence equations, running times,
and asymptotic notations; with sorting; with heaps; and with 2-3 trees. All of the problems on this
assignment are pencil-and-paper problems; this problem set requires no programming.
Working Together: You may work with a partner on this assignment. As usual, you are expected
to work on all of the problems together.
Submission: For each problem, your hardcopy submission should include answers to the pencil
and paper problems. There are no softcopy submissions for any problem. Remember to include
a signed cover sheet (found at the end of this problem set description) at the beginning of your
hardcopy submission.
Problem 1 [10]: Asymptotic Notation
Recall the following from class:
Θ(g) is the set of all functions that have the same asymptotic running time as g.
O(g) is the set of all functions that have an asymptotic running time at most that of g.
Ω(g) is the set of all functions that have an asymptotic running time at least that of g.
There are two other relevant notations for comparing asymptotic running times:
o(g) is the set of all functions that have an asymptotic running time strictly smaller than that
of g.
ω(g) is the set of all functions that have an asymptotic running time strictly larger than that
of g.
Here is an informal summary of the notation:
Notation Pronunciation Loosely
fω(g) f is way bigger than g f > g
fΩ(g) f is at least as big as g fg
fΘ(g) f is about the same as g f=g
fO(g) f is at most as big as g fg
fo(g) f is way smaller than g f < g
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Solved Problems for LAB: Data Structures - Problem Set 7 | CS 230 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS230 Data Structures Handout # 24 Prof. Lyn Turbak Friday, April 30 Wellesley College

Problem Set 7

Due: Friday, May 7

Overview: In this problem set, you will get experience with recurrence equations, running times, and asymptotic notations; with sorting; with heaps; and with 2-3 trees. All of the problems on this assignment are pencil-and-paper problems; this problem set requires no programming.

Working Together: You may work with a partner on this assignment. As usual, you are expected to work on all of the problems together.

Submission: For each problem, your hardcopy submission should include answers to the pencil and paper problems. There are no softcopy submissions for any problem. Remember to include a signed cover sheet (found at the end of this problem set description) at the beginning of your hardcopy submission.

Problem 1 [10]: Asymptotic Notation Recall the following from class:

  • Θ(g) is the set of all functions that have the same asymptotic running time as g.
  • O(g) is the set of all functions that have an asymptotic running time at most that of g.
  • Ω(g) is the set of all functions that have an asymptotic running time at least that of g.

There are two other relevant notations for comparing asymptotic running times:

  • o(g) is the set of all functions that have an asymptotic running time strictly smaller than that of g.
  • ω(g) is the set of all functions that have an asymptotic running time strictly larger than that of g.

Here is an informal summary of the notation:

Notation Pronunciation Loosely f ∈ ω(g) f is way bigger than g f > g f ∈ Ω(g) f is at least as big as g f ≥ g f ∈ Θ(g) f is about the same as g f = g f ∈ O(g) f is at most as big as g f ≤ g f ∈ o(g) f is way smaller than g f < g

w consider the following six functions:

  • a(n) = 2n · log 2 (n) + n + 5
  • b(n) = 3n^2 + 7n + 4
  • c(n) = 17
  • d(n) = 2n^ + 1
  • e(n) = log 3 (n)
  • f (n) = 3n − 2

For each of the following five sets, indicate which of the above functions are members of the set:

  1. O(n)
  2. o(n^2 )
  3. Ω(log 2 (n))
  4. Θ(n2 + 5n + 2)
  5. ω(3n)

Problem 2 [15]: Running Times of Membership Below is a table containing brief descriptions of various data structures that hold collections of integers. For each such data structure, think of the best algorithm for determining if an element is a contained in such a data structure. Then indicate the asymptotic worst-case running time (using Θ notation) of the algorithm in the second column of the table. You should phrase your answer in terms of the number n that appears in each description. Unless the description indicates otherwise, you should not make any assumptions about the arrangement of elements in the data structure.

Description Running Time An unordered array of n elements. An array of n elements ordered from smallest to largest. An array of n elements ordered from largest to smallest. An n x n two-dimensional array of unordered elements. An unordered linked list of n elements. A linked list of n elements ordered from smallest to largest. A linked list of n elements ordered from largest to smallest. A binary tree with n elements. A binary search tree with n elements. A balanced binary search tree with n elements. A complete max heap of n elements. A complete min heap of n elements. A binary tree of height n. A balanced binary search tree of height n. A linked list of n balanced binary search trees, each with n elements.

public static int two1 (int n) { if (n == 0) { return 1; } else { return 2 * two1(n - 1); } }

public static int two2 (int n) { if (n == 0) { return 1; } else { return two2(n - 1) + two2(n - 1); } }

public static int two3 (int n) { if (n == 0) { return 1; } else { return two3(n - 1) + two1(n - 1); // Yes, the second call is to two1, not two3. } }

public static int two4 (int n) { // To simplify analysis, assume that the input to two4 is a power of 2. if (n == 0) { return 1; } else if (n % 2 == 0) { int x = two4(n / 2); return x * x; } else { return 2 * two4(n - 1); } }

public static int two5 (int n) { // To simplify analysis, assume that the input to two5 is a power of 2. if (n == 0) { return 1; } else if (n % 2 == 0) { two5(n / 2) * two5(n / 2) } else { return 2 * two5(n - 1); } }

public static int two6 (int n) { if (n == 0) { return 1; } else { return two6(n - 1) + two4(n - 1) // Yes, the second call is to two4, not two6. } }

Figure 1: Six different method for raising 2 to the nth power.

b. [4] Based on the table from part a, determine for each of Bud’s four routines which of the four sorting algorithms it uses. Briefly explain your reasoning.

c. [3] Answer the following for the four sorting algorithms that Bud has implemented:

  • Which sorting algorithm(s) does much better on sorted lists than on randomly ordered lists. Why?
  • Which sorting algorithm(s) does much worse on sorted lists than on randomly ordered lists. Why?
  • Which sorting algorithm(s) takes about the same amount of time on both sorted and ran- domly ordered lists. Why?

Problem 5 [17]: 2-3 Trees In the following parts, you are asked to draw some 2-3 trees. As a simple check to avoid errors, verify that every tree you draw is a legal 2-3 tree.

a. [8] Draw the sequence of 2-3 trees T 0 , T 1 ,... , T 17 that results from inserting the following letters into the empty tree T 0 : T H E Q U I C K B R O W N Y A M S You need only show the final tree that results from each insertion; you need not show the indi- vidual steps that lead to each intermediate tree.

b. [9] Draw the sequence of 2-3 trees T 17 , T 18 ,... T 34 that results from deleting the following letters one-by-one from the initial tree T 17 : T H E Q U I C K B R O W N Y A M S You need only show the final tree that results from each deletion; you need not show the individual steps that lead to each intermediate tree. When deleting a value from a non-terminal node, you may replace it with either its in-order predecessor or in-order successor, whichever is easier.

Problem 6 [25]: A Heap o’ Heaps In the following parts, you are asked to draw some complete min heaps (not max heaps) and leftist min heaps (not max heaps). As a simple check to avoid errors, verify that every binary tree you draw is a legal complete min heap or leftist min heap.

a. [5] Draw as trees the sequence of complete min heaps C 0 , C 1 ,... , C 17 that results from inserting the following letters into the empty heap C 0 : T H E Q U I C K B R O W N Y A M S For example, C 1 should be the result of inserting T into the empty heap C 0 ; C 2 should be the result of inserting H into C 1 ; and so on. Assume that letters appearing earlier in the alphabet are “less than” those appearing later. For example, the root node of C 17 should be A. You need only show the final tree that results from each insertion; you need not show the individual ”bubble up” steps that lead to the final tree.

b. [2] One advantage of complete min heaps is that they can be represented as arrays. Show how the complete min heap C 17 would be represented as an array.

c. [4] Draw as trees the sequence of complete min heaps C 18 , C 19 , C 20 , C 21 that result from dequeuing the four smallest items of C 17. You need only show the final tree that results from each deletion; you need not show the individual ”bubble down” steps that lead to the final tree.

d. [4] Draw as trees the sequence of leftist min heaps L 10 , L 11 ,... , L 18 that results from inserting the following letters into the empty heap L 10 :

T H E Q U I C K

You need not show the intermediate results of individual merge steps.

e. [4] Draw as trees the sequence of leftist min heaps L 20 , L 21 ,... , L 29 that results from inserting the following letters into the empty heap L 20 :

B R O W N Y A M S

You need not show the intermediate results of individual merge steps.

f. [2] Draw as a tree the leftist min heap L 0 that results from merging the two heaps L 18 and L 29.

g. [4] Draw as trees the sequence of leftist min heaps L 1 , L 2 , L 3 , L 4 that results from dequeuing the four smallest items of L 0. You need only show the final tree that results from each deletion; you need not show the individual merge steps that lead to the final tree.

Problem Set Header Page Please make this the first page of your hardcopy submission.

CS230 Problem Set 7

Due Midnight, Friday, May 7

Name:

Date & Time Submitted:

Collaborators (anyone you worked with on the problem set):

By signing below, I attest that I have followed the collaboration policy as

specified in the Course Information handout.

Signature:

In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.

Part Time Score

General Reading

Problem 1 [10]

Problem 2 [15]

Problem 3 [18]

Problem 4 [15]

Problem 5 [17]

Problem 6 [25]

Total