Breadth-first search does not use queues
Breadth First, Look (BFS) is a graph traversal calculation used to study the center of breadth movement in a graph. Normal use of BFS utilizes the line information structure to keep track of incoming hubs. Regardless, it is conceivable to leverage other information structures to perform BFS without using explicit wires.
An alternative way to implement BFS without wires is to utilize two clusters or records: one for the hub at the current level being investigated, and one for the next level hub to be investigated. Initially, the current level list contains the source center.
Calculation starts by highlighting the current level list and going to each hub. For each hub passed through, its adjacent hubs are inspected. If an adjacent hub is not visited, it is marked as visited and added to the other level list. The handle will continue until all hubs in the current level list have been passed.
Once the current level list is fully traversed, the calculation continues to another level list and re-hashes the method to the hub and inspects the next level list. This preparation continues until there are no more unvisited nodes.
usage instructions
Breadth-first method
Breadth-first method
The BFS algorithm starts at the source hub, investigates its neighbors, and most recently moved to neighbors at another level. Use the line information structure to keep track of the hubs you visit. In each cycle, the computation visits a hub, marks it as completed, and queues unvisited adjacent hubs. This preparation will continue until all accessible centers have been visited.
The code initializes a vector adj to represent the infectious list of the chart. Each file of vectors is compared to a center, and each recorded value contains adjacent centers. A BFS traversal is performed by a BFS job, which takes the source hub, the number of hubs N, the vector vis passing through the hub, a separate dp, and the vector v used to keep track of the hubs to be visited. The bfsTraversal job initializes the vanished hub and deletes the vectors, then calls the BFS job to perform the traversal.
algorithm
Create an infection list representation of the graph.
Initialize a line to store the hub to be accessed.
Initialize the disappearing cluster to track disappearing nodes.
Initialize the delete cluster to store on each hub what was deleted from the source hub. Set the source hub's delimiter to 0.
Enqueue the source hub and check if it has been accessed.
Although the pipeline cannot be purged, please do the following:
Delete the hub at the head of the queue. For each neighbor hub that has been dequeued and not yet traversed, do the following: Queue the neighbor hub. Mark adjacent hubs as visited. Updated neighbor hub deletion to dequeue hub deletion (also 1).
Repeat step 6 until the row is empty.
After the BFS traversal is complete, a separate cluster will contain the intervals from the source node to all other centers in the graph.
(Optional) You can also keep track of each hub's parent hub in a BFS traversal to get from the source hub to all other hubs in the simplest way.
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
in conclusion
This article describes breadth-first search (BFS) calculations that do not use row information structures. BFS calculations are typically used to navigate a chart in a step-by-step fashion starting from a given source center. Typically, a route is used to store hubs to travel to. Regardless, this article examines an alternative approach that utilizes a basic list or clustering to store the next level of hubs.
This selective use completes the breadth-first study of graphs. This article traces the steps of BFS computation, such as initializing infectious records, maintaining go-to and separation clusters, and using circles to emphasize central levels. It also provides C code instructions illustrating BFS traversal without using a single line. The code accurately studies the graph, prints the BFS traversal permutation, and calculates the distance from the source hub to all other nodes. Overall, this article provides a clear explanation and feasible usage of BFS calculations without the use of lines, demonstrating an alternative approach to navigating graphs in a breadth-first manner.
The above is the detailed content of Breadth-first search does not use queues. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





When you click the search field in Windows 11, the search interface automatically expands. It displays a list of recent programs on the left and web content on the right. Microsoft displays news and trending content there. Today's check promotes Bing's new DALL-E3 image generation feature, the "Chat Dragons with Bing" offer, more information about dragons, top news from the Web section, game recommendations, and the Trending Search section. The entire list of items is independent of your activity on your computer. While some users may appreciate the ability to view news, all of this is abundantly available elsewhere. Others may directly or indirectly classify it as promotion or even advertising. Microsoft uses interfaces to promote its own content,

How does Xianyu search for users? In the software Xianyu, we can directly find the users we want to communicate with in the software. But I don’t know how to search for users. Just view it among the users after searching. Next is the introduction that the editor brings to users about how to search for users. If you are interested, come and take a look! How to search for users in Xianyu? Answer: View details among the searched users. Introduction: 1. Enter the software and click on the search box. 2. Enter the user name and click Search. 3. Select [User] under the search box to find the corresponding user.

How to use Baidu Advanced Search Baidu search engine is currently one of the most commonly used search engines in China. It provides a wealth of search functions, one of which is advanced search. Advanced search can help users search for the information they need more accurately and improve search efficiency. So, how to use Baidu advanced search? The first step is to open the Baidu search engine homepage. First, we need to open Baidu’s official website, which is www.baidu.com. This is the entrance to Baidu search. In the second step, click the Advanced Search button. On the right side of the Baidu search box, there is

In the era dominated by intelligence, office software has also become popular, and Wps forms are adopted by the majority of office workers due to their flexibility. At work, we are required not only to learn simple form making and text entry, but also to master more operational skills in order to complete the tasks in actual work. Reports with data and using forms are more convenient, clear and accurate. The lesson we bring to you today is: The WPS table cannot find the data you are searching for. Why please check the search option location? 1. First select the Excel table and double-click to open it. Then in this interface, select all cells. 2. Then in this interface, click the "Edit" option in "File" in the top toolbar. 3. Secondly, in this interface, click "

The following example prints files in a directory based on their extension - Example importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.util.stream.Stream; publicclassDemo{ publicstaticvoidmain(String[]args)throwsIOException{ 

The mobile Taobao app software provides a lot of good products. You can buy them anytime and anywhere, and everything is genuine. The price tag of each product is clear. There are no complicated operations at all, making you enjoy more convenient shopping. . You can search and purchase freely as you like. The product sections of different categories are all open. Add your personal delivery address and contact number to facilitate the courier company to contact you, and check the latest logistics trends in real time. Then some new users are using it for the first time. If you don’t know how to search for products, of course you only need to enter keywords in the search bar to find all the product results. You can’t stop shopping freely. Now the editor will provide detailed online methods for mobile Taobao users to search for store names. 1. First open the Taobao app on your mobile phone,

How to use PHP functions to search and filter data? In the process of developing using PHP, it is often necessary to search and filter data. PHP provides a wealth of functions and methods to help us achieve these operations. This article will introduce some commonly used PHP functions and techniques to help you search and filter data efficiently. String search Commonly used string search functions in PHP are strpos() and strstr(). strpos() is used to find the position of a certain substring in a string. If it exists, it returns

How to use the hash search algorithm in C++ The hash search algorithm is an efficient search and storage technology. It converts keywords into a fixed-length index through a hash function, and then uses this index in the data structure Search. In C++, we can implement hash search algorithms by using hash containers and hash functions from the standard library. This article will introduce how to use the hash search algorithm in C++ and provide specific code examples. Introducing header files and namespaces First, before using the hash search algorithm in C++
