-
Notifications
You must be signed in to change notification settings - Fork 65
/
BFSGraphs.java
122 lines (107 loc) · 2.67 KB
/
BFSGraphs.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Data-Structures-in-Java
* BFSGraphs.java
*/
package com.deepak.data.structures.Graph;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
/**
* Breadth First Search implemented for Graphs
*
* <p> Algorithm :
* 1. Push the root node in the Queue.
* 2. Loop until the Queue is empty.
* 3. Remove the Node from the Queue.
* 4. If the removed node has unvisited children, mark them as visited and insert the unvisited children in the Queue.
* </p>
*
* @author Deepak
*/
public class BFSGraphs<T> {
/**
* Method to perform Breadth First Search on a Graph
* @param root
* @param value
* @return {@link boolean}
*/
public static <T> boolean performBFS(BFSGraphNode<T> root, T value) {
if (root.getValue() == value) {
System.out.println("GraphNode found. Root = " + root);
return true; /* Value exists at root node */
}
/* Create a queue. Set root as visited. Add the root to queue */
Queue<BFSGraphNode<T>> queue = new ArrayDeque<>();
root.setVisited(true);
queue.add(root);
/* Check if queue is not empty */
while (queue.peek() != null) {
/* Remove the first element from the queue */
BFSGraphNode<T> currentNode = queue.poll();
/* Check for all of the neighbors */
for (BFSGraphNode<T> graphNode : currentNode.getNeighbors()) {
/* If the neighbor is unvisited, mark it as visited and add to the queue */
if (!graphNode.isVisited()) {
graphNode.setVisited(true);
if (graphNode.getValue() == value) {
System.out.println("GraphNode found = " + graphNode);
}
queue.add(graphNode);
return true;
}
}
}
return false;
}
/**
* Static class BFSGraphNode
*
* @author Deepak
*
* @param <E>
*/
public static class BFSGraphNode<E> {
private E value;
private BFSGraphNode<E> next;
private List<BFSGraphNode<E>> neighbors;
private boolean visited;
/**
* Parameterized Constructor
* @param value
*/
public BFSGraphNode(E value) {
this.value = value;
}
/**
* Parameterized Constructor
* @param neighbors
*/
public BFSGraphNode(List<BFSGraphNode<E>> neighbors) {
this.neighbors = neighbors;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public BFSGraphNode<E> getNext() {
return next;
}
public void setNext(BFSGraphNode<E> next) {
this.next = next;
}
public List<BFSGraphNode<E>> getNeighbors() {
return neighbors;
}
public void setNeighbors(List<BFSGraphNode<E>> neighbors) {
this.neighbors = neighbors;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
}
}