diff --git a/Graph/DFS_DepthFirstSearch/DFS.cpp b/Graph/DFS_DepthFirstSearch/DFS.cpp new file mode 100644 index 0000000..4d66c04 --- /dev/null +++ b/Graph/DFS_DepthFirstSearch/DFS.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +vector> adj; +vector visited; +void dfs(int from) { + cout << endl << "--dfs(" << from << ") is called--" << endl; + cout << " so visited " << from <<" will be 'true'" << endl; + visited[from] = true; + for (size_t i = 0; i < adj[from].size(); ++i) { + int to = adj[from][i]; + cout << " from : "<(adj.size(), false); + for (size_t i = 0; i < adj.size(); ++i) { + if (!visited[i]) { + cout << endl << "-dfsAll(" << i << ") is called-" << endl; + dfs(i); + } + else { + cout << "node " << i << " is already visited" << endl; + } + } +} + +void showAdj(vector> adjacent) { + for (size_t i = 0; i < adjacent.size(); ++i) { + for (size_t j = 0; j < adj[i].size(); ++j) { + if (adjacent[i][j] != NULL) + { + cout << i << " -> " << adjacent[i][j] << endl; + } + } + } +} +int main() { + adj.resize(7); + visited.resize(7); + adj[0].push_back(1); + adj[0].push_back(2); + adj[1].push_back(3); + adj[1].push_back(4); + adj[5].push_back(6); + showAdj(adj); + dfsAll(); + return 0; +} \ No newline at end of file