Share bfs c++

brownbear351

New member
#khối bề rộng-Tìm kiếm #C ++ #graph #dữ liệu dữ liệu #algorithms ## Tìm kiếm đầu tiên (BFS) trong C ++

Tìm kiếm đầu tiên (BFS) là một thuật toán để đi qua hoặc tìm kiếm một biểu đồ.Nó bắt đầu ở một nút (được gọi là gốc) và khám phá tất cả các nút được kết nối trực tiếp với gốc trước khi di chuyển đến các nút cách đó một bước nữa.Quá trình này tiếp tục cho đến khi tất cả các nút trong biểu đồ đã được truy cập.

BFS là một thuật toán đơn giản và hiệu quả có thể được sử dụng để tìm đường dẫn ngắn nhất giữa hai nút trong biểu đồ.Nó cũng có thể được sử dụng để tìm tất cả các thành phần được kết nối trong một biểu đồ và để tìm sự đóng cửa chuyển tiếp của một mối quan hệ.

### Triển khai trong C ++

Mã sau đây cho thấy cách thực hiện BFS trong C ++.Mã sử dụng hàng đợi để lưu trữ các nút hiện đang được khám phá.Hàng đợi được khởi tạo với nút gốc.Thuật toán sau đó liên tục khử một nút từ hàng đợi, đánh dấu nút khi được truy cập và thêm tất cả các nút không được xác định liền kề của nút vào hàng đợi.Quá trình này tiếp tục cho đến khi hàng đợi trống, điều này chỉ ra rằng tất cả các nút trong biểu đồ đã được truy cập.

`` `C ++
#include <Istream>
#include <Verue>

sử dụng không gian tên STD;

// một cấu trúc để biểu diễn một nút trong một biểu đồ.
Nút cấu trúc {
dữ liệu int;
Vector <nút*> hàng xóm;
};

// một chức năng để in các nút theo đồ thị theo thứ tự đầu tiên.
void bfs (nút* root) {
// Tạo một hàng đợi để lưu trữ các nút hiện đang được khám phá.
Hàng đợi <nút*> q;

// Thêm nút gốc vào hàng đợi.
Q.Push (gốc);

// Đánh dấu nút gốc như đã truy cập.
root-> đã truy cập = true;

// Trong khi hàng đợi không trống, dequeue một nút từ hàng đợi và khám phá các nút không được xác định liền kề của nó.
while (! q.empty ()) {
// dequeue một nút từ hàng đợi.
Nút* nút = q.front ();
Q.Pop ();

// In dữ liệu của nút.
cout << Node-> data << "";

// Thêm các nút không được xác định liền kề của nút vào hàng đợi.
for (nút* lân cận: nút-> hàng xóm) {
if (! lân cận-> đã truy cập) {
Q.Push (hàng xóm);
Neighbor-> đã truy cập = true;
}
}
}
}

int main () {
// Tạo một biểu đồ với 5 nút.
Nút* nút [5] = {
Nút mới {0}, nút mới {1}, nút mới {2}, nút mới {3}, nút mới {4}
};

// Thêm các cạnh vào biểu đồ.
các nút [0]-> hàng xóm.push_back (nút [1]);
các nút [0]-> hàng xóm.push_back (nút [2]);
các nút [1]-> hàng xóm.push_back (nút [2]);
các nút [1]-> hàng xóm.push_back (nút [3]);
các nút [2]-> hàng xóm.push_back (nút [3]);
các nút [3]-> hàng xóm.push_back (nút [4]);

// In các nút trong biểu đồ theo thứ tự đầu tiên.
BFS (nút [0]);

trả lại 0;
}
`` `

### Độ phức tạp thời gian

Độ phức tạp của thời gian của BFS là O (V + E), trong đó v là số lượng các đỉnh trong biểu đồ và E là số lượng các cạnh trong biểu đồ.

### Độ phức tạp không gian

Độ phức tạp không gian của BFS là O (V), trong đó v là số lượng các đỉnh trong biểu đồ.

## hashtags

* #khối bề rộng-Tìm kiếm
* #C ++
* #graph
* #cấu trúc dữ liệu
* #algorithms
=======================================
#breadth-first-search #C++ #graph #data-structures #algorithms ## Breadth-First Search (BFS) in C++

Breadth-first search (BFS) is an algorithm for traversing or searching a graph. It starts at a node (called the root) and explores all of the nodes that are directly connected to the root before moving on to the nodes that are one step further away. This process continues until all of the nodes in the graph have been visited.

BFS is a simple and efficient algorithm that can be used to find the shortest path between two nodes in a graph. It can also be used to find all of the connected components in a graph, and to find the transitive closure of a relation.

### Implementation in C++

The following code shows how to implement BFS in C++. The code uses a queue to store the nodes that are currently being explored. The queue is initialized with the root node. The algorithm then repeatedly dequeues a node from the queue, marks the node as visited, and adds all of the node's adjacent unvisited nodes to the queue. This process continues until the queue is empty, which indicates that all of the nodes in the graph have been visited.

```c++
#include <iostream>
#include <queue>

using namespace std;

// A struct to represent a node in a graph.
struct Node {
int data;
vector<Node*> neighbors;
};

// A function to print the nodes in a graph in breadth-first order.
void BFS(Node* root) {
// Create a queue to store the nodes that are currently being explored.
queue<Node*> q;

// Add the root node to the queue.
q.push(root);

// Mark the root node as visited.
root->visited = true;

// While the queue is not empty, dequeue a node from the queue and explore its adjacent unvisited nodes.
while (!q.empty()) {
// Dequeue a node from the queue.
Node* node = q.front();
q.pop();

// Print the node's data.
cout << node->data << " ";

// Add the node's adjacent unvisited nodes to the queue.
for (Node* neighbor : node->neighbors) {
if (!neighbor->visited) {
q.push(neighbor);
neighbor->visited = true;
}
}
}
}

int main() {
// Create a graph with 5 nodes.
Node* nodes[5] = {
new Node{0}, new Node{1}, new Node{2}, new Node{3}, new Node{4}
};

// Add edges to the graph.
nodes[0]->neighbors.push_back(nodes[1]);
nodes[0]->neighbors.push_back(nodes[2]);
nodes[1]->neighbors.push_back(nodes[2]);
nodes[1]->neighbors.push_back(nodes[3]);
nodes[2]->neighbors.push_back(nodes[3]);
nodes[3]->neighbors.push_back(nodes[4]);

// Print the nodes in the graph in breadth-first order.
BFS(nodes[0]);

return 0;
}
```

### Time Complexity

The time complexity of BFS is O(V + E), where V is the number of vertices in the graph and E is the number of edges in the graph.

### Space Complexity

The space complexity of BFS is O(V), where V is the number of vertices in the graph.

## Hashtags

* #breadth-first-search
* #C++
* #graph
* #data-structures
* #algorithms
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top