Cải thiện bài viết Lưu bài viết ĐọcBàn luậnCải thiện bài viết Lưu bài viết Đọc Bàn luậnBest First Search is to use an evaluation function to decide which adjacent is most promising and then explore.
Trong BFS và DFS, khi chúng ta ở một nút, chúng ta có thể xem xét bất kỳ loại liền kề nào là nút tiếp theo. Vì vậy, cả BFS và DFS khám phá một cách mù quáng các đường dẫn mà không xem xét bất kỳ chức năng chi phí nào. & NBSP; Ý tưởng về tìm kiếm đầu tiên tốt nhất là sử dụng một chức năng đánh giá để quyết định xem liền kề nào là hứa hẹn nhất và sau đó khám phá. Tìm kiếm đầu tiên tốt nhất thuộc danh mục tìm kiếm heuristic hoặc tìm kiếm được thông báo. // Pseudocode for Best First Search
Best-First-Search(Graph g, Node start)
1) Create an empty PriorityQueue
PriorityQueue pq;
2) Insert "start" in pq.
pq.insert(start)
3) Until PriorityQueue is empty
u = PriorityQueue.DeleteMin
If u is the goal
Exit
Else
Foreach neighbor v of u
If v "Unvisited"
Mark v "Visited"
pq.insert(v)
Mark u "Examined"
End procedure Illustration: Thực hiện tìm kiếm đầu tiên tốt nhất:
- Chúng tôi sử dụng hàng đợi ưu tiên hoặc đống để lưu trữ các chi phí của các nút có giá trị chức năng đánh giá thấp nhất. Vì vậy, việc thực hiện là một biến thể của BFS, chúng ta chỉ cần thay đổi hàng đợi thành ưu tiên. & NBSP;
- Hãy để chúng tôi xem xét ví dụ dưới đây:
- Chúng tôi bắt đầu từ Nguồn S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S Sh.
- PQ ban đầu chứa s
- Chúng tôi loại bỏ S khỏi và xử lý các hàng xóm không liên quan đến S đến PQ.
- PQ hiện chứa {a, c, b} (c được đặt trước b vì c có chi phí thấp hơn) & nbsp;
- Chúng tôi loại bỏ một từ PQ và xử lý các hàng xóm không được xác nhận của A đến PQ.
- pq hiện chứa {c, b, e, d} & nbsp;
- Chúng tôi loại bỏ C khỏi PQ và xử lý các hàng xóm không liên quan của C đến PQ.
- PQ hiện chứa {b, h, e, d} & nbsp;
- Chúng tôi loại bỏ B khỏi PQ và xử lý các hàng xóm không liên quan của B đến PQ.
- PQ hiện chứa {h, e, d, f, g}
Chúng tôi loại bỏ H khỏi PQ. & nbsp; C++#include <bits/stdc++.h>
Vì mục tiêu của chúng tôi, tôi là một người hàng xóm của H, chúng tôi trở lại. Dưới đây là việc thực hiện ý tưởng trên: #include <bits/stdc++.h> 1
using namespace std;
using 0
using 1using 2
using 1using 4
using 5
‘ using 0
using 1namespace 6namespace 7namespace 8namespace 9std; 0
using 1std; 2
using 1std; 4
#include <bits/stdc++.h> 2 #include <bits/stdc++.h> 3int #include <bits/stdc++.h> 5int #include <bits/stdc++.h> 7______
using 1std; 9typedef 0typedef 1
#include <bits/stdc++.h> 2 using 7int using 9int namespace 1int namespace 3
using 1int std; 7
typedef 5typedef 9pair< 0typedef 1
typedef 5pair< 3
using 1typedef 3 typedef 4
pair< 7pair< 8typedef 1
typedef 5int typedef 7
typedef 5pair< 5 pair< 6
int 8int 9typedef 0typedef 1
int 8, 3
pair< 7using 5
typedef 5using 5
typedef 5int 1 int 2int int 4
using 5
pair< 7pair< 5 int 7
using 0
using 1using 5
using 1int 8
using 1#include <bits/stdc++.h> 00
using 1#include <bits/stdc++.h> 02
using 1#include <bits/stdc++.h> 04
using 1#include <bits/stdc++.h> 06
using 1#include <bits/stdc++.h> 08
using 1#include <bits/stdc++.h> 10
using 1#include <bits/stdc++.h> 12
using 1#include <bits/stdc++.h> 14
using 1#include <bits/stdc++.h> 16
using 1#include <bits/stdc++.h> 18
using 1#include <bits/stdc++.h> 20
using 1#include <bits/stdc++.h> 22
using 1#include <bits/stdc++.h> 24
int int 2
using 1int int 6
using 1#include <bits/stdc++.h> 32
using 1int #include <bits/stdc++.h> 27
using 5
using1int #include <bits/stdc++.h>30using 1#include <bits/stdc++.h> 34 #include <bits/stdc++.h> 35
Java #include <bits/stdc++.h> 37 #include <bits/stdc++.h> 38
using 0
#include <bits/stdc++.h> 37 #include <bits/stdc++.h> 40
#include <bits/stdc++.h> 41 #include <bits/stdc++.h> 42
#include <bits/stdc++.h> 44using 0
#include <bits/stdc++.h> 44#include <bits/stdc++.h> 45 #include <bits/stdc++.h> 46#include <bits/stdc++.h> 47 #include <bits/stdc++.h> 48
#include <bits/stdc++.h> 44#include <bits/stdc++.h> 45 #include <bits/stdc++.h> 41 #include <bits/stdc++.h> 52#include <bits/stdc++.h> 53 #include <bits/stdc++.h> 54
using 1using 0
#include <bits/stdc++.h> 68#include <bits/stdc++.h> 69#include <bits/stdc++.h> 70
#include <bits/stdc++.h> 68#include <bits/stdc++.h> 69#include <bits/stdc++.h> 73
typedef 5int 1 int 2int int 4
pair< 7pair< 5 int 7
using 1using 0
using 1using 5
int int 2
#include <bits/stdc++.h> 68using 5
#include <bits/stdc++.h> 68#include <bits/stdc++.h> 93
using 1int int 6
using 1using 5
#include <bits/stdc++.h> 44using 5
using 1int #include <bits/stdc++.h> 27
#include <bits/stdc++.h> 44using 0
using 1int #include <bits/stdc++.h> 30
using 1#include <bits/stdc++.h> 34 #include <bits/stdc++.h> 35
using 1using 5
Java #include <bits/stdc++.h> 37 #include <bits/stdc++.h> 38
#include <bits/stdc++.h> 44using 0
#include <bits/stdc++.h> 37 #include <bits/stdc++.h> 40
#include <bits/stdc++.h> 41 #include <bits/stdc++.h> 42
using 1using 48typedef 0typedef 1
#include <bits/stdc++.h> 44#include <bits/stdc++.h> 45 #include <bits/stdc++.h> 46#include <bits/stdc++.h> 47 #include <bits/stdc++.h> 48
#include <bits/stdc++.h> 44#include <bits/stdc++.h> 45 #include <bits/stdc++.h> 41 #include <bits/stdc++.h> 52#include <bits/stdc++.h> 53 #include <bits/stdc++.h> 54
using 1int #include <bits/stdc++.h> 59
#include <bits/stdc++.h> 68using 64pair< 0std; 0
using 1#include <bits/stdc++.h> 61int #include <bits/stdc++.h> 63int #include <bits/stdc++.h> 9
typedef 5pair< 8typedef 1
#include <bits/stdc++.h> 68using 5
using 1#include <bits/stdc++.h> 77 #include <bits/stdc++.h> 78 int #include <bits/stdc++.h> 80
#include <bits/stdc++.h> 68pair< 5 #include <bits/stdc++.h> 85
using 81using 82typedef 0typedef 1
using 81using 86
typedef 5using 5
#include <bits/stdc++.h> 68using 5
using 1using 5
#include <bits/stdc++.h> 44using 5
typedef 5#include <bits/stdc++.h> 34 #include <bits/stdc++.h> 88typedef 1
#include <bits/stdc++.h> 44using 0
using 1namespace 07#include <bits/stdc++.h> 47 namespace 09
using 1namespace 11#include <bits/stdc++.h> 47 namespace 13
#include <bits/stdc++.h> 44using 5
#include <bits/stdc++.h> 44#include <bits/stdc++.h> 78 #include <bits/stdc++.h> 45 #include <bits/stdc++.h> 2 namespace 20
#include <bits/stdc++.h> 44using 0
using 1int namespace 25namespace 26typedef 1
using 1namespace 29#include <bits/stdc++.h> 47 namespace 31
using 1namespace 33using 15, #include <bits/stdc++.h> 88, namespace 38std; 0
using 1namespace 33using 15, namespace 44, namespace 46std; 0
using 1namespace 33using 15, namespace 38, namespace 54std; 0
using 1namespace 33#include <bits/stdc++.h> 88, namespace 60, namespace 62std; 0
using 1namespace 33#include <bits/stdc++.h> 88, namespace 54, namespace 70std; 0
Các using 1namespace 33namespace 44, namespace 84, namespace 26std; 0
using 1namespace 33namespace 38, namespace 70, namespace 84std; 0
using 1namespace 33namespace 70, namespace 62, namespace 54std; 0
using 1namespace 33namespace 70, std; 08, namespace 46std; 0
using 1namespace 33namespace 62, std; 16, #include <bits/stdc++.h> 88std; 0
using 1namespace 33namespace 62, namespace 78, std; 08std; 0
using 1namespace 33namespace 62, std; 32, namespace 44std; 0
using 1int std; 38using 15typedef 1
using 1int std; 43namespace 62typedef 1
using 1#include <bits/stdc++.h> 32
#include <bits/stdc++.h> 44using 5
using 5
Python3std; 51 std; 52#include <bits/stdc++.h> 37 std; 54
std; 55std; 56 namespace 26
std; 58std; 56 std; 60int 1 std; 62std; 63
std; 66 std; 67
using 1std; 69std; 56 std; 71std; 72std; 73std; 74 std; 75
using 1std; 777____456 std; 79
using 1std; 81using 15std; 83
using 1std; 85std; 56 std; 87
using 1typedef 3 std; 90std; 56std; 56 std; 72std; 94
typedef 5std; 96std; 56 std; 98#include <bits/stdc++.h> 88typedef 00
typedef 5typedef 02typedef 03std; 56pair< 0typedef 06
typedef 5pair< 5 std; 96std; 56std; 56 typedef 12
pair< 7pair< 8
typedef 5int 1 typedef 17std; 63 typedef 19
pair< 7pair< 5 typedef 222std; 56std; 56 std; 72std; 94
int 8typedef 22222456 std; 87
int 8typedef 32
using 1typedef 022
std; 66 typedef 37
using 1typedef 39
using 1typedef 41
#include <bits/stdc++.h> 3using 15, #include <bits/stdc++.h> 88, namespace 38typedef 06
#include <bits/stdc++.h> 3using 15, namespace 44, namespace 46typedef 06
#include <bits/stdc++.h> 3using 15, namespace 38, namespace 54typedef 06
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 88, namespace 60, namespace 62typedef 06
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 88, namespace 54, namespace 70typedef 06
#include <bits/stdc++.h> 3namespace 44, namespace 46, namespace 78typedef 06
Các #include <bits/stdc++.h> 3namespace 38, namespace 70, namespace 84typedef 06
#include <bits/stdc++.h> 3namespace 70, namespace 62, namespace 54typedef 06
#include <bits/stdc++.h> 3namespace 70, std; 08, namespace 46typedef 06
#include <bits/stdc++.h> 3namespace 62, std; 16, #include <bits/stdc++.h> 88typedef 06
#include <bits/stdc++.h> 3namespace 62, namespace 78, std; 08typedef 06
#include <bits/stdc++.h> 3namespace 62, std; 32, namespace 44typedef 06
pair< 33std; 56 using 15
pair< 36std; 56 namespace 62
pair< 39
Phân tích: & NBSP; - Độ phức tạp thời gian trong trường hợp xấu nhất cho tìm kiếm đầu tiên tốt nhất là O (N * log n) trong đó n là số lượng nút. Trong trường hợp xấu nhất, chúng ta có thể phải ghé thăm tất cả các nút trước khi đạt được mục tiêu. Lưu ý rằng hàng đợi ưu tiên được triển khai bằng đống tối thiểu (hoặc tối đa) và chèn và xóa các hoạt động mất thời gian O (log n).
- Hiệu suất của thuật toán phụ thuộc vào mức độ chi phí hoặc chức năng đánh giá được thiết kế.
Các trường hợp đặc biệt của tìm kiếm đầu tiên tốt nhất: - Thuật toán tìm kiếm đầu tiên tốt nhất
- Một thuật toán tìm kiếm
Bài viết này được đóng góp bởi Shambhavi Singh. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. & NBSP;Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks
main page and help other Geeks. |