Skip to content

Commit 5263259

Browse files
More practice problems
1 parent c5f37f7 commit 5263259

11 files changed

Lines changed: 141 additions & 34 deletions

OMSCS/Courses/CN/Lesson 03 - Intradomain Routing.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
2-
tags: OMSCS, CN
2+
tags:
3+
- OMSCS
4+
- CN
35
---
46
# Lesson 03 - Intradomain Routing
57
> Focuses on the network layer and a specific function of the network layer: routing within a single administrative domain.

OMSCS/Courses/GA/Office Hours/2026-03-15 - TA Office Hours - Exam 2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ This OH covers the following strategies, outlined below. It also covers the foll
2929
- Correct: "The chosen edge must be in the MST due to the Cut Property."
3030
- Incorrect: "The algorithm is correct due to the Cut Property".
3131
- **Don't** rebuild the entire MST if/when you don't need to.
32-
- [[5.22 - Fast MST Recomputation (TODO)]]
33-
- [[5.23 - Light Spanning Trees (TODO)]]
32+
- [[5.22 - Fast MST Recomputation]]
33+
- [[5.23 - Light Spanning Trees]]
3434

3535
## Max-Flow Strategies
3636
- Convert to flow network

OMSCS/Courses/GA/Practice Problems/5.20 - Perfect Matching Tree (TODO).md

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 5.20 - Perfect Matching Tree
8+
![[Pasted image 20260316173646.png]]
9+
10+
Note, the "feedback edge set" was not part of the problem recommended by course staff.
11+
12+
## Exploration
13+
- The problem statement indicates that $G$ is a connected tree, and not a forest of subtrees.
14+
- If $G$ has an odd number of vertices, then the answer is automatically "no". If $G$ has no vertices, the answer is automatically "yes", with $PM=\emptyset$. The following steps presume that G has an even number of vertices, and at least 2 of them.
15+
- We define a "fringe" vertex as a vertex which only has one edge.
16+
- We can identify the number of edges connected to each v in O(n+m) time. We'll call this structure `cardinality[]`. If $cardinality[v]=1$, then $v$ is a fringe vertex.
17+
- We can then build a queue of the initial "fringe" vertices. There must be at least 2, since $G$ is a nontrivial tree.
18+
- While the queue has fringe vertices.
19+
- We pull a fringe vertex $v$ from the queue.
20+
- We first check whether $v$ has already been removed from $G$. If $v$ has already been removed from $G$, we ignore it, and pull the next vertex from the queue.
21+
- We iterate over all the edges leaving v ($e=(v,u) \in E$) to find the one neighbor of $v$ which has not already been removed from $G$. This will find either 0 or 1 options for $u$.
22+
- If we find 0 vertices, then $v$ was isolated by prior pruning operations, and there no longer exists an edge which touches $v$. Therefore there does not exist a "perfect matching" in G.
23+
- Otherwise, we iterate over all vertices adjacent to $u$, and reduce their cardinality. For all the vertices which now have a cardinality of 1, we add them to the queue.
24+
- We conclude by removing both $v$ and $u$ from $G$.
25+
- If we successfully remove all vertices from $G$ without issue, then $G$ has a perfect matching. We can build this $PM$ set of edges during the algorithm's execution, but the question doesn't ask for it, so we aren't doing it.
26+
27+
I believe this works, but it doesn't use a black box from [[04.0.1 - Graphs - Black Box Algorithms]].
28+
29+
## Algorithm
30+
- Check for an odd number of vertices. If so, return no.
31+
- Check for 0 vertices. If so, return yes.
32+
- DFS on $G$ from any arbitrary vertex $s \in V$ to create `pre[]` and `prev[]`
33+
- Create an ordering of $V$ based on `pre[]`. We'll call this $V_{pre}$
34+
- Walk backwards through $v \in V_{pre}$.
35+
- If $v$ is removed, skip it, and select the next $v$.
36+
- Otherwise
37+
- Remove $v$ from $G$.
38+
- Check whether $prev[v]$ is removed from $G$. If so, we answer "no".
39+
- Otherwise, remove $prev[v]$ from $G$.
40+
- If we managed to get through the whole list without returning "no", then we answer "yes".
41+
42+
## Justifications
43+
- Iterating over vertices by descending order of preorder number prioritizes vertices which are "fringe" vertices. The vertex with the highest preorder number has only one edge.
44+
- We walk "up" the tree from those fringe vertices, towards the arbitrary starting vertex, removing connected pairs from the tree as we go.
45+
- We run into an issue when 2 vertices $v_1$ and $v_2$ share the same parent ($u=prev[v_1]=prev[v_2]$), and are independently selected as $v \in V_{pre}$ in our algorithm. This indicates that the DFS from $s$ encountered a branch at $u$. $u$ is the root of of a subtree $T_u$ of $G$, and removing $u$ from $T_u$ produces at least 2 additional subtrees. At least 2 of these subtrees, symbolized $T_{u,a}$ and $T_{u,b}$, are both independently incapable of producing a $PM$ set of edges. Therefore, both must include an edge to $u$ in order to produce a $PM$ set. However, this would require that the $PM$ set of $G$ contains $u$ at least twice, which is a contradiction in the definition of what a $PM$ set of edges is.
46+
47+
## Runtime
48+
- Checking for odd number of vertices, or 0 vertices: $O(n)$
49+
- DFS: $O(n+m)$
50+
- Creating $V_{pre}$: $O(n)$
51+
- Iterating over $V_{pre}$: $O(n)$
52+
53+
Overall: $O(n+m)$
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 5.21 - Heaviest Edges in Cycles
8+
![[Pasted image 20260317122842.png]]
9+
10+
## 5.21a - Prove this Property
11+
If $e$ is the heaviest edge in a given cycle $C$, then there exists at cut of $G$ where 2 of the candidates are $e$ and some other $e' \in C$ where $e'$ is the minimum weight edge in the cycle. The MST will never contain $e$ if $w(e') < w(e)$. If $w(e)=w(e')$, then the MST can still choose $e'$ instead of $e$.
12+
13+
## 5.21b - Prove Algorithm Correctness
14+
The alg is correct, but I don't know a linear cycle detection alg that's robust to alterations of $G$.
15+
16+
The proof leverages the Cut Property. If we have a cycle $C$, then one of the heaviest edges in $C$ can be removed from $E$. Even with this removal, all vertices in the cycle can still reach each other, therefore $E'$ still spans $G$. We have to assume that $G$ is connected, i.e. that $E$ spans $G$, otherwise there never was an MST of $G$.
17+
18+
Removing all heaviest edges from all cycles will arrive an an MST, iteratively pruning $E'$ until $E'=X$.
19+
20+
## 5.21d - Runtime Analysis
21+
- Sort edges: $O(m \space log \space m)$
22+
- Iter $e \in E$: $O(m)$
23+
- Check for cycle: $O(n)$ or $O(m)$
24+
- Remove edge: $O(n)$
25+
26+
Even if we have a cycle detection algorithm that is initially $O(n+m)$ initially, and then $O(1)$ after each edge removal, the resulting runtime is at least $O(nm)$. If the cycle detection is $O(m)$ on each iteration, then the runtime is $O(m^2)$.

