0% found this document useful (0 votes)
12 views

Problem Set 1

Uploaded by

Bubble jumpo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Problem Set 1

Uploaded by

Bubble jumpo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem 1.

If only all problems were like this one


a) Consider the min-heap shown below:

i. Insert 9 in the minheap. Show the intermediate steps in your solution (you don’t need to
redraw the heap every time, simply indicate which changes happen at every step, and draw
the final result).
ii. From the updated minheap of part i., remove the minimum. Show the intermediate steps in
your solution (you don’t need to redraw the heap every time, simply indicate which changes
happen at every step, and draw the final result).

b) Consider the following sequence: 5, 12, 8, 4, 11, 15, 1.


i. Insert the above numbers, in the given order, in a binary search tree. Show the intermediate
steps.
ii. Suppose we traverse the tree using BFS and print the nodes. Show the order in which the
nodes will be printed.

Problem 2. Real Functions


In this problem, you are given an abstract class (see file Prob2.cpp) that represents a real-valued
function f : R → R. The following well-known facts will be useful: for any n ∈ N,
xn+1
Z
xn dx = (+constant),
n+1
d n
x = nxn−1 .
dx
The class RealFunction has three purely virtual functions:
ˆ double compute (double x) const, which, given a value for the input x, returns f(x).
R x2
ˆ double computeArea(double x1, double x2) const, which, given an interval [x1,x2], returns x1
f (x)dx.

ˆ RealFunction* derivative() which returns (a pointer to) the derivative of the function. (Note that
the return type is Function *, not Function, because Function is an abstract class so we will not
use it as a return type.)
a) Create a class Linear which inherits from RealFunction. Linear represents linear functions, i.e.,
functions of the form f (x) = a1 x+a0 . Therefore the class Linear should have two member variables
to represent a1 and a0 . Implement an appropriate constructor for the class, and implement the
above three functions.

1
b) Create a class Polynomial which inherits from RealFunction
Pn . Polynomial represents polynomial
functions, i.e., functions of the form f (x) = a0 + i=1 ai xi , for some n ∈ N. Therefore the
class Polynomial should have a member variable of type vector coresponding to the coefficients.
Implement an appropriate constructor for the class, and implement the above three functions.

– You may decide that index 0 of the vector corresponds to a0 or it corresponds to an ; both are
acceptable choices; please indicate in comments in your solution which one you are using.
– For full grade, do not use pow in the compute function.

Problem 3. Min-Heap Update


Consider a min-heap of size n, which we implement as an array. We have seen in class how to insert
a new element and how to extract the minimum. In this problem, you are asked to implement two more
operations:

a) Update(A,index,newValue), which is given a min-heap A and two integers index and newValue. It
updates the value of the node at position index to the provided value newValue. For example,
consider the min-heap given in Problem 1-b. The node at index 1 is the node which contains the
value 10 (index 1 is the left child of the root). So if we call Update(A,1,4), the value 10 should be
changed to 4. This change may violate the min-heap property so we may need to fix it.
Provide an algorithm (in pseudo-code) to implement Update(A,index,newValue) (i.e., it changes the
value of the node, and if a min-heap property is violated, it fixes it). Explain why your algorithm
works.

b) Remove(A,index), which is given a min-heap A and an integer index. It removes the node at the
corresponding position. For example, in the min-heap of Problem 1-b, Remove(A,3) should remove
the node that contains the value 12.

Problem 4. Symmetric Difference


Consider two integer arrays A and B of sizes m and n respectively. The symmetric difference between
A and B is the set of elements that are either in A or in B, but not in both (i.e., it is the union of the
elements A and B minus their intersection).
Example: say A = [1, 5, 3, 8, 9] and B = [3, 4, 1, 6], then the symmetric difference is given by
the elements {5, 8, 9, 4, 6} (5 is in A but no in B, similarly for 8 and 9; 4 and 6 are in B but not in A; 1
and 3 are excluded because they are in both A and B).

Provide an algorithm SymmDiff(A,B) (in pseudo-code) which, given two arrays A and B (we have acccess
to their sizes through A.size and B.size), returns an array corresponding to their symmetric difference.

Faster algorithms are worth more points. For full grade, your algorithm should run in expected
O(n + m) time.

2
Problem 5. Range Queries
Given a data structure D storing integers, a range query is a query of the following form: given a and
b where a ≤ b, return an array containing all values x in D such that a ≤ x ≤ b.
For example, say D contains {3, 8, 2, 9, 7, 5}. Then RangeQuery(D,1,6) should return [3,2,5]. We
do not care about the order of the elements in the output, i.e., [2,3,5], [2,5,3], or all other permutations
of the array [3,2,5] are correct answers. Furthermore, we do not care about duplicates, i.e., if 3 appears
twice in D, it can appear once or twice in the output, both are considered correct. (For simplicity, you
may assume that D does not contain duplicates.)
a) Suppose D is an array. Write in pseudo-code an algorithm to implement RangeQuery(D,a,b). What
is its running time?
b) Design a data structure D (or use one of the data structures we learned) so that RangeQuery can
be answered efficiently, and inserting a new element in D is also efficient. Write in pseudo-code an
algorithm to implement RangeQuery. What is its running time? What is the running time of insert?

Problem 6. Applications: RB Tree Insert and Dijkstra


a) Consider the red-black tree shown below:

i. Show the updated tree after inserting 6.


ii. Insert 5.5 in the updated tree.
b) Consider the weighted directed graph shown in the figure below. Using Dijkstra’s algorithm, find
the shortest distances from node (a) to all other nodes. Draw a table to show the updates to the
array of distances. What is the shortest path between (a) and (d)?

You might also like