Breadth first search in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

We have already seen about breadth first search in level order traversal of binary tree.

Graph traversal Algorithms:

Breadth first search is graph traversal algorithm. In this algorithm, lets say we start with node i, then we will visit neighbours of i, then neighbours of neighbours of i and so on.

It is very much similar to which is used in binary tree. We use queue to traverse graph. We put first node in queue . It repeatedly extracts node and put its neighbours in the queue. Only difference with respect to binary tree is that we need to track if node have been visited before or not. It can be easily done with help of boolean variable visited in the node. If node have been already visited then we won’t visit it again.

Algorithm:

Steps for Breadth first search:

  1. Create empty queue and push root node to it.
  2. Do the following when queue is not empty
    • Pop a node from queue and print it.
    • Find neighbours of node with the help of adjacency matrix and check if node is already visited or not.
    • Push neighbours of node into queue if not null

Lets understand with the help of example:
Lets say graph is:

Graph
Breadth first search traversal

Java BFS Example

There are two ways to represent a graph.

  • Using Neighbours list
  • Using Adjacency Matrix

Using Neighbours list

In this, you can have List<Node> as neighbours in Node class as below.

When you run above program, you will get below output:

The BFS traversal of the graph is
40 10 20 30 60 50 70

Using Adjacency Matrix

Adjacency_matrix is used to find the connection between two nodes.

if adjacency_matrix[i][j]==1, then nodes at index i and index j are connected

Below diagram will help you to understand adjacency matrix.

Adjacency Matrix
Create BreadthFirstSearchExample.java

When you run above program, you will get below output:

Please go through Algorithm Interview Programs in java  for more such programs.


import_contacts

You may also like:

Related Posts

  • 25 May

    Remove Double Quotes from String in JavaScript

    Table of ContentsWays to Remove Double Quotes from String in JavaScriptUsing replace()Using replaceAll()Using split() and join() methodUsing for loopConclusion In this post, we will see how to remove double quotes from String in JavaScript. Ways to Remove Double Quotes from String in JavaScript There are multiple ways to do it. Let’s go through them. Using […]

  • 25 May

    Count Decimal Places in Python

    Table of ContentsHow to count decimal places in Python?Using the decimal library to count decimal places in PythonUsing the len() and split() function to count decimal places in PythonUsing the find() function to count decimal places in PythonConclusion How to count decimal places in Python? In Python, we can represent decimal values using the float […]

  • 25 May

    Convert Seconds to Hours Minutes Seconds in Javascript

    Table of ContentsUsing javascript without any external libraryUsing momentJs library In this post, we will see how to convert seconds to Hours Minutes Seconds in JavaScript. There are multiple ways to do it. Let’s go through them. Using javascript without any external library To convert seconds to Hours Minutes Seconds in javascript: Create new date […]

  • 25 May

    Remove First Character from String in JavaScript

    Table of ContentsWays to Remove First Character from string in JavaScriptUsing substring()Using slice()Using substr()Using replace() methodUsing Array’s shift() with split() and join()Conclusion In this post, we will see how to remove first character from string in JavaScript. Since Strings are immutable in JavaScript, you can’t do it in-place. All the methods will return new String […]

  • 25 May

    Get Variable Name as String in JavaScript

    Table of ContentsUsing Object.keys()Using toString() methodUsing hash table In this post, we will see how to get variable name as String in JavaScript. There are multiple ways to get Get variable name as String in JavaScript. Let’s go through them. Using Object.keys() To get variable name as String in JavaScript, use Object.keys() to get the […]

  • 25 May

    Type ‘String’ cannot be used as an index type in Typescript

    In this post, we will see how to fix error Type ‘String’ cannot be used as an index type in Typescript. Type ‘String’ cannot be used as an index type in Typescript occurs when you use String(Starts with capital s) instead of string when trying to index array or object in Typescript. Let’s see with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.