Skip to content

Commit 8e9b1e9

Browse files
committed
-
1 parent e53b7d4 commit 8e9b1e9

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

_posts/2024/2024-10-08-algorithm-exercises-1.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ Given an unweighted, undirected, and connected graph $G = (V, E)$. Construct an
7171

7272
Solution:
7373

74-
Run BFS from each node as the source. Update the max distance that BFS reached in each run. Return the max distance as the diameter of the graph. The time complexity is $O(|V|*(|V|+|E|))$. Pseudocode:
74+
Run BFS from each node as the source. Update the max distance that BFS reached in each run. Return the max distance as the diameter of the graph. The time complexity is $O(|V|*(|V|+|E|))$.
75+
76+
Pseudocode:
7577

7678
```csharp
7779
int findDiameter(G):
@@ -105,7 +107,9 @@ Given an unweighted and undirected graph $G = (V, E)$ and an edge $e ∈ E$. Con
105107

106108
Solution:
107109

108-
Denote nodes form $e$ as $x$ and $y$, i.e. $e=(x,y)$. We choose $x$ as the source and then perform BFS **without** traversing $e$. If we find a route reaching $y$, we prove there is a cycle. The time complexity is the same as pure BFS's, bounded by $O(|V| + |E|)$. Pseudocode:
110+
Denote nodes form $e$ as $x$ and $y$, i.e. $e=(x,y)$. We choose $x$ as the source and then perform BFS **without** traversing $e$. If we find a route reaching $y$, we prove there is a cycle. The time complexity is the same as pure BFS's, bounded by $O(|V| + |E|)$.
111+
112+
Pseudocode:
109113

110114
```csharp
111115
bool detectCycle(G, e):
@@ -134,7 +138,9 @@ A directed graph $G = (V, E)$ is singly connected if $u⇝v$ implies that $G$ co
134138

135139
Solution:
136140

137-
Run DFS from each node in the graph as the source. Track the visit count of each node during the traversal. If the source node is visited $3$ times (counting the DFS initialization as $1$), or any other node is visited $2$ times, we found $2$ paths from the source to the node. Thus, the graph is determined to not be singly connected. Otherwise, the graph is singly connected if we can't visit the source node for $3$ times or any other node for $2$ times.The time complexity is $O(|V|*(|V| + |E|))$. Pseudocode:
141+
Run DFS from each node in the graph as the source. Track the visit count of each node during the traversal. If the source node is visited $3$ times (counting the DFS initialization as $1$), or any other node is visited $2$ times, we found $2$ paths from the source to the node. Thus, the graph is determined to not be singly connected. Otherwise, the graph is singly connected if we can't visit the source node for $3$ times or any other node for $2$ times.The time complexity is $O(|V|*(|V| + |E|))$.
142+
143+
Pseudocode:
138144

139145
```csharp
140146
bool isSinglyConnected(G):
@@ -163,9 +169,9 @@ bool DFS(source, v, visitCount, G):
163169
According to the Topological Sort for DAG described in Lecture 1, given
164170
the graph below
165171

166-
<p align="center">
172+
<div align="center">
167173
<img src="https://raw.githubusercontent.com/blueskyson/image-host/refs/heads/master/2024/algo2.png" height=300 />
168-
</p>
174+
</div>
169175

170176
(a) Please find one possible topological order of the graph in Figure 1.
171177
(b) In addition, could you find all the possible topological orders?
@@ -174,9 +180,10 @@ Solution:
174180

175181
(a) ABCEDGF
176182
(b) Based on backtracking combined with Kahn's algorithm, 7 orders were found.
177-
<p align="center">
183+
184+
<div align="center">
178185
<img src="https://raw.githubusercontent.com/blueskyson/image-host/refs/heads/master/2024/algo3.jpg" height=300 />
179-
</p>
186+
</div>
180187

181188
---
182189

0 commit comments

Comments
 (0)