







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 13
This page cannot be seen from the preview
Don't miss anything!
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:
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.
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.
/** A DList represents a list of Objects. / public class DList { / The rep field of a DList points to a sentinel node in a circularly
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
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.
/** Insert the list of items that starts at START and ends at
Continued
/** 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
/** True iff there is a short in the system. This is defined as
// 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:
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.