Buy Royal UI Officially! Contact Us Buy Now!

Thuật Toán Tìm Kiếm Theo Chiều Rộng BFS

Mahmudul Hasan

THUẬT TOÁN DUYỆT CHIỀU RỘNG BFS



Bài trước mình đã đăng về thuật toán DFS thì bài này sẽ là BFS (Breadth First
Search) duyệt đồ thị theo chiều rộng.


align="center"
cellpadding="0"
cellspacing="0"
class="tr-caption-container"
style="margin-left: auto; margin-right: auto;"
>



href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEzpZfrorWS6coMAmcyT8mmRXI0RIph-kbTGnDK4Fw9LzjEoZ4arloxl1TGsqDNszo-c-tLthrxfWw9HybZwNPYp3XNQIwTrve0BWak9k3D64Vvxbtv9EuMxfYjv0ePhHPcTsMz3bgcAg/s1635/BFS.png"
imageanchor="1"
style="margin-left: auto; margin-right: auto;"
> border="0"
data-original-height="1000"
data-original-width="1635"
src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEzpZfrorWS6coMAmcyT8mmRXI0RIph-kbTGnDK4Fw9LzjEoZ4arloxl1TGsqDNszo-c-tLthrxfWw9HybZwNPYp3XNQIwTrve0BWak9k3D64Vvxbtv9EuMxfYjv0ePhHPcTsMz3bgcAg/s16000/BFS.png"
/>




Thuật Toán Tìm Kiếm Theo Chiều Rộng BFS




THUẬT TOÁN


MÔ TẢ THUẬT TOÁN:




  • BFS: duyệt theo chiều rộng xuất phát từ đỉnh u 


  • Thăm các đỉnh nằm trong danh sách kề của u mà chưa được thăm (gọi là các
    đỉnh mức 1) 


  • Thăm các đỉnh nằm trong danh sách kề của các đỉnh mức 1 mà chưa được thăm
    (gọi là các đỉnh mức 2) 


  • Thăm các đỉnh nằm trong danh sách kề của các đỉnh mức 2 mà chưa được thăm
    (gọi là các đỉnh mức 3) 

  • . . . 

  • Sử dụng cấu trúc hàng đợi (queue)

  • Thuật toán được code trên ngôn ngữ C++


VÍ DỤ:



Vẫn là ví dụ về đồ thị có hướng ở bài trước nhé =)))



href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2TB5sfry8Da9qnVHv1SmP1r15kN1dJflyP82OJ3fum1QbH_JoCkvgBYFONtsQYoubQv-tqXQA5JuZZb_y6lZ81CSoUH4Uob9lcxIk-aY4ZGPq93rGhfLmRaDqPSR1BcAz7kfH3Mu1-SU/s963/Untitled-4.png"
style="margin-left: 1em; margin-right: 1em;"
> border="0"
data-original-height="688"
data-original-width="963"
src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2TB5sfry8Da9qnVHv1SmP1r15kN1dJflyP82OJ3fum1QbH_JoCkvgBYFONtsQYoubQv-tqXQA5JuZZb_y6lZ81CSoUH4Uob9lcxIk-aY4ZGPq93rGhfLmRaDqPSR1BcAz7kfH3Mu1-SU/s16000/Untitled-4.png"
/>





CODE MẪU:





Kết quả: 1 3 6 4 7 2 5

SO SÁNH DFS VÀ BFS:



Dưới đây là so sáng kết quả của 2 thuật toán trên cùng một đồ thị. Hình
bên trái là duyệt theo DFSbên phải là duyệt theo
BFS.


href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQth_m7ZAE6LoNK7tsCOiE_Y8iwp0BCc_VognnIb6R2hQQU94OCBMupHg1lyecaV54AM_uljMtcb-Lq__f7gg48V04r7RMBf2vWJQYoIdVqOvHW_BHGEJWunDvEIMMp4dmvmieZhq7XVs/s1615/adsad.png"
style="margin-left: 1em; margin-right: 1em;"
> border="0"
data-original-height="1000"
data-original-width="1615"
src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQth_m7ZAE6LoNK7tsCOiE_Y8iwp0BCc_VognnIb6R2hQQU94OCBMupHg1lyecaV54AM_uljMtcb-Lq__f7gg48V04r7RMBf2vWJQYoIdVqOvHW_BHGEJWunDvEIMMp4dmvmieZhq7XVs/s16000/adsad.png" />
>



LỜI KẾT



Trên đây mình đã chia sẻ những hiểu biết của mình về thuật toán duyệt chiều
rộng BFS. Nếu có gì sai sót các bạn có thể để lại comment ở bên dưới để mình
chỉnh sửa. Cảm ơn các bạn đã đọc bài viết !


إرسال تعليق

  • A-
  • A+

© Techypremium.Com. All rights reserved.

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.