mafulechka
June 17, 2019, 11:45 a.m.

DFS (Depth-first search) algorithm

Travel means visiting all nodes of the graph. Depth Traversal or Depth First Search is a recursive algorithm for finding all the vertices of a graph or tree data structure. In this article, with the help of the examples below, you will learn: DFS algorithm, DFS pseudocode, and DFS algorithm code with implementation in C++, C, Java, and Python programs.


Algorithm DFS

The standard implementation of DFS puts each graph vertex into one of two categories:

  1. Visited.
  2. Not visited.

The purpose of the algorithm is to mark each vertex as visited, avoiding cycles.

The DFS algorithm works like this:

  1. Start by placing any vertex of the graph on top of the stack.
  2. Take the top element of the stack and add it to the visited list.
  3. Create a list of adjacent nodes of this vertex. Add those not in the visited list to the top of the stack.
  4. Continue repeating steps 2 and 3 until the stack is empty.

Example DFS

Let's see how the depth-first search algorithm works with an example. We are using an undirected graph with 5 vertices.

We start at node 0, the DFS algorithm starts by placing it in the visited list and placing all adjacent nodes on the stack.

We then visit the element at the top of the stack, i.e. 1, and move on to adjacent nodes. Since we have already visited 0, we visit 2 instead.

Vertex 2 has an unvisited adjacent node 4, so we add it to the top of the stack and visit it.

After we visit the last element 3, it has no unvisited adjacent nodes. This concludes the first “depth-first traversal” of the graph.

DFS pseudo-code (recursive implementation)

The pseudocode for DFS is shown below. Note that in the init() function, we start the DFS function on each node. This is because a graph can have two different unconnected parts, so to make sure we cover every vertex, we can also run the DFS algorithm on every node.

  1. DFS(G, u)
  2. u.visited = true
  3. for each v G.Adj[u]
  4. if v.visited == false
  5. DFS(G,v)
  6.  
  7. init() {
  8. For each u G
  9. u.visited = false
  10. For each u G
  11. DFS(G, u)
  12. }

DFS code

The code for the DFS algorithm with an example is shown below. The code has been simplified so we can focus on the algorithm and not other details.

DFS в C

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int vertex;
  7. struct node* next;
  8. };
  9.  
  10. struct node* createNode(int v);
  11.  
  12. struct Graph
  13. {
  14. int numVertices;
  15. int* visited;
  16. struct node** adjLists; // we need int** to store a two dimensional array. Similary, we need struct node** to store an array of Linked lists
  17. };
  18.  
  19. struct Graph* createGraph(int);
  20. void addEdge(struct Graph*, int, int);
  21. void printGraph(struct Graph*);
  22. void DFS(struct Graph*, int);
  23.  
  24.  
  25. int main()
  26. {
  27.  
  28. struct Graph* graph = createGraph(4);
  29. addEdge(graph, 0, 1);
  30. addEdge(graph, 0, 2);
  31. addEdge(graph, 1, 2);
  32. addEdge(graph, 2, 3);
  33.  
  34. printGraph(graph);
  35.  
  36. DFS(graph, 2);
  37.  
  38. return 0;
  39. }
  40.  
  41. void DFS(struct Graph* graph, int vertex) {
  42. struct node* adjList = graph->adjLists[vertex];
  43. struct node* temp = adjList;
  44.  
  45. graph->visited[vertex] = 1;
  46. printf("Visited %d \n", vertex);
  47.  
  48. while(temp!=NULL) {
  49. int connectedVertex = temp->vertex;
  50.  
  51. if(graph->visited[connectedVertex] == 0) {
  52. DFS(graph, connectedVertex);
  53. }
  54. temp = temp->next;
  55. }
  56. }
  57.  
  58.  
  59. struct node* createNode(int v)
  60. {
  61. struct node* newNode = malloc(sizeof(struct node));
  62. newNode->vertex = v;
  63. newNode->next = NULL;
  64. return newNode;
  65. }
  66.  
  67. struct Graph* createGraph(int vertices)
  68. {
  69. struct Graph* graph = malloc(sizeof(struct Graph));
  70. graph->numVertices = vertices;
  71.  
  72. graph->adjLists = malloc(vertices * sizeof(struct node*));
  73.  
  74. graph->visited = malloc(vertices * sizeof(int));
  75.  
  76. int i;
  77. for (i = 0; i < vertices; i++) {
  78. graph->adjLists[i] = NULL;
  79. graph->visited[i] = 0;
  80. }
  81. return graph;
  82. }
  83.  
  84. void addEdge(struct Graph* graph, int src, int dest)
  85. {
  86. // Add edge from src to dest
  87. struct node* newNode = createNode(dest);
  88. newNode->next = graph->adjLists[src];
  89. graph->adjLists[src] = newNode;
  90.  
  91. // Add edge from dest to src
  92. newNode = createNode(src);
  93. newNode->next = graph->adjLists[dest];
  94. graph->adjLists[dest] = newNode;
  95. }
  96.  
  97. void printGraph(struct Graph* graph)
  98. {
  99. int v;
  100. for (v = 0; v < graph->numVertices; v++)
  101. {
  102. struct node* temp = graph->adjLists[v];
  103. printf("\n Adjacency list of vertex %d\n ", v);
  104. while (temp)
  105. {
  106. printf("%d -> ", temp->vertex);
  107. temp = temp->next;
  108. }
  109. printf("\n");
  110. }
  111. }

