Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Traversals/GraphBFSAdjList.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Breadth First Search Using Adjacent List
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Noob-Master-22 You're adding Markdown in a .java file. It has to be in a .md file. I guess you're doing this wrong. This will break the code.

You need to create another markdown file. And add the code over there. And then add those comments.


## BFS Algorithm
The general process of exploring a graph using breadth-first search using adjacency list includes the following steps:-

-Take the input of adjacency list for the graph.
-Initialize a queue.
-Enqueue the root node (in other words, put the root node into the beginning of the queue.
-Dequeue the head (or first element) of the queue, then enqueue all of its neighboring nodes, starting from left to right. If a node has no neighboring nodes which need to be explored, simply dequeue the head and continue the process. (Note: If a neighbor which is already explored or in the queue appears, don’t enqueue it – simply skip it.)
-Keep repeating this process till the queue is empty.



import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
Expand Down Expand Up @@ -59,4 +72,11 @@ public static void main(String[] args) {
System.out.println("BFS:");
g.bfs(2);
}
}
}

## Time and Space Complexities
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again you've done the same changes. You either have to add these as a comment or add a markdown file with the same things. Better to create a file GraphBFSAdjList.md and add the code over there and add your comments.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got that !!!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Noob-Master-22 Where are the new changes?


The running time complexity of the BFS in Java is O(V+E) where V is the number of nodes in the graph, and E is the number of edges.

Since the algorithm requires a queue for storing the nodes that need to be traversed at any point in time, the space complexity is O(V).