Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

University of California Computer Science Division CS61B Final Examination, Exams of Data Structures and Algorithms

The final examination for the university of california computer science division cs61b course in fall 2000. The examination consists of 7 problems related to data structures, algorithms, and java programming. Students are allowed to consult any books, notes, calculators, or other inanimate objects (other than computers) available to them.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIVERSITY OF CALIFORNIA
Department of Electrical Engineering
and Computer Sciences
Computer Science Division
CS61B P. N. Hilfinger
Fall 2000
Final Examination
Your exam should contain 7problems on 13 pages. Officially, it is worth 50 points.
This is an open-book test. You have three hours in which to complete it. You may consult
any books, notes, calculators, or other inanimate objects (other than computers) available to you.
You may use any program text supplied in lectures, problem sets, or solutions. Please write your
answers in the spaces provided in the test. Make sure to put your name, login, and lab section in
the space provided below. Put your login and initials clearly on each page of this test and on any
additional sheets of paper you use for your answers.
Read all the questions carefully to begin with, and first try to answer those parts about which
you feel most confident.
Your name: Login:
Login of person to your left: Login of person to your right:
Discussion section number or time: TA:
1. /12
2. /10
3. /10
4.
5. /5
6. /6
7. /7
TOT /50
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download University of California Computer Science Division CS61B Final Examination and more Exams Data Structures and Algorithms in PDF only on Docsity!

UNIVERSITY OF CALIFORNIA

Department of Electrical Engineering and Computer Sciences Computer Science Division

CS61B P. N. Hilfinger Fall 2000

Final Examination

Your exam should contain 7 problems on 13 pages. Officially, it is worth 50 points. This is an open-book test. You have three hours in which to complete it. You may consult any books, notes, calculators, or other inanimate objects (other than computers) available to you. You may use any program text supplied in lectures, problem sets, or solutions. Please write your answers in the spaces provided in the test. Make sure to put your name, login, and lab section in the space provided below. Put your login and initials clearly on each page of this test and on any additional sheets of paper you use for your answers. Read all the questions carefully to begin with, and first try to answer those parts about which you feel most confident.

Your name: Login:

Login of person to your left: Login of person to your right:

Discussion section number or time: TA:

TOT /

  1. [12 points] Answer each of the following briefly. Where a question asks for a yes/no answer, give a brief reason for the answer (or counter-example, if appropriate).

a. If f (x) ∈ Θ(x^3 ) and g(x) ∈ O(x^2 ), and if there is some x 0 such that f (x 0 ) > g(x 0 ), then is f (x) > g(x) for all x > x 0? Assume f and g are everywhere positive.

b. If g(x) = x^2 cos x, is g(x) ∈ O(x^2 )? Is g(x) ∈ Ω(x)?

c. A sorted list of values is maintained as a Java Vector whose initial capacity is N 0. That is, the representation consists of an array (initially of length N 0 ) and a current size (always less than or equal to the current length of the array), and the array is expanded by factors of two as needed. What are the tightest asymptotic bounds you can give for the best and worst-case times for adding N = K · N 0 values to this list (inserted in the right place to keep the list ordered), assuming the list is initially empty? (The “tightest” bound means “a Θ(·) bound if possible, and otherwise the smallest O(·) bound and largest Ω(·) bound possible.”) We want bounds for the worst-case time and bounds for the best-case time. Include brief descriptions of the best and worst cases.

  1. [10 points] The following fragment of Java code (which continues on the next page) contains several errors in some of the functions marked ‘// ?’.

a. [6 points] Find and (neatly) correct as many as possible, making as little change as possible. Do not correct things unless they really don’t work. WARNING: one of the marked functions is error-free! There may be more than one correction possible; be sure to choose one that makes the resulting program conform to the comments. Not all methods are shown; you don’t need to add any to correct the program.

/** A Mapper represents a function from values to DLists.

  • If M is a Mapper, it is applied to a value, x, with M.map (x). */ public interface Mapper { DList map (Object x); }

/** A DList represents a list of Objects. / public class DList { / The rep field of a DList points to a sentinel node in a circularly

  • linked list. Because the list is circular, the node after the
  • sentinel contains the first item in the list, and the node
  • before the sentinel contains the last item. For an empty DList,
  • the sentinel is its own next and previous element. Example of
  • list containing items A and B:

rep: A B

/** An empty DList. */ public DList () { rep = new DLink (null); rep.next = rep.prev = rep; }

/** True iff THIS is an empty list. */ public boolean empty () { return; } //?

/** The first item in THIS list. */ public Object first () { return rep.next.value; }

/** The last item in THIS list. */ public Object last () { return rep.prev.value; }

