11package com .thealgorithms .datastructures .graphs ;
22
33import java .util .ArrayList ;
4- import java .util .Scanner ;
4+ import java .util .List ;
55
66class Cycle {
77
88 private final int nodes ;
9- private int [][] adjacencyMatrix ;
10- private boolean [] visited ;
11- ArrayList <ArrayList <Integer >> cycles = new ArrayList <ArrayList <Integer >>();
12-
13- Cycle () {
14- Scanner in = new Scanner (System .in );
15- System .out .print ("Enter the no. of nodes: " );
16- nodes = in .nextInt ();
17- System .out .print ("Enter the no. of Edges: " );
18- final int edges = in .nextInt ();
19-
20- adjacencyMatrix = new int [nodes ][nodes ];
21- visited = new boolean [nodes ];
9+ private final int [][] adjacencyMatrix ;
10+ private final boolean [] visited ;
11+ private final List <List <Integer >> cycles = new ArrayList <>();
2212
13+ Cycle (int nodes , int [][] adjacencyMatrix ) {
14+ this .nodes = nodes ;
15+ this .adjacencyMatrix = new int [nodes ][nodes ];
16+ // Deep copy matrix to avoid side-effects
2317 for (int i = 0 ; i < nodes ; i ++) {
24- visited [i ] = false ;
25- }
26-
27- System .out .println ("Enter the details of each edges <Start Node> <End Node>" );
28-
29- for (int i = 0 ; i < edges ; i ++) {
30- int start ;
31- int end ;
32- start = in .nextInt ();
33- end = in .nextInt ();
34- adjacencyMatrix [start ][end ] = 1 ;
18+ System .arraycopy (adjacencyMatrix [i ], 0 , this .adjacencyMatrix [i ], 0 , nodes );
3519 }
36- in . close () ;
20+ this . visited = new boolean [ nodes ] ;
3721 }
3822
3923 public void start () {
4024 for (int i = 0 ; i < nodes ; i ++) {
41- ArrayList <Integer > temp = new ArrayList <>();
42- dfs (i , i , temp );
25+ dfs (i , i , new ArrayList <>());
4326 for (int j = 0 ; j < nodes ; j ++) {
44- adjacencyMatrix [i ][j ] = 0 ;
45- adjacencyMatrix [j ][i ] = 0 ;
27+ this . adjacencyMatrix [i ][j ] = 0 ;
28+ this . adjacencyMatrix [j ][i ] = 0 ;
4629 }
4730 }
4831 }
4932
50- private void dfs (Integer start , Integer curr , ArrayList <Integer > temp ) {
33+ private void dfs (int start , int curr , List <Integer > temp ) {
5134 temp .add (curr );
5235 visited [curr ] = true ;
5336 for (int i = 0 ; i < nodes ; i ++) {
5437 if (adjacencyMatrix [curr ][i ] == 1 ) {
5538 if (i == start ) {
56- cycles .add (new ArrayList <Integer >(temp ));
39+ cycles .add (new ArrayList <>(temp ));
5740 } else {
5841 if (!visited [i ]) {
5942 dfs (start , i , temp );
@@ -62,18 +45,24 @@ private void dfs(Integer start, Integer curr, ArrayList<Integer> temp) {
6245 }
6346 }
6447
65- if (temp .size () > 0 ) {
48+ if (! temp .isEmpty () ) {
6649 temp .remove (temp .size () - 1 );
6750 }
6851 visited [curr ] = false ;
6952 }
7053
54+ public List <List <Integer >> getCycles () {
55+ return cycles ;
56+ }
57+
7158 public void printAll () {
72- for (int i = 0 ; i < cycles .size (); i ++) {
73- for (int j = 0 ; j < cycles .get (i ).size (); j ++) {
74- System .out .print (cycles .get (i ).get (j ) + " -> " );
59+ for (List <Integer > cycle : cycles ) {
60+ for (Integer node : cycle ) {
61+ System .out .print (node + " -> " );
62+ }
63+ if (!cycle .isEmpty ()) {
64+ System .out .println (cycle .get (0 ));
7565 }
76- System .out .println (cycles .get (i ).get (0 ));
7766 System .out .println ();
7867 }
7968 }
@@ -84,7 +73,15 @@ private Cycles() {
8473 }
8574
8675 public static void main (String [] args ) {
87- Cycle c = new Cycle ();
76+ // Example usage with a triangle graph: 0 -> 1 -> 2 -> 0
77+ int nodes = 3 ;
78+ int [][] matrix = {
79+ { 0 , 1 , 1 },
80+ { 1 , 0 , 1 },
81+ { 1 , 1 , 0 }
82+ };
83+
84+ Cycle c = new Cycle (nodes , matrix );
8885 c .start ();
8986 c .printAll ();
9087 }
0 commit comments