From 4ddb115ad922676c563a35cd63285d932410cd13 Mon Sep 17 00:00:00 2001 From: Anurupa-05 <115679435+Anurupa-05@users.noreply.github.com> Date: Wed, 26 Oct 2022 13:40:41 +0530 Subject: [PATCH 1/2] Create Anurupa_hactober DFS algo --- Anurupa_hactober | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Anurupa_hactober diff --git a/Anurupa_hactober b/Anurupa_hactober new file mode 100644 index 0000000..b74c2fd --- /dev/null +++ b/Anurupa_hactober @@ -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; +} From b0850255de4a4a7908a7f6f727c6fa74c2e105a7 Mon Sep 17 00:00:00 2001 From: Anurupa-05 <115679435+Anurupa-05@users.noreply.github.com> Date: Wed, 26 Oct 2022 16:53:30 +0530 Subject: [PATCH 2/2] Rename Anurupa_hactober to Anurupa_hactober.cpp DFS algo.cpp --- Anurupa_hactober => Anurupa_hactober.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Anurupa_hactober => Anurupa_hactober.cpp (100%) diff --git a/Anurupa_hactober b/Anurupa_hactober.cpp similarity index 100% rename from Anurupa_hactober rename to Anurupa_hactober.cpp