CPSC 331 Assignment #1
Instructions for Use of Test Files and Submission of
Assignments)
1
Instructions for Use of Test Files
1. If you have not already done so, please work through at the least of the beginning of Java
Exercise #7, in order to make sure
Question 1:
While it is not very explicitly documented, examination online material that discusses arrays in Java indicatesthat the
length of an array in Java must be small enough to be represented as an int (this value is given in Java by
Integer.MAX_VAL
Question 1 (Background Check)
State the definition of each of the following:
Terminationof an algorithm for a given problem.
Loop variant.
Question 2 (Practise Proving Termination)
Consider the following algorithm.
/ Precondition: n is a positive integer
CPSC 331, F15 (L01), Self-Study List #3: [2]: ANALYSIS & CORRECTNESS
1
1 Algorithm Analysis
1.
Suppose it is known that the running time of an algorithm is (1/3)n2 + 6n, and that the running time of another
algorithm for solving the same problem is 111n 3
Warmup Problems:
1. Consider the following array.
a.
Trace the execution of the Deterministic Partitioning algorithm on this input. That is,
trace the execution of the program after the call
DPartition(A, 0, 7)
if A is the array that is shown above.
b.
No
Question 1
a.
b.
c.
Draw the binary search tree that results from a sequence of insert operations using the
following parameters: 5,6,3,1,11,9,7,8,2,4,6,12,10.
Draw a binary search tree of minimal height that contains the same set of elements as above.
Is
CPSC 331 L02
November 20, 2015
University of Calgary
Exercise Set #10
To be prepared for the tutorials on Nov 2326.
Exercise 10.1
(a) Draw an AVL Tree of height 4 that contains the minimum possible number of nodes.
(b) In a typical binary search tree, ins
Question 1:
Describe a data structure in pseudo-code that allows you to access array entries in constant time, and
to initialize a given array of size n in constant time. (I.e., after an initialization, every read of an array
entry should return the defau
Question 1
Consider the universe U=cfw_0,.,999, a hash table of size m=59, and a hash function h:U->cfw_0,.,58,
where h(x)=x mod 59. Show the hash tables that result from inserting the keys
473, 418, 356, 312, 119, 237, 605, 307
in this given order for ea
Question 1
Consider the algorithm below, which computes the n-th Fibonacci number.
public int fibonacci(int n) cfw_
if(n = 0)
return 0;
else if(n = 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
a.
b.
c.
d.
Show that the running time of th
CPSC 331, F15 (L01), Self-Study List #5: [2]: ANALYSIS & CORRECTNESS
1
ALGORITHM ANALYSIS
What do the following algorithms (given in code or pseudo-code) do? For each one, analyze its recurrence, worstcase running time, and express it using Big-Oh notatio
CPSC 331, F15 (L01), Self-Study List #4: [2]: ANALYSIS & CORRECTNESS
1
ALGORITHM ANALYSIS
Problem 1
In the following, use either a direct proof (by giving values for c and n0 in the definition of bigOh notation) or cite one of the rules given in the book
Below you find Euclids famous algorithm that computes the greatest common divisor, gcd(x, y),
of two integers x and y.
Algorithm 1: Euclid
1
2
3
4
5
6
7
8
9
Input: X, Y
/* Precondition:
x := X; y := Y
while x = y do
if x > y then
x := x y
else
y := y x
en
CPSC 331 L02
September 23, 2015
University of Calgary
Exercise Set #3
To be prepared for the tutorials on Sep 28Oct 1.
Typically not all questions can be discussed in tutorials. This week, the tutorials will give
priorities to Exercises 3.1 and 3.5.
Exerc
1
2
3
4
5
Input: x
/* Precondition:
y := x2
while y > 0 do
x := x 1
y := y 2x 1
end
xN
*/
(a) Prove that
x N cfw_0 and
y = x2
(L)
is a loop invariant for the algorithm above.
(b) Use a loop variant to prove that the algorithm terminates. You can assume th
CPSC 331 Winter, 2014
Solutions for Assignment #3
1. You were rst asked to prove that si Fi+1 + Fi+2 1 for every integer i 1.
Proof: By induction on i. The strong form of mathematical induction will be used and
the case that i = 1 and i = 0 will be consid
CPSC 331 Winter, 2014
Solutions for Assignment #1
1. In this rst equation, you were asked to give a (reasonably short) proof that the function
f (n) = n is a bound function for the recursive algorithm SDumble shown in Figure 1
on page 2.
Solution: Notice
CPSC 331 Winter, 2014
Solutions for Assignment #2
1. In this question you were asked to show that it would be very bad idea to require that a
remove operation should reduce the capacity of the array every time the size of the
array is less than capacity/2
CPSC 331 Winter 2014
Solutions for Required Problems in Assignment #4
1. You were rst asked to complete the implementation of the method dQuickSort1 , that
implements the deterministic version of Quick Sort described in the lecture notes about
this algori
<html><head></head><body><pre style="word-wrap: break-word; white-space: prewrap;">package cpsc331.hogwarts;
public class CDumble cfw_
/*
*
* Computes the n<sup>th</sup> Fibonacci number on input n.
* <br />
*
* <p>
* <strong>
<html><head></head><body><pre style="word-wrap: break-word; white-space: prewrap;">package cpsc331.hogwarts;
public class SDumble cfw_
/*
*
* Computes the n<sup>th</sup> Fibonacci number on input n.
* <br />
*
* <p>
* <strong>
CPSC 331 Term Test #2
March 18, 2014
Name:
Please DO NOT write your ID number on this page.
Instructions:
Answer all questions in the space provided. Continue answers on page 10 if you need
more room.
Point form answers are acceptable if complete enough t
CPSC 331 Supplement for Term Test #1
The Slytherin Series is the sequence of integers S0 , S1 , S2 , . . . such that, for n 0,
0
if n = 0,
Sn = 1
if n = 1,
2 Sn1 Sn2 if n 2.
Algorithms for the following computational problem will be considered on this tes
CPSC 331 Winter, 2014
Solution for Question #1 on Term Test #2
This question concerned the stack and queue abstract data types.
(a) You were rst asked to describe how an array A can be used to implement a bounded
stack. Where should the value at the botto
I - Fundamental Concepts
1. State the definition of each of the following.
precondition
postcondition
correctness of an algorithm (for a given problem)
partial correctness of an algorithm (for a given problem)
loop invariant
2. Explain howcorrectness,part
Question 1:
Show that Insertion Sort has best-case running time (n).
A common measure for how well an array A of length n is sorted, is the number of
its inversions, which is the number of index pairs (i,j), 0i<j<n, such that A[i]>A[j]. The
smaller the nu