You have not implemented a BFS search as you do not iteratively add vertices to the queue as they are reached - you only ever add the vertices and their immediately adjacent neighbours with each pass of the outer loop and that is not a BFS.
In Java:
HashMap.foreach has a time complexity of O(buckets + n) - for a fixed number of hash buckets, this is effectively O(n).
Collection.add has a time complexity of O(1).
Collection.addAll has a time complexity of O(n).
LinkedList.poll has a time complexity of O(1).
Collection.isEmpty has a time complexity of O(1).
HashSet.contains has a worst-case time complexity of O(log(n)) (or O(n) if you are using Java 7 or below). Assuming that your objects hash with an even distribution and the number of items is not orders of magnitude greater than the number of buckets used by the HashSet then the average time-complexity can be considered to be O(1).
So each line of your code has the time complexity:
this.graph.getNodes().forEach((key, value) -> { // O(|V|)
this.queue.add(key); // O(1)
this.queue.addAll(value); // O(|vertex.adjacent|)
while(!this.queue.isEmpty()) { // O(1)
Vertex vertex = this.queue.poll(); // O(1)
if(this.visited.contains(vertex)) { // O(log(|V|)) worst-case
continue; // O(1)
} //
System.out.print(vertex + " -> "); // O(1)
visited.add(vertex); // O(1)
} //
}); //
In this case, using addAll is not actually a problem because, for each vertex, you are adding each adjacent vertex, which is equivalent of iterating over the edges in a single direction. So aggregated over the entirety of the outer loop you are going to add each edge's adjacent vertex in each direction so this is O(2E), or simply O(E).
Where you do have an issue is that checking if a vertex is visited is O(log(n)) worst-case time complexity and that is done for each vertex each time you want to add it to the queue. Since you check for each adjacent vertex of each edge then over the entire process this is O(E.log(V)) worst-case time-complexity (and O(E) average time-complexity, assuming the previously mentioned preconditions hold).
Your entire algorithm is O(V + E.log(V)) worst-case time-complexity (or O(V + E.V) worst-case time complexity if you are using Java 7 or earlier) and O(V + E) best-case/average time-complexity (again, assuming the previous preconditions are met).
To fix the worst-case time complexity issues:
- You could add a
visited flag to the Vertex object and set the flag on the object or, if your vertices each have a unique sequential numerical index (i.e. they are numbered 0 .. |V|-1), you could have an array of boolean flags on the BreadthFirstSearch object corresponding to each vertex and update the flag in the array. Either way you could update the flags in O(1) time.
- You can stop using a
HashMap to store the adjacency lists and, instead, store the lists as an attribute on each vertex.
Something like:
public class BreadthFirstSearch {
private final Graph graph;
private final Queue<Vertex> queue;
private final boolean[] visited;
public BreadthFirstSearch(Graph graph) {
this.graph = graph;
this.queue = new LinkedList<>();
this.visited = new boolean[graph.nodes.size()];
}
private void enqueue(final Vertex vertex)
{
int index = vertex.index;
if (!this.visited[index])
{
this.queue.add(vertex);
}
}
public void doBFS() {
this.graph.getNodes().forEach((key, value) -> {
this.enqueue(key);
while(!this.queue.isEmpty()) {
final Vertex vertex = this.queue.poll();
System.out.print(vertex + " -> ");
LinkedList<Vertex> neighbours = vertex.getAdjacent();
for (final Vertex adjacent: neighbours)
{
this.enqueue(adjacent);
}
}
});
}
}
This assumes that you can write a vertex.getAdjacent method that has an O(1) worst-case time complexity. With your current implementation using a HashMap to store relationships this is unlikely as HashMap.get has O(log(n) worst-case time complexity. Instead, you may need to look a storing the adjacent edges as an attribute on each Vertex instance rather than in a HashMap on the graph.
this.graph = graphYou are storing two copies of your graph? That is going to be horribly memory inefficient.this.graph = graphwill not create two copies of graph. It will simply create an additional reference which points to the same underlying object. I agree with you on the memory efficiency, but here my primary purpose is to understand how the time complexity of BFS is O(V + E)