@@ -8,6 +8,8 @@ public class GraphList {
88 private int countEdges ;
99 private ArrayList <ArrayList <Edge >> adjList ;
1010
11+ private static final int INF = 999 ;
12+
1113 public int getCountNodes () {
1214 return countNodes ;
1315 }
@@ -170,6 +172,145 @@ public boolean subgraph(GraphList g2) {
170172 return true ;
171173 }
172174
175+ public ArrayList <Integer > bfs (int s ) {
176+ int [] desc = new int [this .countNodes ];
177+ ArrayList <Integer > Q = new ArrayList <>();
178+ ArrayList <Integer > R = new ArrayList <>();
179+ Q .add (s );
180+ R .add (s );
181+ desc [s ] = 1 ;
182+
183+ while (Q .size () > 0 ) {
184+ int u = Q .remove (0 );
185+ for (int v = 0 ; v < this .adjList .get (u ).size (); v ++) {
186+ if (desc [this .adjList .get (u ).get (v ).getSink ()] == 0 ) {
187+ Q .add (this .adjList .get (u ).get (v ).getSink ());
188+ R .add (this .adjList .get (u ).get (v ).getSink ());
189+ desc [this .adjList .get (u ).get (v ).getSink ()] = 1 ;
190+ }
191+ }
192+ }
193+ return R ;
194+ }
195+
196+ public ArrayList <Integer > dfs (int s ) {
197+ int [] desc = new int [this .countNodes ];
198+ ArrayList <Integer > S = new ArrayList <>();
199+ ArrayList <Integer > R = new ArrayList <>();
200+ S .add (s );
201+ R .add (s );
202+ desc [s ] = 1 ;
203+
204+ while (S .size () > 0 ) {
205+ boolean unstack = true ;
206+ int u = S .get (S .size () - 1 );
207+ for (int v = 0 ; v < this .adjList .get (u ).size (); v ++) {
208+ if (desc [this .adjList .get (u ).get (v ).getSink ()] == 0 ) {
209+ S .add (this .adjList .get (u ).get (v ).getSink ());
210+ R .add (this .adjList .get (u ).get (v ).getSink ());
211+ desc [this .adjList .get (u ).get (v ).getSink ()] = 1 ;
212+ unstack = false ;
213+ break ;
214+ }
215+ }
216+ if (unstack )
217+ S .remove (S .size () - 1 );
218+ }
219+ return R ;
220+ }
221+
222+ public ArrayList <Integer > dfsRec (int s ) {
223+ int [] desc = new int [this .countNodes ];
224+ ArrayList <Integer > R = new ArrayList <>();
225+ dfsRecAux (s , desc , R );
226+ return R ;
227+ }
228+
229+ public void dfsRecAux (int u , int [] desc , ArrayList <Integer > R ) {
230+ desc [u ] = 1 ;
231+ R .add (u );
232+ for (int idx = 0 ; idx < this .adjList .get (u ).size (); ++idx ) {
233+ int v = this .adjList .get (u ).get (idx ).getSink ();
234+ if (desc [v ] == 0 ) {
235+ dfsRecAux (v , desc , R );
236+ }
237+ }
238+ }
239+
240+ public boolean isConnected () {
241+ return (this .bfs (0 ).size () == this .countNodes );
242+ }
243+
244+ // public ArrayList<Edge> kruskal() {
245+ // ArrayList<Edge> T = new ArrayList<Edge>(this.countNodes - 1);
246+ // int[] F = new int[this.countNodes];
247+ // // makeset(u)
248+ // for (int u = 0; u < this.countNodes; ++u)
249+ // F[u] = u;
250+ // edgeList.sort(null);
251+ // for (int idx = 0; idx < edgeList.size(); ++idx) {
252+ // int u = edgeList.get(idx).getSource();
253+ // int v = edgeList.get(idx).getSink();
254+ // if (F[u] != F[v]) { // findset(u) != findset(v)
255+ // T.add(edgeList.get(idx));
256+ // // Save some iterations if tree is already built
257+ // if (T.size() == countNodes - 1)
258+ // break;
259+ // // union(u, v)
260+ // int k = F[v];
261+ // for (int i = 0; i < F.length; ++i) {
262+ // if (F[i] == k) {
263+ // F[i] = F[u];
264+ // }
265+ // }
266+ // }
267+ // }
268+ // return T;
269+ // }
270+
271+ public ArrayList <Edge > prim () {
272+ ArrayList <Edge > T = new ArrayList <Edge >(this .countNodes - 1 );
273+ int s = 0 ;
274+ int [] dist = new int [this .countNodes ];
275+ int [] parent = new int [this .countNodes ];
276+ // Q represents the nodes that were not connected yet
277+ ArrayList <Integer > Q = new ArrayList <Integer >(this .countNodes );
278+ for (int u = 0 ; u < this .countNodes ; ++u ) {
279+ dist [u ] = INF ;
280+ parent [u ] = -1 ;
281+ Q .add (u );
282+ }
283+ dist [s ] = 0 ;
284+ while (Q .size () != 0 ) {
285+ int u = -1 ;
286+ int min = INF ;
287+ for (int idx = 0 ; idx < Q .size (); ++idx ) {
288+ int i = Q .get (idx );
289+ if (dist [i ] < min ) {
290+ min = dist [i ];
291+ u = i ;
292+ }
293+ }
294+ // Node u is gonna be connected
295+ Q .remove ((Integer ) u );
296+ for (int idx = 0 ; idx < this .adjList .get (u ).size (); ++idx ) {
297+ int v = this .adjList .get (u ).get (idx ).getSink ();
298+ int w = this .adjList .get (u ).get (idx ).getWeight ();
299+ if (Q .contains (v ) && w < dist [v ]) {
300+ dist [v ] = w ;
301+ parent [v ] = u ;
302+ }
303+ }
304+ }
305+ // Recover the tree from parent array
306+ for (int u = 0 ; u < parent .length ; ++u ) {
307+ if (parent [u ] != -1 ) {
308+ T .add (new Edge (u , parent [u ], 1 ));
309+ }
310+ }
311+ return T ;
312+ }
313+
173314 @ Override
174315 public String toString () {
175316 String str = "" ;
0 commit comments