Skip to content

Commit 4d3e73a

Browse files
2 more graph practice problems
1 parent aeac54e commit 4d3e73a

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## 3.22 - Global Source Vertex
2+
> Give an efficient algorithm which takes as input a directed graph $G=(V,E)$, and determines whether or not there is a vertex $s \in V$ from which all other vertices are reachable.
3+
4+
This is eerily similar to HW5. See [[04.4 - Graphs - HW5 Office Hours]]
5+
6+
## Algorithm
7+
- Run SCC to produce the $G_{SCC}=(V_{SCC},E_{SCC})$
8+
- Reverse $G_{SCC}$ to produce $G_{SCC}^R=(V_{SCC}, E_{SCC}^R)$
9+
- We need to find all source metavertices in $G_{SCC}$. For each $v \in V_{SCC}$, check its in-degree in $G_{SCC}$ by checking its out-degree in $G_{SCC}^R$. If it has no outgoing edges in $G_{SCC}^R$, then it has no incoming edges in $G_{SCC}$. Therefore, it's a source vertex in $G_{SCC}$.
10+
- If there are more than one source metavertices in $G_{SCC}$, then there are no vertices in $G$ which can reach all other vertices in $G$.
11+
- If there is only one source metavertex in $G_{SCC}$, then we can return any of the vertices from $V$ which constitute that metavertex in $V_{SCC}$.
12+
13+
## Justification of Correctness
14+
If the metagraph of G has only one source vertex, then that metavertex can reach all of the metavertices in the metagraph of G. Therefore, any vertex within that metavertex can reach all of the vertices in G.
15+
16+
## Runtime
17+
- SCC: O(n+m)
18+
- Reverse G: O(n+m)
19+
- Check out-degree > 0 for each vertex in GR: O(n)
20+
21+
Overall: O(n+m)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
- Alg-Graph
7+
---
8+
# 3.24 - Global Path in DAG
9+
10+
> Give a linear-time algorithm for the following task.
11+
> **Input:** A directed acyclic graph G
12+
> **Question:** Does $G$ contain a directed path that touches every vertex exactly once?
13+
14+
This problem seems similar to [[3.16 - CS Curriculum]].
15+
16+
## Algorithm
17+
Generate the topological order of the DAG. Initialize an array LP which holds the number of vertices along longest path through the DAG for each vertex. Initialize LP to 1 for each vertex.
18+
19+
For each $v \in V$ in topological order, and each edge $e=(v,u) \in E$, set $LP[u]=\max\{LP[u], LP[v]+1\}$.
20+
21+
If the maximum value in $LP$ equals the size of V, then the longest path through the DAG touches every vertex.
22+
23+
## Justification
24+
If there exists a path in the DAG which touches all vertices, the length of the path will be $n$. We can apply the longest path algorithm to the DAG, using the topoorder of the DAG, and forward propagating the longest-path for each vertex. If there's only one sink vertex in the DAG, then there's a chance that the longest path through the DAG for that sink will have a path length equal to the number of vertices in the DAG. If that's the case, then there exists a simple path in G which touches every vertex.
25+
26+
## Runtime
27+
- TopoOrder: O(n+m)
28+
- Propagating LP: O(n+m)
29+
- Max over LP: O(n)
30+
31+
Overall: O(n+m)

OMSCS/Courses/GA/Practice Problems/Suggested Graph Problems.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ tags:
2121
1. Modified BFS (outside the scope of the course)
2222
2. Solution with a propagation step
2323
3. [[3.16 - CS Curriculum]]
24-
- **3.22** – Give an efficient algorithm which takes as input a directed graph $G=(V,E)$...
24+
- **3.22** – Give an efficient algorithm which takes as input a directed graph $G=(V,E)$
25+
- [[3.22 - Global Source Vertex]]
2526
- **3.24** – Give a linear-time algorithm for the following task.
27+
- [[3.24 - Global Path in DAG]]
2628

2729
## Chapter 4 (BFS, Dijkstra's, shortest paths)
2830
- **4.1** – Suppose Dijkstra's algorithm is run...

0 commit comments

Comments
 (0)