diff --git a/7e1810-algo_hw/hw7.typ b/7e1810-algo_hw/hw7.typ index 3e3eaa4..76b59ca 100644 --- a/7e1810-algo_hw/hw7.typ +++ b/7e1810-algo_hw/hw7.typ @@ -9,7 +9,7 @@ Due: 2024.04.28 ] } -== Question 21.1-1 +=== Question 21.1-1 Let $(u,v)$ be a minimum-weight edge in a connected graph $G$. Show that $(u,v)$ belongs to some minimum spanning tree of $G$. @@ -17,7 +17,7 @@ Let $(u,v)$ be a minimum-weight edge in a connected graph $G$. Show that $(u,v)$ Let $T$ be a minimum spanning tree of $G$. If $(u,v)$ is not in $T$, then $T union \{(u,v)\}$ contains a cycle $C$. Since $(u,v)$ is the minimum-weight edge in $G$ that crosses the cut $(V(T), V - V(T))$, we can replace an edge in $C$ with $(u,v)$ to get a spanning tree $T'$ with $w(T') < w(T)$, which contradicts the assumption that $T$ is a minimum spanning tree. ] -== Question 21.2-1 +=== Question 21.2-1 Kruskal's algorithm can return different spanning trees for the same input graph $G$, depending on how it breaks ties when the edges are sorted into order. *Show that for each minimum spanning tree $T$ of $G$, there is a way to sort the edges of $G$ in Kruskal's algorithm so that the algorithm returns $T$.* @@ -25,7 +25,7 @@ Kruskal's algorithm can return different spanning trees for the same input graph Let $T$ be a minimum spanning tree of $G$. We sort the edges of $G$ in nondecreasing order of their weights. If there are ties, we break them arbitrarily. Since $T$ is a minimum spanning tree, the edges of $T$ are sorted before the edges not in $T$. Therefore, Kruskal's algorithm will add the edges of $T$ to the tree before adding any other edges, and the result will be $T$. ] -== Question 21.2-4 +=== Question 21.2-4 Suppose that all edge weights in a grpah are integers in the range from $1$ to $abs(V)$. How fast can you make Kruskal's algorithm run? What if the edge weights are integers in the range from 1 to $W$ for some constant $W$? diff --git a/7e1810-algo_hw/hw8.typ b/7e1810-algo_hw/hw8.typ index 4b96044..410f496 100644 --- a/7e1810-algo_hw/hw8.typ +++ b/7e1810-algo_hw/hw8.typ @@ -9,7 +9,7 @@ Due: 2024.05.05 ] } -== Exerciese 1 +=== Exerciese 1 Proof that Bellman-Ford maximizes $x_1+x_2+dots.c+x_n$ subject to the constraints $x_j - x_i <= w_(i j)$ for all edges $(i,j)$ and $x <= 0$, and also minmizes $max_i {x_i}-min_i {x_i}$. #ans[ @@ -28,7 +28,7 @@ Proof that Bellman-Ford maximizes $x_1+x_2+dots.c+x_n$ subject to the constraint To see why this also minmizes $max_i {x_i}-min_i {x_i}$, we can see that the longest path is the one that maximizes the difference between the maximum and minimum values. (Suppose there exists another set of solutions that has a larger difference, say $max_i {x_i '} - min_i {x_i '}$, the constraint between the two sets of solutions would be violated, since the longest path is the one that maximizes the difference.) ] -== Question 23.2-6 +=== Question 23.2-6 Show how to use the output of the Floyd-Warshall algorithm to detect presence of a negative-weight cycle. @@ -36,7 +36,7 @@ Show how to use the output of the Floyd-Warshall algorithm to detect presence of If the output of the Floyd-Warshall algorithm contains a negative number on the diagonal, then there exists a negative-weight cycle in the graph. This is because the diagonal represents the shortest path from a vertex to itself, and if there exists a negative-weight cycle, then the shortest path from a vertex to itself would be negative infinity. ] -== Question 23.3-4 +=== Question 23.3-4 Professor Greenstreet claims that there is a simpler way to reweight edges than the method used in Johnson's algorithm. Letting $w^*=min_((u,v)in E){w(u,v)}$, just define $hat(w) (u,v)=w(u,v)-w^*$ for all edges $(u,v)in E$. What is wrong with the professor's method of reweighting? @@ -44,7 +44,7 @@ Professor Greenstreet claims that there is a simpler way to reweight edges than Might result in path with more edges longer than direct edge, which is not optimal. ] -== Question 24.3-3 +=== Question 24.3-3 Let $G=(V,E)$ be a bipartite graph with vertex partition $V=L union R$, and let $G$ be its corresponding flow network. Give a good upper bound on the length of any augmenting path found in $G$ during the execution of FORD-FULKERSON. diff --git a/7e1810-algo_hw/hw9.typ b/7e1810-algo_hw/hw9.typ new file mode 100644 index 0000000..ecc5a1d --- /dev/null +++ b/7e1810-algo_hw/hw9.typ @@ -0,0 +1,35 @@ +== HW9 (Week 11) +Due: 2024.05.19 + +#let ans(it) = { + box(inset: 1em, width: 100%)[ + #text(fill: blue)[ + #it + ] + ] +} + +=== Question 32.4-1 +Compute the prefix function $pi$ for the pattern `ababbabbabbababbabb`. + +#ans[ + $ + pi={0,0,1,2,0,1,2,0,1,2,0,1,2,3,4,5,6,7,8} + $ +] + +=== Question 32.4-6 +Show how to improve KMP-MATCHER by replacing the occurrence of $pi$ in line 5(but now line 10) by $pi'$, where $pi'$ is defined recusively for $q=1,2...,m-1$ by the equation +$ + pi'[q]=cases(0 quad & "if" pi[q]=0, pi'[pi[q]] quad &"if" pi[q]!=0 "and" P[pi[q]+1]=P[q+1], pi[q] quad & "otherwise") +$ + +Explain why the modified algorithm is correct, and explain in what sense this change constitutes an improvement. + +#ans[ + If $P[q+1]!=T[i] "and" P[pi[q]+q]=P[q+1]!=T[i]$, there's no need to compare $P[pi[q]+q]$ with $T[i]$, because $P[pi[q]+q]$ is the same as $P[q+1]$, so we can directly compare $P[q+1]$ with $T[i]$. This change improves the efficiency of the algorithm. +] + +#align(center)[ + #image("imgs/sticker_2.jpg", width: 50%) +] \ No newline at end of file diff --git a/7e1810-algo_hw/imgs/sticker_1.jpg b/7e1810-algo_hw/imgs/sticker_1.jpg new file mode 100644 index 0000000..c4072b6 Binary files /dev/null and b/7e1810-algo_hw/imgs/sticker_1.jpg differ diff --git a/7e1810-algo_hw/imgs/sticker_2.jpg b/7e1810-algo_hw/imgs/sticker_2.jpg new file mode 100644 index 0000000..d2a9c7f Binary files /dev/null and b/7e1810-algo_hw/imgs/sticker_2.jpg differ diff --git a/7e1810-algo_hw/main.typ b/7e1810-algo_hw/main.typ index cdeba1d..d1946ac 100644 --- a/7e1810-algo_hw/main.typ +++ b/7e1810-algo_hw/main.typ @@ -7,6 +7,8 @@ PB21000030 马天开 +#include "hw9.typ" +#pagebreak() #include "hw8.typ" #pagebreak() #include "hw7.typ"