OMSCS/Courses/GA/Practice Problems/5.22 - Fast MST Recomputation (TODO).md

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 5.22 - Fast MST Recomputation
8+
![[Pasted image 20260316211257.png]]
9+
10+
## 5.22a
11+
Do nothing. $e$ is already not in the MST of G, and now it's a worse option.
12+
13+
## 5.22b
14+
- Add $e$ to $E'$
15+
- Find the cycle in $T$ containing $e$
16+
- Remove max edge in $C$.
17+
18+
## 5.22c
19+
Do nothing. $e$ is already in the MST of G, and now $e$ is/was an even better choice across the cut of $S$ and $\overline{S}$, where $e=(u,v)$ and $u \in S$ and $v \in \overline{S}$.
20+
21+
## 5.23d
22+
$e=(u,v)$ defines a $cut(S,\overline{S})$ where $u \in S$ and $v \in \overline{S}$. We need to find $\hat{e}=\min\{ cut(S, \overline{S}) \}$. Add $\hat{e}$ to $E'$. Remove $e$ from $E$.
23+
24+
How do we find $\hat{e}=\min\{ cut(S, \overline{S}) \}$ in linear time? If we remove $e$ from $E'$, we can run DFS on $T$, to produce a `ccnum[]`, which identifies $S$ and $\overline{S}$. We can then iterate over $\hat{e}=(u,v) \in E$, filtering out any $\hat{e}$ where $ccnum[u]=ccnum[v]$. Those options for $\hat{e}$ do not cross $cut(S, \overline{S})$, and therefore create a cycle within $S$ or $\overline{S}$, and do not complete the MST. For the options for $\hat{e}$ that remain, we take the one with the lowest edge weight.

