CS 240 Data Structures and Data Management
Assignment 1 Solutions
January 22, 2014
1. Prove each of the following statements from ﬁrst principles
(a) 5n3 + 10n — 17 e 0024).
Solution:
Proof. Let c = 22,no = 1. Then for all n E N with n > no, 5n3 + 10n —
University of Waterloo
CS240  Winter 2014
Assignment 2
Due Date: Wednesday February 5 at 9:30am
Name: Shayan Masood
20430249
There are 85 possible marks available.
Problem 1
[5+5+5=15 marks]
Consider a very keen teaching assistant. As students arrive dur
University of Waterloo
CS240, Winter 2014
Assignment 1
Due Date: Wednesday, January 22, at 9:30am
Name: Shayan Masood
Problem 1
[20 marks]
Prove each of the following statements from the first principles (i.e., directly from the
definitions):
a) 5n3 + 10n
University of Waterloo
CS240  Fall 2015
Assignment 2
Due Date: Wednesday October 14 at 5pm
Please read http:/www.student.cs.uwaterloo.ca/~cs240/f15/guidelines.pdf for
guidelines on submission. All problems are written problems; submit your solutions elec
CS246Assignment 1 (Fall 2016)
J. Avery
C. Kierstead
B. Lushman
Due Date 1: Friday, September 16, 5pm
Due Date 2: Friday, September 23, 5pm
Questions 1 and 2 are due on Due Date 1; the remainder of the assignment is due
on Due Date 2.
1. Provide a Unix com
University of Waterloo
CS240  Fall 2015
Assignment 1
Due Date: Wednesday September 30 at 5:00pm
Please read http:/www.student.cs.uwaterloo.ca/~cs240/f15/guidelines.pdf for
guidelines on submission. Problems 1 8(a) are written problems; submit your soluti
Module 2: Priority Queues
CS 240  Data Structures and Data Management
Shahin Kamali, Yakov Nekrich, Olga Zorin
Based on lecture notes by many previous cs240 instructors
David R. Cheriton School of Computer Science, University of Waterloo
Spring 2015
Kama
#include <algorithm>
#include <functional>
using namespace std;
using namespace std:placeholders;
#include <vector>
#include <iostream>
void mod23 (int x, int& y, int& z ) cfw_
y = x + y;
z = y + z;
cout < "Y is" < y < endl;
cout < "X is" < x < endl;
cout
1 Let p1 = (30:45): 132 = (5320), P3 = (10314): 134 = (80:80), 135 = (50,305,111 = (35:40)
The kdtree is shown below:
2. part (a): Let max,, denote the maximal array value in the interval [1,3] and let min,,
denote the minimal array value on the in
1.
2.
3.
(a)
(b)
(C)
(e)
(a)
(b)
(elf
(dlf
(80
Consider c := 15 + 20 + 2015 = 2050 and no := 1. Then 0 S 15n3 + 20n210gn +
2015 S 15n3 + 20n3 + 2015n3 S on3 for all n 2 no. (Note that logn < n for
n 2 1.)
Consider c := 1 and no := 1024. Then, using the fa
0 1 2 3 4
Manama
0 1 2 3 4
danmnn \
\ I
0 1 2 3 4 K
d) I. / )
2. The rst insertion is 251 with hash valumh\(\9\5_a1) 6 = 110102. The second insertion
is 217 with hash value M217) X22 = 1 102. T e third insertion is 27 with hash value
M27) = 12 = 011002.
1. part(a): we can nd the area of all rectangles in one pass and in linear time. Since the
coordinates are integer, the areas of all rectangles are integer values. So, we basically
need to sort 11 positive integers, all smaller than 112. This can be done,
1. The procedure is not a correct hea pify solution. There are different ways to show that
the order is important. For example, consider array A = [0, 1, 2, 3, 4,5, 6]. Applying
bubbledown as indicated in the code results in array [2,4,6,3, 1,5,0] which
Tries & String Matching:
Tries (Radix Tree):
Similar to radix sort: use individual bits, not the whole key
left child = 0 bit
right child = 1 bit
Keys are not stored in the trie, a node X is flagged if the path from
root to X is a binary string presen
University of Waterloo
CS240, Spring 2015
Tutorial 7 Questions
1. Insert the following keys into a hash table using cuckoo hashing with hash
table size = 7 and the hash functions h1 (k) = k(mod7), h2 (k) = k(mod5): 3,
10, 14, 12, 19
2a. Insert the followi
University of Waterloo
CS240, Spring 2015
Tutorial 5 Questions
1. Suppose that we have n red jugs and n blue jugs. Each blue jug has a corresponding red jug which contains an equivalent volume of water. We want to
pair up the jugs so that every blue jug i
University of Waterloo
CS240, Spring 2015
Tutorial 4 Questions
1a. Use quickselect to nd the 4th smallest element in the following array, using
the rst element as the pivot each time:
10, 22, 3, 1, 6, 9, 15, 8, 4
1b. Use quicksort to sort the following ar
Module 1: Introduction and Asymptotic Analysis
CS 240  Data Structures and Data Management
Shahin Kamali, Yakov Nekrich, Olga Zorin
Based on lecture notes by many previous cs240 instructors
David R. Cheriton School of Computer Science, University of Wate
Module 5: Hashing
CS 240  Data Structures and Data Management
Shahin Kamali, Yakov Nekrich, Olga Zorin
Based on lecture notes by many previous cs240 instructors
David R. Cheriton School of Computer Science, University of Waterloo
Spring 2015
Kamali,Nekri
Module 9: Compression
CS 240  Data Structures and Data Management
Shahin Kamali, Yakov Nekrich,
Olga Zorin
Based on lecture notes by many previous cs240 instructors
David R. Cheriton School of Computer Science, University of Waterloo
Spring 2015
Kamali,N
Module 7: Dictionaries for MultiDimensional Data
CS 240  Data Structures and Data Management
Shahin Kamali, Yakov Nekrich, Olga Zorin
Based on lecture notes by many previous cs240 instructors
David R. Cheriton School of Computer Science, University of W
University of Waterloo
CS240, Spring 2015
Tutorial 6 Questions
1. Suppose that we have an array and the rst n n0.25 elements are initially
sorted. How can we sort the array in (n) time?
2a. Insert a node with key 20 into the following AVL tree:
64
1
32

