

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Computer Science 231 Spring 2009 Due: Tuesday, May 12 Reading. CLRS Sections 17.1, 17.2, 17.3, 17.4, 22.1, 22.2, 22.3, 22.4, pages 405 -- 429, 525 -- 552. CLR Sections 18.1, 18.2, 18.3, 18.4, 23.1, 23.2, 23.3, 23.4, pages 356 - - 367, 463 -- 488. Exercise 9-1. Below is an implementation of a first-in-first-out (FIFO) queue that represents the queue as a pair of two lists, front and back: Empty-Queue() return new record {front = EmptyList(),back = EmptyList()} Empty-Queue?(Q) return EmptyList?(front[Q]) and EmptyList?(back[Q]) Enq(x, Q) back[Q] โ Prepend(x, back[Q]) Deq(Q) if EmptyList?(front[Q]) then front([Q])โ Reverse(back[Q]) {linear-time Reverse} back([Q]) โ EmptyList() if EmptyList?(front[Q]) then error "empty queue" else h โ Head(front[Q]) front[Q] โ Tail(front[Q]) return h a. Show the state of the queue (i.e., the \ front and back lists) after each of the operations in the following sequence: q โ Empty-Queue() Enq(1,q) Enq(2,q) Enq(3,q) Deq(q) Enq(4,q) Enq(5,q) Deq(q) Deq(q) Deq(q)
Assignment 9 Page 2 Computer Science 231 Enq(6,q) Deq(q) Deq(q) b. Using the banker's (accounting) method, argue that the amortized cost of Enq and Deq are each O(1). c. Using the physicist's (potential) method, argue that the amortized cost Enq and Deq are each O(1). Exercise 9-2. Draw the 2- 3 - 4 tree (that is the B-tree with t =2) that result from inserting the following letters one-by-one (from left-to-right) into an empty tree: A L G O R I T H M S You need only show the final that results from inserting all of the letters; do not show the intermediate tree after inserting each letter. b. Draw the sequence of 2- 3 - 4 trees that are obtained by deleting the letters of A L G O one by one from the tree given in part a. Exercise 9 - 3. Consider the following directed graph G:
In the following problems, you should assume that G is represented as a collection of adjacency lists, and that vertices are ordered alphabetically within each adjacency list. a. Draw the tree that is induced by performing breadth-first search starting at node d. Indicate clearly the order of actions taken. Show the queue's contents at all times. b. Draw the tree that is induced by performing a depth-first-search starting at node d. Label each vertex by its discovery and finishing times and classify each edge as tree, back, forward or cross as in lecture.