Skip to content

Commit

Permalink
algo: finishing up; tweaking format
Browse files Browse the repository at this point in the history
  • Loading branch information
tiankaima committed May 19, 2024
1 parent e9a755b commit 900377d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
16 changes: 9 additions & 7 deletions 7e1810-algo_hw/hw1.typ
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ You can also think of insertion sort as a recursive algorithm. In order to sort
The recurrence for its worst-case running time is

$
T(n) = cases(T(n - 1) + Theta(n) space.quad & n>1, Theta(1) & n=1)
T(n) = cases(T(n - 1) + Theta(n) space.quad & n>1, Theta(1) & n=1)
$

The solution to the recurrence is $Theta(n^2)$ worst-case time.

]

=== Question 2-1 Insertion sort on small arrays in merge sort
=== Question 2-1

*Insertion sort on small arrays in merge sort*

Although merge sort runs in $Theta(n lg n)$ worst-case time and insertion sort runs in $Theta(n^2)$ worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus it makes sense to coarsen the leaves of the recursion by using insertion sort within merge sort when subproblems become suffificiently small. Consider a modifification to merge sort in which $n\/k$ sublists of length $k$ are sorted using insertion sort and then merged using the standard merging mechanism, where $k$ is a value to be determined.

Expand All @@ -47,18 +49,18 @@ Although merge sort runs in $Theta(n lg n)$ worst-case time and insertion sort r
- a. For each sublist, the insertion sort can sort the $k$ elements in $Theta(k^2)$ worst-case time. Thus, the insertion sort can sort the $n\/k$ sublists, each of length $k$, in $Theta(n k)$ worst-case time.
- b. Given $n\/k$ sorted sublists, each of length $k$, the recurrence for merging the sublists is
$
T(n) = cases(2 dot.c T(n\/2) + Theta(n) space.quad & n>k, 0 & n=k)
T(n) = cases(2 dot.c T(n\/2) + Theta(n) space.quad & n>k, 0 & n=k)
$
The solution to the recurrence is $Theta(n lg(n\/k))$ worst-case time.

*This could also be viewed as a tree with $lg(n\/k)$ levels with $n$ element in each level. Worst case would be $Theta(n lg (n\/k))$*

- c. Take $Theta(n k + n lg(n \/ k)) = Theta(n lg n)$, consider $k = Theta(lg n)$:
$
Theta(n k + n lg(n \/ k))
&= Theta (n k + n lg n - n lg k) \
&= Theta (n lg n + n lg n - n lg (lg n)) \
&= Theta (n lg n)
Theta(n k + n lg(n \/ k))
&= Theta (n k + n lg n - n lg k) \
&= Theta (n lg n + n lg n - n lg (lg n)) \
&= Theta (n lg n)
$
- d. Choose $k$ to be the largest length of sublist for which insertion sort is faster than merge sort. Use a small constant such as $5$ or $10$.

Expand Down
9 changes: 5 additions & 4 deletions 7e1810-algo_hw/hw3.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
== HW 3 (Week 4)
Due: 2024.03.31
== Question 12.2-3

=== Question 12.2-3
Write the `TREE-PREDECESSOR` procedure(which is symmetric to `TREE-SUCCESSOR`).

