-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vidhika002
committed
Oct 6, 2022
1 parent
dcdc4fc
commit 9a1b114
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |