DFS in all languages

    DFS in Java

    Posted: , Last Updated:

    Java™ is a compiled language used for many purposes, ranging from embedded systems, UI-applications to web servers.

    See Code

    DFS in C

    Posted: , Last Updated:

    C is a compiled language used for many purposes, although it can be primarily found in systems where importance is important. This is because C offers a lot of low-level support for optimization, at the cost of not having some of the convenient abstractions that other languages offer. C is therefore primarily found in situations where available computation power is low such as embedded systems, or situations where required computation power is high, such as simulation or deep learning.

    See Code

    DFS in Javascript

    Posted: , Last Updated:

    JavaScript JavaScript is an interpreted scripting language previously primarily used in web pages (executed in browsers) that has since…

    See Code

    DFS in Python

    Posted: , Last Updated:

    Python Python™ is an interpreted language used for many purposes ranging from embedded programming to web development, with one of the largest use cases being data science.

    See Code

    DFS in R

    Posted: , Last Updated:

    R R is an interpreted language first released in 1993 with a significant increase in popularity in recent years. It is primarily used for data mining and -science as well as statistics, and is a popular language in non-computer science disciplines ranging from Biology to Physics. R is dynamically typed, and has one of the widest variety of libraries for statistics, machine learning, data mining etc.

    See Code

About the algorithm:

Depth-First Search Algorithm

The Depth-First Search (also DFS) algorithm is an algorithm used to find a node in a tree. This means that given a tree data structure, the algorithm will return the first node in this tree that matches the specified condition (i.e. being equal to a value). Nodes are sometimes referred to as vertices (plural of vertex) - here, we’ll call them nodes. The edges have to be unweighted. This algorithm can also work with unweighted graphs if a mechanism to keep track of already visited nodes is added.

Description of the Algorithm

The basic principle of the algorithm is to start with a start node, and then look at the first child of this node. It then looks at the first child of that node (grandchild of the start node) and so on, until a node has no more children (we’ve reached a leaf node). It then goes up one level, and looks at the next child. If there are no more children, it goes up one more level, and so on, until it find more children or reaches the start node. If hasn’t found the goal node after returning from the last child of the start node, the goal node cannot be found, since by then all nodes have been traversed.

Specifically, these are the steps:

  1. For each child of the current node
  2. If it is the target node, return. The node has been found.
  3. Set the current node to this node and go back to 1.
  4. If there are no more child nodes to visit, return to the parent.
  5. If the node has no parent (i.e. it is the root), return. The node has not been found.

Example of the Algorithm

Consider the following tree: Tree for the Depth-First Search algorithm

The steps the algorithm performs on this tree if given node 0 as a starting point, in order, are:

  1. Visiting Node 0
  2. Visiting Node 1
  3. Visiting Node 3
  4. Went through all children of 3, returning to it’s parent.
  5. Visiting Node 4
  6. Went through all children of 4, returning to it’s parent.
  7. Went through all children of 1, returning to it’s parent.
  8. Visiting Node 2
  9. Visiting Node 5
  10. Went through all children of 5, returning to it’s parent.
  11. Visiting Node 6
  12. Found the node we’re looking for!

Runtime of the Algorithm

The runtime of regular Depth-First Search (DFS) is O(|N|) (|N| = number of Nodes in the tree), since every node is traversed at most once. The number of nodes is equal to b^d, where b is the branching factor and d is the depth, so the runtime can be rewritten as O(b^d).

Space of the Algorithm

The space complexity of Depth-First Search (DFS) is, if we exclude the tree itself, O(d), with d being the depth, which is also the size of the call stack at maximum depth. If we include the tree, the space complexity is the same as the runtime complexity, as each node needs to be saved.