/** Add X to the beginning of THIS list. */ public addFront (Object x) { //? DLink item = new DLink (x); rep.next.spliceAfter (item, item); }

/** Add X to the end of THIS list. */ public addBack (Object x) { //? DLink item = new DLink (x); rep.prev.spliceAfter (item, item); }

Continued

/** Remove the first item in THIS list. Requires !empty (). / public void removeFront () { rep.next.unlink (); } /* Remove the last item in THIS list. Requires !empty (). */ public void removeBack () { rep.prev.unlink (); }

/** Remove each value, x, that is currently in THIS list and insert

  • in its place all the items in the list returned by M.map (x),
  • concatenating the values returned by M.map end-to-end. Destructive:
  • the DLists returned by M.map and original contents of THIS are modified.
  • Is guaranteed to send items to M.map in their original order.
  • For example, if M.map (x) returns the list [x, x] for any x,
  • and L contains ["Hello", "world"], then L.mapcat (M) causes L
  • to become [ "Hello", "Hello", "world", "world" ]. If M.map (x)
  • always returns the empty list, then L.mapcat (M) makes L empty. */ public void mapcat (Mapper m) { //? DLink d; d = rep; while (d != rep) { DLink n = d.next, p = d.prev; DList r = m.map (d.value); d.unlink (); p.spliceAfter (r.rep.next, r.rep.prev); d = n; } }

private DLink rep; }

class DLink { /** The next and previous DLinks in the list. / DLink next, prev; /* The item stored in this link. / Object value; /* A DLink whose value is x. */ DLink (Object x) { value = x; next = prev = null; }

/** Remove THIS from the DList it is part of.

  • Does not change THIS. */ void unlink () { prev = next.prev; next = prev.next; } //?

/** Insert the list of items that starts at START and ends at

  • END immediately after THIS. Requires START and END to be
  • non-null. */ void spliceAfter (DLink start, DLink end) { //? end.next = next; start.prev = this; next.prev = end; next = start; } }

Continued

  1. [10 points] Consider a system that models a network of resistors and batteries. Each resistor has a resistance (a number) and each battery has a voltage (we oversimplify). Each resistor and battery has two terminals (which we’ll call terminal 0 and terminal 1) to which wires may be connected. In Java, we might have class declarations like the following.

/** A network resistors and batteries connected by wires. */ class Circuit {

/** An empty network. */ public Circuit () { ... }

/** Add a new battery with voltage V, giving it the label NAME. */ public void battery (String name, double V) { ... }

/** Add a new resistor with resistance R, giving it the label NAME. */ public void resistor (String name, double R) { ... }

/** Connect a wire between terminal #K0 (0 or 1) of the component

  • named C0 and terminal #K1 or the component named C1.
  • Both C0 and C1 must have been defined with method battery
  • or resistor. */ public void connect (String c0, int k0, String c1, int k1) {...}

/** True iff there is a short in the system. This is defined as

  • a sequence of terminals t1, t2, t3, ..., tn, such that t1 and tn
  • are the two opposite terminals of the same battery, and for
  • each pair of terminals ti and ti+1n,
  • either ti and ti+1 are connected by a wire
  • or they are opposite terminals of the same battery. */ public boolean isShort () { ... }

// ADDITIONAL DECLARATIONS OF MEMBERS AND CLASSES (if any) };

For example, the statements on the left below give the Circuit diagrammed on the right.

Circuit C = new Circuit(); C.battery("B1", 9.0); C.battery("B2", 3.0); C.resistor("R1", 1e5); C.resistor("R2", 1e6); C.connect("B1",0,"R1",0); C.connect("B1",0,"R1",1); C.connect("B1",1,"R1",1); C.connect("R1",0,"R2",0); C.connect("R1",1,"R2",1); C.connect("R1",0,"B2",0); C.connect("R1",1,"B2",1); // NOTE: there are two "shorts": // terminal 0 of B1 to 1 of R1 to 1 of B1; // terminal 0 of B1 to 0 of R1 to 0 of R // to 0 of B2 to 1 of B2 to 1 of R2 to // 1 of R1 to 1 of B

a. [3 points] Show how to fill in the “ADDITIONAL DECLARATIONS” of Circuit as needed to provide the necessary representation for these classes. You may assume that only the member functions indicated need to be implemented (but you may add additional member functions if you find it necessary to do so).

Additional declarations for Circuit, if any:

  1. [1 point] Name a poet much of whose work can be sung to the tune of The Yellow Rose of Texas.
  2. [5 points] Dijkstra’s algorithm is not the only way to find the lengths of shortest paths in a graph whose vertices are numbered 1 to n. Let wij be the cost (or weight) of the edge between i and j (= ∞ if there is no such edge). Suppose we define dist(i, j, m) to be the length of the shortest path between vertex number i and vertex number j in the graph that contains no more than m edges (steps). This leads to a simple recursive definition for dist:

dist(i, j, 0) =

0 , if i = j ∞, otherwise. dist(i, j, m) = min 1 ≤k≤n {dist(i, k, m − 1) + wkj } , for m > 0

That is, we look at the length of each path starting at i that is one shorter than m plus the cost of the edge from the end of that path to vertex j. Since this has to include every path between i and j, the minimum of those sums must be the answer we want for the shortest path between i and j. If you implemented this algorithm directly, how long would it take to compute dist? Describe a simple step you could take to speed it up, and tell how fast the result would be.

  1. [6 points] Suppose that an array PQ contains a priority queue, represented by a heap (that is, the array represents a tree satisfying the heap property, with the children of the node stored in element i of the array being at positions 2i + 1 and 2i + 2). Suppose we allow the user of the priority queue to modify the priority of any node in the heap up or down. What must we do to re-establish the heap property after such a modification? Use either Java or precise pseudo-code to give your answer (being sure to describe, at least in precise English, what each primitive operation you use means).

// FILL IN ADDITIONAL CLASSES HERE (IF NEEDED) HERE