1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   | public class DFS {     public static void dfs(GraphNode node) {         if (node == null) return;
          Stack<GraphNode> stack = new Stack<>();         HashSet<GraphNode> set = new HashSet<>();         stack.push(node);         set.add(node);         System.out.print(node.value + " ");         while (!stack.isEmpty()) {             GraphNode cur = stack.pop();             for (GraphNode next : cur.nexts) {                 if (!set.contains(next)) {                     stack.push(cur);                     stack.push(next);                     set.add(next);                     System.out.print(next.value + " ");                     break;                 }             }         }     }
      public static void main(String[] args) {         Integer[][] nodes = {                 {1, 1, 2},                 {1, 1, 3},                 {1, 1, 4},                 {1, 2, 3},                 {1, 2, 7},                 {1, 3, 5},                 {1, 4, 6},                 {1, 7, 3}         };         Graph graph = GraphBuilder.createGraph(nodes);         dfs(graph.nodes.get(1));     } }
  |