You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: OMSCS/Courses/GA/Practice Problems/3.16 - CS Curriculum.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -264,7 +264,7 @@ This requires that we iterate over the $n$ vertices in $TO$. For each vertex $v$
264
264
265
265
This works. In fact, this process effectively finds the highest number of edges that lead backward from a given node (+1).
266
266
267
-
## Algorithm
267
+
## Algorithm 1
268
268
We use the TopoSort algorithm to generate an array of the vertices of $G$ in topological order, calling it $TO$. We also generate the reverse of $G$, $G^R=(V, E^R)$.
269
269
270
270
We initialize an array of length $n$, naming this array $ST$. This array holds the minimum semester during which a given course can be taken.
@@ -284,3 +284,25 @@ This is an adaptation of the "longest path" algorithm. See notes above.
284
284
- Finding the max value in ST: $O(n)$
285
285
286
286
Overall: $O(n+m)$
287
+
288
+
## Algorithm 2
289
+
**This is a minor simplification of Algorithm 1 which does not require $G^R$.**
290
+
291
+
We use the TopoSort algorithm to generate an array of the vertices of $G$ in topological order, calling it $TO$.
292
+
293
+
We initialize an array of length $n$, naming this array $ST$. This array holds the minimum **semester** during which a given course can be **taken**. Each course $v$ starts with $ST[v]=1$.
294
+
295
+
For each course $v$ in V in $TO$, we check the outgoing edges to each course $u$ which explicitly requires $v$ as a prerequisite. We update $TO[u]$ to the max of $TO[u]$ and $TO[v]+1$.
296
+
297
+
Now, we have the semesters that each course was taken packaged within $ST$. To find the minimum number of semesters required to complete this CS program, we take the maximum semester number found in $ST$.
298
+
299
+
## Justification of Correctness
300
+
This is an adaptation of the "longest path" algorithm. See notes above. We're simply propagating forward the information about the longest path through a given DAG $G$.
301
+
302
+
## Runtime
303
+
- Running TopoSort: $O(n+m)$
304
+
- Initializing $ST$: $O(n)$
305
+
- Iterating over $v \in V$ in $TO$, and $(v,u) \in E$, $ST[u] = \max\{ST[u], ST[v]+1\}$ for each of a course's dependents: $O(n+m)$
Copy file name to clipboardExpand all lines: OMSCS/Courses/GA/Practice Problems/3.7 - Bipartite Graphs.md
+14-1Lines changed: 14 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -87,7 +87,7 @@ E -.B-.-> A
87
87
D -.B-.-> B
88
88
```
89
89
90
-
### Algorithm 2 (From TA)
90
+
### Algorithm 3 (From TA)
91
91
We can convert this problem to a 2-SAT problem. See [[04.2 - Graphs - 2-SAT]] for details on that algorithm. To encode this problem as a 2-SAT problem, we need to define the problem in terms of the conjunctive AND of multiple OR clauses, each with 2 literals. How do we do this?
92
92
93
93
In a bipartite graph, all edges must connect one vertex $v \in S_1$ with a vertex in $u \in S_2$. $V = S_1 \cup S_2$ and $S_1 \cap S_2=\emptyset$. Therefore, we need to model vertex $v$ as being in one set or the other set, and vertex $u$ as being in the opposite set. This can be accomplished as an exclusive-or (XOR).
Taking the AND across all of these CNF-XOR conditions produces a 2-SAT problem. The conversion from $G$ to $f$ takes $O(n+m)$ time. The 2-SAT algorithm takes $O(n+m)$ time.
106
106
107
+
### Algorithm 4 (From Classmate)
108
+
This is an adaptation of my original idea of using BFS. We can circumvent the issue with running BFS n-times by making n disjoint subgraphs based on the output of DFS. All of these steps should execute in linear time.
109
+
110
+
1. Run $DFS(G)$ to produce $ccnum[]$, identifying all connected components. O(n+m)
111
+
2. For each unique ccnum value $k$ build subgraph $G_k=(V_k,E_k)$.
112
+
1. add vertex $v$ to $V_k$ if $ccnum[v] = k$
113
+
2. add edge $e=(u,v)$ to $E_k$ if $ccnum[u] = ccnum[v] = k$. O(n+m)
114
+
3. Initialize $color[v] = -1$ for all $v \in V$. O(n)
115
+
4. For each subgraph $G_k$ pick any vertex $s \in V_k$ as a source. Assign $color[s] = 0$. Run $BFS(G_k, s)$, returning $dist_k[]$. The time complexity for this step is O(n+m) total across all subgraphs.
116
+
5. For each vertex v in $V_k$ assign $color[v] = dist_k[v] \mod 2$. O(n)
117
+
6. For each edge $e=(u,v) \in E_k$ check if $color[u] = color[v]$. If yes return “not bipartite.” O(m)
118
+
7. If all subgraphs pass return “bipartite” with $V1 = \{v : color[v] = 0\}$ and $V2 = \{v : color[v] = 1\}$. O(n)
119
+
107
120
## 3.7b - Odd Cycles
108
121
We can't have an odd-length cycle in a bipartite graph, because each edge must connect a vertex $v \in S_1$ with a vertex in $u \in S_2$, where $V = S_1 \cup S_2$ and $S_1 \cap S_2=\emptyset$.
0 commit comments