Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

COMP 271 Design and Analysis of Algorithms Fall 2009 Midterm Exam

1. Quick-Answer Questions (4 5 = 20 pts) For each question below, write down the asymptotic (using ) result. You do not need to justify your answers.
n

1.1 What is
i=1

2 3

1.2 What is the solution of the recurrence T (n) = 8 T (n/2) + (n), T (1) = 1 ? You can assume that n is a power of 2. 1.3 How many random numbers does Randomized Quicksort need to generate when sorting n elements? 1.4 Suppose that one day you got a magic union-nd structure that supports CreateSet, Union, and Find-Set operations all in O(1) time per operation. If you use this magic union-nd structure in Kruskals algorithm on a graph whose edges are given in a weight-sorted order, what will be the running time of Kruskals algorithm? Express your result in terms of |V | and |E|, the number of vertices and the number of edges in the graph, respectively. 1.5 In an undirected graph with n vertices and no cycles, what is the maximum possible number of edges? 2. (20pts) In Homework 1 you designed an algorithm to nd a local minimum in an array. Now you are to do the same on a binary tree. More precisely, you are given a complete binary tree T . A complete binary tree of height h has exactly n = 2h 1 nodes. Each node stores a real number. We assume that all these numbers are distinct. A local minimum on a binary tree is a node whose number is smaller than all of its neighbors. Note that the root of the tree has two neighbors, a leaf has one neighbor, while any other node has three neighbors. In the example below, the nodes numbered with 1, 5, and 6 are all local minima. Your algorithm only needs to nd one if there are multiple local minima. Note that there is at least one local minimum no matter how the numbers are stored; in particular the global minimum is always a local minimum, but nding the global minimum takes O(n) time. For full credits your algorithm should run in O(log n) time.

5 8 2
14 11

9 6

4 1 16 18 13 19 7 10
3. (20 pts) To celebrate the nishing of the midterm exams you get drunk at the university bar. Then you try to get back to your dorm using your remaining sense of directions. To make things simple lets assume that UST is on an innite one-dimensional line as illustrated below, and there are n steps from the bar to the dorm. Starting from the bar you begin to perform a random walk towards your dorm. As you get soberer your sense of directions improves: During the i-th second, with probability 1 1 you take i one step towards your dorm, while with probability 1 you take one step away from your i 1

dorm. After n seconds, how far away from the dorm will you be (in number of steps) in expectation?

UST bar n steps

dorm

prob.

1 i

You

prob. 1 1 i

4. (20 pts) Given a connected, undirected graph G = (V, E). In Homework 2 you designed an algorithm to detect if there is a cycle in G. Now you are in addition given a vertex s in the graph, and your job is to design an algorithm that detects if there is a cycle in G that passes through s. Your algorithm just needs to output a yes/no answer. For example, the graph below on the left has a yes answer, and the one on the right has a no answer. Prove the correctness of your algorithm (note that you need to prove correctness for both the yes case and the no case), and derive its running time. For full credits your algorithm should run in O(|E|) time.

5. (20 pts) Let G be a connected undirected graph with weights on the edges. Assume that all the edge weights are distinct. Let u be any node in G, and let (u, v) be the edge with the smallest weight among all edges connecting to u. Prove that any MST of G must contain the edge (u, v). You have to prove this from rst principles, i.e., you are not allowed to use the MST Lemma or assume the correctness of Kruskals or Prims algorithm.

You might also like