FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Algorithm/Depth_First_Search.cpp at main · vatsalya1102/Algorithm · GitHub
vatsalya1102
/
Algorithm
Public
forked from
heyimShivam/Algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Algorithm
/
Depth_First_Search.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (44 loc) · 1.17 KB
Breadcrumbs
Algorithm
/
Depth_First_Search.cpp
Copy path
File metadata and controls
53 lines (44 loc) · 1.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
C++ program to print the DFS order traversal of a graph
#
include
<
bits/stdc++.h
>
using
namespace
std
;
//
class GRaph represents the graph using the adjacent list representation
class
Graph
{
public:
map<
int
,
bool
> visited;
map<
int
, list<
int
>> adj;
void
addEdge
(
int
v,
int
n);
void
DFS
(
int
v);
};
//
Method to add edge to the graph
void
Graph::addEdge
(
int
v,
int
n) {
adj[v].
push_back
(n);
//
adding n in lsit v
}
//
Method to traverse the vertices reachale from 'v'
void
Graph::DFS
(
int
v) {
visited[v] =
true
;
//
Marking the node as visited
cout << v << endl;
list<
int
>::iterator i;
//
recurring all the connected vertices
for
(i=adj[v].
begin
();i!=adj[v].
end
();++i) {
if
(!visited[*i])
DFS
(*i);
//
recursive call
}
}
//
Driver code
int
main
() {
//
Creating an example graph, and adding edges.
Graph g;
g.
addEdge
(
0
,
1
);
g.
addEdge
(
0
,
2
);
g.
addEdge
(
0
,
3
);
g.
addEdge
(
1
,
3
);
g.
addEdge
(
2
,
4
);
g.
addEdge
(
3
,
5
);
g.
addEdge
(
3
,
6
);
g.
addEdge
(
4
,
5
);
g.
addEdge
(
4
,
7
);
g.
addEdge
(
5
,
2
);
cout <<
"
DFS Traversla from vertex 0 is given below:
"
<< endl;
g.
DFS
(
0
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL