Vous êtes sur la page 1sur 5

CMPT 125 Assignment 1

Page 1 of 5

CMPT 125 Assignment 1

Due date: Type or hand-write your solutions and


submit a hard copy to the CSIL assignment
boxes before 6:00pm on Friday, January 30,
2015. Late assignments will not be marked.
Style considerations for this and future assignments: make your solutions clear
and concise, using a mixture of pseudocode, assertions, and English, as
appropriate/required. When your goal is to communicate an algorithm,
pseudocode will nominally be sufficient, i.e., complete programs are generally
not needed. Your analysis and justifications should be presented with your
steps in a logical order, including the proper use of English grammar and
punctuation.
Remember that you are trying to communicate with an overworked TA/marker correct answers written in an obscure fashion are not likely to be awarded full
credit.

Problem 1 [20 marks]


Background: The purpose of this problem is to give you practice in figuring out
the complexity of iterative programs. Try to get the best Big-O estimate that
you can and briefly justify it.
Sample:
for (i = 1; i <= n; i++) {
sum = sum + i;
}

O(1)

loop
executed

O(n)
times

O(n)
total
running
time

Sample Solution (in words):


The loop body runs in O(1) and the loop is executed O(n) times. Total
running time is O(1) O(n) = O(n) .
The Problems:
a. i = 1;
while (i <= n) {
j = n - i;
while (j >= 2) {
for (k = 1; k <= j; k++) {
s = s + A[k];
}
j = j - 2;
}
i = i + 1;
}
b. for (i = 1; i <= n; i++) {
for (j = 2*i; j <= n; j++) {
puts("hello");

file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015

CMPT 125 Assignment 1

Page 2 of 5

}
}
c. for (i = 1; i <= n; i++) {
for (j = 1; j <= 150; j++) {
printf("%d %d\n", i, j);
}
}
d. i = 1;
j = n;
while (i <= j) {
while (i <= j && A[i] < 0) {
i = i + 1;
}
while (i <= j && A[i] >= 0) {
j = j - 1;
}
printf("%d %d\n", A[i], A[j]);
i = i + 1;
j = j - 1;
}

Problem 2 [20 marks]


Background:
Even though
the Big-O will
distinguish
algorithms with
different growth
rates, it does so
only for large
input sizes. In
other words,
sometimes a
worse algorithm
will do better,
when the input
sizes are small.
For example, if
you compared the typical implementations for merge sort (O(n log n)) and
insertion sort (O(n2 )) , you would find that the insertion sort would run faster
for all n up to about 30. Beyond that, merge sort would run faster. If you can
determine the break-even points for competing algorithms, you can choose the
best one, based on the size of the input.
The Problem: Find the smallest positive integer n beyond which Algorithm B
outperforms Algorithm A.

a.

n/2

4 log2 n

b.

n2

100 n log2 n

file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015

CMPT 125 Assignment 1

Page 3 of 5

c.

n6

6 n5

d.

3n

12 2n

Problem 3 [20 marks]


Background: The purpose of this problem is to give you practice in
manipulating expressions, as they relate to their Big-O growth rate. Use your
algebra skills and the tools from Lecture Packet 7 to find the simplest Big-O
estimate of the following functions.
The Problems:
a.

f(n) = 6 + 12 + + n . You may assume that n is a multiple of 6.

b.

g(n) = 1 + 2 + 4 + 8 + + n . You may assume that n is a power


of 2.

c.

R(N) = (10N + 3N 2 + 20)(4N + log N + 5) .

d.

T (N) = 55N(600 + 50N log N + 20N) + 20N(30N + 20N )(50 + log N)


.

Reminder: For Problems 4 and 5, a mixture of pseudocode and English is


preferred over full-blown C++ code. You are trying to communicate an
algorithm to a fellow human, not to a computer.

Problem 4 [20 marks]

Background: A 3-dimensional surface contains a saddle point when the point


is a global minimum along a line parallel to either the x or the y axis, but a
global maximum along the other axis. You'll usually see a "U" shape meeting
an upside-down "U" shape. The classic horseback riding saddle has one such
saddle point, and, not coincidentally, it is a rider's most stable position.

file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015

CMPT 125 Assignment 1

Page 4 of 5

The Problem: The input will be a 2-dimensional array A[n][n] of numbers,


representing a lattice approximation of a surface. That is, given an (x, y) pair
of integers, the array entry A[x][y] contains the height (z -coordinate) of the
surface.
In the sample surface shown (thank you to Wikipedia!), the array might be:

