diff --git a/Anurupa_hactober.cpp b/Anurupa_hactober.cpp new file mode 100644 index 0000000..b74c2fd --- /dev/null +++ b/Anurupa_hactober.cpp @@ -0,0 +1,53 @@ +// DFS algorithm in C++ + +#include +#include +using namespace std; + +class Graph { + int numVertices; + list *adjLists; + bool *visited; + + public: + Graph(int V); + void addEdge(int src, int dest); + void DFS(int vertex); +}; + +// Initialize graph +Graph::Graph(int vertices) { + numVertices = vertices; + adjLists = new list[vertices]; + visited = new bool[vertices]; +} + +// Add edges +void Graph::addEdge(int src, int dest) { + adjLists[src].push_front(dest); +} + +// DFS algorithm +void Graph::DFS(int vertex) { + visited[vertex] = true; + list adjList = adjLists[vertex]; + + cout << vertex << " "; + + list::iterator i; + for (i = adjList.begin(); i != adjList.end(); ++i) + if (!visited[*i]) + DFS(*i); +} + +int main() { + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 3); + + g.DFS(2); + + return 0; +}