OMSCS/Courses/GA/Practice Problems/5.23 - Light Spanning Trees (TODO).md

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 5.23 - Light Spanning Trees
8+
> Not-Strictly-Minimum Spanning Trees
9+
10+
![[Pasted image 20260316211401.png]]
11+
12+
## Algorithm
13+
1. We make a copy of $G$, $G'=(V',E')$, removing all $v \in U$ from $V'$.
14+
2. We check for connectivity by running DFS on $G'$. If $G'$ is not connected, then we can't make a tree where all of the vertices in $U$ are leaves of a light spanning tree of $G$.
15+
3. We run Kruskal's on $G'$ to produce an MST of $G'$: $T=(V',E'')$
16+
4. We then iterate over all $u \in U$, and find the minimum edge $e_u$ between $u$ and a vertex in $V'$. This edge represents the $\min\{cut(V', \{u\}\}$. We add $e_u$ to a new set $E_u$.
17+
5. We add all edges in $E_u$ to $E''$
18+
19+
## Justification
20+
We want the minimum spanning tree where all vertices in some subset of $V$ are constrained to being leaf vertices. If we make an MST of G where the subset U of V is not included, then we've minimized the tree that spans $V-U$. We can then take those vertices from $U$ and add them to the tree. If each $u \in U$ only connects to vertices in $V-U$, that ensures each $u$ is a leaf of the resulting tree.
21+
22+
## Runtime
23+
- Copy $G$ to $G'$: $O(n+m)$
24+
- Removing all $u \in U$ from $G'$: $O(n)$
25+
- Running DFS on $G'$: $O(n+m)$
26+
- Kruskal's on $G'$, producing $T$: $O(m \space log \space n)$
27+
- Checking connectivity: $O(n)$
28+
- Building $E_u$: $O(n+m)$
29+
- Adding $E_u$ to $T$: $O(n+m)$
30+

OMSCS/Courses/GA/Practice Problems/Suggested MST and Max Flow Problems.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ tags:
1515
- **5.7** – Show how to find the _maximum_ spanning tree of a graph...
1616
- [[5.7 - Maximum Spanning Tree]]
1717
- **5.20** – Give a linear-time algorithm... (_feedback edge set_ not included here)
18-
- [[5.20 - Perfect Matching Tree (TODO)]]
18+
- [[5.20 - Perfect Matching Tree]]
1919
- **5.22** – In this problem, we will develop a new algorithm for finding minimum spanning trees.
20-
- [[5.22 - Fast MST Recomputation (TODO)]]
20+
- [[5.22 - Fast MST Recomputation]]
2121
- **5.23** – You are given a graph $G=(V,E)$ with positive edge weights, and a minimum spanning tree $T=(V,E')$...
22-
- [[5.23 - Light Spanning Trees (TODO)]]
22+
- [[5.23 - Light Spanning Trees]]
2323
- **5.24** – Sometimes we want light spanning trees with certain special properties.
2424
- [[5.24 - Amortized Binary Counter (TODO)]]
2525

0 commit comments

Comments
 (0)