As Needed For Those Other Classes (So If You Get Lucky and Find A Solution in One of
As Needed For Those Other Classes (So If You Get Lucky and Find A Solution in One of
As Needed For Those Other Classes (So If You Get Lucky and Find A Solution in One of
Name:____________________________________________________________
This is an open book, open note exam: you may consult ONLY our class text and
your notes from this class. Texts and notes related to other classes may be consulted
as needed for those other classes (so if you get lucky and find a solution in one of
them by accident, that’s OK – you just cannot go to those books with the intention of
finding a solution to a problem on this exam). You may not consult any other
resources or people regarding the contents of this exam during the completion of the
exam. To be specific, the term “completion of exam” refers to the time from when
you first examine the contents of any part of the exam until the moment that the exam
has been turned in to me.
All binary trees on this exam are assumed to be proper. Also, so that all of our
definitions are consistent with the text, the depth of a node v is recursively defined to
be 0 if v is the root of a tree, and depth(parent) + 1 if v is not the root. The height of a
tree is the maximum depth among the depths of all external nodes in the tree.
Finally, the height of a node v is 0 if v is an external node, and one plus the maximum
depth of a child of v if v is not an external node.
1. (12 points) Prove the following using the precise definition of the “Big-Oh”
concept (i.e. tell me what c is as well as the value of n0).
2 d
a. If f(n) is a polynomial of degree d (i.e. f ( n)=a0 + a1 n+ a2 n + L+ ad n
) then f(n) is O(nd).
b. nx is O(an) for any fixed x>0 and a > 1.
c. log x n is O(n y ) for any fixed constants x>0 and y >0 .
2. (8 points) Let T be a proper binary tree with n nodes. Define the lowest common
ancestor (LCA) between two nodes v and w as the lowest (i.e. farthest from the
root) node in T that has both v and w as descendents (where we allow a node to be
a descendent of itself). Given two nodes v and w, describe a linear time
algorithms for finding the LCA of v and w.
5. (8 points) Let A be a collection of objects. Assuming that these objects are stored
in computer memory and that the maximum amount of storage required for any
object is d bits, describe an efficient method for removing all duplicates from A
into a set. That is, remove all duplicates from A. What is the running time of this
method? (Note: It is not assumed that the objects in the collection are
comparable, nor that they are even numbers. For example, some could be
matrices, others could be integers and still others could be strings. Your
algorithm must work for any such collection.)
6. (12 points) Prove the following: If G is a graph with n vertices and m edges, then:
a. If G is a tree, m = n-1.
b. If G is connected, then m ≥ n-1.
c. If G is a forest, then m ≤ n – 1.
7. (8 points) Given a free tree T and a node v of T, the eccentricity of v is the length
of the longest path from v to any other node of T. A node of T with minimum
eccentricity is called a center of T.
a. Design an efficient algorithm that, given an n-node free tree T, computes a
center of T.
b. How many distinct centers can a free tree have?
8. (6 points) Consider the following game. There are N cards face down on a table.
Each card has a number on it, which need not be an integer and need not be
positive. You know nothing about the values on the cards, i.e. you have no
knowledge of the lowest nor highest card. You are to play the following game.
You can turn cards over one at a time and look at the value on the card. Once a
card is turned over, it stays turned over. You win the game if and only if the last
card you turn over is the card with the maximum number. Devise a strategy that
will let you win, on average, at least one quarter of the time. Put another way,
your strategy must guarantee that the probability that you win any game,
regardless of the number of cards on the table, is at least 0.25.
9. (8 points) Consider the single machine scheduling problem. That is, we have a
single computer (with a single processor) and are given a set T of tasks specified
by their start times and finish times (as in the task scheduling problem). We wish
to maximize the number of tasks that this machine performs. Design a greedy
algorithm that solves this problem. What is the running time of the algorithm?
10. (6 points) Design a divide and conquer algorithm for finding the minimum and
maximum of n elements using no more than 3n/2 comparisons.
11. (8 points) Two ordered trees T′ and T″ (both not necessarily binary) are said to be
isomorphic if one of the following holds
a. Both T′ and T″ consist of a single node
b. Both T′ and T″ have the same number k of subtrees, and the ith subtree of
T′ is isomorphic to the ith subtree of T″, for i = 1,2,3,…
If n is the number of nodes contained by the “larger” of the two trees, design a
O(n) time algorithm that tests whether two given ordered trees are isomorphic. So
you are clear on the definition of isomorphic, two trees are isomorphic if and only
if there is a one-to-one map of the vertices of one tree onto the vertices of the
other and such that corresponding vertices have the same number of children.
This means that they have the same exact structure but with possibly different
vertex labels.
12. The shortest path between two nodes, u and v, in a weighted undirected graph
may not be unique. Assuming nonnegative edge weights and a graph with n
nodes and m edges, give an O(m log n) time algorithm for finding a shortest path
between u and v containing the least number of edges.
13. Find an O(n) time algorithm for computing the common intersection area of n
given rectangles with sides parallel to the x and y axes. Your algorithm should
report that no single common intersection area exists if this is the case. You may
assume that each rectangle is given as a 4-tuples, where the first two numbers
represent the coordinates of the lower left corner of the rectangle, the third
number is the height, and the fourth is the width. So, e.g. (4.5,3,1.5,2) is the
rectangle with lower left corner (4.5, 3), height 1.5, and width 2.
14. Find an O(n) time algorithm for "randomizing" the integers from 1 to n inclusive.
That is, when your algorithm is finished, it should produce an array of size n that
contains all of the numbers from 1 to n but in a random order.
15. Let G be a connected graph. Prove that the following are equivalent:
a. G is biconnected.
b. For any two vertices of G there is a simple cycle containing them.
c. G does not have separation vertices of separation edges.
d.
16. A graceful labeling of the nodes and edges of a rooted tree (not necessarily
binary) satisfies the following constraints:
a. All the node labels are unique;
b. The node labels form a contiguous sequence of positive integers
beginning with 1;
c. All the edge labels are unique
d. The edge labels form a contiguous sequence of positive integers
beginning with 1;
e. Each edge label is equal to the absolute value of the difference between
the labels of its two incident nodes.
Prove that every infinite tree has a graceful labeling (assume that an infinite tree
has finite degree at each node, but contains at least on infinite path from the root).
Warning: This problem is difficult! Unlike the Microsoft problem on the
previous exam, the solution is not a clever trick. Give it a genuine effort, but
don’t spend days working on it. I just like to always give people something
difficult to chew on.
17. Suppose that each row of an n n array A consists of 1’s and 0’s such that, in any
row of A, all the 1’s come before any 0’s in that row. Assuming A is already in
memory, describe a method running in O(n log n) time (not O(n2) time!) for
counting the number of 1’s in A.
18. Suppose we are given an n-element sequence S such that each element in S
represents a different vote in an election, where each vote is given as an integer
representing the ID of the chosen candidate. Suppose further that the number of
candidates running, k, is known to us beforehand, and that k < n. Suppose also
that the representing number of the candidates are not necessarily the integers
from 1 through k (i.e. we know there are k candidates, but we don’t know their
numbers beforehand). Describe an O(n log k) time algorithm for determining
who wins the election.
19. Let A and B be two sequences of n integers each. Given an integer x, describe an
O(n log n) time algorithm for determining if there is an integer a in A and an
integer b in B such that x = a + b.
20. Give a sub-linear time algorithm for finding the median among all the elements in
two given (equal-sized) sorted arrays.
21. Define the edit distance between two strings X and Y of length n and m,
respectively, to be the number of edits that it takes to change X into Y. An edit
consists of a character insertion, a character deletion, or a character replacement.
For example, the strings “algorithm” and “rhythm” have edit distance 6. Design
an O(nm) time algorithm for computing the edit distance between two strings X
and Y.
22. An independent set of an undirected graph G = (V,E) is a subset I of V, such that
no two vertices in I are adjacent. That is, if u and v are in I, then (u,v) is not in E.
A maximal independent set M is an independent set such that, if we
were to add any additional vertex to M, then it would not be independent any
longer. Every graph has a maximal independent set (though you need not prove
this). Give an efficient algorithm that computes a maximal independent set for
graph G.