| 1 |
\documentclass[ |
| 2 |
gantt, |
| 3 |
scheme, |
| 4 |
assembly, |
| 5 |
math, |
| 6 |
pseudocode, |
| 7 |
tabu |
| 8 |
]{brandeis-problemset} |
| 9 |
\bpsset{ |
| 10 |
coursenumber=21a, |
| 11 |
author=Rebecca Turner, |
| 12 |
instructor=Dr.\ Liuba Shrira, |
| 13 |
duedate=2018-10-20, |
| 14 |
number=3, |
| 15 |
} |
| 16 |
\newacronyms{io, cpu} |
| 17 |
\begin{document} |
| 18 |
\maketitle |
| 19 |
|
| 20 |
\Bf{Note:} This example document is provided to demonstrate the capability |
| 21 |
and visual style of the |
| 22 |
\href{https://ctan.org/pkg/brandeis-problemset}{\Tt{brandeis-problemset}} |
| 23 |
document class. The solutions below are not guaranteed to be correct, |
| 24 |
complete, or relevant. |
| 25 |
|
| 26 |
The source code for this document is available at |
| 27 |
\begin{quote} |
| 28 |
\href{http://mirrors.ctan.org/macros/latex/contrib/brandeis-problemset/example.tex}{\Tt{/macros/latex/contrib/brandeis-problemset/example.tex}} |
| 29 |
\end{quote} |
| 30 |
on \href{https://ctan.org/}{\textsc{ctan}} (or, if you have |
| 31 |
\Tt{brandeis-problemset} installed, in your \TeX\ distribution's |
| 32 |
documentation directory). |
| 33 |
|
| 34 |
\tableofcontents |
| 35 |
|
| 36 |
\begin{problem}[part=Textbook problems] |
| 37 |
An assembly language program implements the following loop: |
| 38 |
|
| 39 |
\begin{lstlisting}[language=c] |
| 40 |
int A[51]; |
| 41 |
int i = 1; |
| 42 |
while(i <= 50) { |
| 43 |
A[i] = i; |
| 44 |
i++; |
| 45 |
} |
| 46 |
\end{lstlisting} |
| 47 |
|
| 48 |
The array of integers $A$ is stored at memory location $x + 200$, |
| 49 |
where $x$ is the address of the memory location where the assembly |
| 50 |
program is loaded. Write the assembly program using the assembly |
| 51 |
language introduced in class. |
| 52 |
|
| 53 |
For a completely unrelated problem, see problem~\ref{schedule} (this |
| 54 |
is an example of a \lstinline!\label! / \lstinline!\ref! pair). |
| 55 |
\end{problem} |
| 56 |
|
| 57 |
\begin{assembly} |
| 58 |
LOAD R1, $200 ; A = (program location) + 200 |
| 59 |
LOAD R2, =1 ; i = 1 |
| 60 |
LOOP: STORE R2, @R1 ; *A = i |
| 61 |
ADD R1, =4 ; A++ |
| 62 |
INC R2 ; i++ |
| 63 |
BLEQ R2, =50, LOOP ; Ensure i <= 50 |
| 64 |
HALT |
| 65 |
\end{assembly} |
| 66 |
|
| 67 |
\begin{problem}[number=1.11] |
| 68 |
Direct memory access is used for high-speed \io\ devices in order to |
| 69 |
avoid increasing the \cpu's execution load. |
| 70 |
|
| 71 |
\begin{enumerate} |
| 72 |
\item How does the \cpu\ interface with the device to |
| 73 |
coordinate the transfer? |
| 74 |
\item How does the \cpu\ know when the memory operations are |
| 75 |
complete? |
| 76 |
\item The \cpu\ is allowed to execute other programs while |
| 77 |
the \ac{dma} controller is transferring data. Does |
| 78 |
this process interfere with the execution of user |
| 79 |
programs? If so, describe what forms of interference |
| 80 |
are caused. |
| 81 |
\end{enumerate} |
| 82 |
\end{problem} |
| 83 |
|
| 84 |
\begin{enumerate} |
| 85 |
\item The \cpu\ sets up ``buffers, pointers, and counters for the |
| 86 |
\io\ device'' and then ignores the transaction entirely; |
| 87 |
because \ac{dma} transfers don't involve the \cpu\ at all, |
| 88 |
they're especially efficient because they don't saturate the |
| 89 |
\cpu\ bus. |
| 90 |
\item The device controller sends a \cpu\ interrupt when each block of |
| 91 |
data finishes transferring. |
| 92 |
\item A \ac{dma} transfer only interferes with user programs as much |
| 93 |
as any other \io\ operation might, i.e.\ the program may not |
| 94 |
be able to complete other meaningful work before the |
| 95 |
transfer finishes. From the user's perspective, a \ac{dma} |
| 96 |
transfer is indistinguishable from any other type of \io\ |
| 97 |
operation. |
| 98 |
|
| 99 |
Additionally, a \ac{dma} takes a lock on \ac{ram}; while a |
| 100 |
\ac{dma} transfer is in progress, no other processes may |
| 101 |
access \ac{ram}, which can be extremely limiting. |
| 102 |
\end{enumerate} |
| 103 |
|
| 104 |
\begin{problem} |
| 105 |
In the following, use either a direct proof for the statements (by |
| 106 |
giving values for $c$ and $n_0$ in the definition of big-O notation) |
| 107 |
or cite the rules given in the lecture notes. |
| 108 |
|
| 109 |
\begin{enumerate} |
| 110 |
\item $\max(f(n), g(n))$ is $O(f(n) + g(n))$. Assume that $f(n)$ |
| 111 |
and $g(n)$ are non-negative for $n > 0$ |
| 112 |
\item If $d(n)$ is $O(f(n))$ and $e(n)$ is $O(g(n))$, then |
| 113 |
the product $d(n) \cdot e(n)$ is $O(f(n) \cdot g(n))$ |
| 114 |
\item $(n + 1)^5$ is $O(n^5)$ |
| 115 |
\item $n^2$ is $\Omega(n\log n)$ |
| 116 |
\item $2n^4 - 3n^2 + 32n\sqrt n - 5n + 60$ is $\Theta(n^4)$ |
| 117 |
\item $5n\sqrt n \cdot \log n$ is $O(n^2)$ |
| 118 |
\end{enumerate} |
| 119 |
\end{problem} |
| 120 |
|
| 121 |
An example equation which defines $e$: |
| 122 |
\begin{equation} |
| 123 |
\exists! e \in \Re \text{ such that } |
| 124 |
\int_1^e \frac{1}{t} dt = 1. |
| 125 |
\end{equation} |
| 126 |
|
| 127 |
The definition of the Mandelbrot set: |
| 128 |
\begin{equation} |
| 129 |
\begin{split} |
| 130 |
c \in \mathbb{C},\, z_0 = 0, \\ |
| 131 |
\lim_{n \to \infty} z_n = z_{n - 1}^2 + c \ne \infty |
| 132 |
\implies c \in \mathcal{M} |
| 133 |
\end{split} |
| 134 |
\end{equation} |
| 135 |
|
| 136 |
\begin{solution} |
| 137 |
The blue text here is a solution; it will disappear if the |
| 138 |
\Tt{solutions} class option is removed. |
| 139 |
|
| 140 |
``Rule $n$'' should be taken to refer to the $n$th rule on page 3 of the 5th |
| 141 |
lecture notes, and ``$a$ is faster-growing than $b$'' is written as ``$O(a) |
| 142 |
> O(b)$''. |
| 143 |
|
| 144 |
\begin{enumerate} |
| 145 |
|
| 146 |
\item Given that big-O notation describes asymptotic |
| 147 |
growth, only the fastest-growing term matters --- therefore, given some $a$ |
| 148 |
and $b$ that are functions of $n$, $O(a) > O(b) \implies O(a + b) = O(a)$. |
| 149 |
|
| 150 |
$\max(a, b)$ is defined to be the greater of $a$ and $b$, so $\max(a, b) \ge |
| 151 |
a$ and $\max(a, b) \ge b$. If $O(a) > O(b)$, $O(\max(a, b)) = O(a)$ (and |
| 152 |
vice-versa). |
| 153 |
|
| 154 |
Given these facts, if $O(f(n)) > O(g(n))$, $\lim_{n\to\infty} \max(f(n), |
| 155 |
g(n)) = f(n)$. Alternatively, if $O(f(n)) < O(g(n))$, $\lim_{n\to\infty} |
| 156 |
\max(f(n), g(n)) = g(n)$. More briefly, $O(\max(f(n), g(n)) = O(f(n)) |
| 157 |
\Rm{ or } O(g(n))$. |
| 158 |
|
| 159 |
And finally, because $O(a) > O(b) \implies O(a + b) = O(a)$ and $O(a) < O(b) |
| 160 |
\implies O(a + b) = O(b)$, we may note that $O(a + b)$ simplifies to the |
| 161 |
faster-growing of $O(a)$ and $O(b)$. The mathematical operation for ``the |
| 162 |
greater of two terms'' is $\max(a, b)$, so $\max(f(n), g(n)) = O(f(n) + |
| 163 |
g(n))$. |
| 164 |
|
| 165 |
\item This is true as stated in rule 3, although it's very similar to how $O(a) > |
| 166 |
O(b) \implies O(a + b) = O(a)$ --- in the asymptotic case, the smaller |
| 167 |
factor becomes irrelevant. |
| 168 |
|
| 169 |
\item Given that $(n + 1)^5 = n^5 + 5n^4 + 10n^3 + 10n^2 + 5n +1$ and as rule 5 |
| 170 |
states, only the highest degree of a polynomial matters (because |
| 171 |
$\lim_{n\to\infty} \sum_{i = 0}^{i = k} a_i n^i = a_k n^k$), $(n + 1)^5 = |
| 172 |
O(n^5)$. |
| 173 |
|
| 174 |
\item $c = 1, n_0 = 1$ |
| 175 |
|
| 176 |
\item $c_1 = 1, c_2 = 3, n_0 = 4$ |
| 177 |
|
| 178 |
\item $c = 2, n_0 = 1$ |
| 179 |
|
| 180 |
\end{enumerate} |
| 181 |
\end{solution} |
| 182 |
|
| 183 |
\begin{problem} |
| 184 |
What do the following two algorithms do? Analyze its worst-case |
| 185 |
running time and express it using big-O notation. |
| 186 |
|
| 187 |
\begin{pseudocode}[Foo] |
| 188 |
Foo(a, n) |
| 189 |
Input: two integers, a and n |
| 190 |
Output: a^n |
| 191 |
k <- 0 |
| 192 |
b <- 1 |
| 193 |
while k < n do |
| 194 |
k <- k + 1 |
| 195 |
b <- b * a |
| 196 |
return b |
| 197 |
\end{pseudocode} |
| 198 |
|
| 199 |
\begin{pseudocode}[Bar] |
| 200 |
Bar(a, n) |
| 201 |
Input: two integers, a and n |
| 202 |
Output: a^n |
| 203 |
k <- n |
| 204 |
b <- 1 |
| 205 |
c <- a |
| 206 |
while k > 0 do |
| 207 |
if k mod 2 = 0 then |
| 208 |
k <- k/2 |
| 209 |
c <- c * c |
| 210 |
else |
| 211 |
k <- k - 1 |
| 212 |
b <- b * c |
| 213 |
return b |
| 214 |
\end{pseudocode} |
| 215 |
|
| 216 |
\end{problem} |
| 217 |
|
| 218 |
$\Rm{Foo}(a, n)$ computes $a^n$, and will run in $O(n)$ time always. |
| 219 |
|
| 220 |
$\Rm{Bar}(a, n)$ \It{also} computes $a^n$, and runs in $O(\log n)$ |
| 221 |
time --- this is referred to as exponentiation by squaring. |
| 222 |
|
| 223 |
\begin{problem}[number=5.4, part=Scheduling, label=schedule] |
| 224 |
Consider the following set of processes, with the length of the |
| 225 |
\cpu\ burst given in milliseconds: |
| 226 |
|
| 227 |
\begin{center} |
| 228 |
\begin{tabu} to 0.25\linewidth{X[1,$]rr} |
| 229 |
\Th{Process} & \Th{Burst time} & \Th{Priority} \\ |
| 230 |
P_1 & 10 & 3 \\ |
| 231 |
P_2 & 1 & 1 \\ |
| 232 |
P_3 & 2 & 3 \\ |
| 233 |
P_4 & 1 & 4 \\ |
| 234 |
P_5 & 5 & 2 \\ |
| 235 |
\end{tabu} |
| 236 |
\end{center}%$ |
| 237 |
|
| 238 |
The processes are assumed to have arrived in the order $P_1$, $P_2$, |
| 239 |
$P_3$, $P_4$, $P_5$, all at time 0. |
| 240 |
|
| 241 |
\begin{enumerate} |
| 242 |
\item Draw four Gantt charts that illustrate the execution |
| 243 |
of these processes using the following scheduling |
| 244 |
algorithms: \ac{fcfs}, \ac{sjf}, nonpreemptive |
| 245 |
priority (a smaller priority number implies a higher |
| 246 |
priority), and \ac{rr} (quantum = 1). |
| 247 |
\item What is the turnaround time of each process for each |
| 248 |
of these scheduling algorithms? |
| 249 |
\item What is the waiting time of each process for each of |
| 250 |
the scheduling algorithms? |
| 251 |
\item Which of the algorithms results in the minimum average |
| 252 |
waiting time (over all processes)? |
| 253 |
\end{enumerate} |
| 254 |
\end{problem} |
| 255 |
|
| 256 |
\begin{enumerate} |
| 257 |
\item \ac{sjf} |
| 258 |
|
| 259 |
Average wait $= 3.2$. |
| 260 |
|
| 261 |
\begin{tabu} to 0.25\linewidth{@{}>{$P_\bgroup}X[1]<{\egroup$}rr@{}} |
| 262 |
\Th[@{}l]{Process} & \Th{Turnaround} & \Th[r@{}]{Waiting} \\ |
| 263 |
1 & 19 & 9 \\ |
| 264 |
2 & 1 & 0 \\ |
| 265 |
3 & 4 & 2 \\ |
| 266 |
4 & 2 & 1 \\ |
| 267 |
5 & 9 & 4 |
| 268 |
\end{tabu} |
| 269 |
|
| 270 |
|
| 271 |
\begin{ganttschedule}{19} |
| 272 |
\burst{2}{1} |
| 273 |
\burst{4}{1} |
| 274 |
\burst{3}{2} |
| 275 |
\burst{5}{5} |
| 276 |
\burst{1}{10} |
| 277 |
\end{ganttschedule} |
| 278 |
\end{enumerate} |
| 279 |
|
| 280 |
\begin{problem} |
| 281 |
Write a Scheme procedure to calculate an arbitrary up-arrow $a \uparrow^n |
| 282 |
b$. |
| 283 |
\end{problem} |
| 284 |
|
| 285 |
\begin{scheme} |
| 286 |
;;; (up-arrow 2 3 4) = 2^^^4 |
| 287 |
(define (up-arrow a n b) |
| 288 |
(cond ((= n 1) (expt a b)) |
| 289 |
((and (>= n 1) (= b 0)) 1) |
| 290 |
(else (up-arrow a |
| 291 |
(- n 1) |
| 292 |
(up-arrow a n (- b 1)))))) |
| 293 |
\end{scheme} |
| 294 |
\end{document} |