How to check if Graph is connected <p>I have a un-directed graph, and wanted to know a node is connected to another node or not?</p> <p>for example </p> <pre><code>0 1 0 1 0 1 0 1 0 </code></pre> <p>In this node 1 is connected to node 3 ( because there is a path from 1 to 2 and 2 to 3 hence 1-3 is connected )</p> <p>I have written programs which is using DFS, but i am unable to figure out why is is giving wrong result.</p> <p><em>I don't want to keep any global variable</em> and want my method to return true id node are connected using <em>recursive</em> program</p> <pre><code>public static boolean isConnectedGraph(int[][] graph, int start, int end, int visited[]) { visited[start] = 1; if (graph[start][end] == 1) { System.out.println("Yes connected...."); return true; } for (int i = 0; i &lt; graph[0].length; i++) { if (graph[start][i] == 1 &amp;&amp; visited[i] == 0) { visited[i] =1; isConnectedGraph(graph, i, end, visited); } } return false; } </code></pre>