Depth First Search (DFS) в C++

  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. class Graph
  6. {
  7. int numVertices;
  8. list *adjLists;
  9. bool *visited;
  10.  
  11. public:
  12. Graph(int V);
  13. void addEdge(int src, int dest);
  14. void DFS(int vertex);
  15. };
  16.  
  17. Graph::Graph(int vertices)
  18. {
  19. numVertices = vertices;
  20. adjLists = new list[vertices];
  21. visited = new bool[vertices];
  22. }
  23.  
  24. void Graph::addEdge(int src, int dest)
  25. {
  26. adjLists[src].push_front(dest);
  27. }
  28.  
  29. void Graph::DFS(int vertex)
  30. {
  31.  
  32. visited[vertex] = true;
  33. list adjList = adjLists[vertex];
  34.  
  35. cout << vertex << " ";
  36.  
  37. list::iterator i;
  38. for(i = adjList.begin(); i != adjList.end(); ++i)
  39. if(!visited[*i])
  40. DFS(*i);
  41. }
  42.  
  43. int main()
  44. {
  45. Graph g(4);
  46. g.addEdge(0, 1);
  47. g.addEdge(0, 2);
  48. g.addEdge(1, 2);
  49. g.addEdge(2, 3);
  50.  
  51. g.DFS(2);
  52.  
  53. return 0;
  54. }

DFS Java code

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. class Graph
  6. {
  7. private int numVertices;
  8. private LinkedList<Integer> adjLists[];
  9. private boolean visited[];
  10.  
  11. Graph(int vertices)
  12. {
  13. numVertices = vertices;
  14. adjLists = new LinkedList[vertices];
  15. visited = new boolean[vertices];
  16.  
  17. for (int i = 0; i < vertices; i++)
  18. adjLists[i] = new LinkedList<Integer>();
  19. }
  20.  
  21. void addEdge(int src, int dest)
  22. {
  23. adjLists[src].add(dest);
  24. }
  25.  
  26. void DFS(int vertex)
  27. {
  28. visited[vertex] = true;
  29. System.out.print(vertex + " ");
  30.  
  31. Iterator ite = adjLists[vertex].listIterator();
  32. while (ite.hasNext())
  33. {
  34. int adj = ite.next();
  35. if (!visited[adj])
  36. DFS(adj);
  37. }
  38. }
  39.  
  40.  
  41. public static void main(String args[])
  42. {
  43. Graph g = new Graph(4);
  44.  
  45. g.addEdge(0, 1);
  46. g.addEdge(0, 2);
  47. g.addEdge(1, 2);
  48. g.addEdge(2, 3);
  49.  
  50. System.out.println("Following is Depth First Traversal");
  51.  
  52. g.DFS(2);
  53. }
  54. }

DFS in Python

  1. def dfs(graph, start, visited=None):
  2. if visited is None:
  3. visited = set()
  4. visited.add(start)
  5. print(start)
  6. for next in graph[start] - visited:
  7. dfs(graph, next, visited)
  8. return visited
  9.  
  10. graph = {'0': set(['1', '2']),
  11. '1': set(['0', '3', '4']),
  12. '2': set(['0']),
  13. '3': set(['1']),
  14. '4': set(['2', '3'])}
  15.  
  16. dfs(graph, '0')

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html