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
   | public class BFS {     public static void bfs(GraphNode node) {         if (node == null) return;
          Queue<GraphNode> queue = new LinkedList<>();         HashSet<GraphNode> set = new HashSet<>();         queue.add(node);         set.add(node);         while (!queue.isEmpty()) {             GraphNode curNode = queue.poll();             System.out.print(curNode.value + " ");             for (GraphNode next : curNode.nexts) {                 if (!set.contains(next)) {                     set.add(next);                     queue.add(next);                 }             }         }     }
      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);         bfs(graph.nodes.get(1));     } }
  |