DFS in C

Posted: , Last Updated:

package dfs.c.simple;

/**
 * Used to perform the Depth-First Search (DFS) Algorithm to find the shortest path from a start to a target node.
 */
public class DFS {

    /**
     * Implementation of DFS (depth-first search).
     * Given a start node, this returns the node in the tree below the start node with the target value (or null if it doesn't exist)
     * Runs in O(n), where n is the number of nodes in the tree, or O(b^d), where b is the branching factor and d is the depth.
     *
     * @param start  the node to start the search from
     * @param target the value to search for
     * @return The node containing the target value or null if it doesn't exist.
     */
    public static Node dfs(Node start, int target) {
        System.out.println("Visiting Node " + start.value);
        if (start.value == target) {
            // We have found the goal node we we're searching for
            System.out.println("Found the node we're looking for!");
            return start;
        }

        // Recurse with all children
        for (int i = 0; i < start.children.length; i++) {
            Node result = dfs(start.children[i], target);
            if (result != null) {
                // We've found the goal node while going down that child
                return result;
            }
        }

        // We've gone through all children and not found the goal node
        System.out.println("Went through all children of " + start.value + ", returning to it's parent.");
        return null;
    }
}

// used to store a tree datastructure
class Node {
    Node[] children;
    int value;
}

About the algorithm and language used in this code snippet:

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.

C

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.

Getting to “Hello World” in C

The most important things first - here’s how you can run your first line of code in C.

  1. If you’re on Linux or Mac, download and install the latest version of GCC, a C compiler, from gcc.gnu.org. You can also download an earlier version if your use case requires it.
  2. If you’re on Windows, you can also install GCC, even though it might cause problems. You also have other options outlined e.g. here.
  3. Open a terminal, make sure the gcc command is working (or the according command for whichever compiler you’re using), and that the command your’re going to be using is referring to the version you just installed by running gcc --version. If you’re getting a “command not found” error (or similar), try restarting your command line, and, if that doesn’t help, your computer. If the issue persists, here are some helpful forum questions for each platform:

  4. As soon as that’s working, copy the following snippet into a file named HelloWorld.c:

    #include<stdio.h>
    int main() {
    printf("Hello World\n");
    return 0;
    }
  5. Change directory by typing cd path/to/HelloWorld, then run gcc HelloWorld.c to compile the file (which creates the bytecode), then run ./a.out. This should print “Hello World” to your Terminal.

That’s it! People who know multiple programming languages will notice that the entry barrier in C is a little lower than Java even though it is lower-level, while the entry barrier to Python is lower even though it is higher-level. My personal observation is that low-level and high-level languages tend to have low barriers of entry, whereas mid-level languages have higher barriers.

Fundamentals in C

To understand algorithms and technologies implemented in C, one first needs to understand what basic programming concepts look like in this particular language. Each of the following snippets should be compiled and run using the commands mentioned above.

Variables and Arithmetic

Variables in C are statically typed, meaning the content of a variable needs to be specified when writing the code. The datatype for whole numbers, for example is int. Numbers with decimal places are typed float or double depending on the required precision. The type for text ist String.

#include<stdio.h>

int main() {
    int number = 5;
    double decimalNumber = 3.25;
    double result = number * decimalNumber;
    char callout [] = "The number is ";
    // In this instance, the values are concatenated rather than added because one of them is a String.
    printf("%s", callout);
    printf("%f", result);
    printf("\n");
    return 0;
}

Arrays

Arrays in C are real arrays (as opposed to e.g. Python where they’re implemented as lists). The implications of that are that the size needs to be set when they are created and cannot be changed, but also that they are more efficient in C than they are in Python. Also, contrary to Java, C does not check array bounds. If you access an index that doesn’t exist, the program will read whatever is in the memory at that location (which will probably be gibberish).

int integers[5];
integers[3] = 12; // Assigning values to positions in the array
printf("%d\n", integers[0]); // will be 0
printf("%d\n", integers[3]); // will be 12
printf("%d\n", integers[6]); // will print something random that happened to be at that location in memory
return 0;

Conditions

Just like most programming languages, C can do if-else statements. Additionally, C can also do switch-case statements.

int value = 5;
    if(value == 5){
        printf("%s\n", "Value is 5");
    } else if(value < 5){
        printf("%s\n", "Value is less than 5");
    } else {
        printf("%s\n", "Value is something else");
    }
    
    switch (value){
        case 1:
            printf("%s\n", "Value is 1");
            break; // Don't go further down the cases
        case 2:
            printf("%s\n", "Value is 2");
            break; // Don't go further down the cases
        case 3:
            printf("%s\n", "Value is 3");
            break; // Don't go further down the cases
        case 4:
            printf("%s\n", "Value is 4");
            break; // Don't go further down the cases
        case 5:
            printf("%s\n", "Value is 5");
            break; // Don't go further down the cases
        default:
            printf("%s\n", "Value is something else");
    }

The above C code will print “Value is 5” twice.

Loops

C supports for, while as well as do while loops. break and continue statements are also supported. The below example illustrates the differences:

int value = 2;
for (int i = 0; i < value; i++) {
    printf("%d\n", i);
}
while (value > 0) {
    printf("%d\n", value);
    value--;
}
do {
    printf("%d\n", value);
    value--;
} while (value > 0);

This will print the following to the terminal:

0
1
2
1
0

Note the last 0: it is printed because in the do-while-loop, compared to the while-loop. the code block is executed at least once before the condition is checked.

Functions

Functions in C can be declared similar to Java, but require less boilerplate since they don’t need to be part of classes or objects. Here is a minimal example of a function:

#include<stdio.h>

int addNumbers(int numberOne, int numberTwo) {
    return numberOne + numberTwo;
}

int main() {
    printf("%d\n", addNumbers(3, 4));
}

Syntax

C requires the use of curly brackets ({}) to surround code blocks in conditions, loops, functions etc.; It also requires semicolons at then end of statements. While this can lead to some annoying syntax errors, it also means the use of whitespace for preferred formatting (e.g. indentation of code pieces) does not affect the code. Note how the Syntax of C is very similar to Java. The Syntax of Java, and many other languages that came after and/or were derived from C copy many aspects of its Syntax.

Advanced Knowledge of C

C was first released in 1972, is statically typed and was ported to many platforms with various implementations (one of which is GCC which was presented in this article). For more information, C has a great Wikipedia) article.