0% found this document useful (0 votes)
11 views7 pages

Tutorial 1 - Algorithm Analysis - v2 - 4eeb58f0b9de020 - 202503152343 - 20740

The document outlines exercises from a tutorial on advanced data structures and algorithms, covering topics such as integer representation, abstract data types (ADTs) for strings and bags, and various algorithms for computing Fibonacci numbers. It also includes questions on algorithm efficiency, mathematical proofs, and recurrence relations. Each exercise requires the application of theoretical concepts to practical scenarios in programming and algorithm design.

Uploaded by

hellworedrop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Tutorial 1 - Algorithm Analysis - v2 - 4eeb58f0b9de020 - 202503152343 - 20740

The document outlines exercises from a tutorial on advanced data structures and algorithms, covering topics such as integer representation, abstract data types (ADTs) for strings and bags, and various algorithms for computing Fibonacci numbers. It also includes questions on algorithm efficiency, mathematical proofs, and recurrence relations. Each exercise requires the application of theoretical concepts to practical scenarios in programming and algorithm design.

Uploaded by

hellworedrop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COMP4204: Advance Data Structures and Algorithms

Tutorial 1
(Data Structures and Algorithms & Mathematical Preliminaries)

Chap.01: Solve the following exercises:

1.2 Most programming languages have a built-in integer data type. Normally this representation has a fixed size,
thus placing a limit on how large a value can be stored in an integer variable. Describe a representation for integers
that has no size restriction (other than the limits of the computer’s available main memory), and thus no practical
limit on how large an integer can be stored. Briefly show how your representation can be used to implement the
operations of addition, multiplication, and exponentiation.

1.3 Define an ADT for character strings. Your ADT should consist of typical functions that can be performed on
strings, with each function defined in terms of its input and output. Then define two different physical
representations for strings.

1.9 Does every problem have an algorithm?

1.11 Consider the design for a spelling checker program meant to run on a home computer. The spelling checker
should be able to handle quickly a document of less than twenty pages. Assume that the spelling checker comes
with a dictionary of about 20,000 words. What primitive operations must be implemented on the dictionary, and
what is a reasonable time constraint for each operation?

1.13 Imagine that you are given an array of records that is sorted with respect to some key field contained in each
record. Give two different algorithms for searching the array to find the record with a specified key value. Which
one do you consider “better” and why?

Chap.02:

Solve the following exercises: 2.6, 2.11, 2.14, 2.19, 2.21, 2.25, 2.26, 2.27, 2.29, 2.32, 2.34

2.6 Define an ADT for a bag of integers (remember that a bag may contain duplicates, and has no concept of
order). Your ADT should consist of the functions that can be performed on a bag to control its membership, check
the size, check if a given element is in the set, and so on. Each function should be defined in terms of its input and
output.

2.11 Here is a simple recursive function to compute the Fibonacci sequence:


/** Recursively generate and return the n’th Fibonacci number */
static long fibr(int n) {
// fibr(91) is the largest value that fits in a long
assert (n > 0) && (n <= 91) : "n out of range";
if ((n == 1) || (n == 2)) return 1; // Base case
return fibr(n-1) + fibr(n-2); // Recursive call
}

1
This algorithm turns out to be very slow, calling Fibr a total of Fib(n) times.
Contrast this with the following iterative algorithm:

/** Iteratively generate and return the n’th Fibonacci number */


static long fibi(int n) {
// fibr(91) is the largest value that fits in a long
assert (n > 0) && (n <= 91) : "n out of range";
long curr, prev, past;
if ((n == 1) || (n == 2)) return 1;
curr = prev = 1; // curr holds current Fib value
for (int i=3; i<=n; i++) { // Compute next value
past = prev; // past holds fibi(i-2)
prev = curr; // prev holds fibi(i-1)
curr = past + prev; // curr now holds fibi(i)
}
return curr;
}

Function Fibi executes the for loop n − 2 times.


(a) Which version is easier to understand? Why?
(b) Explain why Fibr is so much slower than Fibi.

2.14 Consider the following function:


static void foo (double val) {
if (val != 0.0)
foo(val/2.0);
}
This function makes progress towards the base case on every recursive call.
In theory (that is, if double variables acted like true real numbers), would this function ever terminate for input
val a nonzero number? In practice (an actual computer implementation), will it terminate?

2.19 (a) Use induction to show that n2 − n is always even.


(b) Give a direct proof in one or two sentences that n2 − n is always even.
(c) Show that n3 − n is always divisible by three.
(d) Is n5 − n aways divisible by 5? Explain your answer.

2.21 Explain why

2.25 Find a closed-form solution and prove (using induction) that your solution is
correct for the summation

2
2.26 Prove that the sum of the first n even numbers is n2 + n
(a) by assuming that the sum of the first n odd numbers is n2.
(b) by mathematical induction.

2.27 Give a closed-form formula for the summation ∑#$%& 𝑖 ∑n


where a is an integer between 1 and n.

2.29 Prove, for n ≥ 1, that

2.32 Prove (using induction) that the recurrence T(n) = T(n − 1) + n; T(1) = 1
has as its closed-form solution T(n) = n(n + 1)/2.

2.34 Expand the following recurrence to help you find a closed-form solution, and
then use induction to prove your answer is correct.
T(n) = T(n − 1) + 3n + 1 for n > 0; T(0) = 1.

You might also like