Depth First Search in java

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

In previous post, we have seen breadth-first search(bfs). In this post, we will see how to implement depth-first search(DFS) in java.

Graph traversal Algorithms

In DFS,  You start with  an un-visited node and start picking an adjacent node, until you have no choice, then you backtrack until you have another choice to pick a node, if not, you select another un-visited node.

DFS can be implemented in two ways.

  • Recursive
  • Iterative

Iterative

Depth-first search can be implemented using iterative approach.
Let see with the help of example:

Depth first search example

We start with node 40. It then visits node 20, node 50, node 70 respectively as they are directly connected. After that, it backtracks to node 20 and visited node 60, node 30 and node 10 respectively.

Recursive

Depth first search can be implemented using recursion too. We do not need to maintain external stack, it will be taken care by recursion.

Java DFS 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.

Here is the complete java program for DFS implementation for iterative as well as recursive method.

When you run above program, you will get below output

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 DepthFirstSearchExample.java

When you run above program, you will get below output

Please go through data structure and algorithm interview programs 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 […]

Comments

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.