当前位置:网站首页>Breadth first search (BFS) and its matlab code
Breadth first search (BFS) and its matlab code
2022-07-29 00:37:00 【Love learning one by one】
List of articles
Preface
Breadth first search is often compared with depth first search , Both of them are graph search algorithms, but their search methods are different , Next, let's study together ~
One 、 What is breadth first search ?
Breadth first search , Its full name is B r e a d t h f i r s t S e a r c h Breadth first Search BreadthfirstSearch, Referred to as B F S BFS BFS, Also known as width first search . It starts from the initial node , Apply production rules and control strategies to generate the first layer nodes , At the same time, check whether the target node is in these generated nodes . If there is no , Then use production rules to put all The first level nodes are expanded one by one , Get the second level node , And check whether the second layer node contains the target node one by one . If there is no , Then the production rules are used to expand the second layer nodes . Expand in this order , Check it out , Until the target node is found . If all nodes are expanded , No target node is found , There is no solution to the problem .
The above introduction is from the following website :
https://blog.csdn.net/ZhuRanCheng/article/details/115588272
Two 、 The basic idea of breadth first search is its matlab Code
1、 The basic idea of breadth first search
Breadth first search uses queues (queue) To achieve , The whole process can also be seen as an inverted tree :
(1) Put the root node at the end of the queue ;
(2) Take one element from the head of the queue at a time , View all the subordinate elements of this element , Put them at the end of the queue . And record this element as the precursor of its next level element ;
(3) End the program when you find the element you are looking for ;
(4) If you traverse the whole tree and haven't found , End procedure .
The above basic ideas refer to Baidu Encyclopedia :
https://baike.baidu.com/item/%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2/5224802?fromtitle=%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2&fromid=2148012&fr=aladdin
2、matlab Code
Breadth first search :BFS.m
clear all;close all;clc
% Initialize adjacency compression table
b=[1 2;1 3;1 4;2 4;
2 5;3 6;4 6;4 7];
m=max(b(:)); % The maximum value in the compressed table is the width and height of the adjacency matrix
A=compresstable2matrix(b); % Construct the matrix representation of the graph from the adjacency compression table
netplot(A,1) % Image represents
head=1; % Queue header
tail=1; % Queue tail , The start queue is empty ,tail==head
queue(head)=1; % Add the first node of the graph to the header
head=head+1; % Queue expansion
flag=1; % Mark whether a node has been accessed
re=[]; % final result
while tail~=head % Determines if the queue is empty
i=queue(tail); % Take the tail node
for j=1:m
if A(i,j)==1 && isempty(find(flag==j,1)) % If the node is connected and has not been visited
queue(head)=j; % New nodes are listed
head=head+1; % Expand queue
flag=[flag j]; % Mark the new node
re=[re;i j]; % Save the edge into the result
end
end
tail=tail+1;
end
A=compresstable2matrix(re);
figure;
netplot(A,1)
Matrix representation of construction graph :compresstable2matrix.m
function A=compresstable2matrix(b)
[n ~]=size(b);
m=max(b(:));
A=zeros(m,m);
for i=1:n
A(b(i,1),b(i,2))=1;
A(b(i,2),b(i,1))=1;
end
end
Drawing function :netplot.m( Matrix generates undirected network graph )
% Function name netplot
% Please input the usage method help netplot
% No return value % Functions can only handle undirected graphs
% author :tiandsp
% Last modified :2012.12.26
function netplot(A,flag)
% Call method input netplot(A,flag), No return value
%A Is adjacency matrix or incidence matrix
%flag=1 When dealing with adjacency matrix
%flag=2 When dealing with incidence matrix
% Functions can only handle undirected graphs
if flag==1 % Adjacency matrix represents undirected graph
ND_netplot(A);
return;
end
if flag==2 % The incidence matrix represents an undirected graph
[m n]=size(A); % Incidence matrix becomes adjacency matrix
W=zeros(m,m);
for i=1:n
a=find(A(:,i)~=0);
W(a(1),a(2))=1;
W(a(2),a(1))=1;
end
ND_netplot(W);
return;
end
function ND_netplot(A)
[n n]=size(A);
w=floor(sqrt(n));
h=floor(n/w);
x=[];
y=[];
for i=1:h % Make the generated random point have its range , Make the display more widely distributed
for j=1:w
x=[x 10*rand(1)+(j-1)*10];
y=[y 10*rand(1)+(i-1)*10];
end
end
ed=n-h*w;
for i=1:ed
x=[x 10*rand(1)+(i-1)*10];
y=[y 10*rand(1)+h*10];
end
plot(x,y,'r*');
title(' Network topology ');
for i=1:n
for j=i:n
if A(i,j)~=0
c=num2str(A(i,j)); % take A The weight in is converted into character
text((x(i)+x(j))/2,(y(i)+y(j))/2,c,'Fontsize',10); % Show the weight of the edge
line([x(i) x(j)],[y(i) y(j)]); % attachment
end
text(x(i),y(i),num2str(i),'Fontsize',14,'color','r'); % Display the sequence number of the point
hold on;
end
end
end
end
The above code is referenced from the following website :
BFS Code
https://www.cnblogs.com/tiandsp/archive/2013/07/05/3174262.html
Matrix generates wireless network diagram ( To use !)
https://www.cnblogs.com/tiandsp/archive/2012/12/26/2834654.html
summary
For me personally , Width first search can better help me understand the characteristics of this algorithm : Looking for nodes layer by layer from the inside out . You can remember according to your own understanding characteristics . The above is the main content of this paper , I hope you can get something ~
边栏推荐
- NPM run serve stuck at 40%
- @Detailed explanation of postconstruct annotation
- 12个MySQL慢查询的原因分析
- Dynamic programming (V)
- Simple use and understanding of laravel message queue
- Software designer - intermediate, exam summary
- 110道 MySQL面试题及答案 (持续更新)
- Statistical analysis of time series
- How to solve the problems of MQ message loss, duplication and backlog?
- 15.模型评估和选择问题
猜你喜欢

Geth installation

vulnhub:Sar

Advanced area of attack and defense world web masters unserialize3

MQ 消息丢失、重复、积压问题,如何解决?

MySQL事务(transaction) (有这篇就足够了..)

PTA (daily question) 7-72 calculate the cumulative sum

How to solve the problems of MQ message loss, duplication and backlog?

2022DASCTF7月赋能赛(复现)

【开发教程10】疯壳·开源蓝牙心率防水运动手环-蓝牙 BLE 收发

Nftscan and nftplay have reached strategic cooperation in the field of NFT data
随机推荐
聊聊异步编程的 7 种实现方式
R语言怎么学
MySQL的存储过程
Advanced area of attack and defense world web masters ics-06
动态规划问题(三)
1331. 数组序号转换 : 简单模拟题
NPM run serve stuck at 40%
Api 接口优化有哪些技巧?
Laravel permission control
12个MySQL慢查询的原因分析
Install mysql5.7 under Linux, super detailed complete tutorial, and cloud MySQL connection
Alibaba Code代码索引技术实践:为Code Review提供本地IDE的阅读体验
PTA (daily question) 7-70 diamond
[small bug diary] Navicat failed to connect to MySQL | MySQL service disappeared | mysqld installation failed (this application cannot run on your computer)
乱打日志的男孩运气怎么样我不知道,加班肯定很多!
动态规划问题(一)
还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...
时间序列统计分析
How to solve the problems of MQ message loss, duplication and backlog?
Upload Excel files with El upload and download the returned files