DSA10 W Name
DSA10 W Name
Div-B(48)
Experiment no : 10
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
};
Graph::Graph(int V) { this-
vector<int>[V];
adj[v].push_back(w);
adj[w].push_back(v);
void Graph::DFS(int v) {
vector<bool> visited(V,
false); stack<int> s;
s.push(v); while
(!s.empty()) { v =
s.top();
s.pop(); if
(!visited[v]) {
visited[v] = true;
}
(!visited[*i]) {
s.push(*i);
void Graph::BFS(int v) {
vector<bool> visited(V,
false); queue<int> q;
visited[v] = true;
q.push(v); while
(!q.empty()) { v =
adj[v]) { if
(!visited[i]) {
visited[i] = true;
q.push(i);
g(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.BFS(0);
return 0;
}
OUTPUT:
0 -- 1
||
2 -- 4