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


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.


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


When you run above program, you will get below output

Please go through data structure and algorithm interview programs for more such programs.

Was this post helpful?


You may also like:

Related Posts

  • 02 December

    Pass Variable From PHP to JavaScript

    Table of ContentsUse echo methodUse json_encode methodUse htmlspecialchars method Use echo method In order to pass variable from PHP to JavaScript, you will need to use the echo statement. This will print out the variable for you. For example, if you have a variable named $name, you would use the following code: [crayon-638b277e287c1945155930/] This will […]

  • 02 December

    Remove Quotes from String in PHP

    Table of ContentsRemove Quotes from String in PhpUsing str_replace() MethodUsing preg_replace() MethodRemove Quotes from Beginning and End of String in PhpUsing trim() MethodUsing preg_Replace() Method Remove Quotes from String in Php Using str_replace() Method Use the str_replace() function to remove quotes from String in PHP. [crayon-638b277e28940900556770/] [crayon-638b277e28943885875070/] We used str_replace() to remove double quotes and […]

  • 30 November

    Check if Array is Empty in PowerShell

    Table of ContentsWhat is an Empty Array?Use the Count Property to Check If an Array is Empty in PowerShellUse the Length Property to Check If an Array is Empty in PowerShell 💡TL;DR To check if an array is empty access its count property e.g. arr.count. If count is equal to 0, then array is empty. […]

  • 30 November

    Split Integer into Digits in Python

    Table of ContentsUsing List ComprehensionUsing for LoopUsing map() with list() & str.split() Using List Comprehension To split integer into digits in Python: Use the str() to transform the specified integer to a string. Use a list comprehension to loop over converted String. Use int() to convert every substring to an integer in each iteration. [crayon-638b277e28b94725793750/] […]

  • 29 November

    Get Length of String in PowerShell

    Table of ContentsUsing String’s Length propertyUsing Measure-Object with Character parameter Using String’s Length property Use string’s length property to get length of String in PowerShell e.g. "Hello World".Length. System.String‘s Length property returns number of Characters present in the String. [crayon-638b277e28d69491058339/] Output [crayon-638b277e28d6e795702433/] Here, we declared a string variable blog_Name which contains string Java2blog. We have […]

  • 29 November

    Prefix b Before String in Python

    Table of ContentsPrefix b Before String in PythonConclusion Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and […]


Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.