Skip to content

Instantly share code, notes, and snippets.

@RitamChakraborty
Created July 29, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RitamChakraborty/27f1600b9f9aab2f678389fbc8145838 to your computer and use it in GitHub Desktop.
Save RitamChakraborty/27f1600b9f9aab2f678389fbc8145838 to your computer and use it in GitHub Desktop.
Breath First Search and Depth First Search implementation of a given graph in Java
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Node {
int data;
List<Node> neighbours;
boolean isVisited;
public Node(int data) {
this.data = data;
}
@Override
public String toString() {
return "{Data: " + data + "}";
}
}
class BFSandDFS {
private static boolean traverse(int searchElement, Stack<Node> stack, boolean isFound) {
if (isFound) {
return true;
} if (stack.peek().data == searchElement) {
isFound = true;
return true;
}
for (Node node: stack.peek().neighbours) {
if (!node.isVisited) {
node.isVisited = true;
stack.push(node);
isFound = traverse(searchElement, stack, isFound);
}
}
System.out.println(stack.pop());
return isFound;
}
private static boolean dfs(Node startingNode, int searchElement) {
if (startingNode.data == searchElement) {
return true;
}
Stack<Node> stack = new Stack<>();
startingNode.isVisited = true;
stack.push(startingNode);
return traverse(searchElement, stack, false);
}
private static boolean bfs(Node startingNode, int searchElement) {
Queue<Node> queue = new ArrayDeque<>();
queue.add(startingNode);
while (!queue.isEmpty()) {
if (queue.element().data == searchElement) {
return true;
}
queue.element().isVisited = true;
for (Node node: queue.element().neighbours) {
if (!node.isVisited) {
queue.add(node);
}
}
System.out.println(queue.poll());
}
return false;
}
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
Node node7 = new Node(7);
Node node8 = new Node(8);
Node node9 = new Node(9);
Node node10 = new Node(10);
node1.neighbours = Stream.of(node2, node4).collect(Collectors.toList());
node2.neighbours = Stream.of(node1, node3, node5, node7, node8).collect(Collectors.toList());
node3.neighbours = Stream.of(node2, node4, node9, node10).collect(Collectors.toList());
node4.neighbours = Stream.of(node1, node3).collect(Collectors.toList());
node5.neighbours = Stream.of(node2, node6, node7, node8).collect(Collectors.toList());
node6.neighbours = Stream.of(node5).collect(Collectors.toList());
node7.neighbours = Stream.of(node2, node5, node8).collect(Collectors.toList());
node8.neighbours = Stream.of(node2, node5, node7).collect(Collectors.toList());
node9.neighbours = Stream.of(node3).collect(Collectors.toList());
node10.neighbours = Stream.of(node3).collect(Collectors.toList());
// Don't run bfs and dfs together
// System.out.println(bfs(node1, 6));
// System.out.println(dfs(node1, 10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment