\documentclass[12pt,twoside]{article}
\usepackage{amsmath}
\newcommand{\profs}{Professors Srini Devadas and Erik Demaine}
\newcommand{\subj}{6.006}
\newlength{\toppush}
\setlength{\toppush}{2\headheight}
\addtolength{\toppush}{\headsep}
\newcommand{\htitle}[3]{\noindent\vspace*{-\toppush}\newline\parbox{6.5in}
{\textit{Introduction to Algorithms: 6.006}\hfill\newline
Massachusetts Institute of Technology \hfill #3\newline
\profs\hfill Handout #1\vspace*{-.5ex}\newline
\mbox{}\hrulefill\mbox{}}\vspace*{1ex}\mbox{}\newline
\begin{center}{\Large\bf #2}\end{center}}
\newcommand{\handout}[3]{\thispagestyle{empty}
\markboth{Handout #1: #2}{Handout #1: #2}
\pagestyle{myheadings}\htitle{#1}{#2}{#3}}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{0pt}
\setlength{\textwidth}{6.5in}
\setlength{\topmargin}{0in}
\setlength{\textheight}{8.5in}
\begin{document}
\handout{5}{Problem Set 3}{March 7, 2008}
\setlength{\parindent}{0pt}
\newcommand{\solution}{
\medskip
{\bf Solution:}
}
This problem set is due {\bf Thursday March 20} at {\bf 11:59PM}.
Solutions should be turned in through the course website in PDF form
using \LaTeX\ or scanned handwritten solutions.
A template for writing up solutions in \LaTeX\ is available on the
course website.
Remember, your goal is to communicate. Full credit will be given only
to the correct solution which is described clearly. Convoluted and
obtuse descriptions might receive low marks, even when they are
correct. Also, aim for concise solutions, as it will save you time
spent on write-ups, and also help you conceptualize the key idea of
the problem.
\medskip
\hrulefill
\medskip
Exercises are for extra practice and should not be turned in.
{\bf Exercises:}
\begin{itemize}
\item CLRS 6.1-3 (page 130)
\item CLRS 6.2-1 (page 132)
\item CLRS 6.3-1 (page 135)
\item CLRS 6.4-1 (page 136)
\item CLRS 6.4-3 (page 136)
\item CLRS 6.5-4 (page 141)
\item CLRS 8.2-2 (page 170)
\item CLRS 8.4-1 (page 177)
\end{itemize}
\hrulefill
\begin{enumerate}
\item {\bf (12 points)} Heap Delete
In this problem you will implement an additional operation in a min
heap. Namely, \texttt{heap.delete(i)} deletes the item at index $i$
in the array.
Download \texttt{ps3\_heap.zip}. In \texttt{heap\_delete.py},
which inherits from the code in \texttt{heap.py}, implement
\texttt{delete(i)}. Your code should run in $O(\log n)$ time where $n$
is the number of nodes currently in the heap.
The added code doesn't need to be more that a couple lines, but be
sure to add comments explaining why it works.
Run \texttt{test\_heap\_delete.py} to help determine if your new
delete method works, and submit \texttt{heap\_delete.py} to the class
website.
\item Monotone Priority Queues
A ``monotone priority queue'' (MPQ) is a (max) priority queue that
only allows monotonically decreasing elements to be extracted. It
supports the following operations:
\begin{itemize}
\item \texttt{Max}($Q$): Returns the key of the most recently
extracted node. If no nodes have been extracted, returns
$\infty$. This does not modify the MPQ.
\item \texttt{Extract\_Max}($Q$): Removes and returns the maximum node
currently in $Q$, and updates \texttt{Max($Q$)}. If $Q$ is empty,
returns \texttt{Max($Q$)}.
\item \texttt{Insert}($Q, x$): Inserts $x$ into $Q$ given that $x \leq
\texttt{Max}(Q)$. If $x> \texttt{Max}(Q)$, then the MPQ is not modified.
\end{itemize}
When asked to ``describe an implementation'', you may start with
something already proven in class or in the book, and simply describe
modifications to that.
\begin{enumerate}
\item {\bf (9 points)} Describe an implementation of a monotone priority
queue that takes $O(m \log m)$ time to perform $m$ operations starting
with an empty data structure.
\item {\bf (9 points)}
Now suppose that every inserted key $x$ is an integer
in the range $[0,k]$ for some fixed integer value~$k$.
Describe an implementation of such a monotone priority queue
that takes $O(m + k)$ time to perform $m$ total operations.
Hint: Use an idea from \texttt{Counting\_Sort}.
Warning: Be careful about the case when the queue becomes empty.
\end{enumerate}
\item Gas Simulation
In this problem, we consider a simulation of $n$ bouncing balls in
two dimensions inside a square box. Each ball has a mass and
radius, as well as a position $(x,y)$ and velocity vector, which
they follow until they collide with another ball or a wall.
Collisions between balls conserve energy and momentum. This model
can be used to simulate how the molecules of a gas behave, for
example. The world is $400\sqrt{n}$ by $400\sqrt{n}$ units wide, so
the area is proportional to the number of balls. Each ball has a
minimum radius of 16 units and a maximum radius of 128 units.
Download \texttt{ps3\_gas.zip} from the class website. For the
graphical interface to work, you will need to have pygame or tkinter
installed. They currently run slightly different
interfaces. Feedback is appreciated.
You may notice that performance, indicated by the rate of simulation steps
per second, is highly dependent on the number of balls. Your
goal is to improve the running time of the
\texttt{detect\_collisions} function. This function computes which pairs of
balls collide (two balls are said to \emph{collide} if they overlap) and
returns a set of \texttt{ball\_pair} objects for collision
handling. You should not need to modify the rest of the simulation.
(If you think something else should be modified, e-mail
\texttt{6.006-staff} with your feedback.)
\begin{enumerate}
\item {\bf (2 points)} What is the running time of
\texttt{detect\_collisions} in terms of $n$, the number of balls?
\item {\bf (6 points)} Argue that the following algorithm is
asymptotically faster:
Divide the world into square bins of width 256. For each ball, put
it in its appropriate bin. Then for each bin, check for collisions
where either both balls are in the bin, or one ball is in the bin,
and the other ball is in an adjacent bin.
\item {\bf (20 points)} Implement the \texttt{detect\_collisions}
algorithm described in part (b). Put your code in
\texttt{detection.py}, and uncomment the line in \texttt{gas.py}
just below \texttt{detect\_collisions} that imports your new code.
Your new code must still find all the same collisions found by the
old code (any pair of balls for which \texttt{colliding} returns
true). To check that you are detecting the same collisions, run
your code and the original code with the same parameters, and make
sure that they detect the same number of collisions.
In your documentation, do not assume that the reader has read this
problem set.
Submit \texttt{detection.py} to the class website.
\item {\bf (2 points)} Using your improved code, after 1000
timesteps with 200 balls, how many collisions did you get? How
many simulation steps per second did you run? How many simulation
steps per second could you run with the original code and the same
number of balls?
\end{enumerate}
\end{enumerate}
\end{document}