bfs traversal code

// BFS algorithm in C

#include <stdio.h>
#include <stdlib.h>
#define SIZE 40

struct queue {
  int items[SIZE];
  int front;
  int rear;
};

struct queue* createQueue();
void enqueue(struct queue* q, int);
int dequeue(struct queue* q);
void display(struct queue* q);
int isEmpty(struct queue* q);
void printQueue(struct queue* q);

struct node {
  int vertex;
  struct node* next;
};

struct node* createNode(int);

struct Graph {
  int numVertices;
  struct node** adjLists;
  int* visited;
};

// BFS algorithm
void bfs(struct Graph* graph, int startVertex) {
  struct queue* q = createQueue();

  graph->visited[startVertex] = 1;
  enqueue(q, startVertex);

  while (!isEmpty(q)) {
    printQueue(q);
    int currentVertex = dequeue(q);
    printf("Visited %d\n", currentVertex);

    struct node* temp = graph->adjLists[currentVertex];

    while (temp) {
      int adjVertex = temp->vertex;

      if (graph->visited[adjVertex] == 0) {
        graph->visited[adjVertex] = 1;
        enqueue(q, adjVertex);
      }
      temp = temp->next;
    }
  }
}

// Creating a node
struct node* createNode(int v) {
  struct node* newNode = malloc(sizeof(struct node));
  newNode->vertex = v;
  newNode->next = NULL;
  return newNode;
}

// Creating a graph
struct Graph* createGraph(int vertices) {
  struct Graph* graph = malloc(sizeof(struct Graph));
  graph->numVertices = vertices;

  graph->adjLists = malloc(vertices * sizeof(struct node*));
  graph->visited = malloc(vertices * sizeof(int));

  int i;
  for (i = 0; i < vertices; i++) {
    graph->adjLists[i] = NULL;
    graph->visited[i] = 0;
  }

  return graph;
}

// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
  // Add edge from src to dest
  struct node* newNode = createNode(dest);
  newNode->next = graph->adjLists[src];
  graph->adjLists[src] = newNode;

  // Add edge from dest to src
  newNode = createNode(src);
  newNode->next = graph->adjLists[dest];
  graph->adjLists[dest] = newNode;
}

// Create a queue
struct queue* createQueue() {
  struct queue* q = malloc(sizeof(struct queue));
  q->front = -1;
  q->rear = -1;
  return q;
}

// Check if the queue is empty
int isEmpty(struct queue* q) {
  if (q->rear == -1)
    return 1;
  else
    return 0;
}

// Adding elements into queue
void enqueue(struct queue* q, int value) {
  if (q->rear == SIZE - 1)
    printf("\nQueue is Full!!");
  else {
    if (q->front == -1)
      q->front = 0;
    q->rear++;
    q->items[q->rear] = value;
  }
}

// Removing elements from queue
int dequeue(struct queue* q) {
  int item;
  if (isEmpty(q)) {
    printf("Queue is empty");
    item = -1;
  } else {
    item = q->items[q->front];
    q->front++;
    if (q->front > q->rear) {
      printf("Resetting queue ");
      q->front = q->rear = -1;
    }
  }
  return item;
}

// Print the queue
void printQueue(struct queue* q) {
  int i = q->front;

  if (isEmpty(q)) {
    printf("Queue is empty");
  } else {
    printf("\nQueue contains \n");
    for (i = q->front; i < q->rear + 1; i++) {
      printf("%d ", q->items[i]);
    }
  }
}

int main() {
  struct Graph* graph = createGraph(6);
  addEdge(graph, 0, 1);
  addEdge(graph, 0, 2);
  addEdge(graph, 1, 2);
  addEdge(graph, 1, 4);
  addEdge(graph, 1, 3);
  addEdge(graph, 2, 4);
  addEdge(graph, 3, 4);

  bfs(graph, 0);

  return 0;
}

3
1

                                    #include &lt;bits/stdc++.h&gt;
using namespace std;
#define ll  long long int
#define ull unsigned long long int
void addedge(vector&lt;int&gt;adj[],int u,int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}
void print_list(vector &lt;int&gt; adj[],int n)
{
    for(int i=0;i&lt;n;i++)
    {
        cout &lt;&lt; &quot;adjacency list for node&quot; &lt;&lt; i &lt;&lt; &quot;is :\n&quot;;
        for(auto ele: adj[i])
        {
            cout &lt;&lt; &quot;-&gt;&quot; &lt;&lt; ele &lt;&lt; &quot; \n&quot;;
        }

    }
}
void BFS(int v,int e,vector&lt;int&gt;adj[])
{
    bool visited[e];
    for(int i=0;i&lt;e;i++)
    {
        visited[i]= false;
    }
    list&lt;int&gt;queue;
    visited[v]= true;
    queue.push_back(v);
    while (!queue.empty())
    {
        v = queue.front();
        cout &lt;&lt; v &lt;&lt; &quot; &quot;;
        queue.pop_front();
        for(auto ele:adj[v])
        {
            if(!visited[ele])
            {
                visited[ele]= true;
                queue.push_back(ele);

            }
        }
    }
}


int main()
{
    int n,e;
    cin &gt;&gt; n &gt;&gt; e;
    vector &lt;int&gt; adj[n];
    int u,v;
    for(int i=0;i&lt;e;i++)
    {

        cin &gt;&gt; u &gt;&gt; v;
        addedge(adj,u,v);

    }
    print_list(adj,n);
    BFS(v,e,adj);


    return 0;
}

