DAA-Assignment-1
Question 1
Given the input A=[11, -7, 8, 12, 6, 5, -6, 3] , track the divide-and-conquer algorithm to find the maximum contiguous subarray. You need to show the recursive calls as a tree. The input and output for each recursive call should also be indicated as well.
- Input: A=[11, -7, 8, 12, 6, 5, -6, 3]
- Output: MCS of A
\begin{algorithm}
\caption{Maximum Contiguous Subarray Problem}
\begin{algorithmic}
\If{$i = j$}
\State \Return $R[i]$
\Else
\State $s1 \gets \text{MCS}(R, i, \lfloor(i+j)/2\rfloor)$ \Comment{Using floor function for lower rounding}
\State $s2 \gets \text{MCS}(R, \lfloor(i+j)/2\rfloor + 1, j)$
\State $A \gets \text{MaxSuffix}(R, i, \lfloor(i+j)/2\rfloor) + \text{MaxPrefix}(R, \lfloor(i+j)/2\rfloor + 1, j)$
\State \Return $\text{MAX}(s1, s2, A)$
\EndIf
\end{algorithmic}
\end{algorithm}
Question 2
Given two polynomials
- Input: Two polynomials A(X) and B(x) of order n
- Output: The polynomial C(X) = A(x)B(x)
\usepackage{amsmath}
\begin{algorithm}
\caption{Polynomial Multi2($A(x),B(x)$)}
\begin{algorithmic}
\IF{$n=0$}
\RETURN $a_0 \times b_0$
\ELSE
\STATE $A_0(x) = a_0 + a_1x + \dots + a_{\lfloor \frac{n}{2} \rfloor - 1}x^{\lfloor \frac{n}{2} \rfloor -1}$
\STATE $A_1(x) = a_{\lfloor \frac{n}{2} \rfloor} + a_{\lfloor \frac{n}{2} \rfloor +1}x + \dots + a_nx^{n-\lfloor \frac{n}{2} \rfloor}$
\STATE $B_0(x) = b_0 + b_1x + \dots + b_{\lfloor \frac{n}{2} \rfloor - 1}x^{\lfloor \frac{n}{2} \rfloor -1}$
\STATE $B_1(x) = b_{\lfloor \frac{n}{2} \rfloor} + b_{\lfloor \frac{n}{2} \rfloor +1}x + \dots + b_nx^{n-\lfloor \frac{n}{2} \rfloor}$
\STATE $Y(x) = \text{PolyMult2}(A_0(x)+A_1(x), B_0(x)+B_1(x))$
\STATE $U(x) = \text{PolyMult2}(A_0(x), B_0(x))$
\STATE $Z(x) = \text{PolyMult2}(A_1(x), B_1(x))$
\RETURN $U(x) + (Y(x) - U(x) - Z(x))x^{\lfloor \frac{n}{2} \rfloor} + Z(x)x^{2\lfloor \frac{n}{2} \rfloor}$
\ENDIF
\end{algorithmic}
\end{algorithm}
Question 3
In the deterministic linear-time divide-and-conquer algorithm taught in class for the selection problem, the input array is divided into groups of 5 elements. Analyze the running time of the algorithm if the input array is divided into groups of 7. Does your algorithm run in linear time?
The Algorithm is still run in linear time
\usepackage{amsmath}
\begin{algorithm}
\caption{Deterministic-Select($A,p,r,i$)}
\begin{algorithmic}
\IF{$p=r$}
\RETURN $A[p]$
\ENDIF
\STATE $q = Deterministic-Partition(A,p,r)$
\STATE $k = q - p + 1$
\IF{$i = k$}
\RETURN $A[q]$
\ENDIF
\IF{$i < k$}
\RETURN $Deterministic-Select(A,p,q-1,i)$
\ENDIF
\IF{$i > k$}
\RETURN $Deterministic-Select(A,q+1,r,i-k)$
\ENDIF
\end{algorithmic}
\end{algorithm}
Assume always Deterministic-Select Right (wlog)
*Proof * $$T(n)=O(n)$$ Let
By strong inductionBase case: When $$n=n_0=14$$ *Assume for all * $$n_{0}\leq k \leq n-1$$
Goal $$T(n)\leq 14a\cdot n$$
Question 4
A segment is a pair of positive integers
Given a sequence of
Describe your algorithm in a high-level presentation.
- Input:
- Segments a sequence of
of segments sorted increasingly by ’s ( if ) within the r ( for all ) - Left to indicate left endpoint
- Right to indicate right endpoint
- R to indicate r (
for all )
- Segments a sequence of
- Output:
- Boolean value indicating whether there is at least one pair of intersecting segments.
- If the left and right indexes coincide, the intersect cannot be found. Return false;
- Divide the array into two halves: the left sublist and the right sublist.
- Recursively apply the algorithm to the left sublist to check for intersections.
- Recursively apply the algorithm to the right sublist to check for intersections.
- If any recursive call finds an intersection, return 'true' because we have already found the intersecting segments.
- Otherwise look for MaxLeftB in the left sublist and MinRightA in the right list
- If MinRightA is less than MaxLeftB, intersect is found
- Otherwise return 'false'.
Write down the pseudo code.
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{algorithm}
\caption{Check for Intersection in Segments}
\begin{algorithmic}
\FUNCTION{DoesIntersect}{$Segments$, $Left$, $Right$, $R$}
\IF{$Left \geq Right$}
\RETURN \textbf{false}
\ENDIF
\STATE $Mid \gets \lfloor(Left + Right) / 2\rfloor$
\STATE $LeftIntersect \gets$ \CALL{DoesIntersect}{$Segments$, $Left$, $Mid$, $R$}
\STATE $RightIntersect \gets$ \CALL{DoesIntersect}{$Segments$, $Mid + 1$, $Right$, $R$}
\IF{$LeftIntersect$ \textbf{or} $RightIntersect$}
\RETURN \textbf{true}
\ENDIF
\STATE $MaxLeftB \gets$ \CALL{FindMaxB}{$Segments$, $Left$, $Mid$}
\STATE $MinRightA \gets$ \CALL{FindMinA}{$Segments$, $Mid + 1$, $Right$}
\IF{$MaxLeftB > MinRightA$}
\RETURN \textbf{true}
\ENDIF
\RETURN \textbf{false}
\ENDFUNCTION
\Function{FindMaxB}{$Segments, Left, Mid$}
\State $MaxB \gets -\infty$ \For{$i \gets Left$ \textbf{to} $Mid$} \State $MaxB \gets \max(MaxB, Segments[i].b)$ \EndFor \State \Return $MaxB$
\EndFunction
\Function{FindMinA}{$Segments, Mid, Right$} \State $MinA \gets \infty$ \For{$i \gets Mid + 1$ \textbf{to} $Right$} \State $MinA \gets \min(MinA, Segments[i].a)$ \EndFor \State \Return $MinA$ \EndFunction
\end{algorithmic}
\end{algorithm}
Analyze the time complexity of your algorithm.
When
When
Question 5
Assume: The number of terms in the polynomials is always an integer power of
In other words,
Define a method to convert polynomials in general to polynomials whose number of terms is an integer power of 2.
- If the number of terms of the polynomial is not a power of two
- Then by adding the number of terms to the polynomial until the number of terms becomes a power of two, the coefficient of the added term is 0
Discuss the consequence by doing so. Does it increase the time complexity comparing to the original algorithm? Why?
Hint: please pay attention to the input size
- Input: Two polynomials
and of order - Output: The polynomials
\begin{algorithm}
\caption{PolyMulti(A(x),B(x))}
\begin{algorithmic}
\State \State $A^{'}(x)=M(A(x))$
\State $B^{'}(x)=M(B(x))$
\RETURN GPolyMultic($A^{'}(x),B^{'}(x)$)
\end{algorithmic}
\end{algorithm}
It will not increase the time complexity comparing to the original algorithm. The time cost will slightly increase. Suppose there are 2 polynomials with