0.50 0.62 0.72 0.80 0.88 0.93 0.97 0.99 1.00 0.99 0.97 0.93 0.88 0.80 0.72 0.62 0.50
0.38 0.50 0.60 0.69 0.76 0.81 0.85 0.88 0.88 0.88 0.85 0.81 0.76 0.69 0.60 0.50 0.38
0.28 0.40 0.50 0.59 0.66 0.71 0.75 0.77 0.78 0.77 0.75 0.71 0.66 0.59 0.50 0.40 0.28
0.20 0.31 0.41 0.50 0.57 0.62 0.66 0.69 0.70 0.69 0.66 0.62 0.57 0.50 0.41 0.31 0.20
0.12 0.24 0.34 0.43 0.50 0.55 0.59 0.62 0.62 0.62 0.59 0.55 0.50 0.43 0.34 0.24 0.12
0.07 0.19 0.29 0.38 0.45 0.50 0.54 0.56 0.57 0.56 0.54 0.50 0.45 0.38 0.29 0.19 0.07
0.03 0.15 0.25 0.34 0.41 0.46 0.50 0.52 0.53 0.52 0.50 0.46 0.41 0.34 0.25 0.15 0.03
0.01 0.12 0.23 0.31 0.38 0.44 0.48 0.50 0.51 0.50 0.48 0.44 0.38 0.31 0.23 0.12 0.01
0.00 0.12 0.22 0.30 0.38 0.43 0.47 0.49 0.50 0.49 0.47 0.43 0.38 0.30 0.22 0.12 0.00
0.01 0.12 0.23 0.31 0.38 0.44 0.48 0.50 0.51 0.50 0.48 0.44 0.38 0.31 0.23 0.12 0.01
0.03 0.15 0.25 0.34 0.41 0.46 0.50 0.52 0.53 0.52 0.50 0.46 0.41 0.34 0.25 0.15 0.03
0.07 0.19 0.29 0.38 0.45 0.50 0.54 0.56 0.57 0.56 0.54 0.50 0.45 0.38 0.29 0.19 0.07
0.12 0.24 0.34 0.43 0.50 0.55 0.59 0.62 0.62 0.62 0.59 0.55 0.50 0.43 0.34 0.24 0.12
0.20 0.31 0.41 0.50 0.57 0.62 0.66 0.69 0.70 0.69 0.66 0.62 0.57 0.50 0.41 0.31 0.20
0.28 0.40 0.50 0.59 0.66 0.71 0.75 0.77 0.78 0.77 0.75 0.71 0.66 0.59 0.50 0.40 0.28
0.38 0.50 0.60 0.69 0.76 0.81 0.85 0.88 0.88 0.88 0.85 0.81 0.76 0.69 0.60 0.50 0.38
0.50 0.62 0.72 0.80 0.88 0.93 0.97 0.99 1.00 0.99 0.97 0.93 0.88 0.80 0.72 0.62 0.50
There are 5 saddle points in the sample. One is at A[8][8] and has the value
0.50, which is the maximum value of its row, but the minimum value of its
column. The other 4 are at the corners of the matrix (A[0][0], A[0][16], A
[16][0] and A[16][16]) each of which is the minimum of its row, and the
maximum of its column.
Your algorithm will locate and output all saddle points, i.e., all entries A[x][y]
for which the entry is the max in its row and min in its column, or vice versa.
There might be 1 saddle point, several saddle points or perhaps no saddle
points.
Write your algorithm using pseudocode and document it with assertions and
comments. You may assume that max and min are functions that work in the

file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015

CMPT 125 Assignment 1

Page 5 of 5

expected fashion, i.e., your algorithm can freely make use of max and min.
That doesn't mean that max and min are "free" in terms of cost! Each of them
will still cost time in proportion to the number of elements it needs to scan.
Determine the running time of your algorithm using O -notation, as a function of
n . Remember that time is a precious resource: the faster your algorithm, the
better your grade.
Problem 5 [20 marks]
Background: Resource collection is a
common optimization problem in the
domain of AI. The input is a map with
a description of the benefits (e.g.,
food/energy) or hazards (e.g.,
predators/obstacles) for each location,
and the problem is to plan the best
route, i.e., the one that maximizes the
total energy collected.
In a resource-rich map, the best route might be to greedily visit everywhere; the
other extreme in a completely hazardous environment is to just stay put.
The Problem:
The input will be an array A[n] of integers, representing a 1-dimensional map
where positive numbers are beneficial, but negative numbers are harmful.
j

Devise an algorithm that finds the largest possible value of the expression
k=i

A[k], the sum of a contiguous block of integers. (When j


defined to be zero).

< i , this value is

Document your algorithm with assertions and comments. Give the running time
of your algorithm using O -notation, as a function of n . You should devote
some time to trying to find the most efficient algorithm that you can. It is fairly
easy to get an O(n3 ) algorithm, and there are a couple of different O(n2 )
algorithms. But there is a very clever O(n) algorithm which can be
implemented in six lines of C. A portion of your grade will be tied to the
efficiency of your algorithm.
Examples:
For A = [5, -6, 7, -5, 10, -1], the best sum is A[2..4],
for a total of 7 + (5) + 10 = 12 .
For A = [1, 2, 4, -6, 4, 2, 1], the best sum is A[0..6],
the entire array, for a total of 8 .
For A = [-5, 2, -3, 1, -5, 4, -2], the best sum is A
[5..5], the largest positive number, 4 .
End of Assignment 1.

file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015

Vous aimerez peut-être aussi