Data Structures CS357 First Midterm Tuesday, September 16, 2003 12:30 pm 2:00pm
Student name . Student number . For full credit solve problems 1 through 6. Problem 7 is for extra credit. You can use a letter-size page of your own hand-written notes, writt
Solution to Exercise 15.1 Let us first see where the keys will get hashed. 1 mod 7 is 1, 4 mod 7 is 4, 8 mod 7 is 1, 5 mod 7 is 5, 22 mod 7 is 1, 12 mod 7 is 5, so h(1)=1 so h(4)=4 so h(8)=1 so h(5)=5 so h(22)=1 so h(12)=5
We insert an item at the beginni
Solution to Project 14.1 The maximum number of keys mapped to a hash value for the universal hash was in the range between 23 and 31, for different runs of the code below. The maximum was 26 for the polynomial hash.
#include #include #include #include <st
Solution to Exercise 13.1 The key observation is that we can negate the keys, because keys are integers. The smallest among negated keys, when negated, will be the largest among the original keys. Before we insert a key to the min-heap, we negate the key.
Solution to Exercise 12.1 From the picture we see, that a heap of height h can have at most 1+2+4+.+2h-1+2h = 2h+1-1 keys, and it has that many keys only when the last level h is completely filled in with keys. So the number of keys n, can be at most 2h+1
Solution to Exercise 7.1 Let us begin with 4 pictures.
pin A pin B pin C
1 2 3 4 5 6 7 . n-1 n pin A pin B pin C
n pin A
1 2 3 4 5 6 7 . n-1 pin B pin C
1 2 3 4 5 6 7 . n-1 pin A pin B
n pin C 1 2 3 4 5 6 7 . n-1 n
The key observation that we make so as t
Solution to Exercise 10.1 See Figure 10.10 on page 215 of the book by Cormen, Leiserson, Rivest, and Stein: Introduction to Algorithms (2nd Edition). MIT Press (2001). Solution to Project 10.1 The key idea is that we can make decisions where to go next ba
Solution to Exercise 9.1 In the following example, each node, except for the root, has a parent, but the root has no relationship with the other three nodes. We need an additional property that for any node v, we can reach the root by following the parent
Solution to Exercise 6.4 When the list is empty or it has just one node, then the reverse list is equal to the list, so we do not need to do anything in order to reverse the list. When the list has at least two nodes, then we can traverse the list from th
Solution to Exercise 3.1 a) We give a bound on the number of primitive operations that the function foo performs as a function of n. Our bound does not have to be equal to the worst case number of primitive operations, because we want to analyze time comp
Solution to Exercise 2.1 We show that 20+21+22+23+24+.+2n-1+2n = 2n+1 - 1. The proof is by induction. We first show the base case. When n=0 then 20 is 1, and so is 20+1-1. For the inductive step we take n0, and want to show that the equation is true for n
Solution to Project 5.1
#include <stdlib.h> #include <stdio.h> #include <time.h> class MyVector cfw_ private: int capacity; int size; int *elements; public: MyVector(); ~MyVector(); int elemAtRank(int r); void replaceAtRank(int r, int e); void insertAtRan
Solution to Exercise 1.1 We express the number of primitive operations performed by the algorithm prefixAverages, as a function of n.
Algorithm prefixAverages(X,n) Input: An n-element array X of numbers Output: An n-element array A of numbers such that A[
Solution 6.1 It is natural to assume that an empty list has head=tail=NULL. The implementation from the lecture fails on an insert of an element at the end of the empty list, because it tries to set the filed next of a node pointed by the tail, while the