-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateAgraph.cpp
More file actions
51 lines (40 loc) · 1.07 KB
/
Copy pathcreateAgraph.cpp
File metadata and controls
51 lines (40 loc) · 1.07 KB
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
#include <iostream>
#include <vector>
using namespace std;
void DFS(int node, vector<int> adj[], vector<bool> &visited)
{
visited[node] = true;
cout << node + 1 << " ";
for (int neighbor : adj[node])
{
if (!visited[neighbor])
{
DFS(neighbor, adj, visited);
}
}
}
int main()
{
int vertices, edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
cout << "Enter the number of edges: ";
cin >> edges;
vector<int> adj[100];
cout << "Enter the edges (u v) where 1 <= u,v <= " << vertices << ":\n";
for (int i = 0; i < edges; i++)
{
int u, v;
cin >> u >> v;
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
int startVertex;
cout << "Enter the starting vertex for DFS: ";
cin >> startVertex;
vector<bool> visited(vertices, false);
cout << "DFS Traversal starting from vertex " << startVertex << ": ";
DFS(startVertex - 1, adj, visited);
cout << "\n";
return 0;
}