diff --git a/Graphs/BFS b/Graphs/BFS deleted file mode 100644 index 8b13789..0000000 --- a/Graphs/BFS +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Graphs/Graph Image.jpg b/Graphs/Graph Image.jpg new file mode 100644 index 0000000..c3fa7a7 Binary files /dev/null and b/Graphs/Graph Image.jpg differ diff --git a/Graphs/README.md b/Graphs/README.md new file mode 100644 index 0000000..c7415a3 --- /dev/null +++ b/Graphs/README.md @@ -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) + + diff --git a/Graphs/keshave_BFS.cpp b/Graphs/keshave_BFS.cpp new file mode 100644 index 0000000..287d66c --- /dev/null +++ b/Graphs/keshave_BFS.cpp @@ -0,0 +1,81 @@ +// Program to print BFS traversal from a givensource vertex. +#include +#include + +using namespace std; + +// This class represents a directed graph using adjacency list representation +class Graph +{ + int V; // No. of vertices + list *adj; // Pointer to an array containing adjacency lists +public: + Graph(int V) // Constructor + { + this->V = V; + adj = new list[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 queue; // queue for BFS + + // Mark the current node as visited and enqueue it + visited[s] = true; + queue.push_back(s); + + list::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; +}