3 (1 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 traversal in a graph possible of number BFS traversal from graph bfs traversal order bfs traversal in tree bfs traversal of binary tree dfs bfs traversal bfs algorithm graph in c bfs traversal algorithm in c bfs traversal example bfs traversal fro binary tree bfs traversal is what level order traversal of binary tree using bfs bfs traversal geeksforgeeks bfs graph traversal in real world c program Implementation of Graph traversal - Depth First Search tree bfs traversal simple graph bfs traversal in cpp best traversal bfs in binary tree how to traversing a graph by bfs bfs graph traversal hard example bfs and dfs graph traversal program in c graph bfs and dfs graph traversal program in c stl for bfs traversal traversing graph by bfs binary tree bfs traversal Data structure used in BFS traversal of a graph tree traversal dfs and bfs bfs inorder traversal bfs algorithm traversal graph traversal bfs question graph traversal bfs bfs graph traversal program in c output given bfs traversal graph bfs traversal of graph in cpp bfs is equivalent to which traversal bfs graph traversal program in c graph representation and breadth first traversal program in c bfs and dfs traversal BFS traversal of bibary tree bfs traversal gfg level order traversal in bfs BFS search for graph traversal algorithm bfs level order traversal graph bfs is equivalent to which traversal in binary tree bfs level order traversal in tree stl for bfs and dfs traversal graph to bfs traversal breadth first traversal of a graph in c bfs traversal of tree ca bfs level order traversal purpose of bfs graph traversal bfs traversal of graph gfg bfs traversal algorithm how to find all bfs traversals of a graph bfs matrix traversal BFS traversal in a level by level manner. bfs is similar to which tree traversal bfs traversal and dfs traversal graph BFS traversal and DFS traversal bfs tree traversal graph bfs traversal how to traverse a graph using bfs bfs traversal code c++ bfs traversal code Write a C program to traverse the below graph using Depth first traversal and Breadth first traversal bfs graph traversal example bfs graph traversal bfs traversal of grapht bfs and dfs in c++ bfs and dfs bfs traversal of a graph bfs traversal of tree bfs python bfs traversal BFS undirected graph in JAVA DFS and BFS programiz e breadth-first search java bfs algorithm output example Develop a program to implement BFS and DFS traversal of graph bfs algorithm in c Bread First search alogorithm bfs implementation c graph traversal in c breath irst search bfs implementation in c breadth first graph traversal a breadth-first search bfs linked list c bfs c linked list The Breadth First Traversal algorithm is implemented on given Graph using Queue. One of the possible order for visiting node on given graph: cpp program to implement bfs graph bfs in data structure in c how to print in bfs c programming python bfs search bfs in directed graph breadth first search. how to do breadth first traversal bfs linked list in c++ breadth first traversal of a tree in cpp bfs graph programiz bfs dfs Breadth First Search algorithm can be used to find the graph is connected or not. breadth search and breadth first search breadth first search program in c++ breathe first search dfs and bfs in python BFS traversal in tree cpp bfs traversal of graph data structure suitable for implementing BFS and bound strategy4 python breadth first search what is bfs in a graph BFS directed graph c# Breadth-first search bredth first search bfs search algorithm in c breadth first search algorithm Which of the following data structures is best to implement BFS Breadth First Search c breath first search bfs and dfs algorithm in python bfs dfs program in c code for bfs in c generating a graph for bfs python generating a graph for bfs bfs algorithm graph list c bfs code breathfirst search write a program to implement breadth first search algorithm using queue bfs c bfs pseudocode in c dfs and bfs in c bfs and dfs of graph in c bfs and dfs algorithm in c c bfs graph breadth first search in c breadth first search c exemple Implement Breadth First Search Graph Traversal technique on a Graph in c Implement Breadth First Search Graph Traversal technique on a Graph. Implement breadth First Search Graph Traversal using c breadth first search in c how to code bfs graph bfs in graph in c Write a program to traverse a graph using breadth-first search (BFS) bfs algorithm python bfs in c language using adjacency list breadth first search directed graph c=+ implement bfs and dfs in c Bfs for graphs in c bfs algos using queues bfs in python bfs and dfs program in c bfs traversal of undirected graph Implement BFS algorithm. bfs c program bfs code with graph output python c program for breadth first search traversal of a graph bfs traversal of graph in c breathfirst algorithm to code bfs and dfs in c graph traversal code c rbreadth first search binar first search cide in c cretae agraph and perform bfs from each node in c breadth first search algorithm in c breadth first order using adjacency matrix c bfs using adjacency matrix in c Write a program to implement breadth first search algorithm using adjacency matrix in c bfs c implementation breadth for search bfs program in c with explanation bfs traversal in c bfs program in c bfs using adjacency list in c graph search algorithm in c Discuss/Illustrate traversing a graph using BFS algorithm bfs using c Write the program for traversal of graph by using BFS how to traverse bfs and dfs bfs in c how to bfs c++ bfs using queue program in c++ bfs and dfs program in c++ how to impliment bfs in c++ bfs using python simple bfs program in python bfs codencode bfs code in c python graphs &quot;breadth first traversal&quot; bfs and dfs using python
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