mid.sample

# mid.sample - b Write a set of recurrence relations that...

This preview shows page 1. Sign up to view the full content.

Sample Midterm Exam Questions for CS130B Data Structures and Computer Algorithms Divide-and-Conquer The classical Towers of Hanoi problem begins with a stack of n 1 disks on one of three pegs. No two discs are the same size, and the discs are stacked in order, with the largest on the bottom. Solving the problem requires moving the stack from peg A to peg B in such a way that only one disc is moved at a time and no disc can be placed on top of a disc smaller than itself. Solutions to the classical problem are shown in many texts. We discussed a divide-and- conquer solution in class, which can be stated as: Hanoi ( n, A, B, C ) Hanoi ( n - 1 , A, C, B ) + Hanoi (1 , A, B, C ) + Hanoi ( n - 1 , C, B, A ) . The Cyclic Towers of Hanoi problem adds the following constraint: The pegs are placed at the vertices of a triangle, and discs can only be moved to the adjacent peg in the cyclic order. Thus a single move can transfer a disk from A to B, from B to C, or from C to A. All other moves are illegal. a. Write a pseudocode recursive procedure for solving the Cyclic Towers of Hanoi problem.
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: b. Write a set of recurrence relations that characterize the complexity of the problem. c. Show that the complexity of the problem is O (3 n ). Greedy Even though greed pays oF in Dijkstra’s algorithm, greedy strategies may not ±nd the shortest path for a graph. a. Give an example to show that the following “greedy” strategy doesn’t work in ±nding the shortest path: “at each step, visit the unvisited vertex closest to the one just visited ” (In Dijkstra’s algorithm, the greedy strategy is to visit the unvisited vertex which is closest to the set of vertices that have already been visited ). b. One important reason that Dijkstra’s algorithm works is that the costs of edges are all positive. If it is not true, i.e., there exit edges with negative costs, will Dijkstra’s algorithm work for this particular type of graphs?...
View Full Document

## This note was uploaded on 08/06/2008 for the course CS 130B taught by Professor Suri during the Winter '08 term at UCSB.

Ask a homework question - tutors are online