Skip to content

Commit

Permalink
added advenced algos 4-7
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipNelson5 committed Aug 28, 2018
1 parent a732319 commit 2af0f6e
Show file tree
Hide file tree
Showing 21 changed files with 1,477 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CS5050_AdvanvedAlgorithms/hw4/1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
OBJS = *.hpp *.cpp

CC = g++
FLAGS = -std=c++17
DEBUG_FLAGS = -g3 -Og
RELEASE_FLAGS = -g0 -O3
WARNINGS = -Wall -Wextra -Werror

release: $(OBJS)
$(CC) $(FLAGS) $(RELEASE_FLAGS) $(WARNINGS) $(OBJS) -o heap.out

debug: $(OBJS)
$(CC) $(FLAGS) $(DEBUG_FLAGS) $(WARNINGS) $(OBJS) -o debug.out
1 change: 1 addition & 0 deletions CS5050_AdvanvedAlgorithms/hw4/1/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12 13
35 changes: 35 additions & 0 deletions CS5050_AdvanvedAlgorithms/hw4/1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <iomanip>
#include "minHeap.hpp"

int main()
{
minHeap<int> h;
h.insert(4);
h.insert(1);
h.insert(13);
h.insert(9);
h.insert(3);
h.insert(8);
h.insert(2);
h.insert(11);
h.insert(6);
h.insert(7);
h.insert(5);
h.insert(12);
h.insert(10);


std::cout << h.toString() << "\n";

while(true)
{
auto k = 0;
auto x = 0;
std::cin >> k;
std::cin >> x;
std::cout << std::boolalpha << h.kthLessThanX(k, x) << "\n";
}

std::cout << std::endl;
}
70 changes: 70 additions & 0 deletions CS5050_AdvanvedAlgorithms/hw4/1/minHeap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

template <typename T>
class minHeap
{
public:
void insert(T t)
{
heap.push_back(t);
auto pos = heap.size() - 1;

while (heap[pos] < heap[pos / 2])
{
std::swap(heap[pos], heap[pos / 2]);
pos /= 2;
}
}

bool kthLessThanX(unsigned int k, T x)
{
std::queue<unsigned int> q;
auto pos = 0u, ct = 0u, c1 = 0u, c2 = 0u;
q.push(pos);
while (ct <= k && q.size() != 0)
{
pos = q.front();
q.pop();
if (heap[pos] < x)
{
++ct;
c1 = pos * 2 + 1;
c2 = pos * 2 + 2;
if (c1 < heap.size()) q.push(c1);
if (c2 < heap.size()) q.push(c2);
}
}

if (ct >= k) return true;
return false;
}

std::string toString(unsigned int i = 0u, std::string tab = "")
{
if (i > heap.size() - 1) return "";
std::stringstream ss;

ss << toString(i * 2 + 2, tab + " ");
ss << tab << "(" << i << ")" << heap[i] << "\n";
ss << toString(i * 2 + 1, tab + " ");

return ss.str();
}

std::string serialize()
{
std::stringstream ss;
for (auto&& e : heap)
{
ss << e << " ";
}
return ss.str();
}

private:
std::vector<T> heap;
};
Binary file added CS5050_AdvanvedAlgorithms/hw4/source.pdf
Binary file not shown.
144 changes: 144 additions & 0 deletions CS5050_AdvanvedAlgorithms/hw4/source.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
% latex first.tex
% latex first.tex
% xdvi first.dvi
% dvips -o first.ps first.dvi
% gv first.ps
% lpr first.ps
% pdflatex first.tex
% acroread first.pdf
% dvipdf first.dvi first.pdf
% xpdf first.pdf
\documentclass[11pt]{article}

\usepackage{latexsym}
%\newcommand{\epsfig}{\psfig}
%\usepackage{tabularx,booktabs,multirow,delarray,array}
%\usepackage{graphicx,amssymb,amsmath,amssymb,mathrsfs}
%\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[linesnumbered, vlined, ruled]{algorithm2e}




%\usepackage[T1]{fontenc}


%\aboverulesep=0pt
%\belowrulesep=0pt

%\marginparwidth=0in
%\marginparsep=0in
\oddsidemargin=0.0in
\evensidemargin=0.0in
\headheight=0.0in
%\headsep=0in
\topmargin=-0.40in %0.35
\textheight=9.0in %9.1in
\textwidth=6.5in %6.55in

%\usepackage{fullpage}
\usepackage[in]{fullpage}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}


%\setlength{\headheight}{0.2in}
%\setlength{\headsep}{0.2in}
%\setlength{\voffset}{-0.2in}

\def\report{{\em Report-Max}$(1)$}
\def\reportk{{\em Report-Max}$(k)$}

%\pagestyle{plain}

%\usepackage{listings}
%\lstloadlanguages{C, csh, make} \lstset{
% language=C,tabsize=4,
% keepspaces=true,
% mathescape=true,
% breakindent=22pt,
% numbers=left,stepnumber=1,numberstyle=\footnotesize,
% basicstyle=\normalsize,
% showspaces=false,
% flexiblecolumns=true,
% breaklines=true, breakautoindent=true,breakindent=1em,
% escapeinside={/*@}{@*/}
%}

%\lhead{Solution of Homework 1}
%\rhead{Haitao Wang}

\begin{document}
\baselineskip=14.0pt

