Answers for "bfs"

C
0

BFS in 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;
}
Posted by: Guest on December-08-2020
0

bfs

vector <int> v[10] ;   //Vector for maintaining adjacency list explained above
    int level[10]; //To determine the level of each node
    bool vis[10]; //Mark the node if visited 
    void bfs(int s) {
        queue <int> q;
        q.push(s);
        level[ s ] = 0 ;  //Setting the level of the source node as 0
        vis[ s ] = true;
        while(!q.empty())
        {
            int p = q.front();
            q.pop();
            for(int i = 0;i < v[ p ].size() ; i++)
            {
                if(vis[ v[ p ][ i ] ] == false)
                {
            //Setting the level of each node with an increment in the level of parent node
                    level[ v[ p ][ i ] ] = level[ p ]+1;                 
                     q.push(v[ p ][ i ]);
                     vis[ v[ p ][ i ] ] = true;
      }
            }
        }
    }
Posted by: Guest on April-27-2021
0

bfs

1  procedure BFS(G, root) is
 2      let Q be a queue
 3      label root as discovered
 4      Q.enqueue(root)
 5      while Q is not empty do
 6          v := Q.dequeue()
 7          if v is the goal then
 8              return v
 9          for all edges from v to w in G.adjacentEdges(v) do
10              if w is not labeled as discovered then
11                  label w as discovered
12                  Q.enqueue(w)
Posted by: Guest on May-18-2021
0

bfs

function breadthFirstSearch (Start, Goal)
   { 
       enqueue(Queue,Start)
       setVisited(start)
       while notEmpty(Queue)
       {
           Node := dequeue(Queue)
           if Node = Goal
           {
               return Node
           }
           for each Child in Expand(Node)
           {
               if notVisited(Child)
               {              
                   setVisited(Child)
                   enqueue(Queue, Child)
               }
           }
       }
   }
Posted by: Guest on July-24-2020
-2

bfs algorithm

function breadthFirstSearch (Start, Goal)
   { 
       enqueue(Queue,Start)
       setVisited(start)
       while notEmpty(Queue)
       {
           Node := dequeue(Queue)
           if Node = Goal
           {
               return Node
           }
           for each Child in Expand(Node)
           {
               if notVisited(Child)
               {              
                   setVisited(Child)
                   enqueue(Queue, Child)
               }
           }
       }
   }
Posted by: Guest on May-22-2020

Code answers related to "C"

Browse Popular Code Answers by Language