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

Breadth-First Search - Data Structures - Exams, Exams of Data Structures and Algorithms

Main points of this exam paper are: Breadth-First Search, Graph Adjacency, Binary Heap, Throws Exception, Leaf Nodes, Search Tree, Game Search, Beta Pruning, Breadth-First Search, Depth-First Search

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of California at Berkeley
Department of Electrical Engineering and Computer Sciences
Computer Science Division
Spring 2009 Jonathan Shewchuk
CS 61B: Midterm Exam II
This is an open book, open notes exam. Electronic devices are forbidden on your person, including cell
phones, iPods, headphones, laptops, and PDAs. Turn your cell phone off and leave it and all electronics at
the front of the room, or risk getting a zero on the exam. Do not open your exam until you are told to do
so!
Name:
Login:
Lab TA:
Lab day and time:
Do not write in these boxes.
Problem # Possible Score
1. Miscellaneous 12
2. Trees 6
3. Graph Adjacency 7
Total 25
pf3
pf4
pf5

Partial preview of the text

Download Breadth-First Search - Data Structures - Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of California at Berkeley Department of Electrical Engineering and Computer Sciences Computer Science Division

Spring 2009 Jonathan Shewchuk

CS 61B: Midterm Exam II

This is an open book, open notes exam. Electronic devices are forbidden on your person, including cell phones, iPods, headphones, laptops, and PDAs. Turn your cell phone off and leave it and all electronics at the front of the room, or risk getting a zero on the exam. Do not open your exam until you are told to do so!

Name:

Login:

Lab TA:

Lab day and time:

Do not write in these boxes. Problem # Possible Score

  1. Miscellaneous 12
  2. Trees 6
  3. Graph Adjacency 7

Total 25

Problem 1. (12 points) A Miscellany.

a. (3 points) Draw the following binary heap after removeMin(), then again after insert(2).

b. (1 point) Each of the exceptions XException, YException, ZException, BException, CException, DException, and EException is declared so it “extends Exception”. Which exception, if any, propagates out of the following code?.

public static void z() throws Exception { try { throw new XException(); } catch (YException y) { throw new ZException(); } catch (Throwable t) { try { throw new BException(); } catch (RunTimeException c) { throw new CException(); } throw new DException(); } catch (BException b) { throw new EException(); } }

c. (3 points) The leaf nodes of the following game search tree are scored as indicated. Cross out all the nodes that will be pruned (i.e. not visited) if alpha-beta pruning is used. Assume children are explored from left to right.



 





5 −2 6 8 5 −9 −

9 3

B C D

E F G H I J

K L M N O P Q R

MAX

MIN

MAX

A

7

Problem 2. (6 points) Trees.

a. (1 point) Fill in the following tree so its postorder traversal is K O I N C R.

b. (1 point) Suppose that you run the following code on a 2-3-4 tree that initially contains n entries. Recall that first() is the method that returns the entry with the smallest key (but doesn’t remove it). Recall also that a 2-3-4 tree can store multiple entries with the same key.

for (i = 0; i < r; i++) { insert(first().key, first().value); }

Expressed as a function of the initial tree size n and the number r of loop iterations, the worst-case running time of this loop is in Θ( ).

c. (2 points) Draw the following 2-3-4 tree after execution of the operation insert(13).

8 40

12 27 33

14 19 25 31

d. (2 points) Suppose you have a binary search tree that is perfectly balanced; it has exactly n = 2d+1^ − 1 nodes, where d is its depth. It is stored in an array by level numbering (though it’s not a heap). The tree contains n numerical keys with no duplicates. Describe (in English) how to find the ith-largest key as quickly as possible. What is the asymptotic running time of your algorithm in terms of n?

Problem 3. (7 points) Graph Adjacency Running Times.

Let G = (V, E) be a directed graph, and let u be a vertex in V , where

n = |V | is the number of vertices, e = |E| is the number of edges, and d is the outdegree of u (in other words, the number of vertices v such that (u, v) ∈ E, where E is the set of edges in the graph). We want to support four operations on graphs:

insert (u, v). Add a new edge (u, v) to the graph. One may insert an edge that’s already in the graph, in which case the graph does not change. remove (u, v). Remove an edge (u, v) from the graph. member (u, v). Is edge (u, v) in the graph? printList (u). Print (on the screen) every edge whose source is u, in any order. Each vertex is represented by an integer in the range 0... n − 1. We will consider five different ways to store the edges (E), including an adjacency matrix, two variations on adjacency lists, an edge list, and a dictionary:

Adjacency matrix. As discussed in class, the entire set of edges is represented as an n × n array of booleans. Adjacency list with sorted arrays. For each vertex v, we store a sorted array of vertices w for which (v, w) ∈ E. This array is not like a row of an adjacency matrix: missing edges do not take up any space. However, the array has empty space at the end so it never needs to be resized. Duplicate items are forbidden in any one list. Adjacency list with balanced search trees. For each vertex v, we maintain a 2-3-4 tree containing every vertex w such that (v, w) ∈ E. Duplicate items are forbidden in any one search tree. One sorted array. Instead of having a separate array for each vertex, we have one array that stores every edge (v, w) ∈ E. The edges are sorted in order by v, with ties broken by w. The array has empty space at the end so it never needs to be resized. Dictionary. All of the edges (for every source vertex) are stored in a single hash table (with chaining). The hash table’s size is in Θ(e). Assume it never needs to be resized, and that there are only O(1) collisions in any one bucket.

Assuming you implement each data structure as intelligently as you can, what is your tightest (smallest) bound on the worst-case running time for each operation, expressed in terms of n, e, and d?

insert (u, v) or remove (u, v) member (u, v) printList (u) Adjacency matrix

Sorted arrays Θ(d)

Balanced search trees

One sorted array

Dictionary