Skip to content

[Optimization ] : BFS and DFS code optiimisation #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 86 additions & 85 deletions C++/BFS and DFS traversal of a graph using adjacency list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,121 @@ BFS and DFS traversal of a graph using adjacency list
// BFS Traversal using adjacency list
// Code

#include<bits/stdc++.h>
// optimised code

#include <bits/stdc++.h>
using namespace std;
class Graph
{
int V;
vector<list<int>> adj;

class Graph {
int V;
vector<vector<int>> adj; // Using vector of vectors instead of list
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};

Graph::Graph(int V)
{
this->V = V;
adj.resize(V);
Graph::Graph(int V) {
this->V = V;
adj.resize(V);
}

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
}

void Graph::BFS(int s)
{
vector<bool> visited;
visited.resize(V,false);
list<int> queue;
visited[s] = true;
queue.push_back(s);

while(!queue.empty())
{
s = queue.front();
cout << s << " ";
queue.pop_front();
for (auto adjecent: adj[s])
{
if (!visited[adjecent])
{
visited[adjecent] = true;
queue.push_back(adjecent);
}
}
}
void Graph::BFS(int s) {
vector<bool> visited(V, false);
queue<int> q; // Using queue instead of list

visited[s] = true;
q.push(s);

while (!q.empty()) {
int v = q.front();
cout << v << " ";
q.pop();

for (int u : adj[v]) {
if (!visited[u]) {
visited[u] = true;
q.push(u);
}
}
}
}

int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.BFS(2);
return 0;
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Breadth First Traversal (starting from vertex 2):\n";
g.BFS(2);
return 0;
}


// DFS Traversal using adjacency list.
// code

// optmised code

#include <bits/stdc++.h>
using namespace std;

class Graph {
int V;
vector<vector<int>> adj; // Using vector of vectors instead of list
vector<bool> visited; // Moved visited to be a member variable
public:
map<int, bool> visited;
map<int, list<int> > adj;

void addEdge(int v, int w);

void DFS(int v);
Graph(int V);
void addEdge(int v, int w);
void DFS(int v);
private:
void DFSUtil(int v);
};

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
Graph::Graph(int V) {
this->V = V;
adj.resize(V);
visited.resize(V, false); // Initialize visited vector in constructor
}

void Graph::DFS(int v)
{
visited[v] = true;
cout << v << " ";

list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
}

int main()
{
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";

g.DFS(2);

return 0;
void Graph::DFS(int v) {
fill(visited.begin(), visited.end(), false); // Ensure visited is reset before every DFS
DFSUtil(v);
}

void Graph::DFSUtil(int v) {
visited[v] = true;
cout << v << " ";

for (int u : adj[v]) {
if (!visited[u]) {
DFSUtil(u);
}
}
}

int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Depth First Traversal (starting from vertex 2):\n";
g.DFS(2);
return 0;
}