@@ -40,7 +40,9 @@ public TarjanStronglyConnectedComponents(int vertices)
4040 public void AddEdge ( int u , int v )
4141 {
4242 if ( u < 0 || u >= graph . Length || v < 0 || v >= graph . Length )
43- throw new ArgumentOutOfRangeException ( ) ;
43+ {
44+ throw new ArgumentOutOfRangeException ( nameof ( u ) , "Vertex indices must be within valid range." ) ;
45+ }
4446
4547 graph [ u ] . Add ( v ) ;
4648 }
@@ -54,7 +56,9 @@ public List<List<int>> FindSCCs()
5456 for ( int i = 0 ; i < graph . Length ; i ++ )
5557 {
5658 if ( ids [ i ] == - 1 )
59+ {
5760 Dfs ( i ) ;
61+ }
5862 }
5963
6064 return sccs ;
@@ -63,19 +67,24 @@ public List<List<int>> FindSCCs()
6367 /// <summary>
6468 /// Gets the number of strongly connected components.
6569 /// </summary>
66- public int GetSCCCount ( ) => sccs . Count ;
70+ public int GetSccCount ( ) => sccs . Count ;
6771
6872 /// <summary>
6973 /// Checks if two vertices are in the same SCC.
7074 /// </summary>
71- public bool InSameSCC ( int u , int v )
75+ public bool InSameScc ( int u , int v )
7276 {
73- if ( sccs . Count == 0 ) FindSCCs ( ) ;
77+ if ( sccs . Count == 0 )
78+ {
79+ FindSCCs ( ) ;
80+ }
7481
7582 foreach ( var scc in sccs )
7683 {
7784 if ( scc . Contains ( u ) && scc . Contains ( v ) )
85+ {
7886 return true ;
87+ }
7988 }
8089
8190 return false ;
@@ -84,9 +93,12 @@ public bool InSameSCC(int u, int v)
8493 /// <summary>
8594 /// Gets the SCC containing the given vertex.
8695 /// </summary>
87- public List < int > ? GetSCC ( int vertex )
96+ public List < int > ? GetScc ( int vertex )
8897 {
89- if ( sccs . Count == 0 ) FindSCCs ( ) ;
98+ if ( sccs . Count == 0 )
99+ {
100+ FindSCCs ( ) ;
101+ }
90102
91103 return sccs . FirstOrDefault ( scc => scc . Contains ( vertex ) ) ;
92104 }
@@ -96,18 +108,25 @@ public bool InSameSCC(int u, int v)
96108 /// </summary>
97109 public List < int > [ ] BuildCondensationGraph ( )
98110 {
99- if ( sccs . Count == 0 ) FindSCCs ( ) ;
111+ if ( sccs . Count == 0 )
112+ {
113+ FindSCCs ( ) ;
114+ }
100115
101116 var sccIndex = new int [ graph . Length ] ;
102117 for ( int i = 0 ; i < sccs . Count ; i ++ )
103118 {
104119 foreach ( var vertex in sccs [ i ] )
120+ {
105121 sccIndex [ vertex ] = i ;
122+ }
106123 }
107124
108125 var condensation = new List < int > [ sccs . Count ] ;
109126 for ( int i = 0 ; i < sccs . Count ; i ++ )
127+ {
110128 condensation [ i ] = new List < int > ( ) ;
129+ }
111130
112131 var edges = new HashSet < ( int , int ) > ( ) ;
113132 for ( int u = 0 ; u < graph . Length ; u ++ )
@@ -136,8 +155,15 @@ private void Dfs(int at)
136155
137156 foreach ( var to in graph [ at ] )
138157 {
139- if ( ids [ to ] == - 1 ) Dfs ( to ) ;
140- if ( onStack [ to ] ) low [ at ] = Math . Min ( low [ at ] , low [ to ] ) ;
158+ if ( ids [ to ] == - 1 )
159+ {
160+ Dfs ( to ) ;
161+ }
162+
163+ if ( onStack [ to ] )
164+ {
165+ low [ at ] = Math . Min ( low [ at ] , low [ to ] ) ;
166+ }
141167 }
142168
143169 if ( ids [ at ] == low [ at ] )
@@ -148,7 +174,10 @@ private void Dfs(int at)
148174 int node = stack . Pop ( ) ;
149175 onStack [ node ] = false ;
150176 scc . Add ( node ) ;
151- if ( node == at ) break ;
177+ if ( node == at )
178+ {
179+ break ;
180+ }
152181 }
153182 sccs . Add ( scc ) ;
154183 }
0 commit comments