0% found this document useful (0 votes)
6 views2 pages

Daa 8

The document contains C++ code for a graph implementation that performs a depth-first search (DFS) to find reachable nodes from a given starting node. It includes functionality to add edges, track visited nodes, and measure the execution time of the DFS algorithm. The main function prompts the user for the number of vertices, edges, and the edges themselves, before executing the DFS from the specified starting node.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Daa 8

The document contains C++ code for a graph implementation that performs a depth-first search (DFS) to find reachable nodes from a given starting node. It includes functionality to add edges, track visited nodes, and measure the execution time of the DFS algorithm. The main function prompts the user for the number of vertices, edges, and the edges themselves, before executing the DFS from the specified starting node.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

//Shruti Cherkuthotawar }

#include <iostream> }

#include <vector>

#include <chrono> void reachableNodes(int start) {

using namespace std; vector<bool> visited(V, false);

using namespace std::chrono;

auto start_time =

class Graph { high_resolution_clock::now();

int V; DFSUtil(start, visited);

vector<vector<int>> adj; auto stop_time =

high_resolution_clock::now();

public:

Graph(int V) { auto duration =

this->V = V; duration_cast<microseconds>(stop_time -

adj.resize(V); start_time);

} cout << "\nTime taken by DFS: " <<

duration.count() << " microseconds\n";

void addEdge(int u, int v) { }

adj[u].push_back(v); };

int main() {

void DFSUtil(int v, vector<bool>& visited) { int V, E, u, v, start;

visited[v] = true; cout << "Execution Time Analysis of DFS" << endl;

cout << v << " "; cout << "Enter number of vertices: ";

cin >> V;

for (int u : adj[v]) { Graph g(V);

if (!visited[u]) {

DFSUtil(u, visited); cout << "Enter number of edges: ";

} cin >> E;
cout << "Enter edges (u v):\n";

for (int i = 0; i < E; i++) {

cin >> u >> v;

g.addEdge(u, v);

cout << "Enter starting node: ";

cin >> start;

cout << "Nodes reachable from " << start << ": ";

g.reachableNodes(start);

return 0;

You might also like