Académique Documents
Professionnel Documents
Culture Documents
Page 1 of 5
O(1)
loop
executed
O(n)
times
O(n)
total
running
time
file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015
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;
}
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
Page 3 of 5
c.
n6
6 n5
d.
3n
12 2n
b.
c.
d.
file:///C:/Users/jwh9/Google%20Drive/SFU/CMPT125/Assignments/Assignment%201%2... 1/16/2015
Page 4 of 5
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
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
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