From e7ac14351a26676ed41245eb70e8c30179c5d088 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Tue, 1 Oct 2019 03:24:04 +0530 Subject: [PATCH] Depth First Search Traversal --- depth_first_search.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 depth_first_search.cpp diff --git a/depth_first_search.cpp b/depth_first_search.cpp new file mode 100644 index 0000000..d65f39a --- /dev/null +++ b/depth_first_search.cpp @@ -0,0 +1,48 @@ +//c++ program for Depth First Search traversal +#include +#include +using namespace std; + +vector adj[100]; + +bool visited[100]; //Initialising the visited vector + +void dfs(int s) { + visited[s] = true; + + cout<> nodes; //Number of nodes + cout << "Enter the number of edges\n" ; + cin >> edges; //Number of edges + for(int i = 0;i < edges;++i) { + cin >> x >> y; //Undirected Graph + adj[x].push_back(y); //Edge from vertex x to vertex y + adj[y].push_back(x); //Edge from vertex y to vertex x + } + + initialize(); //Initialize all nodes as not visited + + cout<<"Nodes are visited in the order\n"; + for(int i = 1;i <= nodes;++i) { + if(visited[i] == false) { + dfs(i); + } +} + + return 0; + }