Skip to content

Commit

Permalink
Question 1 and Question 3 half done
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitjain17 committed Sep 28, 2022
1 parent 5629e70 commit 9d281ad
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
Binary file added HW4/hw4.pdf
Binary file not shown.
186 changes: 186 additions & 0 deletions HW4/hw4.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
\documentclass[letterpaper,11pt]{article}

\usepackage{geometry}
\usepackage{pslatex}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{color}
\usepackage{enumitem} % for ordered list labels
\usepackage{amssymb} % for symbols
\usepackage{scrextend} % for indentation
\usepackage{tabto} % for tabs
\usepackage{amsmath} % for text in equation
\usepackage{listings} % to highlight code

% Define a custom color
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{codegreen}{rgb}{0,0.6,0}

% Define a custom style
\lstdefinestyle{myStyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
}

% Use \lstset to make myStyle the global default
\lstset{style=myStyle}

\graphicspath{ {./} }
\geometry{ margin = 1.0in }

%%% TODO modify these variables %%%
\def\homeworknum{4}
\def\myname{Harshit Jain}
\def\myaccessid{hmj5262}
\def\myrecitation{8}
%%%%

\pagestyle{fancy}
\lhead{{\bf CMPSC 465 Fall 2022}}
\chead{{\bf Assignment~\homeworknum}}
\rhead{{\bf \today}}

\newcounter{problemid}
%\stepcounter{problemid}
\def\newproblem{\clearpage\newpage{\bf Problem~\arabic{problemid}\stepcounter{problemid}}\hfill\fbox{\parbox{0.16\textwidth}{\bf Points:}}\par}

\setlength\parindent{0em}
\setlength\parskip{8pt}
\setlength{\fboxsep}{6pt}


\begin{document}

\framebox[\textwidth]{
\parbox{0.96\textwidth}{
\parbox{0.12\textwidth}{\bf Name:}\parbox{0.6\textwidth}{\myname}\\
\parbox{0.12\textwidth}{\bf Access ID:}\parbox{0.6\textwidth}{\myaccessid}\\
\parbox{0.12\textwidth}{\bf Recitation:}\parbox{0.6\textwidth}{\myrecitation}
}
}


%% your solutions %%%

\newproblem
\textbf{Acknowledgements}
\begin{enumerate}[label=(\alph*)]
\item I did not work in a group.
\item I did not consult without anyone my group members.
\item I did not consult any non-class materials.
\end{enumerate}


% PROBLEM 1
\newproblem
\textbf{Divide-and-Conquer}
\begin{enumerate}[label=(\alph*)]

\item We will first have the function which returns the frequency of the element in the given list. This function will take $O(n)$ time.

\begin{lstlisting}[language=Python]
def frequencyCalculator(Array, element):
count = 0
for ele in Array:
if (element == ele):
count += 1
return count
\end{lstlisting}

Now, to start, we will split the array $A$ into $2$ subarrays $A_1$ and $A_2$ of half the size. Then we will calculate the majority elements of $A_1$ and $A_2$.
The algorithm is as follows$:$

\begin{lstlisting}[language=Python]
def majorityElement(Array, low, high):

subArray = Array[low:high+1]

# base case
if (len(subArray)==1):
return subArray[0]

mid = (low+high)//2
leftMajorityElement = majorityElement(Array, low, mid)
rightMajorityElement = majorityElement(Array, mid+1, high)

if (leftMajorityElement == rightMajorityElement):
return leftMajorityElement

leftFrequency = frequencyCalculator(subArray, leftMajorityElement)
rightFrequency = frequencyCalculator(subArray, rightMajorityElement)

if (leftFrequency > len(subArray)//2):
return leftMajorityElement
elif (rightFrequency > len(subArray)//2):
return rightMajorityElement
else:
return -1 # no majority element found
\end{lstlisting}

\item

\item Here, we are choosing the majority element of sub-arrays after dividing them in half.
Moreover, frequency calculation is using $O(n)$ time as showen above.
Therefore, the recurrence relation for this algorithm is given by$:$ \[T(n)=2T(n/2)+O(n)\]
According to Master's Theorem, the time complexity$: \underline{O(n logn)}$

\end{enumerate}



% PROBLEM 2
\newproblem
\textbf{Reverse graph}
\begin{enumerate}[label=(\alph*)]

\item

\item

\end{enumerate}


% PROBLEM 3
\newproblem
\textbf{Graph Basics}
\begin{enumerate}[label=(\alph*)]

\item
\begin{minipage}{.22 \linewidth}
Adjacency-list

\begin{tabular}{l | l}

$A$ & $\rightarrow B \rightarrow E$\\
$B$ & $\rightarrow G \rightarrow D$\\
$C$ & $\rightarrow I \rightarrow H$\\
$D$ & $\rightarrow E$\\
$E$ & $\rightarrow D$\\
$F$ & $\rightarrow$\\
$G$ & $\rightarrow D \rightarrow F$\\
$H$ & $\rightarrow I$\\
$I$ & $\rightarrow$ \\
\end{tabular}\\
\end{minipage}

\item The most number of edges that an undirected graph can have are $: \frac{|V|(|V|-1)}{2}$

\item Since the degree of a vertex is the number of edges incident with that vertex, the sum of degree counts the total number of times an edge is incident with a vertex.
Since every edge is incident with exactly two vertices, each edge gets counted twice, once at each end. Thus the sum of the degrees is equal twice the number of edges.
Let $n$ be the number of edges in a simple graph $G(E,V)$. We proceed our proof

\item

\item
\end{enumerate}
\end{document}

0 comments on commit 9d281ad

Please sign in to comment.