Skip to content

Commit

Permalink
algo: add hw9
Browse files Browse the repository at this point in the history
fix heading issue
  • Loading branch information
tiankaima committed May 19, 2024
1 parent 92d29f3 commit e9a755b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions 7e1810-algo_hw/hw7.typ
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ 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$.

#ans[
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$.*

#ans[
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$?

Expand Down
8 changes: 4 additions & 4 deletions 7e1810-algo_hw/hw8.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand All @@ -28,23 +28,23 @@ 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.

#ans[
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?

#ans[
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.

Expand Down
35 changes: 35 additions & 0 deletions 7e1810-algo_hw/hw9.typ
Original file line number Diff line number Diff line change
@@ -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%)
]
Binary file added 7e1810-algo_hw/imgs/sticker_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 7e1810-algo_hw/imgs/sticker_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions 7e1810-algo_hw/main.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

PB21000030 马天开

#include "hw9.typ"
#pagebreak()
#include "hw8.typ"
#pagebreak()
#include "hw7.typ"
Expand Down

0 comments on commit e9a755b

Please sign in to comment.