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: _posts/2024/2024-10-08-algorithm-exercises-1.md
+14-7Lines changed: 14 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,9 @@ Given an unweighted, undirected, and connected graph $G = (V, E)$. Construct an
71
71
72
72
Solution:
73
73
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:
75
77
76
78
```csharp
77
79
intfindDiameter(G):
@@ -105,7 +107,9 @@ Given an unweighted and undirected graph $G = (V, E)$ and an edge $e ∈ E$. Con
105
107
106
108
Solution:
107
109
108
-
Denotenodesform $e$ as $x$ and $y$, i.e. $e=(x,y)$. Wechoose $x$ asthesourceandthenperformBFS**without**traversing $e$. Ifwefindaroutereaching $y$, weprovethereisacycle. ThetimecomplexityisthesameaspureBFS's, boundedby $O(|V|+|E|)$. Pseudocode:
110
+
Denotenodesform $e$ as $x$ and $y$, i.e. $e=(x,y)$. Wechoose $x$ asthesourceandthenperformBFS**without**traversing $e$. Ifwefindaroutereaching $y$, weprovethereisacycle. ThetimecomplexityisthesameaspureBFS's, boundedby $O(|V|+|E|)$.
111
+
112
+
Pseudocode:
109
113
110
114
```csharp
111
115
booldetectCycle(G, e):
@@ -134,7 +138,9 @@ A directed graph $G = (V, E)$ is singly connected if $u⇝v$ implies that $G$ co
0 commit comments