Example 6.1 Six Dimensions

This question is an extension of breadth first search and requires additional recording layers. A simple idea is to set the layer number of the node to its parent node layer number +1 when each node enters the queue. This method is well understood and relatively simple to implement, so I won't study it. Grandma Chen gave another solution. There is no need to set a variable to record the number of layers at each node. Only a tail, last, and level three variables can solve this problem. The space saved is O(n).

The specific realization idea, I will explain according to the above hand-drawn diagram:

First, visit vertex 1 first, let vertex 1 join the team, because vertex 1 has level=0 in the 0th layer, and vertex 1 is the last element in the 0th layer, so last = 1;

Then let vertex 1 dequeue, visit each adjacent point of vertex 1 according to the number from small to large, and assign the number of this adjacent point to tail every time a neighbor is visited, so tail records actually the last one visited currently vertex. When all the neighboring points of vertex 1 are visited, it is found that last == vertex 1, which means that all the elements of the 0th layer have been popped out, and all the neighbors of all the elements of the 0th layer have been visited, that is, the first All elements of level 1 have been accessed. At this time, tail points to the last element of the first level (vertex 7). When all elements of the first level have been accessed, the natural level++ indicates that the first level has been accessed. . Don't forget to let last = tail at this time. Because last always points to the last node of the previous layer of the last visited element.

Now start to visit the third layer, let vertex 2 dequeue first, visit all the adjacent points of vertex 2 again, find that vertex 2 != last (vertex 7), so let vertex 3 dequeue, remove all the adjacent points of vertex 3 Visit it again and find vertex 3 != last (vertex 7), continue to dequeue vertex 4... Finally, dequeue vertex 7 and visit all the neighboring points of vertex 7, then we find vertex 7==last (vertex 7), it means that all the vertices of the first layer have been dequeued and all the elements of the second layer have been visited. At this time, the tail should point to the vertex 19. We let level++,last = tail(19).

Then start to visit the fourth layer, let Vertex 8 dequeue first...

... ... ... ...

When level==6, we don't need to continue BFS, just exit BFS directly. Of course, this question also needs to record the number of vertices in the 6 layers. We only need to initialize a count variable to 0, and count++ every time an element is accessed.

Below is the code for this question, it will be easier to understand with the diagram and explanation.

#include<cstdio>
#include<queue>
using namespace std;
#define MAXV 1005
int n,G[MAXV][MAXV];
int BFS(int v,int vis[])
{
	fill(vis,vis+MAXV,false);
	int last,tail,level,w;
	int count = 0;
	queue<int> q;
	vis[v] = true; count++;
	q.push(v); 
	level = 0; last = v;//第0层最后一个入队的元素是v
	/*进入核心循环*/
	while(!q.empty())
	{
		v = q.front(); q.pop();//相当于dequeue()操作
		for(w=1; w<=n; w++)
		{
			if(G[v][w]==1&&vis[w]==false)
			{
				vis[w] = true; count++;
				q.push(w); tail = w;
			}
		}
		if(v==last)
		{
			level++; last = tail;
		}
		if(level==6) break;
	}
	return count;
}
void SDS(int v,int vis[])
{
	int count;
	count = BFS(v,vis);
	printf("%d: %.2f%%\n",v,count*100.0/n);
}
int main()
{
	int m,v1,v2; int vis[MAXV];
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++)
	{
		scanf("%d%d",&v1,&v2);
		G[v1][v2] = 1;
		G[v2][v1] = 1;
	}
	for(int v=1; v<=n; v++)
	{
		SDS(v,vis);
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/yiwaite/article/details/100520442