Skip to content

Commit 18d0e7a

Browse files
committed
fix lvert rvert
1 parent 62ab42a commit 18d0e7a

1 file changed

Lines changed: 3 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ int BFS(source, G):
101101
---
102102

103103
Given an unweighted and undirected graph $G = (V, E)$ and an edge $eE$. Construct an algorithm to determine whether the graph $G$ has a cycle containing that specific given edge $e$. Also, determine the time complexity of your algorithm and justify your answer.
104-
**Note**: To become eligible for full credits on this problem, the running time of your algorithm should be bounded by $O(|V| + |E|)$.
104+
**Note**: To become eligible for full credits on this problem, the running time of your algorithm should be bounded by $O(\lvert V \rvert + \lvert E \rvert)$.
105105

106106
Solution:
107107

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|)$.
109-
110-
Pseudocode:
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(\lvert V \rvert + \lvert E \rvert)$. Pseudocode:
111109

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

137135
Solution:
138136

139-
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|))$.
140-
141-
Pseudocode:
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(\lvert V \rvert *(\lvert V \rvert + \lvert E \rvert))$. Pseudocode:
142138

143139
```csharp
144140
bool isSinglyConnected(G):

0 commit comments

Comments
 (0)