Questions of Data Structure and Software Principle - Final Exam 2 | CS 225, Exams of Data Structures and Algorithms

Material Type: Exam; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-uki
koofers-user-uki 🇺🇸

10 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
Final Examination
CS 225 Data Structures and Software Principles
Sample Exam 2
3 hours permitted
Name:
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. No electronic aids are allowed, either.
You should have 11 sheets total (the cover sheet, plus numbered pages 1-21). The last sheet
is scratch paper; you may detach it while taking the exam, but must turn it in with the exam
when you leave.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any
compiler error is an exam typo (though hopefully there are not any typos), and (2) assume
you are NOT allowed to write any helper functions to help solve the problem, nor are you
allowed to use additional arrays, lists, or other collection data structures unless we have said
you can.
Problem Points Score Grader
1 60
2 40
3 20
4 20
5 20
6 20
Total 180
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Questions of Data Structure and Software Principle - Final Exam 2 | CS 225 and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

Final Examination

CS 225 Data Structures and Software Principles

Sample Exam 2

3 hours permitted

Name: NetID: Lab Section (Day/Time):

  • This is a closed book and closed notes exam. No electronic aids are allowed, either.
  • You should have 11 sheets total (the cover sheet, plus numbered pages 1-21). The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper functions to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can.

Problem Points Score Grader 1 60 2 40 3 20 4 20 5 20 6 20 Total 180

  1. [Algorithms - 60 points (6 points each)].

(a) Explain how the middle node of a singly-linked list can be removed in constant time, given a pointer to that node.

(b) In a “perfect” skiplist, we expect to never have to traverse forward more than one node on any level. Why is this?

(e) How do we “mark a vertex known” in the heap implementation of Dijkstra’s Algorithm

  • that is, how do we ensure that a vertex we have selected in one step will never again be selected in a future step – and how long does this process take, for one vertex?

(f) Given a graph that is both undirected, and connected (i.e. given any two vertices, there is a path between them in the graph), can breadth-first-search ever produce a spanning forest instead of a spanning tree? Justify your answer.

(g) Consider a hash table where h(x) = x mod 11 and h 2 (x) = 5 − x mod 5. Insert the values 80, 58, 30, 5, 21, and 27, in that order, into the hash table. Use double hashing to resolve collisions. We have provided the relevant values of the two hash functions, in the table below.

h(x) 3 3 8 5 10 5

h 2 (x) 5 2 5 5 4 3

(h) How many “array nodes” (as opposed to leaves containing info records for our keys, rather than arrays) would there be in a Patricia Tree containing the words cart, car, carthage, cardio, and cartoon?

(j) For the given graph, run Prim’s algorithm, indicating in the table below the distances at each vertex at the end of each step (dv), and whether or not the vertex has been marked known yet at the end of each step (kv).

A B C D E F G

A 0 7 8 9 0 0 0

B 7 0 0 5 1 0 0

C 8 0 0 4 0 6 0

D 9 5 4 0 2 3 11

E 0 1 0 2 0 0 12

F 0 0 6 3 0 0 10

G 0 0 0 11 12 10 0

V dv kv dv kv dv kv dv kv dv kv dv kv dv kv dv kv A ∞ 0 B 0 0 C ∞ 0 D ∞ 0 E ∞ 0 F ∞ 0 G ∞ 0

  • Start Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7
  1. [Analysis – 40 points (10 points each)].

(a) You have a pointer head to a doubly-linked list of n nodes, made up of nodes of the ListNode class on page 19. You need to do three things: (1) explain what the following code does, (2) tell us what the the order of growth of the worst-case running time of the following code is (in big-O notation), and (3) explain your answer to (2) in enough detail to be convincing. // you are given the pointer ‘‘head’’ to a list of size n ListNode* temp = NULL; ListNode* latestHead = NULL; while (head != NULL) { ListNode* toEnd = head; while (toEnd->next != NULL) toEnd = toEnd->next; if (toEnd->prev != NULL) toEnd->prev->next = NULL; else head = NULL; toEnd->prev = temp; if (temp != NULL) temp->next = toEnd; else latestHead = toEnd; temp = toEnd; }

(c) Suppose you want to add a vertex to graph of V vertices and E edges. Assume that this graph is implemented using an adjacency matrix of exactly the right size needed for the graph. Express (using big-O notation) the order of growth of the worst-case running time of this operation, in terms of V and E. Explain your answer in enough detail to be convincing.

(d) Explain why the time to lookup a value in a trie does not depend on how many values there are in the trie.

(Range Removal, continued)

  1. [Array of Lists - 20 points].

You have the use of the Array, ListNode, and TreeNode classes seen on page 19 , as well as a standard Queue class with a no-argument constructor that initializes the Queue to be empty. You want to write a function levels that has one parameter, a pointer to TreeNode, which points to the root of a binary tree made of TreeNode objects. The function should return an Array object indexed from 0 to maxDepth, where maxDepth is the depth of the deepest leaf in the tree. For every index i between 0 and maxDepth, inclusive, the array cell at index i should point to a linked list containing every node of depth i in the binary tree, in order from left-to-right. For example, cell 0 of the array would point to a list containing the tree’s root node, cell 1 of the array would point to a list containing the root’s left and right children, in that order, and so on. You are also allowed to use one standard Queue, which has a no-argument constructor that initializes the Queue to be empty.

Array<ListNode> levels(TreeNode ptr) // your code goes here

  1. [Dijkstra’s Algorithm - 20 points].

You have the following classes:

class EdgeNode { class VertexRecord { public: public: int target; // index of target vertex int distance; int weight; // weight of edge int known; EdgeNode* next; // next node in list EdgeNode* edgePtr; }; };

We can implement graphs using the adjacency list implementation, by having a variable theGraph of type Array that is indexed from 1 to theGraph.size(). In this graph, the vertices have indices from 1 to theGraph.size(), and all edge weights are positive. Your task is to run Dijkstra’s algorithm on such a graph. That is, want to write a function dijkstra that has two parameters. The first is a reference to a variable theGraph as described above, and the second is an integer between 1 and theGraph.size() inclusive, which is the index of the source vertex. The distance and known variables in each VertexRecord object are initially not initialized in any way, so you will need to take care of that yourself. You may use those two variables however you see fit, but at the end of your function, each VertexRecord should store the minimum distance from the source to that vertex, in the distance variable for that vertex. You should use the “table implementation” to implement this algorithm.

void dijkstra(Array & theGraph, int source) { // your code goes here

(Dijkstra’s Algorithm, continued)

(Optimizing Tries, continued)

These nodes and classes are used on the exam:

template class Array { Array(); // creates array of size 0, with // lower bound == 0 and upper bound == -

void setBounds(theLo, theHigh); // resizes array to have lower bound // theLow and upper bound theHigh; any // values in that index range already, // remain in the resized array

int size(); // returns the number of values

Etype& operator[](int index) // returns the cell at parameter index // there are other member functions but // they are not used on this exam };

template <typename Etype1, typename Etype2> class pair { public: Etype1 first; Etype2 second; };

class ListNode { public: int element; ListNode* next; ListNode* prev; ListNode(int elem) {element = elem; next = NULL; prev = NULL;} };

class TreeNode { public: int element; TreeNode* left; TreeNode* right; TreeNode* parent; TreeNode(int elem) {element = elem; left = NULL; right = NULL; parent = NULL; } };