55import java .io .FileReader ;
66import java .io .IOException ;
77import java .util .HashMap ;
8+ import java .util .Iterator ;
89import java .util .LinkedList ;
910import java .util .List ;
1011import java .util .Map ;
@@ -80,6 +81,7 @@ public void dfs(int s) {
8081 } else if (color [x ] = color [s ]) {
8182 bipart = false ;
8283 } else {
84+ //not working need dfs(s, origin)
8385 isCycle = true ;
8486 }
8587 });
@@ -100,18 +102,20 @@ public boolean pathExist(int a, int b) {
100102 }
101103
102104 /* get a path between a and b */
103- public List <Integer > getPath (int a , int b ) {
104- if (! pathExist ( a , b )) {
105- return null ;
106- }
107- List < Integer > list = new Stack ();
105+ public Stack <Integer > getPath (int a , int b )
106+ {
107+ if (! pathExist ( a , b )) return null ;
108+ Stack < Integer > path = new Stack <>();
109+
108110 while (a != b ) {
109- list . add (b );
111+ path . push (b );
110112 b = parent [b ];
111113 }
112- return list ;
114+ path .push (a );
115+ return path ;
113116 }
114117
118+
115119 /* check if all element of the graph are connected */
116120 public boolean isConnexe () {
117121 exProfondeur (adj [0 ].get (0 ));
@@ -226,10 +230,22 @@ public static void main(String[] args) {
226230 System .out .println ("yes" );
227231 else System .out .println ("no" );
228232
229- System .out .println ("------- pathExist -------" );
230- if (g .pathExist (1 , 0 ))
233+ System .out .println ("------- pathExist(0, 4) -------" );
234+ if (g .pathExist (0 , 4 ))
231235 System .out .println ("yes" );
232236 else System .out .println ("no" );
237+
238+ System .out .println ("------- pathExist(3, 0) -------" );
239+ if (g .pathExist (3 , 0 ))
240+ System .out .println ("yes" );
241+ else System .out .println ("no" );
242+
243+ System .out .println ("------- path(3, 0) -------" );
244+ Stack <Integer > path1 = g .getPath (3 ,0 );
245+ while (!path1 .isEmpty ()) {
246+ System .out .print (path1 .pop () + " -> " );
247+ }
248+ System .out .println ("" );
233249
234250 System .out .println ("------- isConnexe -------" );
235251 if (g .isConnexe ())
0 commit comments