Created
September 20, 2016 07:42
-
-
Save slothC0der/c6a6620f0ff23b753f82bcd08c9bdc68 to your computer and use it in GitHub Desktop.
Simple Implementation of Breadth First Search Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package breadfirstserach; | |
public class App { | |
public static void main(String[] args) { | |
BFS bfs = new BFS(); | |
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); | |
/** | |
* Create our adjacenyList | |
*/ | |
node1.addAdjacentNodes(node2); | |
node1.addAdjacentNodes(node3); | |
node1.addAdjacentNodes(node6); | |
node2.addAdjacentNodes(node1); | |
node2.addAdjacentNodes(node3); | |
node2.addAdjacentNodes(node4); | |
node3.addAdjacentNodes(node1); | |
node3.addAdjacentNodes(node2); | |
node3.addAdjacentNodes(node4); | |
node3.addAdjacentNodes(node5); | |
node4.addAdjacentNodes(node2); | |
node4.addAdjacentNodes(node3); | |
node4.addAdjacentNodes(node5); | |
node4.addAdjacentNodes(node6); | |
node5.addAdjacentNodes(node3); | |
node5.addAdjacentNodes(node4); | |
node5.addAdjacentNodes(node6); | |
node6.addAdjacentNodes(node1); | |
node6.addAdjacentNodes(node4); | |
node6.addAdjacentNodes(node5); | |
bfs.bfs(node5); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package breadfirstserach; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
public class BFS { | |
public void bfs (Node root){ | |
// Our FIFO structure | |
// A queue is a LinkedList | |
// no need to reinvent the wheel | |
Queue <Node> queue = new LinkedList(); | |
/* | |
* We need to set our root node as visited to avoid | |
* an infinite loop, then | |
* add the node to our queue | |
*/ | |
root.setVisited(true); | |
queue.add(root); | |
/* | |
* While our queue is not empty | |
* 1. remove current node in the queue | |
* 2. Check if the node has been visited, if not then | |
* 3. process the node by setting it's | |
* bool value to True, then adding its | |
* adjacent nodes to the adjacency list | |
*/ | |
while (!queue.isEmpty()) { | |
Node currentVertex = queue.remove(); | |
System.out.println(currentVertex + " "); | |
for (Node v : currentVertex.getAdjList()) { | |
if (!v.isVisited()) { | |
v.setVisited(true); | |
queue.add(v); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package breadfirstserach; | |
// import basic data structures | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Node { | |
public int data; // random data type that our vertex will hole | |
public boolean visited; // checks if our node has been visited | |
public List <Node> adjList; // list of vertices | |
// constructor | |
// initiate our instances with the data below | |
public Node(int data) { | |
this.data = data; | |
this.adjList = new ArrayList(); | |
} | |
public int getData() { | |
return data; | |
} | |
public void setData(int data) { | |
this.data = data; | |
} | |
public boolean isVisited() { | |
return visited; | |
} | |
public void setVisited(boolean visited) { | |
this.visited = visited; | |
} | |
public List<Node> getAdjList() { | |
return adjList; | |
} | |
public void setAdjList(List <Node> adjList) { | |
this.adjList = adjList; | |
} | |
@Override | |
public String toString() { | |
return ""+ this.data; | |
} | |
// add adjacent nodes to our adjList | |
public void addAdjacentNodes(Node vertex) { | |
this.adjList.add(vertex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment