Skip to content
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

BFS Code Added with a descriptive README #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion Graphs/BFS

This file was deleted.

81 changes: 81 additions & 0 deletions Graphs/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Program to print BFS traversal from a givensource vertex.
#include<iostream>
#include <list>

using namespace std;

// This class represents a directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
public:
Graph(int V) // Constructor
{
this->V = V;
adj = new list<int>[V];
}

// function to add an edge to graph
void addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}

// prints BFS traversal from a given source s
void BFS(int s);
};

void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;

list<int> queue; // queue for BFS

// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);

list<int>::iterator i; // 'i' will be used to get all adjacent vertices of a vertex

while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();

/* Get all adjacent vertices of the dequeued
vertex s. If a adjacent has not been visited,
then mark it visited and enqueue it*/
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}

// Driver program to test methods of graph class
int main()
{
Graph g(4); // creating the graph shown in .md file
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;
}
Binary file added Graphs/BFS.exe
Binary file not shown.
Binary file added Graphs/Graph Image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Graphs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

# BFS(Breadth First Search)

Breadth first search is a graph traversal algorithm that starts traversing the graph from root node and explores all the neighbouring nodes. Then, it selects the nearest node and explore all the unexplored nodes. The algorithm follows the same process for each of the nearest node until it finds the goal.

The algorithm of breadth first search is given below. The algorithm starts with examining the node A and all of its neighbours. In the next step, the neighbours of the nearest node of A are explored and process continues in the further steps. The algorithm explores all neighbours of all the nodes and ensures that each node is visited exactly once and no node is visited twice.

**Step 1:** SET STATUS = 1 (ready state)
for each node in G

**Step 2:** Enqueue the starting node A
and set its STATUS = 2
(waiting state)


**Step 3:** Repeat Steps 4 and 5 until
QUEUE is empty

**Step 4:** Dequeue a node N. Process it
and set its STATUS = 3
(processed state).

**Step 5:** Enqueue all the neighbours of
N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]

**Step 6:** EXIT
## Graph Taken

![Graph](https://github.com/Kesh2001/Algorithms/blob/main/Graphs/Graph%20Image.jpg)