diff --git a/C++/BFS and DFS traversal of a graph using adjacency list.cpp b/C++/BFS and DFS traversal of a graph using adjacency list.cpp index 35fabcc..a14b909 100644 --- a/C++/BFS and DFS traversal of a graph using adjacency list.cpp +++ b/C++/BFS and DFS traversal of a graph using adjacency list.cpp @@ -3,120 +3,121 @@ BFS and DFS traversal of a graph using adjacency list // BFS Traversal using adjacency list // Code -#include +// optimised code + +#include using namespace std; -class Graph -{ - int V; - vector> adj; + +class Graph { + int V; + vector> 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 visited; - visited.resize(V,false); - list 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 visited(V, false); + queue 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 using namespace std; class Graph { + int V; + vector> adj; // Using vector of vectors instead of list + vector visited; // Moved visited to be a member variable public: - map visited; - map > 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::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; +}