Answer to question 9, 1995 to 1999

advertisement
Group Assignment #11 (Nov 24 & 25, 2005)
Final 1995 #10
Consider the following program where the operator “/” is the division
operator
test n = “no”, if n<1
test n = “yes”, if n=1
test n = test (n/2), if n>1
Draw a labeled graph showing the complexity of this program with respect to
the size of the input number where the unit of work is the number of times
operator (/) is applied.
test 2 => test (2/2)
=> “yes”
test 3 => test (3/2)
=> test (1.5/2)
=>
“no”
test 4 => test (4/2)
=> test (2/2)
=>
“yes”
1) Depth of recursion (the number of levels in the computation) = log n,
where n is the number that is input to the program
2) The amount of work on each level is one ‘/’ therefore is constant
3) Therefore the total work is proportional to log n
4) Therefore, the time complexity with respect to the number input n, is
O (log n).
(Logarithmic)
5) Represented graphically as follows, where the vertical axis is time.
Final 1996 #9
Calculate, and illustrate on a graph, the time complexity of the program
expand with respect to the length of the input list, given that: a++b has time
complexity of O(n) with respect to the length of the list a.
expand [ ] = [3]
expand (x:xs) = expand xs ++ expand xs
Some example traces:
expand [3] => expand [ ] ++ expand [ ]
=> [3]
[3]
=>
[3, 3]
expand [2,4]
=> expand [4]
=> expand [ ] ++ expand [ ]
=> [3]
[3]
=> [3, 3, 3, 3]
expand [3,5,7] =>
++
expand [4]
expand [ ] ++ expand [ ]
[3]
[3]
expand [5, 7]
++
expand [5, 7]
=>
expand [7] ++
expand [7]
expand [7]
++
expand [7]
=> expand [ ] ++ expand [ ] expand [ ]++ expand [ ]
expand [ ] ++ expand [ ]
expand [ ] ++ expand [ ]
=> [3]
[3]
[3]
[3]
[3]
[3]
[3]
[3]
=>
[3, 3, 3, 3, 3, 3, 3, 3]
1) The number of levels in the computation is proportional to n, the length of
the input
2) The width of the computation at the bottom level is proportional to 2^n
and the total work done at the bottom level is proportional to 2^n (add all
of the lengths of the left-hand arguments of all applications of (++).
3) Therefore, the total work done is at least proportional to 2^n.
4) Therefore the complexity is O(n^2) – exponential
(Continued on next page)
5) Represented graphically as follows, where the vertical axis is time.
Final 1997 #9
Calculate , and illustrate on a graph, the time complexity of the program
p8 with respect to the length of the second input list.
p8 a [ ]
=a
p8 a (x:xs) = p8 ([x] ++ a) xs
Some example traces
p8 [ ] [1,2] => p8 ([1] ++ [ ]) [2]
=> p8 ([2, 1]) [ ]
=>
[2, 1]
Work done in each step = k (as the ++ always has a list of length 1 on the
left)
p8 [ ] [3,4,5] => p8 ([3] ++ [ ])
[4, 5]
=> p8 ([4] ++ [3]) [5]
=> p8 ([5] ++ [4, 3]) [ ]
=> [5, 4, 3]
1) In general, the number of levels in the computation is proportional to
the length n of the second input.
2) The work done on each level is one application of (++), i.e constant k
3) Therefore the total work done is proportional to n * k
4) Therefore the time complexity is O(n) (i.e. linear) with respect to the
length n of the second input.
5) Depicted graphically as follows, where the vertical axis is time
(Continued on next page)
Final 1998 #9
Calculate and illustrate on a labeled graph, the time complexity of the
program with respect to the input number n. Note that [1..n] generates the
list of all integers from 1 to n.
p9 n = [ ], if n<1
p9 n = [1..n] ++ p9 (n/2)
Some example traces
p9 4 => [1, 2, 3, 4]
=>
=>
=>
++
p9 (4/2)
[1, 2] ++ p9 (2/2)
[1] ++ p9 (1/2)
[]
p9 8 => [1, 2, 3, 4, 5, 6, 7, 8] ++ p9 (8/2)
=>
[1, 2, 3, 4] ++ p9 (4/2)
=>
[1, 2] ++ p9 (2/2)
=>
[1] ++ p9 (1/2)
=>
[]
1) In general, the number of levels in the computation is proportional to
log2 n, where n is the input number. This is because n is divided by two
on each level until it becomes less than 1.
2) The work done on the first level is proportional to n (because ++ has
O(m) complexity where m is the length of its left input list), the work
done on the second level is proportional to n/2, the work done on the
third level is proportional to n/4, etc. So what is the average work done
on each level. This is difficult to compute. Therefore instead of
calculating the total work done by multiplying the number of levels by
the average work done, we can calculate it by simply adding the work
at level 1 to the work at level 2 to the work at level 3, etc. This gives: n
+ n/2 + n/4 + n/8 etc. The total of which is 2n
3) Therefore the total work done is proportional to n.
4) Therefore the complexity is O9N) where n is the input number.
5) Graphically, this is depicted as follows, where the vertical axis is time:
Final #1999 #9
Calculate, and illustrate on a labeled graph, the time complexity of the
program p8 below, with respect to the length of the first input list.
p8 [ ] a = a
p8 (x:xs) a = p8 xs ((x/2):a)
Note that (/) and (:) are O(k)
Example traces
p8 [4, 8] [1] => p8 [8] ((4/2) : [1])
=> p8 [ ] ((8/2) : [2,1])
=>
[4, 2, 1]
p8 [10, 6, 4] [3,4] => p8 [6, 4] ((10/2) : [3,4])
=> p8 [4]
((6/2) : [5, 3, 4])
=> p8 [ ]
((4/2) : [3, 5, 3, 4])
=>
[2, 3, 5, 3, 4]
1) In general the number of levels in the computation is proportional to the
length n of the first input list (because one element is removed on each
recursive call).
2) The work done on each level is constant k as it involves one (/) and (:)
3) Therefore, the total work done is proportional to n * k.
4) Therefore the time complexity is O(n).
5) Depicted graphically as shown on the next page:
Download