\title{CS5050 \textsc{Advanced Algorithms}
\\{\large Spring Semester, 2018}
\\ Assignment 4: Data Structure Design
\\ {\large {\bf Due Date: 3:00 p.m.}, Tuesday, Mar. 20, 2018 ({\bf at the beginning of CS5050 class})}}
\date{}
%\date{\today}


\maketitle
%\theoremstyle{plain}\newtheorem{theorem}{\textbf{Theorem}}

\vspace{-0.6in}


\begin{enumerate}
\item
{\bf (20 points)}
Suppose we have a min-heap with $n$ distinct keys that are stored in an array $A[1\ldots n]$ (a min-heap is one that stores the smallest key at its root). Given a value $x$ and an integer $k$ with $1\leq k\leq n$, design an algorithm to determine whether the $k$-th smallest key in the heap is smaller than $x$ (so your answer should be ``yes'' or ``no''). The running time of your algorithm should be $O(k)$, independent of the size of the heap.

\vspace{-0.15in}
\paragraph{Remark.} If we were to find the $k$-th smallest key of the heap, denoted by $y$, then the best way would be to perform $k$ times {\em deleteMin} operations, which would take $O(k\log n)$ time (or using the selection algorithm, which would take $O(n)$ time). Our above problem, however, is actually a {\em decision problem}. Namely, you only need to decide whether $y$ is smaller than $x$, and you do not have to know what the exact value of $y$ is. Hence, the problem is easier and we are able to solve it in a faster way, i.e., $O(k)$ time.

\item
{\bf (20 points)}
Suppose you are given a binary search tree $T$ of $n$ nodes (as discussed in class, each node $v$ has $v.left$, $v.right$, and $v.key$). We assume that no two keys in $T$ are equal. Given a value $x$, the {\em rank} operation $rank(x)$ is to return the {\em rank} of $x$ in $T$, which is defined to be one plus the number of keys of $T$ smaller than $x$. For example, if $T$ has 3 keys smaller than $x$, then $rank(x)=4$. Note that $x$ may or may not be a key in $T$. Refer to Figure~\ref{fig:bstsucc} for more examples.


\begin{figure}[h]
\begin{minipage}[t]{\linewidth}
\begin{center}
\includegraphics[totalheight=1.7in]{bstsucc.eps}
\caption{\footnotesize
$rank(16)=3$, $rank(21)=6$, $rank(25)=7$, $rank(26)=8$.
}
\label{fig:bstsucc}
\end{center}
\end{minipage}
\vspace*{-0.10in}
\end{figure}

Let $h$ be the height of $T$. We know that $T$ can support the ordinary {\em search, insert}, and {\em delete} operations, each in $O(h)$ time. You are asked to augment $T$, such that the {\em rank} operation, as well as the normal {\em search, insert}, and {\em delete} operations, all take $O(h)$ time each.

Please explain clearly how you augment $T$ and give your algorithm for performing the {\em rank} operations (please give the pseudocode). You do not need to give the details for other operations (search, insert, delete), but only need to briefly explain why they still take $O(h)$ time after you augment $T$.




\item
{\bf (20 points)}
This problem is concerned with {\bf range queries} (we have discussed a similar problem in class) on a binary search tree $T$ whose keys are real numbers (no two keys in $T$ are equal). Let $h$ denote the height of $T$. The range query is a generalization of the ordinary {\em search} operation. The {\bf range} of a range query on $T$ is defined by a pair $[x_l,x_r]$, where $x_l$ and $x_r$ are real numbers and $x_l\leq x_r$. Note that $x_l$ and $x_r$ may not be the keys in $T$.

You already know that $T$ can support the ordinary {\em search, insert}, and {\em delete} operations, each in $O(h)$ time. You are asked to design an algorithm to efficiently perform the {\em range queries}. That is, in each range query, you are given a range $[x_l,x_r]$, and your algorithm should report all keys $x$ stored in $T$ such that $x_l\leq x\leq x_r$. Your algorithm should run in $O(h+k)$ time, where $k$ is the number of keys of $T$ in the range $[x_l,x_r]$. In addition, it is required that all keys in $[x_l,x_r]$ be reported in a {\em sorted order}. Please give the pseudocode for your algorithm.

\vspace{-0.13in}
\paragraph{Remark.} Such an algorithm of $O(h+k)$ time is an {\em output-sensitive} algorithm because the running time (i.e., $O(h+k)$) is also a function of the output size $k$.
As an application of the range queries, suppose the keys of $T$ are the student scores of an exam. A range query like $[70,80]$ would report all scores in the range in sorted order.

\item
{\bf (20 points)}
Consider one more operation on the above binary search tree $T$ in Problem~3: {\em range-sum}$(x_l,x_r)$. Given any range $[x_l,x_r]$ with $x_l\leq x_r$, the operation {\em range-sum}$(x_l,x_r)$ reports the {\em sum} of the keys in $T$ that are in the range $[x_l,x_r]$.

You are asked to augment the binary search tree $T$, such that the {\em range-sum}$(x_l,x_r)$ operations, as well as the normal {\em search, insert}, and {\em delete} operations, all take $O(h)$ time each, where $h$ is the height of $T$.

You must present: (1) the design of your data structure (i.e., how you augment $T$); (2) the algorithm for implementing the {\em range-sum}$(x_l,x_r)$ operation (please give the pseudocode).
\end{enumerate}


{\bf Total Points: 80}

\end{document}

Binary file added CS5050_AdvanvedAlgorithms/hw4/writeup/hw4.pdf
Binary file not shown.
Loading

0 comments on commit 2af0f6e

Please sign in to comment.