
\documentclass[11pt]{article}

\setlength{\topmargin}{-.8in}  
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.5in}

\usepackage{graphicx}
\begin{document}

\title{In-class exercises: Divide-and-conquer}

\date{}
\maketitle

The skyline problem/the upper envelope problem:

In this problem we design a divide-and-conquer algorithm for computing
the skyline of a set of $n$ buildings.
  
A {\em building} $B_i$ is represented as a triplet $({\bf L_i},H_i,
{\bf R_i})$ where ${\bf L_i}$ and ${\bf R_i}$ denote the left and
right $x$ coordinates of the building, and $H_i$ denotes the height of
the building (note that the $x$ coordinates are drawn boldfaced.)
  
  A {\em skyline} of a set of $n$ buildings is a list of $x$ coordinates
  and the heights connecting them arranged in order from left to right
  (note that the list is of length at most $4n$). 

  \vspace*{2cm}
  Example: The skyline of the buildings 
  $$\{({\bf 3},13,{\bf 9}), ({\bf 1},11,{\bf 5}), ({\bf 12},7,{\bf 16}),
  ({\bf 14},3,{\bf 25}), ({\bf 19},18,{\bf 22}), ({\bf 2},6,{\bf 7}), ({\bf
    23},13,{\bf 29}), ({\bf 23},4,{\bf 28}) \}$$
  is 
  $$\{{\bf 1},11,{\bf 3},13,{\bf 9},0,{\bf 12},7,{\bf 16},3,{\bf
    19},18,{\bf 22},3,{\bf 23},13,{\bf 29},0 \}$$ (note that the $x$
  coordinates in a skyline are sorted).

  \vspace*{0.5cm}
  \includegraphics[height=4.5cm]{skyline.pdf}


  \hspace{3cm} buildings \hspace{6cm} skyline
  \vspace*{1cm}

\begin{enumerate}

\item Let the size of a skyline be the total number of elements
  (coordinates and heights) in its list. 
  
  Describe an algorithm for combining a skyline $A$ of size $n_1$ and a
  skyline $B$ of size $n_2$ into one skyline $S$ of size $O(n_1+n_2)$. Your
  algorithm should run in time $O(n_1+n_2)$.

\item Describe an $O(n\log n)$ algorithm for finding the skyline of $n$
  buildings.
\end{enumerate} 


\newpage
{\bf Solution:} 


(a) We are given two skylines $A$ and $B$, and we want to merge them
in linear time.  Let $x$ be the next x-coordinate, in order. It can be
from A or from B (or both). 

We'll  keep track of the current skyline height, crtH. Initially crtH=0.  

The basic idea is that the skyline will be the larger of A and B.

When we look at $x$, two things can happen:

\begin{itemize} 

\item if $height(x) > crtH$ then the skyline jumps up.  We output $(x,
  height(x))$ to the skyline, and update $crtH = height(x)$.

\item if $height(x) > crtH$ then the skyline drops. It can drop to
  $height(x)$ or to the last height in the other skyline, whichever is
  larger.

\end{itemize} 

Overall, each $x$ can be handled in $O(1)$ time. 

\vspace{1cm}

(b) To get an $O(n \lg n)$ algorithm for finding the skyline of $n$
buildings we use divide-and-conquer. This is divide-and-conquer at its
best --- simple and elegant!!

\vspace{1cm} 
Skyline(n buildings)
\begin{itemize} 
\item if $n==1$ return the building

\item $A = $Skyline(the first n/2 buildings)

\item $B = $Skyline(the last n/2 buildings)

\item merge $A$ and $B$ as in (a) above and return merged skyline
\end{itemize} 

The recurrence is the same as mergesort, with solution $O(n \lg n)$.


Note: The beauty of it is that you can do this part without having done part (1) --- it's sufficient to know that it is possible to merge two skylines in linear time. 

\end{document}

