feat: 3230 WA2
This commit is contained in:
parent
547b8d178c
commit
91eaea2130
BIN
cs3230/WA2/main.pdf
Normal file
BIN
cs3230/WA2/main.pdf
Normal file
Binary file not shown.
127
cs3230/WA2/main.typ
Normal file
127
cs3230/WA2/main.typ
Normal file
@ -0,0 +1,127 @@
|
||||
#import "template.typ": *
|
||||
|
||||
// Take a look at the file `template.typ` in the file panel
|
||||
// to customize this template and discover how it works.
|
||||
#show: project.with(
|
||||
title: "CS3230 WA2",
|
||||
authors: (
|
||||
"Yadunand Prem, A0253252M",
|
||||
),
|
||||
)
|
||||
|
||||
= Question 1
|
||||
|
||||
Since there is a favourable seating constraint, and $f_i$ is distinct set of numbers, we can sort the guests in a decreasing order of favour score. This can be done in $O(n log n)$ timing
|
||||
|
||||
We can think about this problem as picking a guest from the sorted list and adding them to either the left of the host or the right of the host. Brute forcing this would run in $O(2^n)$ time complexity. We can reframe this as such, `DP(num_left, num_right, last_left, last_right)`. This keeps track of the number of people on the left, number of people on the right, and the index of the person last placed on the left and right. last_left and last_right is used to calculate the hatred when a new person is added to the corresponding side.
|
||||
|
||||
To show that there are overlapping subproblems, we need to show that 2 different paths then result in solving a similar problem. We can generate them as such: With `num_left == num_right`, and the last `x` moves are the same, and they contain at least one `L` and one `R`, they are overlapping. For example, if the pathing taken was `LRLRLR` and `LLRRLR`, both would end up with `DP(3, 3, 6, 7)`. Thus, there are overlapping subproblems.
|
||||
|
||||
At the $k$th iteration, the $k$th most favourable guest must be added to either the left end or the right end. Suppose not, that there is a more optimal solution where another guest is added instead. This would form a contradiction of the favourability constraint. Therefore, the $k$th most favourable guest must be added to either the left end or the right end.
|
||||
|
||||
Base Case: `dp(n, n, last_left, last_right) = 0`
|
||||
|
||||
This occurs when this has reached the last node of the recursion tree, when there are $n$ guests on both the left and right.
|
||||
|
||||
General Case:
|
||||
Let $"last_left", "last_right", "num_left", "num_right"$ be $l_l, l_r, n_l, n_r$, and $"curr"$ = `max(l_l, l_r)+1`
|
||||
|
||||
$"dp" = min cases(
|
||||
"hatred"[l_l]["curr"] + "dp"(n_l+1, n_r, "curr", l_r),
|
||||
"hatred"[l_r]["curr"] + "dp"(n_l, n_r+1, l_l, "curr"),
|
||||
)$
|
||||
|
||||
The solution is the minimum of _going_ both left and right, and then calculating the min hatred from that. We can form the memoization by indexing the table by (num_left, num_right, last_left, last_right). Since each value ranges from $0-n$, the total number of subproblems is $O(n^4)$. Each subproblem only has addition and comparsions, all of which run in $O(1)$ timing. Therefore, this algorithm runs in $O(n^4)$.
|
||||
|
||||
We can optimise this by deriving `num_right = max(last_left, last_right) - num_left`. This reduces the number of subproblems to $O(n^3)$, and thus, the total time reduces to $O(n^3)$.
|
||||
|
||||
```
|
||||
dp(num_left, num_right, last_left, last_right):
|
||||
next = max(last_left, last_right) + 1
|
||||
|
||||
if num_left == n and num_right == n:
|
||||
return 0
|
||||
|
||||
if num_left != n:
|
||||
left = h[last_left][next] + dp(num_left + 1, num_right, next, last_right)
|
||||
if num_right != n:
|
||||
right = h[last_right][next] + dp(num_left, num_right + 1, last_left, next)
|
||||
return min(left, right)
|
||||
```
|
||||
|
||||
#pagebreak()
|
||||
|
||||
= Question 2
|
||||
== 2bi)
|
||||
$P("any random element from T has T-rank in"[k,k+r)) = r/n$
|
||||
$P("any random element from T does not have T-rank in"[k,k+r)) = 1 - r/n$ \#
|
||||
|
||||
== 2bii)
|
||||
Show that $P("none of elements in S has T rank in" [k,k+r))$ is bounded from above by $1/n^3$
|
||||
|
||||
$P("none of elements in S has T rank in" [k,k+r)) = (1-r/n)^(2n^(2/3)) = (1-(5n^(1/3)ln(n))/n)^(2n^(2/3)) = (1-(5ln(n))/n^(2/3))^(2n^(2/3)) $
|
||||
|
||||
let $x = n^(2/3)/(5ln(n))$, it can be seen that $forall n > 1, x > 0$.
|
||||
|
||||
$(1 - 1/x)^x &<= 1/e ("Using Lemma 1b")\
|
||||
(1-1/x)^(n^(2/3)/(5ln(n))) &<= 1/e equiv
|
||||
(1-1/x)^(n^(2/3)) <= (1/e)^(5ln(n))\
|
||||
(1-1/x)^(2n^(2/3)) &<= (1/e)^(10ln(n)) equiv
|
||||
(1-1/x)^(2n^(2/3)) <= (1/n^10)\
|
||||
(1-(5ln(n))/(n^(2/3)))^(2n^(2/3)) &<= (1/n^10) <= (1/n^3)$
|
||||
|
||||
== 2c)
|
||||
Let the interval $r_i, r_(i+1)$ be called a range.
|
||||
|
||||
Show that the $P("S contains at least 1 element from each range") >= 1-1/n^2$. This is the same as $P("S contains no elements from each range") < 1/n^2$
|
||||
|
||||
$P("none from each range") < sum P("none from range i")$ (Union Bound)
|
||||
|
||||
$P("none from range i") <= 1/n^3, "num of ranges" = n/(5n^(1/3)ln(n))$
|
||||
|
||||
$sum P("none from range i") <= (n/(5n^(1/3)ln(n)))/(1/n^3) = 1/(n^2 dot 5n^(1/3)ln(n))$
|
||||
|
||||
$forall n > 1, ln(n) > 0\
|
||||
therefore, 1/(n^2 dot 5n^(1/3)ln(n)) < 1/n^2$
|
||||
|
||||
$P("S contains no elements from each range") < 1/n^2$
|
||||
|
||||
$P("S contains at least 1 element from each range") >= 1-1/n^2$ \#
|
||||
|
||||
== 2d)
|
||||
Let the event where $a <= M$ be $A$ and $b >= M$ be $B$.
|
||||
|
||||
$P(A) = P(B) >= 1-2/n^3$
|
||||
|
||||
$P(A sect B) = P(overline(overline(A) union overline(B))) = 1 - P(overline(A) union overline(B))$
|
||||
|
||||
$P(overline(A) union overline(B)) <= P(overline(A)) + P(overline(B)) = 4/n^3\
|
||||
1 - P(overline(A) union overline(B)) >= 1 - 4/n^3$
|
||||
|
||||
$P(A sect B) >= 1-4/n^3 >= 1-1/n$ \#
|
||||
|
||||
Between $a$ and $b$, there are $2 * 5n^(1/3)ln(n)$ elements in $S$. With a $1-1/n^2$ probability, we can say that each element must at least be from 1 range in $S'$. Therefore, there are at most $10n^(1/3)ln(n) dot 5n^(1/3) ln(n) = 50n^(2/3)(ln(n)^2)$ elements between $a$ and $b$ in $S'$.
|
||||
|
||||
$P("S contains at least 1 element of T rank from each range") = \
|
||||
P("S' contains at most" 50n^(2/3)(ln(n))^2) = 1-1/n^2 < 1-1/n$\#
|
||||
|
||||
== 2e)
|
||||
To prove that the algorithm runs in linear time, we need to show that each step of the algorithm runs in at most $O(n)$.
|
||||
|
||||
1. Randomly selecting $2n^(2/3)$ elements take $O(n)$ timing
|
||||
|
||||
2. Sorting $S$ of $2n^(2/3)$ elements takes $2n^(2/3) log (2n^(2/3)) = O(n^(2/3) log(n)) < O(n)$ using a comparison based sorting algorithm
|
||||
|
||||
3. This can be calculated in $O(n)$ by counting the number of elements lesser than $a$ and $b$ in $T$
|
||||
|
||||
4-6. $O(1)$
|
||||
|
||||
7. This can also be done in $O(n)$, by iterating through $T$ and selecting the valid values that are $a <= n <= b$
|
||||
|
||||
8. $S$ can have at most $1000n^(2/3)(ln n)^2$ elements, and sorting them takes $1000n^(2/3)(ln n)^2 log(1000n^(2/3)(ln n)^2)$. $1000n^(2/3)(ln n)^2 < O(n)$. $log(1000n^(2/3)(ln n)^2) < O(n). therefore$. This runs in at most $O(n)$
|
||||
|
||||
Step 9: Indexing a sorted array can be done in $O(1)$
|
||||
|
||||
Therefore, since each operation runs in at most $O(n)$, this algorithm runs in linear time
|
||||
|
||||
From part $d$, $P(a <= M <= b) >= 1-1/n$. So With probability of $1-1/n$, M lies in $S'$. Since $S'$ is sorted and a portion of $T$, the median would be in the center of $T$. It then gets the index of the element at middle of $T$, the Median, and by subtracting $R_a$, it gets the index of the median with respect to $S'$. Therefore, the algorithm would return the median with $1-1/n$ probability.
|
31
cs3230/WA2/template.typ
Normal file
31
cs3230/WA2/template.typ
Normal file
@ -0,0 +1,31 @@
|
||||
// The project function defines how your document looks.
|
||||
// It takes your content and some metadata and formats it.
|
||||
// Go ahead and customize it to your liking!
|
||||
#let project(title: "", authors: (), body) = {
|
||||
// Set the document's basic properties.
|
||||
set document(author: authors, title: title)
|
||||
set page(numbering: "1", number-align: center)
|
||||
set text(font: "Linux Libertine", lang: "en")
|
||||
|
||||
// Title row.
|
||||
align(center)[
|
||||
#block(text(weight: 700, 1.75em, title))
|
||||
]
|
||||
|
||||
// Author information.
|
||||
pad(
|
||||
top: 0.5em,
|
||||
bottom: 0.5em,
|
||||
x: 2em,
|
||||
grid(
|
||||
columns: (1fr,) * calc.min(3, authors.len()),
|
||||
gutter: 1em,
|
||||
..authors.map(author => align(center, strong(author))),
|
||||
),
|
||||
)
|
||||
|
||||
// Main body.
|
||||
set par(justify: true)
|
||||
|
||||
body
|
||||
}
|
Loading…
Reference in New Issue
Block a user