graph using queue c++

#include<iostream>
#include <list>
 
using namespace std;
 


class Graph
{
    int V;   
 
  
    list<int> *adj;   
public:
    Graph(int V);  
 
    
    void addEdge(int v, int w); 
 
    
    void BFS(int s);  
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); 
}
 
void Graph::BFS(int s)
{
  
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;
 
   
    list<int> queue;
 
   
    visited[s] = true;
    queue.push_back(s);
 
   
    list<int>::iterator i;
 
    while(!queue.empty())
    {
       
        s = queue.front();
        cout << s << " ";
        queue.pop_front();
 
      
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }
}
 

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;
}

3.33
3
Wrschneider 100 points

                                    int v means

3.33 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
bfs tree in c++ bfs and dfs cpp c++ bfs algorithm bfs in graph in c++ BFS of graph program in cpp BFS progarm in cpp bfs binary tree c++ c++ program for BFS code bfs on frid cpp code for finding bfs in c++ bfsutil cpp bfs on graph c++ bfs class c++ implementation of bfs in graph in c++ BFS in cp bfs on tree c++ bfs algorithm program c++ bfs tree cpp bfs algorithm in cpp bfs algorithm cpp bfs program in c plus plus code implementation of BFS algorithm in c++ BFS and DFS in c++ code for bfs in c++ bfs c++ tree bfs code c++ bfs data structure code in c++ BFS in graph c++ graph bfs c++ program in cpp to bfs BFS search C++ bfs in tree in c++ program to implement BFS algorithm in C++ c++ bfs code bfs graph c++ bfs code for c++ bfs tree c++ bfs in tree c++ bfs c++ display bfs c++ github bfs c++ using class bfs algorithm c++ BFS CP algorithms bfs code in c++ bfs in c++ using array bfs in c++ code bfs and dfs program in cpp c++ graph bfs bfs c++ stl bfc cpp bfs vector c++ bfs code c++ stl bfs algorithm in c++ bfs example bfs algorithm in c++ bfs java bfs on graph cpp simple bfs code in c++ how to make algorithm bfs in cpp methode bfs in cpp bfs in graph in cpp bfs program in data structure using c++ bfs using stl in c++ bfs in binary tree c++ breadthfirst search pyhon bfs and dfs c++ a* graph search example java breadth first search graph java bfs cpp bfs c++ code bfs implementation cpp bfs in graph bfs algorithm example g.LSC_BF() c++ BFS implementation in bfs graph code in python bfs in cpp breadth first traversal of a graph bfs implementation in cpp what is breadth first search java bfs and dfs in cpp breadth first search python bfs stl recursive bfs solutions java python breadth first search adjacency list bfs c++ breadth first search and depth first search in graphs bredth first search java graph bfs bfs function c++ breadth first search algorithm bfs queue java c++ code of bfs bfs on graph java bfs in c++ using stl bfs using set c++ bfs traversal in graph bfs implementation using c++ bfs using queue in c++ graph create using list and queue graph creation using breadth first search bfs implementation in java example java bfs algorithm java breadth first graph using queue c++ Program to create a graph and use breath First Search porogram of bfsin cpp bfs in c++ using queue breadth first search in c++ using adjacency list BFS approachable nodes from a given source of a given graph using queue as an intermediate data structure (BFS). java breadth-first traverses bfs code in cpp breadth-first search python (C) Bredth first Search and Depth first bfs java implementation algorithm java bfs Write a program to find approachable nodes from a given source of a given graph using queue as an intermediate data structure (BFS). (Only code) bfs algorithm using geeksforgeeks Program for Breadth First Search (BFS) for graph traversal IN CPP cpp adjacency list bfs algo cpp adjacency list bfs find out the breardh first tree automatically form the graph Breadth-First Search c++ breadth first search java breath first search of a graph python geeksforgeeks bfs dfs breadth first search c++ how to do a breadth first search java BFS in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source