title: Pathfinding using DFS and java
body: <p>I'm developing a search algorithm for finding paths in a graph. In this algorithm i need to find all the paths in an undirected, not weighted graph that go trough each graph connection only once. It must start and end in the same node. Currently, what my program is doing, is finding all the paths that go trough each node only once. I need connections and not nodes.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.*;
  
  public class dfs {  
  
      private static Map&lt;Integer, LinkedHashSet&lt;Integer&gt;&gt; map = new HashMap&lt;Integer, LinkedHashSet&lt;Integer&gt;&gt;();
      private int startNode;
      private int numLinks;
  
      public dfs(int startNode, int numLinks) {
          super();
          this.startNode = startNode;
          this.numLinks = numLinks;
      }
  
      public int getNumLinks(){
          return numLinks;    
      }
  
      public void addEdge(int source, int destiny) {
          LinkedHashSet&lt;Integer&gt; adjacente = map.get(source);
          if(adjacente==null) {
              adjacente = new LinkedHashSet&lt;Integer&gt;();
              map.put(source, adjacente);
          }
          adjacente.add(destiny);
      }
  
      public void addLink(int source, int destiny) {
          addEdge(source, destiny);
          addEdge(destiny, source);
      }
  
      public LinkedList&lt;Integer&gt; adjacentNodes(int adj) {
          LinkedHashSet&lt;Integer&gt; adjacente = map.get(adj);
          System.out.println("adjacentes:" + adjacente);
          if(adjacente==null) {
              return new LinkedList&lt;Integer&gt;();
          }
          return new LinkedList&lt;Integer&gt;(adjacente);
      }
  
  
  public static void main(String[] args) {
  
      Scanner input = new Scanner(System.in);
  
      int numVertices = input.nextInt();
      int numLinks = input.nextInt();
      int startNode = input.nextInt();
      int endNode = startNode;
  
      dfs mapa = new dfs(startNode, numLinks);
  
      for(int i = 0; i&lt;numLinks; i++){
          int inicio = input.nextInt();
          int fim = input.nextInt();
          mapa.addLink(inicio, fim);
  
      }
  
      List&lt;ArrayList&lt;Integer&gt;&gt; paths = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();
      List&lt;Integer&gt; visited = new ArrayList&lt;Integer&gt;();
      visited.add(startNode);
      Integer currentNode = 0;
  
      Iterator it = map.entrySet().iterator();
      while (it.hasNext()) {
          Map.Entry pairs = (Map.Entry)it.next();
          currentNode = (Integer) pairs.getKey(); 
  
          mapa.findAllPaths(mapa, visited, paths, currentNode);
  
      }
  }
  
  private void findAllPaths(dfs mapa, List&lt;Integer&gt; visited,
          List&lt;ArrayList&lt;Integer&gt;&gt; paths, Integer currentNode) {
  
      if (currentNode.equals(startNode)) { 
          paths.add(new ArrayList&lt;Integer&gt;(visited));
  
          LinkedList&lt;Integer&gt; nodes = mapa.adjacentNodes(currentNode); 
  
          for (Integer node : nodes) {
  
              List&lt;Integer&gt; temp = new ArrayList&lt;Integer&gt;();
              temp.addAll(visited);
              temp.add(node);  
              System.out.println("temp:" + temp);
  
              findAllPaths(mapa, temp, paths, node);
          }   
      }
      else {
          LinkedList&lt;Integer&gt; nodes = mapa.adjacentNodes(currentNode);  
          System.out.println("currentNode:" + currentNode);
  
          List&lt;Integer&gt; inseridos = new ArrayList&lt;Integer&gt;();
  
          for (Integer node : nodes) {   
              if (visited.contains(node)) {
                  continue;
              } 
              List&lt;Integer&gt; temp = new ArrayList&lt;Integer&gt;();
              inseridos.add(currentNode);
              temp.addAll(visited);
              System.out.println("visited:" + visited);
  
              temp.add(node);
  
              findAllPaths(mapa, temp, paths, node);
          }
      }
  } 
  }
  </code></pre>
  
  <p>The program receives integers on his input. The first one is the number of nodes, the second one is the number of links and the third is the start node and end note, which are the same. All the integers that come after represent the connections between nodes.</p>
  
  <p>As an example: the program receives "1 2 3 3 4 5 6" 1 is the number of nodes, 2 is the number of links, 3 is the starting node, 3 4 is a connection from node 3 to node 4 and 5 6 is a connection from node 5 to 6.</p>
  
  <p>Right now, i think the following code:</p>
  
  <pre><code>if (visited.contains(node)) {
   continue;
  }
  </code></pre>
  
  <p>is making that the program not going through each node more than once. I need help transforming my program to go trough each connection only once, and not through each node once. </p>
  
  <p>Any ideas on how i can do this?</p>