Breadth First Search without using Queue


Breadth To begin with, Look (BFS) may be a chart traversal calculation utilised to investigate hubs in a chart in a breadthward movement. The normal usage of BFS utilises a line information structure to keep track of hubs to come. In any case, it is conceivable to execute BFS without utilising an unequivocal line by utilising other information structures.

One elective approach to actualizing BFS without a line is to utilise two clusters or records: one for the current level of hubs being investigated and another for the next level of hubs to be investigated. At first, the current level list contained, as it were, the source hub.

The calculation begins by emphasising the current level list and going to each hub. For each gone−by hub, its adjoining hubs are inspected. In the event that an adjoining hub has not been gone to, it is stamped as gone by and added to the other level list. This handle proceeds until all hubs within the current level list have been gone by.

Once the current level list is completely traversed, the calculation continues to another level list and rehashes the method of going to hubs and overhauling the following level list. This preparation proceeds until there are no more unvisited nodes.

Methods Used

Breadth First approach

Breadth First Approach

The BFS algorithm begins at a source hub and investigates its neighbours, recently moving to another level of neighbours. Use the circuit information structure to keep track of which hubs you visit. On each cycle, the computation visits a hub, marks it as finished, and queues unvisited neighbouring hubs. This preparation proceeds until all reachable hubs are visited.

The code initialises a vector adj to speak to the contagiousness list of the chart. Each file of the vector compares to a hub, and the values at each record contain the neighbouring hubs. The BFS traversal is performed by the BFS work, which takes the source hub, the number of hubs N, vectors for going by hubs vis, a separate dp, and a vector v to keep track of the hubs to visit. The bfsTraversal work initialises the gone−by hubs and removes vectors, and after that, it calls the BFS work to perform the traversal.

Algorithm

  • Create a contagion list representation of the chart.

  • Initialise a line to store the hubs to visit.

  • Initialise a gone−to cluster to keep track of gone−to nodes.

  • Initialise a remove cluster to store the remove from the source hub on each hub. Set the separator of the source hub to 0.

  • Enqueue the source hub into the line and check it as visited.

  • While the line isn't purgeable, do the following:

  • Remove the hub at the head of the queue. For each neighbour hub that has been dequeued and has not yet been walked, do the following: Enqueue neighbouring hubs. Mark the adjacent hub as visited. Update neighbour hub deletions to be dequeued hub deletions (also 1).

  • Repeat step 6 until the line is empty.

  • After the BFS traversal is total, the separate cluster will contain the separations from the source node to all other hubs within the graph.

  • Optionally, you'll also track the parent of each hub amid the BFS traversal to make the most brief way from the source hub to all other hubs.

Example

#include <iostream>
#include <queue>
#include <unordered_set>
using namespace std;

void bfsTraversal(int adjacencyList[][2], int numVertices, int source) {
   bool visited[numVertices + 1] = {false};
   int distances[numVertices + 1] = {0};

   queue<int> vertices;
   vertices.push(source);
   visited[source] = true;

   while (!vertices.empty()) {
      int node = vertices.front();
      cout << node << ", ";
      vertices.pop();

      for (int i = 0; i < 2; i++) {
         int next = adjacencyList[node][i];
            
         if (!visited[next]) {
            vertices.push(next);
            distances[next] = distances[node] + 1;
            visited[next] = true;
         }
      }
   }
}

int main() {
    int adjacencyList[][2] = {{0, 0}, {1, 2}, {3, 4}, {0, 0}, {0, 0}};
    int numVertices = 4;
    int source = 2;

    bfsTraversal(adjacencyList, numVertices, source);

    return 0;
}

Output

2,3,4,0

Example

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

void bfsTraversal(vector<vector<int>>& adjacencyList, int N, int source) {
    vector<bool> visited(N + 1, false);
    vector<int> distances(N + 1, 0);
    vector<int> vertices;

    vertices.push_back(source);
    visited[source] = true;

    int curr = 0;
    while (curr < vertices.size()) {
        int node = vertices[curr];
        cout << node << ", ";

        for (int i = 0; i < adjacencyList[node].size(); i++) {
            int next = adjacencyList[node][i];

            if (!visited[next]) {
                vertices.push_back(next);
                distances[next] = distances[node] + 1;
                visited[next] = true;
            }
        }

        curr++;
    }

    cout << "\nDistances from source " << source << ":\n";
    for (int i = 1; i <= N; i++) {
        cout << "Node " << i << ": " << distances[i] << endl;
    }
}

int main() {
    int N = 8;
    vector<vector<int>> adjacencyList(N + 1);
    adjacencyList[0] = {1, 2};
    adjacencyList[1] = {2};
    adjacencyList[2] = {0, 3};
    adjacencyList[3] = {3};
    adjacencyList[4] = {5};
    adjacencyList[5] = {6, 7};
    adjacencyList[6] = {};
    adjacencyList[7] = {};
    adjacencyList[8] = {};

    int source = 5;

    bfsTraversal(adjacencyList, N, source);

    return 0;
}

Output

5, 6, 7, 
Distances from source 5:
Node 1: 0
Node 2: 0
Node 3: 0
Node 4: 0
Node 5: 0
Node 6: 1
Node 7: 1
Node 8: 0

Conclusion

This article gives a clarification of the Breadth−first−look (BFS) calculation without employing a line information structure. The BFS calculation is commonly utilised to navigate charts in a level−by−level way, beginning from a given source hub. Regularly, a line is utilised to store the hubs to be gone to. In any case, this article investigates an elective approach that utilises a basic list or cluster to store the hubs for the following level.

This elective usage accomplishes the breadth−first investigation of the graph. The article traces the steps of the BFS calculation, such as initialising contagiousness records, keeping up go−to and separation clusters, and utilising a circle to emphasise the levels of hubs. It too presents a C code illustration that illustrates the BFS traversal without employing a line. The code accurately investigates the chart, prints the BFS traversal arrangement, and computes the separations from the source hub to all other nodes. Overall, this article gives a clear clarification and viable usage of the BFS calculation without utilising a line, displaying an elective approach for navigating charts in a breadth−first way.

Updated on: 17-Jul-2023

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements