Skip to content

Commit

Permalink
DFS added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidhika002 committed Oct 6, 2022
1 parent dcdc4fc commit 9a1b114
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\MinGW\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/MinGW/bin/cpp.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
58 changes: 58 additions & 0 deletions DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bits/stdc++.h>
using namespace std;

class Graph {

void DFSUtil(int v);

public:
map<int, bool> visited;
map<int, list<int> > adj;
void addEdge(int v, int w);

void DFS();
};

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
}

void Graph::DFSUtil(int v)
{
visited[v] = true;
cout << v << " ";

list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i);
}


void Graph::DFS()
{

for (auto i : adj)
if (visited[i.first] == false)
DFSUtil(i.first);
}

int main()
{

Graph g;
g.addEdge(0, 1);
g.addEdge(0, 9);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(9, 3);

cout << "Following is Depth First Traversal \n";


g.DFS();

return 0;
}

0 comments on commit 9a1b114

Please sign in to comment.