Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. A version of depth-first search was investigated in the 19th century by French mathematician Charles Pierre Trémaux as a strategy for solving mazes. For applications of DFS in relation to specific domains, such as searching for solutions in artificial intelligence or web-crawling, the graph to be traversed is often either too large to visit in its entirety or infinite (DFS may suffer from non-termination). In such cases, search is only performed to a limited depth; due to limited resources, such as memory or disk space, one typically does not use data structures to keep track of the set of all previously visited vertices.

This is a recursive implementation of DFS: procedure DFS(G, v) is label v as discovered for all directed edges from v to w that are in G.adjacentEdges(v) do if vertex w is not labeled as discovered then recursively call DFS(G, w)

```
#include <iostream>
using namespace std;
int v = 4;
void DFSUtil_(int graph[4][4], bool visited[], int s)
{
visited[s] = true;
cout << s << " ";
for (int i = 0; i < v; i++)
{
if (graph[s][i] == 1 && visited[i] == false)
{
DFSUtil_(graph, visited, i);
}
}
}
void DFS_(int graph[4][4], int s)
{
bool visited[v];
memset(visited, 0, sizeof(visited));
DFSUtil_(graph, visited, s);
}
int main()
{
int graph[4][4] = {{0, 1, 1, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}};
cout << "DFS: ";
DFS_(graph, 2);
cout << endl;
return 0;
}
```