MultiDimensional Data and Tries
CS240
University of Waterloo
[email protected]
July 15, 2015
CS240 (UW)
Tutorial 9
July 15, 2015
1/1
Overview
CS240 (UW)
Tutorial 9
July 15, 2015
2/1
Quad Trees
With quad trees we recursivley divide a square in
University of Waterloo
CS240, Spring 2015
Tutorial 9 Questions
1a. Find the failure array for the pattern: abacadaba 1b. Use KMP to search
for the pattern from 1a in the text: abacadabacabadaba 2. Use BoyerMoore to
search for the pattern aabaa in the the
Module 8: Tries and String Matching
CS 240  Data Structures and Data Management
Shahin Kamali, Yakov Nekrich, Olga Zorin
Based on lecture notes by many previous cs240 instructors
David R. Cheriton School of Computer Science, University of Waterloo
Spring
University of Waterloo
CS240, Spring 2015
Midterm Review
1.Use rst principles to prove the following:
a) nn (n20 )
b) n + nlog(n) o(n n)
2
c) 10n + 11n + 12 O(n2 )
d) 10n2 11n 12 (n2 )
e) logb (n) (logn)
2. Analyze the following codes runtime:
foo(n):
1.
University of Waterloo
CS240, Spring 2015
Tutorial 3 Questions
1. Consider the following heap:
5
5
2
1
3
2
1
a. Insert 17 and then 8 into the above heap
b. Delete the maximum element in the heap
2. Heapify the following Tree:
1
6
7
2
1
8
10
14
5
20
3. Use
University of Waterloo
CS240, Spring 2015
Tutorial 2 Questions
1. Show that 2 (logn) o(n)
2. Find the runtime of the following code:
for i = 1 to n
for j = 1 to i
k = j
while k>1
k /= 2
3. Find the runtime of the following code:
for i = 1 to n
j = i
while
Questions for Max
1. RAII
What is its purpose?
On the midterm, I see how making the rep inv an atrribute of the class
helps with the exception handling (since destructors will be called for things
created in whichever frame the exception was thrown in), b
1. Suppose that G has p vertices, q edges, maximum degree k, and no cycles of
length 3. Prove that q k(p k). For p = 8 and k = 4, give an example in
which equality holds.
2. Definition of component, examples
3. Euler's Formula
4. Platonic Solid Theorem
5.