diff --git a/cs3230/PA2/main.pdf b/cs3230/PA2/main.pdf new file mode 100644 index 0000000..a1e11a7 Binary files /dev/null and b/cs3230/PA2/main.pdf differ diff --git a/cs3230/PA2/main.typ b/cs3230/PA2/main.typ new file mode 100644 index 0000000..59601f1 --- /dev/null +++ b/cs3230/PA2/main.typ @@ -0,0 +1,70 @@ +#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 PA2", + authors: ( + "Yadunand Prem, A0253252M", + ), +) + += Task 1 +== A +No I did not need to implement Strassen's Algorithm. The Freivalds algorithm allows us to calculate teh solution of $A times B = C$ in $O(k n^2)$ time, where k is a chosen constant, which is faster than Strassen's Algorithm. The benefits that strassens algorithm would give us is the 100% accuracy, but with a large enough $k$ value, Freivalds Algorithm is good enough. + +== B +Monte Carlo + +== C +=== Run faster than worst-case time complexity? +TC2, as the frievald's algorithm terminates early when the multiplication is wrong. For example, if $k = 8$,but in the first iteration, $A times B r != C r$, then this terminates and returns "WA". + +=== ii Always correct vs may be wrong? + +TC1 is always correct. Since the matrix multiplication is correct, any operation done on any given row, $A times B r = C r$ will always be correct, and thus, the algorithm will return "AC". + +TC2 operations on the other hand, may return the wrong result. Since the columns are chosen at random, there could be the case where $A times B != C$, but the chosen $r$ gives $A times B r = C r$. Since the algorithm doesn't check every column on every row, the result might be wrong. + +== D +I chose $k==8$ cause after trying with a few $k$ values, 8 was what had passed the testcases. When I had initially picked $6$, $1-1/2^6 approx 0.98$, it didn't work, but with $8, 1-1/2^8 approx 0.99$, it worked + +== E +No of tries = $1/((1-(1/2^8))^500) approx 8$, just looking at the 500 type 2 operations from the above given TC2. + + += Task 2 + +== A +The subproblem can be represented as `DP(i, at_S)`, where `at_S` is the side of the road Cissi is on, and it returns the number of crossings needed from Home to `i`. + +Thus, at the beginning, `DP(0, 0) = 0`, and `DP(0, 1) = 1`, as to get to the south side, she needs to cross once +- Base Case + - `DP(0, 0) = 0` as She is at Home + - `DP(0, 1) = 1` as she needs to cross the road once to reach the south side +- General Case + - If `crossing[i] = 'B'` + - `DP(i, 0) = DP(i-1, 0) + 1` + - `DP(i, 1) = DP(i-1, 1) + 1` + - Basically, +1 on both north and south side + - If `crossing[i] = 'N'` + - `DP(i,0) = min(DP(i-1,0) + 1, DP(i-1,1) + 1)` + - to get to the North side, its either you cross the NORTH street from the north side of the road OR you cross the horizontal street from the south side of the road + - `DP(i,1) = min(DP(i-1,0) + 1 + 1, DP(i-1,1))` + - Basically, to get to the south side, its either you cross the NORTH street from the north side of the road and cross the horizontal street OR you don't cross at all from teh south side of the street + - If `crossing[i] = 'S'` + - `DP(i,0) = min(DP(i-1,0), DP(i-1,1) + 2)` + - `DP(i,1) = min(DP(i-1,0) + 1, DP(i-1,1)) + 1` + +The final result will be in `DP(n-1, 0)` as the school is on the north side of the road at `n-1` +== B +- Base Case + - `DP[i][0] = 0` + - `DP[i][1] = 1` + +== C +$O(2^n)$ + +== D +$O(n)$ + diff --git a/cs3230/PA2/template.typ b/cs3230/PA2/template.typ new file mode 100644 index 0000000..ad2caec --- /dev/null +++ b/cs3230/PA2/template.typ @@ -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 +} diff --git a/cs3230/main.pdf b/cs3230/main.pdf index 97229f1..4cb13c5 100644 Binary files a/cs3230/main.pdf and b/cs3230/main.pdf differ diff --git a/cs3230/main.typ b/cs3230/main.typ index f29f0a4..8598acb 100644 --- a/cs3230/main.typ +++ b/cs3230/main.typ @@ -179,8 +179,35 @@ $T(n) = T(n/2) + O(1)$ = Tutorial 3 = Lecture 4 +== Lower bound for sorting +- Any comparison based sorting runs in $Omega (n log n)$ +- The tree must contain at least $n!$ leaves for every possible permutation +- Height of binary tree = $log(n!) = Omega n log n$ = Tutorial 4 = Lecture 5 +- Las Vegas Algorithms + - Output always correct +- Monte Carlo + - Answer may be incorrect with small probability = Tutorial 5 = Lecture 6 -= Tutorial 6 +== LCS +=== Brute Force +- Check all possible subsequences of A and check if its a subsequence of B and output longest one +- $2^n$ possible subsequences, Total Time $O(m 2^n)$ +=== Recursive +Base Case: +- $"LCS"(i, 0) = emptyset$ ($i$ is index A, $j$ is index B) +- $"LCS"(0, j) = emptyset$ ($i$ is index A, $j$ is index B) + +If $a_n = b_m$, then $"LCS"(n-1, m-1) ::a_n$ +Proof by contradiction +- If last symbol in $S = "LCS"(n, m)$ is not same as $a_n$, then last symbol must be a past of$a_1, ... a_(n-1)$, and $b_1, ... b_(n-1)$. +- $S$ is subsequence of $a_1, ... a_(n-1)$, and $b_1, ... b_(n-1)$. +- Append $a_n$ with $S$ i.e. $S :: a_n$ and get subsequence of length 1 more +- Thus $S$ canont be largest subsequence (Contradiction) +- So far, we only argued $a_n$ must be last symbol in $"LCS"(n, m)$ +- It is ifne to match $a_n$ with $b_m$ (since $a_n$ is last symbol) +- Therefore $"LCS"(n, m) = "LCS"(n-1, m-1)::a_n$ + +If $a_n != b_m, "LCS"(n, m) = max("LCS"(n-1,m), "LCS"(n, m-1))$ diff --git a/cs3230/template.pdf b/cs3230/template.pdf index bb0f6da..5fb5a22 100644 Binary files a/cs3230/template.pdf and b/cs3230/template.pdf differ diff --git a/cs3230/template.typ b/cs3230/template.typ index bcbc3a4..442b31e 100644 --- a/cs3230/template.typ +++ b/cs3230/template.typ @@ -9,7 +9,7 @@ numbering: "1", number-align: center, ) - set text(font: "Source Sans Pro", lang: "en", size: 11pt) + set text(font: "Source Sans Pro", lang: "en", size: 8pt) // set heading(numbering: "1.1") set par(justify: true, leading: 0.65em)