The breadth-first algorithm, also known as the width-first search, is one of the simple graph search algorithms. The search method is roughly like this:
Until the target node is searched (the nodes are the ball balls, one or more of them are the target nodes) or the entire graph is searched without finding the target node to stop the search.
Implementing this is hard to do with a function-nested function like the depth-first algorithm (at least I can't).
Like this:
What is the number of nodes in the shortest path from a to B?
This problem is very simple, it must be a-c-b ah, the answer is 3 ah. How to do it in C language?
Deep search words: A road a way to read, take the smallest number of nodes, can also, but the problem comes, when the number of nodes is particularly large, the figure is very "vast", how to do?
With a wide search!
Here the mark of a is 0 (so many code), and so on, so the path a-c-b is 0-2-1.
Depending on whether the road is not the same, we can create a Boolean two-dimensional array way,way[w1][w2]=1 representative from W1 can reach w2,=0 when the representative cannot, notice, because we do not backtrack, so all roads are unidirectional, such as c->b is possible, but B- >c is not possible.
_bool way[6][6]={
{0,0,1,1,1,0},
{0,0,0,0,0,0},
{0,1,0,0,0,0},
{0,0,0,0,0,1},
{0,0,0,0,0,1},
{0,1,0,0,0,0},
};
Create a step variable with an initial value of 0, which stores a few levels of sub-nodes, that is, the current number of nodes for all paths
Create a queue, for example, called que (which is actually the number of groups)
Simply talk about the concept of the queue: there are team Head (head) and tail (tail) two pointers, pointing to the first and last element of the queue, respectively. Queue is only allowed to join the team from the end of the team, from the team first exit team.
At this point head=tail=0:
Add flag 0 for the a node in queue que, step is to Tail+=1 and que[tail]=0 ()
Then search for the child nodes of a, such as this:
Loop (i=0;i<=5;i++)//due to a total of 6 nodes
if (way[que[head+1]][i]==1) the node joins the queue;
The way to join a queue is to:
Tail+=1;
que[tail]= child node flag;
Final effect
When the loop is finished, it is equal to the child node of a full find out, listed an entire once node, step+1, delete the first node, head-=1, so head refers to a child node C (flag 3)
Continue to judge, search 3 of the child nodes, and join the queue, then delete 3, search for 4 of the child nodes ... Until the last tail pointer refers to B (flag 2), the output step is the shortest path number. If the Head=tail, the queue is empty, and did not find the answer, so there is no way to reach the target node, the output of whatever you have.
Attach the answer to this question:
#include <stdio.h>_bool way[6][6]={{0,0,1,1,1,0},{0,0,0,0,0,0},{0,1,0,0,0,0},{0,0,0,0,0,1},{0,0,0,0,0,1},{0,1,0,0,0,0},};intMain () {intque[101],head=0, tail=0, i,step=1; Tail++; Que[tail]=0; Do{Step++; for(i=0; i<=5; i++) { if(way[que[head+1]][i]==1) {Tail++; Que[tail]=i; if(que[tail]==1) {printf ("%d", step); return 0; }}} head++; } while(head!=tail); printf ("Foolish Man,no answer!"); return 0;}
The breadth-first algorithm of C language