VBA – Depth-First-Search Algorithm with VBA

Depth first search algorithm is one of the two famous algorithms in graphs. I am now in “Algorithm Wave” as far as I am watching some videos from SoftUni Algorithm courses.

In the current article I will show how to use VBA in Excel to traverse a graph to find its connected components. This is how the graphs looks:


In the input, I give for each vertex a list of vertices to which it is connected. Like this:

As shown on the picture, g8 e.g. 7 is empty and g9 is related to g3 (8 is related to 2). Yup, not the best way to take input 🙂 but it is ok 🙂

The code produces the magic here:


How the code works? DFS is really well described in Wikipedia, so I would not repeat it! 🙂

Here is the VBA DFS:

Finally, here comes the code in GitHub! Enjoy it! 😀

Tagged with: , , ,