#text(fill: blue)[
Expand All @@ -18,7 +19,7 @@ Write the `TREE-PREDECESSOR` procedure(which is symmetric to `TREE-SUCCESSOR`).
```
]

== Question 13.1-5
=== Question 13.1-5
Show that the longest simple path from a node $x$ in red-black tree to a descendant leaf at most twice that of the shortest simple path from node $x$ to a descendant leaf.

#text(fill: blue)[
Expand All @@ -29,7 +30,7 @@ Show that the longest simple path from a node $x$ in red-black tree to a descend
Thus at most $floor((s - 1) / 2)$ of the nodes in the longest path are red, so $ t >= ceil((s+1)/2) $ If by way of contradiction, we had $s > t dot 2$, then $ t >= ceil((s+1) / 2) >= ceil(t+1) = t+1 $ which is a contradiction.
]

== Question 17.1-7
=== Question 17.1-7
Show how to use an order-statistic tree to count the number of inversions in an array of $n$ distinct elements in $O(n lg n)$ time.

#text(fill: blue)[
Expand All @@ -38,7 +39,7 @@ Show how to use an order-statistic tree to count the number of inversions in an
$O(n lg(n))$ time is required to build a red-black treem so everytime we insert a node, we can calculate the number of inversion using $"OS-RANK"$ (which is the rank of the node, thus calculating inversions).
]

== Question 17.3-2
=== Question 17.3-2
Describe an efficient algorithm that, given an interval $i$, returns an interval overlapping $i$ that has the minimum low endpoint, or $T."nil"$ if no such interval exists.

#text(fill: blue)[
Expand Down
8 changes: 4 additions & 4 deletions 7e1810-algo_hw/hw4.typ
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
== HW4 (Week 5)
Due: 2024.04.07
== Question 14.4-2
=== Question 14.4-2
Give pseudocode to reconstruct an LCS from te completed c table (See Theorem 14.1 Optimal substructure of an LCS) and the original sequences $X = angle.l x_1, x_2, dots.c, x_m angle.r$ and $Y = angle.l y_1, y_2, dots.c y_n angle.r$ in $O(m+n)$ time, without using the b table.

#text(fill: blue)[
== Solution 14.4-2
=== Solution 14.4-2

Consider the following pseudocode:

Expand All @@ -22,11 +22,11 @@ Give pseudocode to reconstruct an LCS from te completed c table (See Theorem 14.
```
]

== Question 14.4-5
=== Question 14.4-5
Give an $O(n^2)$-time algorithm to find the longest monotonically increasing subsequence of a sequence of $n$ numbers.

#text(fill: blue)[
== Solution 14.4-5
=== Solution 14.4-5

Given a sequence of numbers $L$, make a copy and sort it, let the $L^'$ be the sorted array:
$o(n^2)$ time to sort $L$
Expand Down
53 changes: 35 additions & 18 deletions 7e1810-algo_hw/main.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
size: 10pt,
)

= USTC/ALGO24 算法基础 书面作业
#align(horizon + right)[
#text(size: 12pt)[
= USTC/ALGO24 算法基础 书面作业
]
PB21000030 马天开

PB21000030 马天开
#v(4em)

#include "hw9.typ"
#pagebreak()
#include "hw8.typ"
#pagebreak()
#include "hw7.typ"
#pagebreak()
#include "hw6.typ"
#pagebreak()
#include "hw5.typ"
#pagebreak()
#include "hw4.typ"
#pagebreak()
#include "hw3.typ"
#pagebreak()
2024 年 春季学期 中国科学技术大学 算法基础 课程书面作业.

#v(4em)

使用 Typst 编写而成, 代码托管在: #link("https://github.com/tiankaima/typst-notes")

本文档以 CC BY-NC-SA 4.0 协议发布. 请遵守学术诚信, 不得用于商业用途.

#image("imgs/sticker_1.jpg", width: 30%)
]

#pagebreak(weak: true)

#include "hw1.typ"
#pagebreak(weak: true)
#include "hw2.typ"
#pagebreak()
#include "hw1.typ"
#pagebreak(weak: true)
#include "hw3.typ"
#pagebreak(weak: true)
#include "hw4.typ"
#pagebreak(weak: true)
#include "hw5.typ"
#pagebreak(weak: true)
#include "hw6.typ"
#pagebreak(weak: true)
#include "hw7.typ"
#pagebreak(weak: true)
#include "hw8.typ"
#pagebreak(weak: true)
#include "hw9.typ"

0 comments on commit 900377d

Please sign in to comment.